blob: f413739a685e6770ed7faebeafb9f7a35a2859bc [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());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100400 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402 }
403
Alexandre Rames9931f312015-06-19 14:47:01 +0100404 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
405
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700407 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
408};
409
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100410class ArraySetSlowPathX86_64 : public SlowPathCode {
411 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000412 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
415 LocationSummary* locations = instruction_->GetLocations();
416 __ Bind(GetEntryLabel());
417 SaveLiveRegisters(codegen, locations);
418
419 InvokeRuntimeCallingConvention calling_convention;
420 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
421 parallel_move.AddMove(
422 locations->InAt(0),
423 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
424 Primitive::kPrimNot,
425 nullptr);
426 parallel_move.AddMove(
427 locations->InAt(1),
428 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
429 Primitive::kPrimInt,
430 nullptr);
431 parallel_move.AddMove(
432 locations->InAt(2),
433 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
434 Primitive::kPrimNot,
435 nullptr);
436 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
437
Roland Levillain0d5a2812015-11-13 10:07:31 +0000438 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100439 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000440 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 RestoreLiveRegisters(codegen, locations);
442 __ jmp(GetExitLabel());
443 }
444
445 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
446
447 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100448 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
449};
450
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100451// Slow path marking an object reference `ref` during a read
452// barrier. The field `obj.field` in the object `obj` holding this
453// reference does not get updated by this slow path after marking (see
454// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
455//
456// This means that after the execution of this slow path, `ref` will
457// always be up-to-date, but `obj.field` may not; i.e., after the
458// flip, `ref` will be a to-space reference, but `obj.field` will
459// probably still be a from-space reference (unless it gets updated by
460// another thread, or if another thread installed another object
461// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000462class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
463 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100464 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
465 Location ref,
466 bool unpoison_ref_before_marking)
467 : SlowPathCode(instruction),
468 ref_(ref),
469 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000470 DCHECK(kEmitCompilerReadBarrier);
471 }
472
473 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
474
475 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
476 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100477 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
478 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000479 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000481 DCHECK(instruction_->IsInstanceFieldGet() ||
482 instruction_->IsStaticFieldGet() ||
483 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100484 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000485 instruction_->IsLoadClass() ||
486 instruction_->IsLoadString() ||
487 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100488 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100489 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
490 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 << "Unexpected instruction in read barrier marking slow path: "
492 << instruction_->DebugName();
493
494 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000496 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 }
Roland Levillain4359e612016-07-20 11:32:19 +0100499 // No need to save live registers; it's taken care of by the
500 // entrypoint. Also, there is no need to update the stack mask,
501 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000502 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 DCHECK_NE(ref_reg, RSP);
504 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100505 // "Compact" slow path, saving two moves.
506 //
507 // Instead of using the standard runtime calling convention (input
508 // and output in R0):
509 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100511 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100513 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // of a dedicated entrypoint:
516 //
517 // rX <- ReadBarrierMarkRegX(rX)
518 //
519 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100521 // This runtime call does not require a stack map.
522 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000523 __ jmp(GetExitLabel());
524 }
525
526 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100527 // The location (register) of the marked object reference.
528 const Location ref_;
529 // Should the reference in `ref_` be unpoisoned prior to marking it?
530 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000531
532 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
533};
534
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100535// Slow path marking an object reference `ref` during a read barrier,
536// and if needed, atomically updating the field `obj.field` in the
537// object `obj` holding this reference after marking (contrary to
538// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
539// `obj.field`).
540//
541// This means that after the execution of this slow path, both `ref`
542// and `obj.field` will be up-to-date; i.e., after the flip, both will
543// hold the same to-space reference (unless another thread installed
544// another object reference (different from `ref`) in `obj.field`).
545class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
546 public:
547 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
548 Location ref,
549 CpuRegister obj,
550 const Address& field_addr,
551 bool unpoison_ref_before_marking,
552 CpuRegister temp1,
553 CpuRegister temp2)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp1_(temp1),
560 temp2_(temp2) {
561 DCHECK(kEmitCompilerReadBarrier);
562 }
563
564 const char* GetDescription() const OVERRIDE {
565 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
566 }
567
568 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
569 LocationSummary* locations = instruction_->GetLocations();
570 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
571 Register ref_reg = ref_cpu_reg.AsRegister();
572 DCHECK(locations->CanCall());
573 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
574 // This slow path is only used by the UnsafeCASObject intrinsic.
575 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
576 << "Unexpected instruction in read barrier marking and field updating slow path: "
577 << instruction_->DebugName();
578 DCHECK(instruction_->GetLocations()->Intrinsified());
579 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
580
581 __ Bind(GetEntryLabel());
582 if (unpoison_ref_before_marking_) {
583 // Object* ref = ref_addr->AsMirrorPtr()
584 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
585 }
586
587 // Save the old (unpoisoned) reference.
588 __ movl(temp1_, ref_cpu_reg);
589
590 // No need to save live registers; it's taken care of by the
591 // entrypoint. Also, there is no need to update the stack mask,
592 // as this runtime call will not trigger a garbage collection.
593 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
594 DCHECK_NE(ref_reg, RSP);
595 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
596 // "Compact" slow path, saving two moves.
597 //
598 // Instead of using the standard runtime calling convention (input
599 // and output in R0):
600 //
601 // RDI <- ref
602 // RAX <- ReadBarrierMark(RDI)
603 // ref <- RAX
604 //
605 // we just use rX (the register containing `ref`) as input and output
606 // of a dedicated entrypoint:
607 //
608 // rX <- ReadBarrierMarkRegX(rX)
609 //
610 int32_t entry_point_offset =
611 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
612 // This runtime call does not require a stack map.
613 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
614
615 // If the new reference is different from the old reference,
616 // update the field in the holder (`*field_addr`).
617 //
618 // Note that this field could also hold a different object, if
619 // another thread had concurrently changed it. In that case, the
620 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
621 // operation below would abort the CAS, leaving the field as-is.
622 NearLabel done;
623 __ cmpl(temp1_, ref_cpu_reg);
624 __ j(kEqual, &done);
625
626 // Update the the holder's field atomically. This may fail if
627 // mutator updates before us, but it's OK. This is achived
628 // using a strong compare-and-set (CAS) operation with relaxed
629 // memory synchronization ordering, where the expected value is
630 // the old reference and the desired value is the new reference.
631 // This operation is implemented with a 32-bit LOCK CMPXLCHG
632 // instruction, which requires the expected value (the old
633 // reference) to be in EAX. Save RAX beforehand, and move the
634 // expected value (stored in `temp1_`) into EAX.
635 __ movq(temp2_, CpuRegister(RAX));
636 __ movl(CpuRegister(RAX), temp1_);
637
638 // Convenience aliases.
639 CpuRegister base = obj_;
640 CpuRegister expected = CpuRegister(RAX);
641 CpuRegister value = ref_cpu_reg;
642
643 bool base_equals_value = (base.AsRegister() == value.AsRegister());
644 Register value_reg = ref_reg;
645 if (kPoisonHeapReferences) {
646 if (base_equals_value) {
647 // If `base` and `value` are the same register location, move
648 // `value_reg` to a temporary register. This way, poisoning
649 // `value_reg` won't invalidate `base`.
650 value_reg = temp1_.AsRegister();
651 __ movl(CpuRegister(value_reg), base);
652 }
653
654 // Check that the register allocator did not assign the location
655 // of `expected` (RAX) to `value` nor to `base`, so that heap
656 // poisoning (when enabled) works as intended below.
657 // - If `value` were equal to `expected`, both references would
658 // be poisoned twice, meaning they would not be poisoned at
659 // all, as heap poisoning uses address negation.
660 // - If `base` were equal to `expected`, poisoning `expected`
661 // would invalidate `base`.
662 DCHECK_NE(value_reg, expected.AsRegister());
663 DCHECK_NE(base.AsRegister(), expected.AsRegister());
664
665 __ PoisonHeapReference(expected);
666 __ PoisonHeapReference(CpuRegister(value_reg));
667 }
668
669 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
670
671 // If heap poisoning is enabled, we need to unpoison the values
672 // that were poisoned earlier.
673 if (kPoisonHeapReferences) {
674 if (base_equals_value) {
675 // `value_reg` has been moved to a temporary register, no need
676 // to unpoison it.
677 } else {
678 __ UnpoisonHeapReference(CpuRegister(value_reg));
679 }
680 // No need to unpoison `expected` (RAX), as it is be overwritten below.
681 }
682
683 // Restore RAX.
684 __ movq(CpuRegister(RAX), temp2_);
685
686 __ Bind(&done);
687 __ jmp(GetExitLabel());
688 }
689
690 private:
691 // The location (register) of the marked object reference.
692 const Location ref_;
693 // The register containing the object holding the marked object reference field.
694 const CpuRegister obj_;
695 // The address of the marked reference field. The base of this address must be `obj_`.
696 const Address field_addr_;
697
698 // Should the reference in `ref_` be unpoisoned prior to marking it?
699 const bool unpoison_ref_before_marking_;
700
701 const CpuRegister temp1_;
702 const CpuRegister temp2_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
705};
706
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707// Slow path generating a read barrier for a heap reference.
708class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
709 public:
710 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
711 Location out,
712 Location ref,
713 Location obj,
714 uint32_t offset,
715 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000716 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000717 out_(out),
718 ref_(ref),
719 obj_(obj),
720 offset_(offset),
721 index_(index) {
722 DCHECK(kEmitCompilerReadBarrier);
723 // If `obj` is equal to `out` or `ref`, it means the initial
724 // object has been overwritten by (or after) the heap object
725 // reference load to be instrumented, e.g.:
726 //
727 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000728 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000729 //
730 // In that case, we have lost the information about the original
731 // object, and the emitted read barrier cannot work properly.
732 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
733 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
734}
735
736 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
737 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
738 LocationSummary* locations = instruction_->GetLocations();
739 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
740 DCHECK(locations->CanCall());
741 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100742 DCHECK(instruction_->IsInstanceFieldGet() ||
743 instruction_->IsStaticFieldGet() ||
744 instruction_->IsArrayGet() ||
745 instruction_->IsInstanceOf() ||
746 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700747 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000748 << "Unexpected instruction in read barrier for heap reference slow path: "
749 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000750
751 __ Bind(GetEntryLabel());
752 SaveLiveRegisters(codegen, locations);
753
754 // We may have to change the index's value, but as `index_` is a
755 // constant member (like other "inputs" of this slow path),
756 // introduce a copy of it, `index`.
757 Location index = index_;
758 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100759 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000760 if (instruction_->IsArrayGet()) {
761 // Compute real offset and store it in index_.
762 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
763 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
764 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
765 // We are about to change the value of `index_reg` (see the
766 // calls to art::x86_64::X86_64Assembler::shll and
767 // art::x86_64::X86_64Assembler::AddImmediate below), but it
768 // has not been saved by the previous call to
769 // art::SlowPathCode::SaveLiveRegisters, as it is a
770 // callee-save register --
771 // art::SlowPathCode::SaveLiveRegisters does not consider
772 // callee-save registers, as it has been designed with the
773 // assumption that callee-save registers are supposed to be
774 // handled by the called function. So, as a callee-save
775 // register, `index_reg` _would_ eventually be saved onto
776 // the stack, but it would be too late: we would have
777 // changed its value earlier. Therefore, we manually save
778 // it here into another freely available register,
779 // `free_reg`, chosen of course among the caller-save
780 // registers (as a callee-save `free_reg` register would
781 // exhibit the same problem).
782 //
783 // Note we could have requested a temporary register from
784 // the register allocator instead; but we prefer not to, as
785 // this is a slow path, and we know we can find a
786 // caller-save register that is available.
787 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
788 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
789 index_reg = free_reg;
790 index = Location::RegisterLocation(index_reg);
791 } else {
792 // The initial register stored in `index_` has already been
793 // saved in the call to art::SlowPathCode::SaveLiveRegisters
794 // (as it is not a callee-save register), so we can freely
795 // use it.
796 }
797 // Shifting the index value contained in `index_reg` by the
798 // scale factor (2) cannot overflow in practice, as the
799 // runtime is unable to allocate object arrays with a size
800 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
801 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
802 static_assert(
803 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
804 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
805 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
806 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100807 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
808 // intrinsics, `index_` is not shifted by a scale factor of 2
809 // (as in the case of ArrayGet), as it is actually an offset
810 // to an object field within an object.
811 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000812 DCHECK(instruction_->GetLocations()->Intrinsified());
813 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
814 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
815 << instruction_->AsInvoke()->GetIntrinsic();
816 DCHECK_EQ(offset_, 0U);
817 DCHECK(index_.IsRegister());
818 }
819 }
820
821 // We're moving two or three locations to locations that could
822 // overlap, so we need a parallel move resolver.
823 InvokeRuntimeCallingConvention calling_convention;
824 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
825 parallel_move.AddMove(ref_,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
827 Primitive::kPrimNot,
828 nullptr);
829 parallel_move.AddMove(obj_,
830 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
831 Primitive::kPrimNot,
832 nullptr);
833 if (index.IsValid()) {
834 parallel_move.AddMove(index,
835 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
836 Primitive::kPrimInt,
837 nullptr);
838 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
839 } else {
840 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
841 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
842 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100843 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000844 instruction_,
845 instruction_->GetDexPc(),
846 this);
847 CheckEntrypointTypes<
848 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
849 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
850
851 RestoreLiveRegisters(codegen, locations);
852 __ jmp(GetExitLabel());
853 }
854
855 const char* GetDescription() const OVERRIDE {
856 return "ReadBarrierForHeapReferenceSlowPathX86_64";
857 }
858
859 private:
860 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
861 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
862 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
863 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
864 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
865 return static_cast<CpuRegister>(i);
866 }
867 }
868 // We shall never fail to find a free caller-save register, as
869 // there are more than two core caller-save registers on x86-64
870 // (meaning it is possible to find one which is different from
871 // `ref` and `obj`).
872 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
873 LOG(FATAL) << "Could not find a free caller-save register";
874 UNREACHABLE();
875 }
876
Roland Levillain0d5a2812015-11-13 10:07:31 +0000877 const Location out_;
878 const Location ref_;
879 const Location obj_;
880 const uint32_t offset_;
881 // An additional location containing an index to an array.
882 // Only used for HArrayGet and the UnsafeGetObject &
883 // UnsafeGetObjectVolatile intrinsics.
884 const Location index_;
885
886 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
887};
888
889// Slow path generating a read barrier for a GC root.
890class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
891 public:
892 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000893 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000894 DCHECK(kEmitCompilerReadBarrier);
895 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
898 LocationSummary* locations = instruction_->GetLocations();
899 DCHECK(locations->CanCall());
900 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000901 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
902 << "Unexpected instruction in read barrier for GC root slow path: "
903 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904
905 __ Bind(GetEntryLabel());
906 SaveLiveRegisters(codegen, locations);
907
908 InvokeRuntimeCallingConvention calling_convention;
909 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
910 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100911 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000912 instruction_,
913 instruction_->GetDexPc(),
914 this);
915 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
916 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
917
918 RestoreLiveRegisters(codegen, locations);
919 __ jmp(GetExitLabel());
920 }
921
922 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
923
924 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000925 const Location out_;
926 const Location root_;
927
928 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
929};
930
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100931#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100932// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
933#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100934
Roland Levillain4fa13f62015-07-06 18:11:54 +0100935inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700936 switch (cond) {
937 case kCondEQ: return kEqual;
938 case kCondNE: return kNotEqual;
939 case kCondLT: return kLess;
940 case kCondLE: return kLessEqual;
941 case kCondGT: return kGreater;
942 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700943 case kCondB: return kBelow;
944 case kCondBE: return kBelowEqual;
945 case kCondA: return kAbove;
946 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700947 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 LOG(FATAL) << "Unreachable";
949 UNREACHABLE();
950}
951
Aart Bike9f37602015-10-09 11:15:55 -0700952// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100953inline Condition X86_64FPCondition(IfCondition cond) {
954 switch (cond) {
955 case kCondEQ: return kEqual;
956 case kCondNE: return kNotEqual;
957 case kCondLT: return kBelow;
958 case kCondLE: return kBelowEqual;
959 case kCondGT: return kAbove;
960 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700961 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100962 };
963 LOG(FATAL) << "Unreachable";
964 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700965}
966
Vladimir Markodc151b22015-10-15 18:02:30 +0100967HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
968 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100969 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000970 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100971}
972
Serguei Katkov288c7a82016-05-16 11:53:15 +0600973Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
974 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800975 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000976 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
977 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100978 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000979 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100980 uint32_t offset =
981 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
982 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000983 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100984 }
Vladimir Marko58155012015-08-19 12:49:41 +0000985 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000986 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000987 break;
988 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +0000989 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +0000990 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000991 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000992 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000993 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000994 // Bind a new fixup label at the end of the "movl" insn.
995 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000996 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000997 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000998 }
Vladimir Marko58155012015-08-19 12:49:41 +0000999 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001000 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00001001 Register method_reg;
1002 CpuRegister reg = temp.AsRegister<CpuRegister>();
1003 if (current_method.IsRegister()) {
1004 method_reg = current_method.AsRegister<Register>();
1005 } else {
1006 DCHECK(invoke->GetLocations()->Intrinsified());
1007 DCHECK(!current_method.IsValid());
1008 method_reg = reg.AsRegister();
1009 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
1010 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001011 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01001012 __ movq(reg,
1013 Address(CpuRegister(method_reg),
1014 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01001015 // temp = temp[index_in_cache];
1016 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
1017 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00001018 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
1019 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01001020 }
Vladimir Marko58155012015-08-19 12:49:41 +00001021 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06001022 return callee_method;
1023}
1024
1025void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
1026 Location temp) {
1027 // All registers are assumed to be correctly set up.
1028 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00001029
1030 switch (invoke->GetCodePtrLocation()) {
1031 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1032 __ call(&frame_entry_label_);
1033 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001034 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1035 // (callee_method + offset_of_quick_compiled_code)()
1036 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1037 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001038 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001039 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001040 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001041
1042 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001043}
1044
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001045void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
1046 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1047 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1048 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001049
1050 // Use the calling convention instead of the location of the receiver, as
1051 // intrinsics may have put the receiver in a different register. In the intrinsics
1052 // slow path, the arguments have been moved to the right place, so here we are
1053 // guaranteed that the receiver is the first register of the calling convention.
1054 InvokeDexCallingConvention calling_convention;
1055 Register receiver = calling_convention.GetRegisterAt(0);
1056
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001057 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001058 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001059 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001060 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001061 // Instead of simply (possibly) unpoisoning `temp` here, we should
1062 // emit a read barrier for the previous class reference load.
1063 // However this is not required in practice, as this is an
1064 // intermediate/temporary reference and because the current
1065 // concurrent copying collector keeps the from-space memory
1066 // intact/accessible until the end of the marking phase (the
1067 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001068 __ MaybeUnpoisonHeapReference(temp);
1069 // temp = temp->GetMethodAt(method_offset);
1070 __ movq(temp, Address(temp, method_offset));
1071 // call temp->GetEntryPoint();
1072 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001073 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001074}
1075
Vladimir Markoaad75c62016-10-03 08:46:48 +00001076void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
1077 DCHECK(GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001078 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001079 __ Bind(&string_patches_.back().label);
1080}
1081
Vladimir Marko1998cd02017-01-13 13:02:58 +00001082void CodeGeneratorX86_64::RecordBootTypePatch(HLoadClass* load_class) {
1083 boot_image_type_patches_.emplace_back(load_class->GetDexFile(),
1084 load_class->GetTypeIndex().index_);
1085 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001086}
1087
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001088Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001089 type_bss_entry_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
1090 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001091}
1092
Vladimir Markoaad75c62016-10-03 08:46:48 +00001093Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1094 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001095 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001096 return &string_patches_.back().label;
1097}
1098
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001099Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
1100 uint32_t element_offset) {
1101 // Add a patch entry and return the label.
1102 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
1103 return &pc_relative_dex_cache_patches_.back().label;
1104}
1105
Vladimir Markoaad75c62016-10-03 08:46:48 +00001106// The label points to the end of the "movl" or another instruction but the literal offset
1107// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1108constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1109
1110template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1111inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1112 const ArenaDeque<PatchInfo<Label>>& infos,
1113 ArenaVector<LinkerPatch>* linker_patches) {
1114 for (const PatchInfo<Label>& info : infos) {
1115 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1116 linker_patches->push_back(
1117 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1118 }
1119}
1120
Vladimir Marko58155012015-08-19 12:49:41 +00001121void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1122 DCHECK(linker_patches->empty());
1123 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001124 pc_relative_dex_cache_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001125 string_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001126 boot_image_type_patches_.size() +
1127 type_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001128 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001129 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1130 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001131 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001132 DCHECK(boot_image_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001133 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
1134 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001135 // These are always PC-relative, see GetSupportedLoadClassKind()/GetSupportedLoadStringKind().
Vladimir Marko1998cd02017-01-13 13:02:58 +00001136 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
1137 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001138 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001139 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001140 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1141 linker_patches);
1142 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001143}
1144
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001145void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001146 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147}
1148
1149void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001150 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151}
1152
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001153size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1155 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001156}
1157
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001158size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1159 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1160 return kX86_64WordSize;
1161}
1162
1163size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001164 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001165 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001166 } else {
1167 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1168 }
1169 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001170}
1171
1172size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001173 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001174 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001175 } else {
1176 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1177 }
1178 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001179}
1180
Calin Juravle175dc732015-08-25 15:42:32 +01001181void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1182 HInstruction* instruction,
1183 uint32_t dex_pc,
1184 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001185 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001186 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1187 if (EntrypointRequiresStackMap(entrypoint)) {
1188 RecordPcInfo(instruction, dex_pc, slow_path);
1189 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001190}
1191
Roland Levillaindec8f632016-07-22 17:10:06 +01001192void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1193 HInstruction* instruction,
1194 SlowPathCode* slow_path) {
1195 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001196 GenerateInvokeRuntime(entry_point_offset);
1197}
1198
1199void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001200 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1201}
1202
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001203static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001204// Use a fake return address register to mimic Quick.
1205static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001206CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001207 const X86_64InstructionSetFeatures& isa_features,
1208 const CompilerOptions& compiler_options,
1209 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001210 : CodeGenerator(graph,
1211 kNumberOfCpuRegisters,
1212 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001213 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001214 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1215 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001216 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001217 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1218 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001219 compiler_options,
1220 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001221 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001223 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001224 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001225 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001226 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001227 constant_area_start_(0),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001228 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001229 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001230 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1231 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001232 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001233 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1234 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001235 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1236}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001237
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001238InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1239 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001240 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 assembler_(codegen->GetAssembler()),
1242 codegen_(codegen) {}
1243
David Brazdil58282f42016-01-14 12:45:10 +00001244void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001246 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001248 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001249 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250}
1251
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001252static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001253 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001254}
David Srbecky9d8606d2015-04-12 09:35:32 +01001255
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001256static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001257 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001258}
1259
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001261 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001262 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001263 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001264 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001265 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001266
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001267 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001268 __ testq(CpuRegister(RAX), Address(
1269 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001270 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001271 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001272
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001273 if (HasEmptyFrame()) {
1274 return;
1275 }
1276
Nicolas Geoffray98893962015-01-21 12:32:32 +00001277 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001278 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001279 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001280 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001281 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1282 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001283 }
1284 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001285
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001286 int adjust = GetFrameSize() - GetCoreSpillSize();
1287 __ subq(CpuRegister(RSP), Immediate(adjust));
1288 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001289 uint32_t xmm_spill_location = GetFpuSpillStart();
1290 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001291
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001292 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1293 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001294 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1295 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1296 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001297 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001298 }
1299
Mingyao Yang063fc772016-08-02 11:02:54 -07001300 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1301 // Initialize should_deoptimize flag to 0.
1302 __ movl(Address(CpuRegister(RSP), xmm_spill_location - kShouldDeoptimizeFlagSize),
1303 Immediate(0));
1304 }
1305
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001306 // Save the current method if we need it. Note that we do not
1307 // do this in HCurrentMethod, as the instruction might have been removed
1308 // in the SSA graph.
1309 if (RequiresCurrentMethod()) {
1310 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1311 CpuRegister(kMethodRegisterArgument));
1312 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001313}
1314
1315void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001316 __ cfi().RememberState();
1317 if (!HasEmptyFrame()) {
1318 uint32_t xmm_spill_location = GetFpuSpillStart();
1319 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1320 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1321 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1322 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1323 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1324 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1325 }
1326 }
1327
1328 int adjust = GetFrameSize() - GetCoreSpillSize();
1329 __ addq(CpuRegister(RSP), Immediate(adjust));
1330 __ cfi().AdjustCFAOffset(-adjust);
1331
1332 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1333 Register reg = kCoreCalleeSaves[i];
1334 if (allocated_registers_.ContainsCoreRegister(reg)) {
1335 __ popq(CpuRegister(reg));
1336 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1337 __ cfi().Restore(DWARFReg(reg));
1338 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001339 }
1340 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001341 __ ret();
1342 __ cfi().RestoreState();
1343 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001344}
1345
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001346void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1347 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001348}
1349
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001350void CodeGeneratorX86_64::Move(Location destination, Location source) {
1351 if (source.Equals(destination)) {
1352 return;
1353 }
1354 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001355 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001357 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001358 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001359 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001360 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001361 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1362 } else if (source.IsConstant()) {
1363 HConstant* constant = source.GetConstant();
1364 if (constant->IsLongConstant()) {
1365 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1366 } else {
1367 Load32BitValue(dest, GetInt32ValueOf(constant));
1368 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001369 } else {
1370 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001371 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001372 }
1373 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001374 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001375 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001376 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001377 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001378 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1379 } else if (source.IsConstant()) {
1380 HConstant* constant = source.GetConstant();
1381 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1382 if (constant->IsFloatConstant()) {
1383 Load32BitValue(dest, static_cast<int32_t>(value));
1384 } else {
1385 Load64BitValue(dest, value);
1386 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001387 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001388 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001389 } else {
1390 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001391 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001392 }
1393 } else if (destination.IsStackSlot()) {
1394 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001395 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001396 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001397 } else if (source.IsFpuRegister()) {
1398 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001400 } else if (source.IsConstant()) {
1401 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001402 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001403 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001404 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001405 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001406 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1407 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001408 }
1409 } else {
1410 DCHECK(destination.IsDoubleStackSlot());
1411 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001412 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001413 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001414 } else if (source.IsFpuRegister()) {
1415 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001416 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001417 } else if (source.IsConstant()) {
1418 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001419 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1420 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001421 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001422 } else {
1423 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001424 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1425 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001426 }
1427 }
1428}
1429
Calin Juravle175dc732015-08-25 15:42:32 +01001430void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1431 DCHECK(location.IsRegister());
1432 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1433}
1434
Calin Juravlee460d1d2015-09-29 04:52:17 +01001435void CodeGeneratorX86_64::MoveLocation(
1436 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1437 Move(dst, src);
1438}
1439
1440void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1441 if (location.IsRegister()) {
1442 locations->AddTemp(location);
1443 } else {
1444 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1445 }
1446}
1447
David Brazdilfc6a86a2015-06-26 10:33:45 +00001448void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001449 DCHECK(!successor->IsExitBlock());
1450
1451 HBasicBlock* block = got->GetBlock();
1452 HInstruction* previous = got->GetPrevious();
1453
1454 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001455 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001456 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1457 return;
1458 }
1459
1460 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1461 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1462 }
1463 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001464 __ jmp(codegen_->GetLabelOf(successor));
1465 }
1466}
1467
David Brazdilfc6a86a2015-06-26 10:33:45 +00001468void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1469 got->SetLocations(nullptr);
1470}
1471
1472void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1473 HandleGoto(got, got->GetSuccessor());
1474}
1475
1476void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1477 try_boundary->SetLocations(nullptr);
1478}
1479
1480void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1481 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1482 if (!successor->IsExitBlock()) {
1483 HandleGoto(try_boundary, successor);
1484 }
1485}
1486
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001487void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1488 exit->SetLocations(nullptr);
1489}
1490
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001491void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001492}
1493
Mark Mendell152408f2015-12-31 12:28:50 -05001494template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001495void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001496 LabelType* true_label,
1497 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001498 if (cond->IsFPConditionTrueIfNaN()) {
1499 __ j(kUnordered, true_label);
1500 } else if (cond->IsFPConditionFalseIfNaN()) {
1501 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001502 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001503 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001504}
1505
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001506void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001507 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001508
Mark Mendellc4701932015-04-10 13:18:51 -04001509 Location left = locations->InAt(0);
1510 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001511 Primitive::Type type = condition->InputAt(0)->GetType();
1512 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001513 case Primitive::kPrimBoolean:
1514 case Primitive::kPrimByte:
1515 case Primitive::kPrimChar:
1516 case Primitive::kPrimShort:
1517 case Primitive::kPrimInt:
1518 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001519 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001520 break;
1521 }
Mark Mendellc4701932015-04-10 13:18:51 -04001522 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001523 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001524 break;
1525 }
1526 case Primitive::kPrimFloat: {
1527 if (right.IsFpuRegister()) {
1528 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1529 } else if (right.IsConstant()) {
1530 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1531 codegen_->LiteralFloatAddress(
1532 right.GetConstant()->AsFloatConstant()->GetValue()));
1533 } else {
1534 DCHECK(right.IsStackSlot());
1535 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1536 Address(CpuRegister(RSP), right.GetStackIndex()));
1537 }
Mark Mendellc4701932015-04-10 13:18:51 -04001538 break;
1539 }
1540 case Primitive::kPrimDouble: {
1541 if (right.IsFpuRegister()) {
1542 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1543 } else if (right.IsConstant()) {
1544 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1545 codegen_->LiteralDoubleAddress(
1546 right.GetConstant()->AsDoubleConstant()->GetValue()));
1547 } else {
1548 DCHECK(right.IsDoubleStackSlot());
1549 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1550 Address(CpuRegister(RSP), right.GetStackIndex()));
1551 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001552 break;
1553 }
1554 default:
1555 LOG(FATAL) << "Unexpected condition type " << type;
1556 }
1557}
1558
1559template<class LabelType>
1560void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1561 LabelType* true_target_in,
1562 LabelType* false_target_in) {
1563 // Generated branching requires both targets to be explicit. If either of the
1564 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1565 LabelType fallthrough_target;
1566 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1567 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1568
1569 // Generate the comparison to set the CC.
1570 GenerateCompareTest(condition);
1571
1572 // Now generate the correct jump(s).
1573 Primitive::Type type = condition->InputAt(0)->GetType();
1574 switch (type) {
1575 case Primitive::kPrimLong: {
1576 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1577 break;
1578 }
1579 case Primitive::kPrimFloat: {
1580 GenerateFPJumps(condition, true_target, false_target);
1581 break;
1582 }
1583 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001584 GenerateFPJumps(condition, true_target, false_target);
1585 break;
1586 }
1587 default:
1588 LOG(FATAL) << "Unexpected condition type " << type;
1589 }
1590
David Brazdil0debae72015-11-12 18:37:00 +00001591 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001592 __ jmp(false_target);
1593 }
David Brazdil0debae72015-11-12 18:37:00 +00001594
1595 if (fallthrough_target.IsLinked()) {
1596 __ Bind(&fallthrough_target);
1597 }
Mark Mendellc4701932015-04-10 13:18:51 -04001598}
1599
David Brazdil0debae72015-11-12 18:37:00 +00001600static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1601 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1602 // are set only strictly before `branch`. We can't use the eflags on long
1603 // conditions if they are materialized due to the complex branching.
1604 return cond->IsCondition() &&
1605 cond->GetNext() == branch &&
1606 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1607}
1608
Mark Mendell152408f2015-12-31 12:28:50 -05001609template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001610void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001611 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001612 LabelType* true_target,
1613 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001614 HInstruction* cond = instruction->InputAt(condition_input_index);
1615
1616 if (true_target == nullptr && false_target == nullptr) {
1617 // Nothing to do. The code always falls through.
1618 return;
1619 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001620 // Constant condition, statically compared against "true" (integer value 1).
1621 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001622 if (true_target != nullptr) {
1623 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001624 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001625 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001626 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001627 if (false_target != nullptr) {
1628 __ jmp(false_target);
1629 }
1630 }
1631 return;
1632 }
1633
1634 // The following code generates these patterns:
1635 // (1) true_target == nullptr && false_target != nullptr
1636 // - opposite condition true => branch to false_target
1637 // (2) true_target != nullptr && false_target == nullptr
1638 // - condition true => branch to true_target
1639 // (3) true_target != nullptr && false_target != nullptr
1640 // - condition true => branch to true_target
1641 // - branch to false_target
1642 if (IsBooleanValueOrMaterializedCondition(cond)) {
1643 if (AreEflagsSetFrom(cond, instruction)) {
1644 if (true_target == nullptr) {
1645 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1646 } else {
1647 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1648 }
1649 } else {
1650 // Materialized condition, compare against 0.
1651 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1652 if (lhs.IsRegister()) {
1653 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1654 } else {
1655 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1656 }
1657 if (true_target == nullptr) {
1658 __ j(kEqual, false_target);
1659 } else {
1660 __ j(kNotEqual, true_target);
1661 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001662 }
1663 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001664 // Condition has not been materialized, use its inputs as the
1665 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001666 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001667
David Brazdil0debae72015-11-12 18:37:00 +00001668 // If this is a long or FP comparison that has been folded into
1669 // the HCondition, generate the comparison directly.
1670 Primitive::Type type = condition->InputAt(0)->GetType();
1671 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1672 GenerateCompareTestAndBranch(condition, true_target, false_target);
1673 return;
1674 }
1675
1676 Location lhs = condition->GetLocations()->InAt(0);
1677 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001678 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001679 if (true_target == nullptr) {
1680 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1681 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001682 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001683 }
Dave Allison20dfc792014-06-16 20:44:29 -07001684 }
David Brazdil0debae72015-11-12 18:37:00 +00001685
1686 // If neither branch falls through (case 3), the conditional branch to `true_target`
1687 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1688 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001689 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690 }
1691}
1692
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001694 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1695 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696 locations->SetInAt(0, Location::Any());
1697 }
1698}
1699
1700void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001701 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1702 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1703 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1704 nullptr : codegen_->GetLabelOf(true_successor);
1705 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1706 nullptr : codegen_->GetLabelOf(false_successor);
1707 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001708}
1709
1710void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1711 LocationSummary* locations = new (GetGraph()->GetArena())
1712 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001713 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001714 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715 locations->SetInAt(0, Location::Any());
1716 }
1717}
1718
1719void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001720 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001721 GenerateTestAndBranch<Label>(deoptimize,
1722 /* condition_input_index */ 0,
1723 slow_path->GetEntryLabel(),
1724 /* false_target */ nullptr);
1725}
1726
Mingyao Yang063fc772016-08-02 11:02:54 -07001727void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1728 LocationSummary* locations = new (GetGraph()->GetArena())
1729 LocationSummary(flag, LocationSummary::kNoCall);
1730 locations->SetOut(Location::RequiresRegister());
1731}
1732
1733void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1734 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1735 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1736}
1737
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001738static bool SelectCanUseCMOV(HSelect* select) {
1739 // There are no conditional move instructions for XMMs.
1740 if (Primitive::IsFloatingPointType(select->GetType())) {
1741 return false;
1742 }
1743
1744 // A FP condition doesn't generate the single CC that we need.
1745 HInstruction* condition = select->GetCondition();
1746 if (condition->IsCondition() &&
1747 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1748 return false;
1749 }
1750
1751 // We can generate a CMOV for this Select.
1752 return true;
1753}
1754
David Brazdil74eb1b22015-12-14 11:44:01 +00001755void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1756 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1757 if (Primitive::IsFloatingPointType(select->GetType())) {
1758 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001759 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001760 } else {
1761 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001762 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001763 if (select->InputAt(1)->IsConstant()) {
1764 locations->SetInAt(1, Location::RequiresRegister());
1765 } else {
1766 locations->SetInAt(1, Location::Any());
1767 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001768 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001769 locations->SetInAt(1, Location::Any());
1770 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001771 }
1772 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1773 locations->SetInAt(2, Location::RequiresRegister());
1774 }
1775 locations->SetOut(Location::SameAsFirstInput());
1776}
1777
1778void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1779 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001780 if (SelectCanUseCMOV(select)) {
1781 // If both the condition and the source types are integer, we can generate
1782 // a CMOV to implement Select.
1783 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001784 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001785 DCHECK(locations->InAt(0).Equals(locations->Out()));
1786
1787 HInstruction* select_condition = select->GetCondition();
1788 Condition cond = kNotEqual;
1789
1790 // Figure out how to test the 'condition'.
1791 if (select_condition->IsCondition()) {
1792 HCondition* condition = select_condition->AsCondition();
1793 if (!condition->IsEmittedAtUseSite()) {
1794 // This was a previously materialized condition.
1795 // Can we use the existing condition code?
1796 if (AreEflagsSetFrom(condition, select)) {
1797 // Materialization was the previous instruction. Condition codes are right.
1798 cond = X86_64IntegerCondition(condition->GetCondition());
1799 } else {
1800 // No, we have to recreate the condition code.
1801 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1802 __ testl(cond_reg, cond_reg);
1803 }
1804 } else {
1805 GenerateCompareTest(condition);
1806 cond = X86_64IntegerCondition(condition->GetCondition());
1807 }
1808 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001809 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001810 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1811 __ testl(cond_reg, cond_reg);
1812 }
1813
1814 // If the condition is true, overwrite the output, which already contains false.
1815 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001816 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1817 if (value_true_loc.IsRegister()) {
1818 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1819 } else {
1820 __ cmov(cond,
1821 value_false,
1822 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1823 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001824 } else {
1825 NearLabel false_target;
1826 GenerateTestAndBranch<NearLabel>(select,
1827 /* condition_input_index */ 2,
1828 /* true_target */ nullptr,
1829 &false_target);
1830 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1831 __ Bind(&false_target);
1832 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001833}
1834
David Srbecky0cf44932015-12-09 14:09:59 +00001835void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1836 new (GetGraph()->GetArena()) LocationSummary(info);
1837}
1838
David Srbeckyd28f4a02016-03-14 17:14:24 +00001839void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1840 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001841}
1842
1843void CodeGeneratorX86_64::GenerateNop() {
1844 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001845}
1846
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001849 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001850 // Handle the long/FP comparisons made in instruction simplification.
1851 switch (cond->InputAt(0)->GetType()) {
1852 case Primitive::kPrimLong:
1853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetInAt(1, Location::Any());
1855 break;
1856 case Primitive::kPrimFloat:
1857 case Primitive::kPrimDouble:
1858 locations->SetInAt(0, Location::RequiresFpuRegister());
1859 locations->SetInAt(1, Location::Any());
1860 break;
1861 default:
1862 locations->SetInAt(0, Location::RequiresRegister());
1863 locations->SetInAt(1, Location::Any());
1864 break;
1865 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001866 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001867 locations->SetOut(Location::RequiresRegister());
1868 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869}
1870
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001871void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001872 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001873 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001874 }
Mark Mendellc4701932015-04-10 13:18:51 -04001875
1876 LocationSummary* locations = cond->GetLocations();
1877 Location lhs = locations->InAt(0);
1878 Location rhs = locations->InAt(1);
1879 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001880 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001881
1882 switch (cond->InputAt(0)->GetType()) {
1883 default:
1884 // Integer case.
1885
1886 // Clear output register: setcc only sets the low byte.
1887 __ xorl(reg, reg);
1888
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001889 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001890 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001891 return;
1892 case Primitive::kPrimLong:
1893 // Clear output register: setcc only sets the low byte.
1894 __ xorl(reg, reg);
1895
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001896 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001897 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001898 return;
1899 case Primitive::kPrimFloat: {
1900 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1901 if (rhs.IsConstant()) {
1902 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1903 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1904 } else if (rhs.IsStackSlot()) {
1905 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1906 } else {
1907 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1908 }
1909 GenerateFPJumps(cond, &true_label, &false_label);
1910 break;
1911 }
1912 case Primitive::kPrimDouble: {
1913 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1914 if (rhs.IsConstant()) {
1915 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1916 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1917 } else if (rhs.IsDoubleStackSlot()) {
1918 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1919 } else {
1920 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1921 }
1922 GenerateFPJumps(cond, &true_label, &false_label);
1923 break;
1924 }
1925 }
1926
1927 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001928 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001929
Roland Levillain4fa13f62015-07-06 18:11:54 +01001930 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001931 __ Bind(&false_label);
1932 __ xorl(reg, reg);
1933 __ jmp(&done_label);
1934
Roland Levillain4fa13f62015-07-06 18:11:54 +01001935 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001936 __ Bind(&true_label);
1937 __ movl(reg, Immediate(1));
1938 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001939}
1940
1941void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001955}
1956
1957void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001987}
1988
Aart Bike9f37602015-10-09 11:15:55 -07001989void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001991}
1992
1993void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001995}
1996
1997void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001999}
2000
2001void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002003}
2004
2005void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002007}
2008
2009void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002021void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002024 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002025 case Primitive::kPrimBoolean:
2026 case Primitive::kPrimByte:
2027 case Primitive::kPrimShort:
2028 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002029 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002030 case Primitive::kPrimLong: {
2031 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002032 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2034 break;
2035 }
2036 case Primitive::kPrimFloat:
2037 case Primitive::kPrimDouble: {
2038 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002039 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002040 locations->SetOut(Location::RequiresRegister());
2041 break;
2042 }
2043 default:
2044 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2045 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002046}
2047
2048void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002049 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002051 Location left = locations->InAt(0);
2052 Location right = locations->InAt(1);
2053
Mark Mendell0c9497d2015-08-21 09:30:05 -04002054 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002055 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002056 Condition less_cond = kLess;
2057
Calin Juravleddb7df22014-11-25 20:56:51 +00002058 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002059 case Primitive::kPrimBoolean:
2060 case Primitive::kPrimByte:
2061 case Primitive::kPrimShort:
2062 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002063 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002064 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002065 break;
2066 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002067 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002068 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002069 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002070 }
2071 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002072 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2073 if (right.IsConstant()) {
2074 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2075 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2076 } else if (right.IsStackSlot()) {
2077 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2078 } else {
2079 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2080 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002081 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002082 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002083 break;
2084 }
2085 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002086 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2087 if (right.IsConstant()) {
2088 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2089 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2090 } else if (right.IsDoubleStackSlot()) {
2091 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2092 } else {
2093 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2094 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002095 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002096 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002097 break;
2098 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002099 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002100 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002101 }
Aart Bika19616e2016-02-01 18:57:58 -08002102
Calin Juravleddb7df22014-11-25 20:56:51 +00002103 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002104 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002105 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002106
Calin Juravle91debbc2014-11-26 19:01:09 +00002107 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002108 __ movl(out, Immediate(1));
2109 __ jmp(&done);
2110
2111 __ Bind(&less);
2112 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002113
2114 __ Bind(&done);
2115}
2116
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002117void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002118 LocationSummary* locations =
2119 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002120 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002121}
2122
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002123void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002124 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125}
2126
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002127void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2128 LocationSummary* locations =
2129 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2130 locations->SetOut(Location::ConstantLocation(constant));
2131}
2132
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002133void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002134 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002135}
2136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002137void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002138 LocationSummary* locations =
2139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002140 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002141}
2142
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002143void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002144 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002145}
2146
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002147void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2148 LocationSummary* locations =
2149 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2150 locations->SetOut(Location::ConstantLocation(constant));
2151}
2152
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002153void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002154 // Will be generated at use site.
2155}
2156
2157void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2158 LocationSummary* locations =
2159 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2160 locations->SetOut(Location::ConstantLocation(constant));
2161}
2162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002163void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2164 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002165 // Will be generated at use site.
2166}
2167
Igor Murashkind01745e2017-04-05 16:40:31 -07002168void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2169 constructor_fence->SetLocations(nullptr);
2170}
2171
2172void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2173 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2174 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2175}
2176
Calin Juravle27df7582015-04-17 19:12:31 +01002177void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2178 memory_barrier->SetLocations(nullptr);
2179}
2180
2181void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002182 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002183}
2184
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002185void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2186 ret->SetLocations(nullptr);
2187}
2188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002189void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002190 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002191}
2192
2193void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002194 LocationSummary* locations =
2195 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002196 switch (ret->InputAt(0)->GetType()) {
2197 case Primitive::kPrimBoolean:
2198 case Primitive::kPrimByte:
2199 case Primitive::kPrimChar:
2200 case Primitive::kPrimShort:
2201 case Primitive::kPrimInt:
2202 case Primitive::kPrimNot:
2203 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002204 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205 break;
2206
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002207 case Primitive::kPrimFloat:
2208 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002209 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002210 break;
2211
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002213 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002214 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002215}
2216
2217void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2218 if (kIsDebugBuild) {
2219 switch (ret->InputAt(0)->GetType()) {
2220 case Primitive::kPrimBoolean:
2221 case Primitive::kPrimByte:
2222 case Primitive::kPrimChar:
2223 case Primitive::kPrimShort:
2224 case Primitive::kPrimInt:
2225 case Primitive::kPrimNot:
2226 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002227 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228 break;
2229
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 case Primitive::kPrimFloat:
2231 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002232 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 XMM0);
2234 break;
2235
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002236 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002237 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002238 }
2239 }
2240 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002241}
2242
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002243Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2244 switch (type) {
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:
2252 return Location::RegisterLocation(RAX);
2253
2254 case Primitive::kPrimVoid:
2255 return Location::NoLocation();
2256
2257 case Primitive::kPrimDouble:
2258 case Primitive::kPrimFloat:
2259 return Location::FpuRegisterLocation(XMM0);
2260 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002261
2262 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002263}
2264
2265Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2266 return Location::RegisterLocation(kMethodRegisterArgument);
2267}
2268
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002269Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002270 switch (type) {
2271 case Primitive::kPrimBoolean:
2272 case Primitive::kPrimByte:
2273 case Primitive::kPrimChar:
2274 case Primitive::kPrimShort:
2275 case Primitive::kPrimInt:
2276 case Primitive::kPrimNot: {
2277 uint32_t index = gp_index_++;
2278 stack_index_++;
2279 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002280 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002281 } else {
2282 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2283 }
2284 }
2285
2286 case Primitive::kPrimLong: {
2287 uint32_t index = gp_index_;
2288 stack_index_ += 2;
2289 if (index < calling_convention.GetNumberOfRegisters()) {
2290 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002291 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002292 } else {
2293 gp_index_ += 2;
2294 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2295 }
2296 }
2297
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002298 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002299 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002300 stack_index_++;
2301 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002302 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002303 } else {
2304 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2305 }
2306 }
2307
2308 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002309 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002310 stack_index_ += 2;
2311 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002312 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 } else {
2314 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2315 }
2316 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002317
2318 case Primitive::kPrimVoid:
2319 LOG(FATAL) << "Unexpected parameter type " << type;
2320 break;
2321 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002322 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002323}
2324
Calin Juravle175dc732015-08-25 15:42:32 +01002325void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2326 // The trampoline uses the same calling convention as dex calling conventions,
2327 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2328 // the method_idx.
2329 HandleInvoke(invoke);
2330}
2331
2332void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2333 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2334}
2335
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002336void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002337 // Explicit clinit checks triggered by static invokes must have been pruned by
2338 // art::PrepareForRegisterAllocation.
2339 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002340
Mark Mendellfb8d2792015-03-31 22:16:59 -04002341 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002342 if (intrinsic.TryDispatch(invoke)) {
2343 return;
2344 }
2345
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002346 HandleInvoke(invoke);
2347}
2348
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002349static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2350 if (invoke->GetLocations()->Intrinsified()) {
2351 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2352 intrinsic.Dispatch(invoke);
2353 return true;
2354 }
2355 return false;
2356}
2357
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002358void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002359 // Explicit clinit checks triggered by static invokes must have been pruned by
2360 // art::PrepareForRegisterAllocation.
2361 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002362
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002363 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2364 return;
2365 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002366
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002367 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002368 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002369 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002370 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002371}
2372
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002373void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002374 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002375 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002376}
2377
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002378void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002379 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002380 if (intrinsic.TryDispatch(invoke)) {
2381 return;
2382 }
2383
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002384 HandleInvoke(invoke);
2385}
2386
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002387void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002388 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2389 return;
2390 }
2391
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002392 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002393 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002394 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002395}
2396
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002397void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2398 HandleInvoke(invoke);
2399 // Add the hidden argument.
2400 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2401}
2402
2403void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2404 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002405 LocationSummary* locations = invoke->GetLocations();
2406 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2407 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002408 Location receiver = locations->InAt(0);
2409 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2410
Roland Levillain0d5a2812015-11-13 10:07:31 +00002411 // Set the hidden argument. This is safe to do this here, as RAX
2412 // won't be modified thereafter, before the `call` instruction.
2413 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002414 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002415
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002416 if (receiver.IsStackSlot()) {
2417 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002418 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002419 __ movl(temp, Address(temp, class_offset));
2420 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002421 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002422 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002423 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002424 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002425 // Instead of simply (possibly) unpoisoning `temp` here, we should
2426 // emit a read barrier for the previous class reference load.
2427 // However this is not required in practice, as this is an
2428 // intermediate/temporary reference and because the current
2429 // concurrent copying collector keeps the from-space memory
2430 // intact/accessible until the end of the marking phase (the
2431 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002432 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002433 // temp = temp->GetAddressOfIMT()
2434 __ movq(temp,
2435 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2436 // temp = temp->GetImtEntryAt(method_offset);
2437 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002438 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002439 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002440 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002442 __ call(Address(
2443 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444
2445 DCHECK(!codegen_->IsLeafMethod());
2446 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2447}
2448
Orion Hodsonac141392017-01-13 11:53:47 +00002449void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2450 HandleInvoke(invoke);
2451}
2452
2453void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2454 codegen_->GenerateInvokePolymorphicCall(invoke);
2455}
2456
Roland Levillain88cb1752014-10-20 16:36:47 +01002457void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2458 LocationSummary* locations =
2459 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2460 switch (neg->GetResultType()) {
2461 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002462 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002463 locations->SetInAt(0, Location::RequiresRegister());
2464 locations->SetOut(Location::SameAsFirstInput());
2465 break;
2466
Roland Levillain88cb1752014-10-20 16:36:47 +01002467 case Primitive::kPrimFloat:
2468 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002469 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002470 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002471 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002472 break;
2473
2474 default:
2475 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2476 }
2477}
2478
2479void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2480 LocationSummary* locations = neg->GetLocations();
2481 Location out = locations->Out();
2482 Location in = locations->InAt(0);
2483 switch (neg->GetResultType()) {
2484 case Primitive::kPrimInt:
2485 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002486 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002487 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002488 break;
2489
2490 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002491 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002492 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002493 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002494 break;
2495
Roland Levillain5368c212014-11-27 15:03:41 +00002496 case Primitive::kPrimFloat: {
2497 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002498 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002499 // Implement float negation with an exclusive or with value
2500 // 0x80000000 (mask for bit 31, representing the sign of a
2501 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002502 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002504 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002505 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002506
Roland Levillain5368c212014-11-27 15:03:41 +00002507 case Primitive::kPrimDouble: {
2508 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002509 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002510 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002511 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002512 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002513 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002515 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002516 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002517
2518 default:
2519 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2520 }
2521}
2522
Roland Levillaindff1f282014-11-05 14:15:05 +00002523void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2524 LocationSummary* locations =
2525 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2526 Primitive::Type result_type = conversion->GetResultType();
2527 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002528 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002529
David Brazdilb2bd1c52015-03-25 11:17:37 +00002530 // The Java language does not allow treating boolean as an integral type but
2531 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002532
Roland Levillaindff1f282014-11-05 14:15:05 +00002533 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002534 case Primitive::kPrimByte:
2535 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002536 case Primitive::kPrimLong:
2537 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002538 case Primitive::kPrimBoolean:
2539 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
2542 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002543 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002544 locations->SetInAt(0, Location::Any());
2545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2546 break;
2547
2548 default:
2549 LOG(FATAL) << "Unexpected type conversion from " << input_type
2550 << " to " << result_type;
2551 }
2552 break;
2553
Roland Levillain01a8d712014-11-14 16:27:39 +00002554 case Primitive::kPrimShort:
2555 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002556 case Primitive::kPrimLong:
2557 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002558 case Primitive::kPrimBoolean:
2559 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002560 case Primitive::kPrimByte:
2561 case Primitive::kPrimInt:
2562 case Primitive::kPrimChar:
2563 // Processing a Dex `int-to-short' instruction.
2564 locations->SetInAt(0, Location::Any());
2565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2566 break;
2567
2568 default:
2569 LOG(FATAL) << "Unexpected type conversion from " << input_type
2570 << " to " << result_type;
2571 }
2572 break;
2573
Roland Levillain946e1432014-11-11 17:35:19 +00002574 case Primitive::kPrimInt:
2575 switch (input_type) {
2576 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002577 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002578 locations->SetInAt(0, Location::Any());
2579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2580 break;
2581
2582 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002583 // Processing a Dex `float-to-int' instruction.
2584 locations->SetInAt(0, Location::RequiresFpuRegister());
2585 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002586 break;
2587
Roland Levillain946e1432014-11-11 17:35:19 +00002588 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002589 // Processing a Dex `double-to-int' instruction.
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
Roland Levillaindff1f282014-11-05 14:15:05 +00002600 case Primitive::kPrimLong:
2601 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002602 case Primitive::kPrimBoolean:
2603 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002604 case Primitive::kPrimByte:
2605 case Primitive::kPrimShort:
2606 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002607 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002608 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002609 // TODO: We would benefit from a (to-be-implemented)
2610 // Location::RegisterOrStackSlot requirement for this input.
2611 locations->SetInAt(0, Location::RequiresRegister());
2612 locations->SetOut(Location::RequiresRegister());
2613 break;
2614
2615 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002616 // Processing a Dex `float-to-long' instruction.
2617 locations->SetInAt(0, Location::RequiresFpuRegister());
2618 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002619 break;
2620
Roland Levillaindff1f282014-11-05 14:15:05 +00002621 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002622 // Processing a Dex `double-to-long' instruction.
2623 locations->SetInAt(0, Location::RequiresFpuRegister());
2624 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 }
2631 break;
2632
Roland Levillain981e4542014-11-14 11:47:14 +00002633 case Primitive::kPrimChar:
2634 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002635 case Primitive::kPrimLong:
2636 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002637 case Primitive::kPrimBoolean:
2638 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002639 case Primitive::kPrimByte:
2640 case Primitive::kPrimShort:
2641 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002642 // Processing a Dex `int-to-char' instruction.
2643 locations->SetInAt(0, Location::Any());
2644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2645 break;
2646
2647 default:
2648 LOG(FATAL) << "Unexpected type conversion from " << input_type
2649 << " to " << result_type;
2650 }
2651 break;
2652
Roland Levillaindff1f282014-11-05 14:15:05 +00002653 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002654 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002655 case Primitive::kPrimBoolean:
2656 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002657 case Primitive::kPrimByte:
2658 case Primitive::kPrimShort:
2659 case Primitive::kPrimInt:
2660 case Primitive::kPrimChar:
2661 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002662 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002663 locations->SetOut(Location::RequiresFpuRegister());
2664 break;
2665
2666 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002667 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002668 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002669 locations->SetOut(Location::RequiresFpuRegister());
2670 break;
2671
Roland Levillaincff13742014-11-17 14:32:17 +00002672 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002673 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002674 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002675 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
2681 };
2682 break;
2683
Roland Levillaindff1f282014-11-05 14:15:05 +00002684 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002685 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002686 case Primitive::kPrimBoolean:
2687 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002688 case Primitive::kPrimByte:
2689 case Primitive::kPrimShort:
2690 case Primitive::kPrimInt:
2691 case Primitive::kPrimChar:
2692 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002693 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002694 locations->SetOut(Location::RequiresFpuRegister());
2695 break;
2696
2697 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002698 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002699 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002700 locations->SetOut(Location::RequiresFpuRegister());
2701 break;
2702
Roland Levillaincff13742014-11-17 14:32:17 +00002703 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002704 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002705 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002706 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002707 break;
2708
2709 default:
2710 LOG(FATAL) << "Unexpected type conversion from " << input_type
2711 << " to " << result_type;
2712 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002713 break;
2714
2715 default:
2716 LOG(FATAL) << "Unexpected type conversion from " << input_type
2717 << " to " << result_type;
2718 }
2719}
2720
2721void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2722 LocationSummary* locations = conversion->GetLocations();
2723 Location out = locations->Out();
2724 Location in = locations->InAt(0);
2725 Primitive::Type result_type = conversion->GetResultType();
2726 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002727 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002728 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002729 case Primitive::kPrimByte:
2730 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002731 case Primitive::kPrimLong:
2732 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002733 case Primitive::kPrimBoolean:
2734 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002735 case Primitive::kPrimShort:
2736 case Primitive::kPrimInt:
2737 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002738 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002739 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002740 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002741 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002743 Address(CpuRegister(RSP), in.GetStackIndex()));
2744 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002745 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002746 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002747 }
2748 break;
2749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
2753 }
2754 break;
2755
Roland Levillain01a8d712014-11-14 16:27:39 +00002756 case Primitive::kPrimShort:
2757 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002758 case Primitive::kPrimLong:
2759 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002760 case Primitive::kPrimBoolean:
2761 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002762 case Primitive::kPrimByte:
2763 case Primitive::kPrimInt:
2764 case Primitive::kPrimChar:
2765 // Processing a Dex `int-to-short' instruction.
2766 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002768 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002770 Address(CpuRegister(RSP), in.GetStackIndex()));
2771 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002773 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002774 }
2775 break;
2776
2777 default:
2778 LOG(FATAL) << "Unexpected type conversion from " << input_type
2779 << " to " << result_type;
2780 }
2781 break;
2782
Roland Levillain946e1432014-11-11 17:35:19 +00002783 case Primitive::kPrimInt:
2784 switch (input_type) {
2785 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002786 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002787 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002788 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002789 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002791 Address(CpuRegister(RSP), in.GetStackIndex()));
2792 } else {
2793 DCHECK(in.IsConstant());
2794 DCHECK(in.GetConstant()->IsLongConstant());
2795 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002797 }
2798 break;
2799
Roland Levillain3f8f9362014-12-02 17:45:01 +00002800 case Primitive::kPrimFloat: {
2801 // Processing a Dex `float-to-int' instruction.
2802 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2803 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002804 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002805
2806 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002807 // if input >= (float)INT_MAX goto done
2808 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002809 __ j(kAboveEqual, &done);
2810 // if input == NaN goto nan
2811 __ j(kUnordered, &nan);
2812 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002813 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002814 __ jmp(&done);
2815 __ Bind(&nan);
2816 // output = 0
2817 __ xorl(output, output);
2818 __ Bind(&done);
2819 break;
2820 }
2821
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002822 case Primitive::kPrimDouble: {
2823 // Processing a Dex `double-to-int' instruction.
2824 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2825 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002826 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002827
2828 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002829 // if input >= (double)INT_MAX goto done
2830 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002831 __ j(kAboveEqual, &done);
2832 // if input == NaN goto nan
2833 __ j(kUnordered, &nan);
2834 // output = double-to-int-truncate(input)
2835 __ cvttsd2si(output, input);
2836 __ jmp(&done);
2837 __ Bind(&nan);
2838 // output = 0
2839 __ xorl(output, output);
2840 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002841 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002842 }
Roland Levillain946e1432014-11-11 17:35:19 +00002843
2844 default:
2845 LOG(FATAL) << "Unexpected type conversion from " << input_type
2846 << " to " << result_type;
2847 }
2848 break;
2849
Roland Levillaindff1f282014-11-05 14:15:05 +00002850 case Primitive::kPrimLong:
2851 switch (input_type) {
2852 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002853 case Primitive::kPrimBoolean:
2854 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002855 case Primitive::kPrimByte:
2856 case Primitive::kPrimShort:
2857 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002858 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002859 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002860 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002861 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002862 break;
2863
Roland Levillain624279f2014-12-04 11:54:28 +00002864 case Primitive::kPrimFloat: {
2865 // Processing a Dex `float-to-long' instruction.
2866 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2867 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002868 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002869
Mark Mendell92e83bf2015-05-07 11:25:03 -04002870 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002871 // if input >= (float)LONG_MAX goto done
2872 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002873 __ j(kAboveEqual, &done);
2874 // if input == NaN goto nan
2875 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002876 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002877 __ cvttss2si(output, input, true);
2878 __ jmp(&done);
2879 __ Bind(&nan);
2880 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002881 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002882 __ Bind(&done);
2883 break;
2884 }
2885
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002886 case Primitive::kPrimDouble: {
2887 // Processing a Dex `double-to-long' instruction.
2888 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2889 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002890 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002891
Mark Mendell92e83bf2015-05-07 11:25:03 -04002892 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002893 // if input >= (double)LONG_MAX goto done
2894 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002895 __ j(kAboveEqual, &done);
2896 // if input == NaN goto nan
2897 __ j(kUnordered, &nan);
2898 // output = double-to-long-truncate(input)
2899 __ cvttsd2si(output, input, true);
2900 __ jmp(&done);
2901 __ Bind(&nan);
2902 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002903 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002904 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002905 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002906 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002907
2908 default:
2909 LOG(FATAL) << "Unexpected type conversion from " << input_type
2910 << " to " << result_type;
2911 }
2912 break;
2913
Roland Levillain981e4542014-11-14 11:47:14 +00002914 case Primitive::kPrimChar:
2915 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002916 case Primitive::kPrimLong:
2917 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002918 case Primitive::kPrimBoolean:
2919 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002920 case Primitive::kPrimByte:
2921 case Primitive::kPrimShort:
2922 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002923 // Processing a Dex `int-to-char' instruction.
2924 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002925 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002926 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002927 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002928 Address(CpuRegister(RSP), in.GetStackIndex()));
2929 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002931 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002932 }
2933 break;
2934
2935 default:
2936 LOG(FATAL) << "Unexpected type conversion from " << input_type
2937 << " to " << result_type;
2938 }
2939 break;
2940
Roland Levillaindff1f282014-11-05 14:15:05 +00002941 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002942 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002943 case Primitive::kPrimBoolean:
2944 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002945 case Primitive::kPrimByte:
2946 case Primitive::kPrimShort:
2947 case Primitive::kPrimInt:
2948 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002949 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002950 if (in.IsRegister()) {
2951 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2952 } else if (in.IsConstant()) {
2953 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2954 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002955 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002956 } else {
2957 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2958 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2959 }
Roland Levillaincff13742014-11-17 14:32:17 +00002960 break;
2961
2962 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002963 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002964 if (in.IsRegister()) {
2965 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2966 } else if (in.IsConstant()) {
2967 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2968 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002969 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002970 } else {
2971 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2972 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2973 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002974 break;
2975
Roland Levillaincff13742014-11-17 14:32:17 +00002976 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002977 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002978 if (in.IsFpuRegister()) {
2979 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2980 } else if (in.IsConstant()) {
2981 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2982 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002983 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002984 } else {
2985 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2986 Address(CpuRegister(RSP), in.GetStackIndex()));
2987 }
Roland Levillaincff13742014-11-17 14:32:17 +00002988 break;
2989
2990 default:
2991 LOG(FATAL) << "Unexpected type conversion from " << input_type
2992 << " to " << result_type;
2993 };
2994 break;
2995
Roland Levillaindff1f282014-11-05 14:15:05 +00002996 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002997 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002998 case Primitive::kPrimBoolean:
2999 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003000 case Primitive::kPrimByte:
3001 case Primitive::kPrimShort:
3002 case Primitive::kPrimInt:
3003 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003004 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003005 if (in.IsRegister()) {
3006 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3007 } else if (in.IsConstant()) {
3008 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3009 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003010 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003011 } else {
3012 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3013 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3014 }
Roland Levillaincff13742014-11-17 14:32:17 +00003015 break;
3016
3017 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003018 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003019 if (in.IsRegister()) {
3020 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3021 } else if (in.IsConstant()) {
3022 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3023 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003024 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003025 } else {
3026 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3027 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3028 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003029 break;
3030
Roland Levillaincff13742014-11-17 14:32:17 +00003031 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003032 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003033 if (in.IsFpuRegister()) {
3034 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3035 } else if (in.IsConstant()) {
3036 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3037 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003038 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003039 } else {
3040 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3041 Address(CpuRegister(RSP), in.GetStackIndex()));
3042 }
Roland Levillaincff13742014-11-17 14:32:17 +00003043 break;
3044
3045 default:
3046 LOG(FATAL) << "Unexpected type conversion from " << input_type
3047 << " to " << result_type;
3048 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003049 break;
3050
3051 default:
3052 LOG(FATAL) << "Unexpected type conversion from " << input_type
3053 << " to " << result_type;
3054 }
3055}
3056
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003057void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003058 LocationSummary* locations =
3059 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003060 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003061 case Primitive::kPrimInt: {
3062 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003063 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3064 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003065 break;
3066 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003067
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003068 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003069 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003070 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003071 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003073 break;
3074 }
3075
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003076 case Primitive::kPrimDouble:
3077 case Primitive::kPrimFloat: {
3078 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003079 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003080 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003081 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003082 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003083
3084 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003085 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003086 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003087}
3088
3089void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3090 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003091 Location first = locations->InAt(0);
3092 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003093 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003094
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003095 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003096 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003097 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003098 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3099 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003100 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3101 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003102 } else {
3103 __ leal(out.AsRegister<CpuRegister>(), Address(
3104 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3105 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003106 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003107 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3108 __ addl(out.AsRegister<CpuRegister>(),
3109 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3110 } else {
3111 __ leal(out.AsRegister<CpuRegister>(), Address(
3112 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3113 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003114 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003115 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003116 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003117 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003118 break;
3119 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003120
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003121 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003122 if (second.IsRegister()) {
3123 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3124 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003125 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3126 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003127 } else {
3128 __ leaq(out.AsRegister<CpuRegister>(), Address(
3129 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3130 }
3131 } else {
3132 DCHECK(second.IsConstant());
3133 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3134 int32_t int32_value = Low32Bits(value);
3135 DCHECK_EQ(int32_value, value);
3136 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3137 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3138 } else {
3139 __ leaq(out.AsRegister<CpuRegister>(), Address(
3140 first.AsRegister<CpuRegister>(), int32_value));
3141 }
3142 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003143 break;
3144 }
3145
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003146 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003147 if (second.IsFpuRegister()) {
3148 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3149 } else if (second.IsConstant()) {
3150 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003151 codegen_->LiteralFloatAddress(
3152 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003153 } else {
3154 DCHECK(second.IsStackSlot());
3155 __ addss(first.AsFpuRegister<XmmRegister>(),
3156 Address(CpuRegister(RSP), second.GetStackIndex()));
3157 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003158 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003159 }
3160
3161 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003162 if (second.IsFpuRegister()) {
3163 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3164 } else if (second.IsConstant()) {
3165 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003166 codegen_->LiteralDoubleAddress(
3167 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003168 } else {
3169 DCHECK(second.IsDoubleStackSlot());
3170 __ addsd(first.AsFpuRegister<XmmRegister>(),
3171 Address(CpuRegister(RSP), second.GetStackIndex()));
3172 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003173 break;
3174 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003175
3176 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003177 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003178 }
3179}
3180
3181void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003182 LocationSummary* locations =
3183 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003184 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003185 case Primitive::kPrimInt: {
3186 locations->SetInAt(0, Location::RequiresRegister());
3187 locations->SetInAt(1, Location::Any());
3188 locations->SetOut(Location::SameAsFirstInput());
3189 break;
3190 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003191 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003192 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003193 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003194 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003195 break;
3196 }
Calin Juravle11351682014-10-23 15:38:15 +01003197 case Primitive::kPrimFloat:
3198 case Primitive::kPrimDouble: {
3199 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003200 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003201 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003202 break;
Calin Juravle11351682014-10-23 15:38:15 +01003203 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003204 default:
Calin Juravle11351682014-10-23 15:38:15 +01003205 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003206 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003207}
3208
3209void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3210 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003211 Location first = locations->InAt(0);
3212 Location second = locations->InAt(1);
3213 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003214 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003215 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003216 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003218 } else if (second.IsConstant()) {
3219 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003220 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003221 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003223 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003224 break;
3225 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003226 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003227 if (second.IsConstant()) {
3228 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3229 DCHECK(IsInt<32>(value));
3230 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3231 } else {
3232 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3233 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003234 break;
3235 }
3236
Calin Juravle11351682014-10-23 15:38:15 +01003237 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003238 if (second.IsFpuRegister()) {
3239 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3240 } else if (second.IsConstant()) {
3241 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003242 codegen_->LiteralFloatAddress(
3243 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003244 } else {
3245 DCHECK(second.IsStackSlot());
3246 __ subss(first.AsFpuRegister<XmmRegister>(),
3247 Address(CpuRegister(RSP), second.GetStackIndex()));
3248 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003249 break;
Calin Juravle11351682014-10-23 15:38:15 +01003250 }
3251
3252 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003253 if (second.IsFpuRegister()) {
3254 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3255 } else if (second.IsConstant()) {
3256 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003257 codegen_->LiteralDoubleAddress(
3258 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003259 } else {
3260 DCHECK(second.IsDoubleStackSlot());
3261 __ subsd(first.AsFpuRegister<XmmRegister>(),
3262 Address(CpuRegister(RSP), second.GetStackIndex()));
3263 }
Calin Juravle11351682014-10-23 15:38:15 +01003264 break;
3265 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003266
3267 default:
Calin Juravle11351682014-10-23 15:38:15 +01003268 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003269 }
3270}
3271
Calin Juravle34bacdf2014-10-07 20:23:36 +01003272void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3273 LocationSummary* locations =
3274 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3275 switch (mul->GetResultType()) {
3276 case Primitive::kPrimInt: {
3277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003279 if (mul->InputAt(1)->IsIntConstant()) {
3280 // Can use 3 operand multiply.
3281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3282 } else {
3283 locations->SetOut(Location::SameAsFirstInput());
3284 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003285 break;
3286 }
3287 case Primitive::kPrimLong: {
3288 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003289 locations->SetInAt(1, Location::Any());
3290 if (mul->InputAt(1)->IsLongConstant() &&
3291 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003292 // Can use 3 operand multiply.
3293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3294 } else {
3295 locations->SetOut(Location::SameAsFirstInput());
3296 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003297 break;
3298 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003299 case Primitive::kPrimFloat:
3300 case Primitive::kPrimDouble: {
3301 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003302 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003303 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003304 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003305 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003306
3307 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003308 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003309 }
3310}
3311
3312void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3313 LocationSummary* locations = mul->GetLocations();
3314 Location first = locations->InAt(0);
3315 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003316 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003317 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003318 case Primitive::kPrimInt:
3319 // The constant may have ended up in a register, so test explicitly to avoid
3320 // problems where the output may not be the same as the first operand.
3321 if (mul->InputAt(1)->IsIntConstant()) {
3322 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3323 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3324 } else if (second.IsRegister()) {
3325 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003327 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003328 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003330 __ imull(first.AsRegister<CpuRegister>(),
3331 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003332 }
3333 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003335 // The constant may have ended up in a register, so test explicitly to avoid
3336 // problems where the output may not be the same as the first operand.
3337 if (mul->InputAt(1)->IsLongConstant()) {
3338 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3339 if (IsInt<32>(value)) {
3340 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3341 Immediate(static_cast<int32_t>(value)));
3342 } else {
3343 // Have to use the constant area.
3344 DCHECK(first.Equals(out));
3345 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3346 }
3347 } else if (second.IsRegister()) {
3348 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003349 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003350 } else {
3351 DCHECK(second.IsDoubleStackSlot());
3352 DCHECK(first.Equals(out));
3353 __ imulq(first.AsRegister<CpuRegister>(),
3354 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003355 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003356 break;
3357 }
3358
Calin Juravleb5bfa962014-10-21 18:02:24 +01003359 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003360 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003361 if (second.IsFpuRegister()) {
3362 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3363 } else if (second.IsConstant()) {
3364 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003365 codegen_->LiteralFloatAddress(
3366 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003367 } else {
3368 DCHECK(second.IsStackSlot());
3369 __ mulss(first.AsFpuRegister<XmmRegister>(),
3370 Address(CpuRegister(RSP), second.GetStackIndex()));
3371 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003372 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003373 }
3374
3375 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003376 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003377 if (second.IsFpuRegister()) {
3378 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3379 } else if (second.IsConstant()) {
3380 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003381 codegen_->LiteralDoubleAddress(
3382 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003383 } else {
3384 DCHECK(second.IsDoubleStackSlot());
3385 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3386 Address(CpuRegister(RSP), second.GetStackIndex()));
3387 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003388 break;
3389 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003390
3391 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003392 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003393 }
3394}
3395
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003396void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3397 uint32_t stack_adjustment, bool is_float) {
3398 if (source.IsStackSlot()) {
3399 DCHECK(is_float);
3400 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3401 } else if (source.IsDoubleStackSlot()) {
3402 DCHECK(!is_float);
3403 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3404 } else {
3405 // Write the value to the temporary location on the stack and load to FP stack.
3406 if (is_float) {
3407 Location stack_temp = Location::StackSlot(temp_offset);
3408 codegen_->Move(stack_temp, source);
3409 __ flds(Address(CpuRegister(RSP), temp_offset));
3410 } else {
3411 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3412 codegen_->Move(stack_temp, source);
3413 __ fldl(Address(CpuRegister(RSP), temp_offset));
3414 }
3415 }
3416}
3417
3418void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3419 Primitive::Type type = rem->GetResultType();
3420 bool is_float = type == Primitive::kPrimFloat;
3421 size_t elem_size = Primitive::ComponentSize(type);
3422 LocationSummary* locations = rem->GetLocations();
3423 Location first = locations->InAt(0);
3424 Location second = locations->InAt(1);
3425 Location out = locations->Out();
3426
3427 // Create stack space for 2 elements.
3428 // TODO: enhance register allocator to ask for stack temporaries.
3429 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3430
3431 // Load the values to the FP stack in reverse order, using temporaries if needed.
3432 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3433 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3434
3435 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003436 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003437 __ Bind(&retry);
3438 __ fprem();
3439
3440 // Move FP status to AX.
3441 __ fstsw();
3442
3443 // And see if the argument reduction is complete. This is signaled by the
3444 // C2 FPU flag bit set to 0.
3445 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3446 __ j(kNotEqual, &retry);
3447
3448 // We have settled on the final value. Retrieve it into an XMM register.
3449 // Store FP top of stack to real stack.
3450 if (is_float) {
3451 __ fsts(Address(CpuRegister(RSP), 0));
3452 } else {
3453 __ fstl(Address(CpuRegister(RSP), 0));
3454 }
3455
3456 // Pop the 2 items from the FP stack.
3457 __ fucompp();
3458
3459 // Load the value from the stack into an XMM register.
3460 DCHECK(out.IsFpuRegister()) << out;
3461 if (is_float) {
3462 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3463 } else {
3464 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3465 }
3466
3467 // And remove the temporary stack space we allocated.
3468 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3469}
3470
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3472 DCHECK(instruction->IsDiv() || instruction->IsRem());
3473
3474 LocationSummary* locations = instruction->GetLocations();
3475 Location second = locations->InAt(1);
3476 DCHECK(second.IsConstant());
3477
3478 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3479 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003480 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481
3482 DCHECK(imm == 1 || imm == -1);
3483
3484 switch (instruction->GetResultType()) {
3485 case Primitive::kPrimInt: {
3486 if (instruction->IsRem()) {
3487 __ xorl(output_register, output_register);
3488 } else {
3489 __ movl(output_register, input_register);
3490 if (imm == -1) {
3491 __ negl(output_register);
3492 }
3493 }
3494 break;
3495 }
3496
3497 case Primitive::kPrimLong: {
3498 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003499 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 } else {
3501 __ movq(output_register, input_register);
3502 if (imm == -1) {
3503 __ negq(output_register);
3504 }
3505 }
3506 break;
3507 }
3508
3509 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003510 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511 }
3512}
3513
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003514void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003515 LocationSummary* locations = instruction->GetLocations();
3516 Location second = locations->InAt(1);
3517
3518 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3519 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3520
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003521 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003522 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3523 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003524
3525 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3526
3527 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003528 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003529 __ testl(numerator, numerator);
3530 __ cmov(kGreaterEqual, tmp, numerator);
3531 int shift = CTZ(imm);
3532 __ sarl(tmp, Immediate(shift));
3533
3534 if (imm < 0) {
3535 __ negl(tmp);
3536 }
3537
3538 __ movl(output_register, tmp);
3539 } else {
3540 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3541 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3542
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003543 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 __ addq(rdx, numerator);
3545 __ testq(numerator, numerator);
3546 __ cmov(kGreaterEqual, rdx, numerator);
3547 int shift = CTZ(imm);
3548 __ sarq(rdx, Immediate(shift));
3549
3550 if (imm < 0) {
3551 __ negq(rdx);
3552 }
3553
3554 __ movq(output_register, rdx);
3555 }
3556}
3557
3558void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3559 DCHECK(instruction->IsDiv() || instruction->IsRem());
3560
3561 LocationSummary* locations = instruction->GetLocations();
3562 Location second = locations->InAt(1);
3563
3564 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3565 : locations->GetTemp(0).AsRegister<CpuRegister>();
3566 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3567 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3568 : locations->Out().AsRegister<CpuRegister>();
3569 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3570
3571 DCHECK_EQ(RAX, eax.AsRegister());
3572 DCHECK_EQ(RDX, edx.AsRegister());
3573 if (instruction->IsDiv()) {
3574 DCHECK_EQ(RAX, out.AsRegister());
3575 } else {
3576 DCHECK_EQ(RDX, out.AsRegister());
3577 }
3578
3579 int64_t magic;
3580 int shift;
3581
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003582 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 if (instruction->GetResultType() == Primitive::kPrimInt) {
3584 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3585
3586 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3587
3588 __ movl(numerator, eax);
3589
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 __ movl(eax, Immediate(magic));
3591 __ imull(numerator);
3592
3593 if (imm > 0 && magic < 0) {
3594 __ addl(edx, numerator);
3595 } else if (imm < 0 && magic > 0) {
3596 __ subl(edx, numerator);
3597 }
3598
3599 if (shift != 0) {
3600 __ sarl(edx, Immediate(shift));
3601 }
3602
3603 __ movl(eax, edx);
3604 __ shrl(edx, Immediate(31));
3605 __ addl(edx, eax);
3606
3607 if (instruction->IsRem()) {
3608 __ movl(eax, numerator);
3609 __ imull(edx, Immediate(imm));
3610 __ subl(eax, edx);
3611 __ movl(edx, eax);
3612 } else {
3613 __ movl(eax, edx);
3614 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 } else {
3616 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3617
3618 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3619
3620 CpuRegister rax = eax;
3621 CpuRegister rdx = edx;
3622
3623 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3624
3625 // Save the numerator.
3626 __ movq(numerator, rax);
3627
3628 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003629 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003630
3631 // RDX:RAX = magic * numerator
3632 __ imulq(numerator);
3633
3634 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003635 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003636 __ addq(rdx, numerator);
3637 } else if (imm < 0 && magic > 0) {
3638 // RDX -= numerator
3639 __ subq(rdx, numerator);
3640 }
3641
3642 // Shift if needed.
3643 if (shift != 0) {
3644 __ sarq(rdx, Immediate(shift));
3645 }
3646
3647 // RDX += 1 if RDX < 0
3648 __ movq(rax, rdx);
3649 __ shrq(rdx, Immediate(63));
3650 __ addq(rdx, rax);
3651
3652 if (instruction->IsRem()) {
3653 __ movq(rax, numerator);
3654
3655 if (IsInt<32>(imm)) {
3656 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3657 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003658 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003659 }
3660
3661 __ subq(rax, rdx);
3662 __ movq(rdx, rax);
3663 } else {
3664 __ movq(rax, rdx);
3665 }
3666 }
3667}
3668
Calin Juravlebacfec32014-11-14 15:54:36 +00003669void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3670 DCHECK(instruction->IsDiv() || instruction->IsRem());
3671 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003672 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravlebacfec32014-11-14 15:54:36 +00003673
3674 bool is_div = instruction->IsDiv();
3675 LocationSummary* locations = instruction->GetLocations();
3676
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003677 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3678 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003679
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003681 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003682
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003683 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003684 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003685
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003686 if (imm == 0) {
3687 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3688 } else if (imm == 1 || imm == -1) {
3689 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003690 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003691 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003692 } else {
3693 DCHECK(imm <= -2 || imm >= 2);
3694 GenerateDivRemWithAnyConstant(instruction);
3695 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003696 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003697 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003698 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003699 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003700 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003701
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003702 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3703 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3704 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3705 // so it's safe to just use negl instead of more complex comparisons.
3706 if (type == Primitive::kPrimInt) {
3707 __ cmpl(second_reg, Immediate(-1));
3708 __ j(kEqual, slow_path->GetEntryLabel());
3709 // edx:eax <- sign-extended of eax
3710 __ cdq();
3711 // eax = quotient, edx = remainder
3712 __ idivl(second_reg);
3713 } else {
3714 __ cmpq(second_reg, Immediate(-1));
3715 __ j(kEqual, slow_path->GetEntryLabel());
3716 // rdx:rax <- sign-extended of rax
3717 __ cqo();
3718 // rax = quotient, rdx = remainder
3719 __ idivq(second_reg);
3720 }
3721 __ Bind(slow_path->GetExitLabel());
3722 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003723}
3724
Calin Juravle7c4954d2014-10-28 16:57:40 +00003725void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3726 LocationSummary* locations =
3727 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3728 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003729 case Primitive::kPrimInt:
3730 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003731 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003732 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003733 locations->SetOut(Location::SameAsFirstInput());
3734 // Intel uses edx:eax as the dividend.
3735 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003736 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3737 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3738 // output and request another temp.
3739 if (div->InputAt(1)->IsConstant()) {
3740 locations->AddTemp(Location::RequiresRegister());
3741 }
Calin Juravled0d48522014-11-04 16:40:20 +00003742 break;
3743 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003744
Calin Juravle7c4954d2014-10-28 16:57:40 +00003745 case Primitive::kPrimFloat:
3746 case Primitive::kPrimDouble: {
3747 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003748 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003749 locations->SetOut(Location::SameAsFirstInput());
3750 break;
3751 }
3752
3753 default:
3754 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3755 }
3756}
3757
3758void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3759 LocationSummary* locations = div->GetLocations();
3760 Location first = locations->InAt(0);
3761 Location second = locations->InAt(1);
3762 DCHECK(first.Equals(locations->Out()));
3763
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003764 Primitive::Type type = div->GetResultType();
3765 switch (type) {
3766 case Primitive::kPrimInt:
3767 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003768 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003769 break;
3770 }
3771
Calin Juravle7c4954d2014-10-28 16:57:40 +00003772 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003773 if (second.IsFpuRegister()) {
3774 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3775 } else if (second.IsConstant()) {
3776 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003777 codegen_->LiteralFloatAddress(
3778 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003779 } else {
3780 DCHECK(second.IsStackSlot());
3781 __ divss(first.AsFpuRegister<XmmRegister>(),
3782 Address(CpuRegister(RSP), second.GetStackIndex()));
3783 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003784 break;
3785 }
3786
3787 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003788 if (second.IsFpuRegister()) {
3789 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3790 } else if (second.IsConstant()) {
3791 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003792 codegen_->LiteralDoubleAddress(
3793 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003794 } else {
3795 DCHECK(second.IsDoubleStackSlot());
3796 __ divsd(first.AsFpuRegister<XmmRegister>(),
3797 Address(CpuRegister(RSP), second.GetStackIndex()));
3798 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003799 break;
3800 }
3801
3802 default:
3803 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3804 }
3805}
3806
Calin Juravlebacfec32014-11-14 15:54:36 +00003807void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003808 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003809 LocationSummary* locations =
3810 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003811
3812 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003813 case Primitive::kPrimInt:
3814 case Primitive::kPrimLong: {
3815 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003816 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003817 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3818 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003819 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3820 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3821 // output and request another temp.
3822 if (rem->InputAt(1)->IsConstant()) {
3823 locations->AddTemp(Location::RequiresRegister());
3824 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003825 break;
3826 }
3827
3828 case Primitive::kPrimFloat:
3829 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003830 locations->SetInAt(0, Location::Any());
3831 locations->SetInAt(1, Location::Any());
3832 locations->SetOut(Location::RequiresFpuRegister());
3833 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003834 break;
3835 }
3836
3837 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003838 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003839 }
3840}
3841
3842void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3843 Primitive::Type type = rem->GetResultType();
3844 switch (type) {
3845 case Primitive::kPrimInt:
3846 case Primitive::kPrimLong: {
3847 GenerateDivRemIntegral(rem);
3848 break;
3849 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003850 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003851 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003852 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003853 break;
3854 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003855 default:
3856 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3857 }
3858}
3859
Calin Juravled0d48522014-11-04 16:40:20 +00003860void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003861 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003862 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003863}
3864
3865void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003866 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003867 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3868 codegen_->AddSlowPath(slow_path);
3869
3870 LocationSummary* locations = instruction->GetLocations();
3871 Location value = locations->InAt(0);
3872
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003873 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003874 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003875 case Primitive::kPrimByte:
3876 case Primitive::kPrimChar:
3877 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003878 case Primitive::kPrimInt: {
3879 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003880 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003881 __ j(kEqual, slow_path->GetEntryLabel());
3882 } else if (value.IsStackSlot()) {
3883 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3884 __ j(kEqual, slow_path->GetEntryLabel());
3885 } else {
3886 DCHECK(value.IsConstant()) << value;
3887 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003888 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003889 }
3890 }
3891 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003892 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003893 case Primitive::kPrimLong: {
3894 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003895 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003896 __ j(kEqual, slow_path->GetEntryLabel());
3897 } else if (value.IsDoubleStackSlot()) {
3898 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3899 __ j(kEqual, slow_path->GetEntryLabel());
3900 } else {
3901 DCHECK(value.IsConstant()) << value;
3902 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003903 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003904 }
3905 }
3906 break;
3907 }
3908 default:
3909 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003910 }
Calin Juravled0d48522014-11-04 16:40:20 +00003911}
3912
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3914 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3915
3916 LocationSummary* locations =
3917 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3918
3919 switch (op->GetResultType()) {
3920 case Primitive::kPrimInt:
3921 case Primitive::kPrimLong: {
3922 locations->SetInAt(0, Location::RequiresRegister());
3923 // The shift count needs to be in CL.
3924 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3925 locations->SetOut(Location::SameAsFirstInput());
3926 break;
3927 }
3928 default:
3929 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3930 }
3931}
3932
3933void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3934 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3935
3936 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 Location second = locations->InAt(1);
3939
3940 switch (op->GetResultType()) {
3941 case Primitive::kPrimInt: {
3942 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944 if (op->IsShl()) {
3945 __ shll(first_reg, second_reg);
3946 } else if (op->IsShr()) {
3947 __ sarl(first_reg, second_reg);
3948 } else {
3949 __ shrl(first_reg, second_reg);
3950 }
3951 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003952 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003953 if (op->IsShl()) {
3954 __ shll(first_reg, imm);
3955 } else if (op->IsShr()) {
3956 __ sarl(first_reg, imm);
3957 } else {
3958 __ shrl(first_reg, imm);
3959 }
3960 }
3961 break;
3962 }
3963 case Primitive::kPrimLong: {
3964 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003965 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003966 if (op->IsShl()) {
3967 __ shlq(first_reg, second_reg);
3968 } else if (op->IsShr()) {
3969 __ sarq(first_reg, second_reg);
3970 } else {
3971 __ shrq(first_reg, second_reg);
3972 }
3973 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003974 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003975 if (op->IsShl()) {
3976 __ shlq(first_reg, imm);
3977 } else if (op->IsShr()) {
3978 __ sarq(first_reg, imm);
3979 } else {
3980 __ shrq(first_reg, imm);
3981 }
3982 }
3983 break;
3984 }
3985 default:
3986 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003987 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003988 }
3989}
3990
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003991void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3992 LocationSummary* locations =
3993 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3994
3995 switch (ror->GetResultType()) {
3996 case Primitive::kPrimInt:
3997 case Primitive::kPrimLong: {
3998 locations->SetInAt(0, Location::RequiresRegister());
3999 // The shift count needs to be in CL (unless it is a constant).
4000 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4001 locations->SetOut(Location::SameAsFirstInput());
4002 break;
4003 }
4004 default:
4005 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4006 UNREACHABLE();
4007 }
4008}
4009
4010void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4011 LocationSummary* locations = ror->GetLocations();
4012 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4013 Location second = locations->InAt(1);
4014
4015 switch (ror->GetResultType()) {
4016 case Primitive::kPrimInt:
4017 if (second.IsRegister()) {
4018 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4019 __ rorl(first_reg, second_reg);
4020 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004021 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004022 __ rorl(first_reg, imm);
4023 }
4024 break;
4025 case Primitive::kPrimLong:
4026 if (second.IsRegister()) {
4027 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4028 __ rorq(first_reg, second_reg);
4029 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004030 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004031 __ rorq(first_reg, imm);
4032 }
4033 break;
4034 default:
4035 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4036 UNREACHABLE();
4037 }
4038}
4039
Calin Juravle9aec02f2014-11-18 23:06:35 +00004040void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4041 HandleShift(shl);
4042}
4043
4044void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4045 HandleShift(shl);
4046}
4047
4048void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4049 HandleShift(shr);
4050}
4051
4052void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4053 HandleShift(shr);
4054}
4055
4056void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4057 HandleShift(ushr);
4058}
4059
4060void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4061 HandleShift(ushr);
4062}
4063
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004064void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004065 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004067 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004068 if (instruction->IsStringAlloc()) {
4069 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4070 } else {
4071 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004072 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004073 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004074}
4075
4076void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004077 // Note: if heap poisoning is enabled, the entry point takes cares
4078 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004079 if (instruction->IsStringAlloc()) {
4080 // String is allocated through StringFactory. Call NewEmptyString entry point.
4081 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004082 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004083 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4084 __ call(Address(temp, code_offset.SizeValue()));
4085 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4086 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004087 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004088 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004089 DCHECK(!codegen_->IsLeafMethod());
4090 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004091}
4092
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004093void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4094 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004095 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004096 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004097 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004098 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4099 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004100}
4101
4102void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004103 // Note: if heap poisoning is enabled, the entry point takes cares
4104 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004105 QuickEntrypointEnum entrypoint =
4106 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4107 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004108 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004109 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004110}
4111
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004112void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004113 LocationSummary* locations =
4114 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004115 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4116 if (location.IsStackSlot()) {
4117 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4118 } else if (location.IsDoubleStackSlot()) {
4119 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4120 }
4121 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004122}
4123
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004124void InstructionCodeGeneratorX86_64::VisitParameterValue(
4125 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004126 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004127}
4128
4129void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4130 LocationSummary* locations =
4131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4132 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4133}
4134
4135void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4136 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4137 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004138}
4139
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004140void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4141 LocationSummary* locations =
4142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4143 locations->SetInAt(0, Location::RequiresRegister());
4144 locations->SetOut(Location::RequiresRegister());
4145}
4146
4147void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4148 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004149 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004150 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004151 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004152 __ movq(locations->Out().AsRegister<CpuRegister>(),
4153 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004154 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004155 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004156 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004157 __ movq(locations->Out().AsRegister<CpuRegister>(),
4158 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4159 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004160 __ movq(locations->Out().AsRegister<CpuRegister>(),
4161 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004162 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004163}
4164
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004165void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004167 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004168 locations->SetInAt(0, Location::RequiresRegister());
4169 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004170}
4171
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004172void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4173 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4175 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004176 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004177 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004178 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004179 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004180 break;
4181
4182 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004183 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004184 break;
4185
4186 default:
4187 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4188 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004189}
4190
David Brazdil66d126e2015-04-03 16:02:44 +01004191void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4192 LocationSummary* locations =
4193 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4194 locations->SetInAt(0, Location::RequiresRegister());
4195 locations->SetOut(Location::SameAsFirstInput());
4196}
4197
4198void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004199 LocationSummary* locations = bool_not->GetLocations();
4200 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4201 locations->Out().AsRegister<CpuRegister>().AsRegister());
4202 Location out = locations->Out();
4203 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4204}
4205
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004206void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 LocationSummary* locations =
4208 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004209 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004210 locations->SetInAt(i, Location::Any());
4211 }
4212 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004213}
4214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004215void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004216 LOG(FATAL) << "Unimplemented";
4217}
4218
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004219void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004220 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004221 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004222 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004223 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4224 */
4225 switch (kind) {
4226 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004227 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004228 break;
4229 }
4230 case MemBarrierKind::kAnyStore:
4231 case MemBarrierKind::kLoadAny:
4232 case MemBarrierKind::kStoreStore: {
4233 // nop
4234 break;
4235 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004236 case MemBarrierKind::kNTStoreStore:
4237 // Non-Temporal Store/Store needs an explicit fence.
4238 MemoryFence(/* non-temporal */ true);
4239 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004240 }
4241}
4242
4243void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4244 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4245
Roland Levillain0d5a2812015-11-13 10:07:31 +00004246 bool object_field_get_with_read_barrier =
4247 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004248 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004249 new (GetGraph()->GetArena()) LocationSummary(instruction,
4250 object_field_get_with_read_barrier ?
4251 LocationSummary::kCallOnSlowPath :
4252 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004253 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004254 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004255 }
Calin Juravle52c48962014-12-16 17:02:57 +00004256 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004257 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4258 locations->SetOut(Location::RequiresFpuRegister());
4259 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004260 // The output overlaps for an object field get when read barriers
4261 // are enabled: we do not want the move to overwrite the object's
4262 // location, as we need it to emit the read barrier.
4263 locations->SetOut(
4264 Location::RequiresRegister(),
4265 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004266 }
Calin Juravle52c48962014-12-16 17:02:57 +00004267}
4268
4269void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4270 const FieldInfo& field_info) {
4271 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4272
4273 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004274 Location base_loc = locations->InAt(0);
4275 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004276 Location out = locations->Out();
4277 bool is_volatile = field_info.IsVolatile();
4278 Primitive::Type field_type = field_info.GetFieldType();
4279 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4280
4281 switch (field_type) {
4282 case Primitive::kPrimBoolean: {
4283 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4284 break;
4285 }
4286
4287 case Primitive::kPrimByte: {
4288 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4289 break;
4290 }
4291
4292 case Primitive::kPrimShort: {
4293 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4294 break;
4295 }
4296
4297 case Primitive::kPrimChar: {
4298 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4299 break;
4300 }
4301
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004302 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004303 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4304 break;
4305 }
4306
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004307 case Primitive::kPrimNot: {
4308 // /* HeapReference<Object> */ out = *(base + offset)
4309 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004310 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004311 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004312 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004313 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004314 if (is_volatile) {
4315 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4316 }
4317 } else {
4318 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4319 codegen_->MaybeRecordImplicitNullCheck(instruction);
4320 if (is_volatile) {
4321 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4322 }
4323 // If read barriers are enabled, emit read barriers other than
4324 // Baker's using a slow path (and also unpoison the loaded
4325 // reference, if heap poisoning is enabled).
4326 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4327 }
4328 break;
4329 }
4330
Calin Juravle52c48962014-12-16 17:02:57 +00004331 case Primitive::kPrimLong: {
4332 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4333 break;
4334 }
4335
4336 case Primitive::kPrimFloat: {
4337 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4338 break;
4339 }
4340
4341 case Primitive::kPrimDouble: {
4342 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4343 break;
4344 }
4345
4346 case Primitive::kPrimVoid:
4347 LOG(FATAL) << "Unreachable type " << field_type;
4348 UNREACHABLE();
4349 }
4350
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004351 if (field_type == Primitive::kPrimNot) {
4352 // Potential implicit null checks, in the case of reference
4353 // fields, are handled in the previous switch statement.
4354 } else {
4355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004356 }
Roland Levillain4d027112015-07-01 15:41:14 +01004357
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004358 if (is_volatile) {
4359 if (field_type == Primitive::kPrimNot) {
4360 // Memory barriers, in the case of references, are also handled
4361 // in the previous switch statement.
4362 } else {
4363 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4364 }
Roland Levillain4d027112015-07-01 15:41:14 +01004365 }
Calin Juravle52c48962014-12-16 17:02:57 +00004366}
4367
4368void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4369 const FieldInfo& field_info) {
4370 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4371
4372 LocationSummary* locations =
4373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004374 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004375 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004376 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004377 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004378
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004379 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004380 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004381 if (is_volatile) {
4382 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4383 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4384 } else {
4385 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4386 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004387 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004388 if (is_volatile) {
4389 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4390 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4391 } else {
4392 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4393 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004394 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004395 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004396 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004397 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004398 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004399 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4400 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004401 locations->AddTemp(Location::RequiresRegister());
4402 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004403}
4404
Calin Juravle52c48962014-12-16 17:02:57 +00004405void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004406 const FieldInfo& field_info,
4407 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004408 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4409
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004410 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004411 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4412 Location value = locations->InAt(1);
4413 bool is_volatile = field_info.IsVolatile();
4414 Primitive::Type field_type = field_info.GetFieldType();
4415 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4416
4417 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004418 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004419 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420
Mark Mendellea5af682015-10-22 17:35:49 -04004421 bool maybe_record_implicit_null_check_done = false;
4422
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423 switch (field_type) {
4424 case Primitive::kPrimBoolean:
4425 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004426 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004427 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004428 __ movb(Address(base, offset), Immediate(v));
4429 } else {
4430 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4431 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432 break;
4433 }
4434
4435 case Primitive::kPrimShort:
4436 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004437 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004438 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004439 __ movw(Address(base, offset), Immediate(v));
4440 } else {
4441 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4442 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443 break;
4444 }
4445
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004446 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004448 if (value.IsConstant()) {
4449 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004450 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4451 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4452 // Note: if heap poisoning is enabled, no need to poison
4453 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004454 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004455 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004456 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4457 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4458 __ movl(temp, value.AsRegister<CpuRegister>());
4459 __ PoisonHeapReference(temp);
4460 __ movl(Address(base, offset), temp);
4461 } else {
4462 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4463 }
Mark Mendell40741f32015-04-20 22:10:34 -04004464 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465 break;
4466 }
4467
4468 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004469 if (value.IsConstant()) {
4470 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004471 codegen_->MoveInt64ToAddress(Address(base, offset),
4472 Address(base, offset + sizeof(int32_t)),
4473 v,
4474 instruction);
4475 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004476 } else {
4477 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4478 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004479 break;
4480 }
4481
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004482 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004483 if (value.IsConstant()) {
4484 int32_t v =
4485 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4486 __ movl(Address(base, offset), Immediate(v));
4487 } else {
4488 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4489 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004490 break;
4491 }
4492
4493 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004494 if (value.IsConstant()) {
4495 int64_t v =
4496 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4497 codegen_->MoveInt64ToAddress(Address(base, offset),
4498 Address(base, offset + sizeof(int32_t)),
4499 v,
4500 instruction);
4501 maybe_record_implicit_null_check_done = true;
4502 } else {
4503 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4504 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004505 break;
4506 }
4507
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 case Primitive::kPrimVoid:
4509 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004510 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004511 }
Calin Juravle52c48962014-12-16 17:02:57 +00004512
Mark Mendellea5af682015-10-22 17:35:49 -04004513 if (!maybe_record_implicit_null_check_done) {
4514 codegen_->MaybeRecordImplicitNullCheck(instruction);
4515 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004516
4517 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4518 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4519 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004520 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004521 }
4522
Calin Juravle52c48962014-12-16 17:02:57 +00004523 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004524 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004525 }
4526}
4527
4528void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4529 HandleFieldSet(instruction, instruction->GetFieldInfo());
4530}
4531
4532void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004533 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534}
4535
4536void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004537 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004538}
4539
4540void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004541 HandleFieldGet(instruction, instruction->GetFieldInfo());
4542}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004543
Calin Juravle52c48962014-12-16 17:02:57 +00004544void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4545 HandleFieldGet(instruction);
4546}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547
Calin Juravle52c48962014-12-16 17:02:57 +00004548void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4549 HandleFieldGet(instruction, instruction->GetFieldInfo());
4550}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551
Calin Juravle52c48962014-12-16 17:02:57 +00004552void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4553 HandleFieldSet(instruction, instruction->GetFieldInfo());
4554}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555
Calin Juravle52c48962014-12-16 17:02:57 +00004556void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004557 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004558}
4559
Calin Juravlee460d1d2015-09-29 04:52:17 +01004560void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4561 HUnresolvedInstanceFieldGet* instruction) {
4562 FieldAccessCallingConventionX86_64 calling_convention;
4563 codegen_->CreateUnresolvedFieldLocationSummary(
4564 instruction, instruction->GetFieldType(), calling_convention);
4565}
4566
4567void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4568 HUnresolvedInstanceFieldGet* instruction) {
4569 FieldAccessCallingConventionX86_64 calling_convention;
4570 codegen_->GenerateUnresolvedFieldAccess(instruction,
4571 instruction->GetFieldType(),
4572 instruction->GetFieldIndex(),
4573 instruction->GetDexPc(),
4574 calling_convention);
4575}
4576
4577void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4578 HUnresolvedInstanceFieldSet* instruction) {
4579 FieldAccessCallingConventionX86_64 calling_convention;
4580 codegen_->CreateUnresolvedFieldLocationSummary(
4581 instruction, instruction->GetFieldType(), calling_convention);
4582}
4583
4584void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4585 HUnresolvedInstanceFieldSet* instruction) {
4586 FieldAccessCallingConventionX86_64 calling_convention;
4587 codegen_->GenerateUnresolvedFieldAccess(instruction,
4588 instruction->GetFieldType(),
4589 instruction->GetFieldIndex(),
4590 instruction->GetDexPc(),
4591 calling_convention);
4592}
4593
4594void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4595 HUnresolvedStaticFieldGet* instruction) {
4596 FieldAccessCallingConventionX86_64 calling_convention;
4597 codegen_->CreateUnresolvedFieldLocationSummary(
4598 instruction, instruction->GetFieldType(), calling_convention);
4599}
4600
4601void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4602 HUnresolvedStaticFieldGet* instruction) {
4603 FieldAccessCallingConventionX86_64 calling_convention;
4604 codegen_->GenerateUnresolvedFieldAccess(instruction,
4605 instruction->GetFieldType(),
4606 instruction->GetFieldIndex(),
4607 instruction->GetDexPc(),
4608 calling_convention);
4609}
4610
4611void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4612 HUnresolvedStaticFieldSet* instruction) {
4613 FieldAccessCallingConventionX86_64 calling_convention;
4614 codegen_->CreateUnresolvedFieldLocationSummary(
4615 instruction, instruction->GetFieldType(), calling_convention);
4616}
4617
4618void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4619 HUnresolvedStaticFieldSet* instruction) {
4620 FieldAccessCallingConventionX86_64 calling_convention;
4621 codegen_->GenerateUnresolvedFieldAccess(instruction,
4622 instruction->GetFieldType(),
4623 instruction->GetFieldIndex(),
4624 instruction->GetDexPc(),
4625 calling_convention);
4626}
4627
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004628void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004629 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4630 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4631 ? Location::RequiresRegister()
4632 : Location::Any();
4633 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004634}
4635
Calin Juravle2ae48182016-03-16 14:05:09 +00004636void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4637 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004638 return;
4639 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004640 LocationSummary* locations = instruction->GetLocations();
4641 Location obj = locations->InAt(0);
4642
4643 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004644 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004645}
4646
Calin Juravle2ae48182016-03-16 14:05:09 +00004647void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004648 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004649 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650
4651 LocationSummary* locations = instruction->GetLocations();
4652 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004653
4654 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004655 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004656 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004657 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004658 } else {
4659 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004660 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004661 __ jmp(slow_path->GetEntryLabel());
4662 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004663 }
4664 __ j(kEqual, slow_path->GetEntryLabel());
4665}
4666
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004667void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004668 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004669}
4670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004671void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004672 bool object_array_get_with_read_barrier =
4673 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004674 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004675 new (GetGraph()->GetArena()) LocationSummary(instruction,
4676 object_array_get_with_read_barrier ?
4677 LocationSummary::kCallOnSlowPath :
4678 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004679 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004680 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004681 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004682 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004683 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004684 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4685 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4686 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004687 // The output overlaps for an object array get when read barriers
4688 // are enabled: we do not want the move to overwrite the array's
4689 // location, as we need it to emit the read barrier.
4690 locations->SetOut(
4691 Location::RequiresRegister(),
4692 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004693 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694}
4695
4696void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4697 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004698 Location obj_loc = locations->InAt(0);
4699 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004701 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004702 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004704 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004705 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004707 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004708 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709 break;
4710 }
4711
4712 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004713 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004714 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004715 break;
4716 }
4717
4718 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004719 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004720 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 break;
4722 }
4723
4724 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004725 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004726 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4727 // Branch cases into compressed and uncompressed for each index's type.
4728 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4729 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004730 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004731 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004732 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4733 "Expecting 0=compressed, 1=uncompressed");
4734 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004735 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4736 __ jmp(&done);
4737 __ Bind(&not_compressed);
4738 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4739 __ Bind(&done);
4740 } else {
4741 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4742 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 break;
4744 }
4745
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004746 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004747 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004748 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749 break;
4750 }
4751
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004752 case Primitive::kPrimNot: {
4753 static_assert(
4754 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4755 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004756 // /* HeapReference<Object> */ out =
4757 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4758 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004759 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004760 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004761 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004762 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004763 } else {
4764 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004765 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4766 codegen_->MaybeRecordImplicitNullCheck(instruction);
4767 // If read barriers are enabled, emit read barriers other than
4768 // Baker's using a slow path (and also unpoison the loaded
4769 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004770 if (index.IsConstant()) {
4771 uint32_t offset =
4772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004773 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4774 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004775 codegen_->MaybeGenerateReadBarrierSlow(
4776 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4777 }
4778 }
4779 break;
4780 }
4781
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004783 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004784 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004785 break;
4786 }
4787
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004788 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004789 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004790 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004791 break;
4792 }
4793
4794 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004795 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004796 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004797 break;
4798 }
4799
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004801 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004802 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004803 }
Roland Levillain4d027112015-07-01 15:41:14 +01004804
4805 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004806 // Potential implicit null checks, in the case of reference
4807 // arrays, are handled in the previous switch statement.
4808 } else {
4809 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004811}
4812
4813void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004814 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004815
4816 bool needs_write_barrier =
4817 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004818 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004819
Nicolas Geoffray39468442014-09-02 15:17:15 +01004820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004821 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004822 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004823 LocationSummary::kCallOnSlowPath :
4824 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004825
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004826 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004827 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4828 if (Primitive::IsFloatingPointType(value_type)) {
4829 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 } else {
4831 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4832 }
4833
4834 if (needs_write_barrier) {
4835 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004836 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004838 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004839}
4840
4841void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4842 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004843 Location array_loc = locations->InAt(0);
4844 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004845 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004846 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004847 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004848 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004849 bool needs_write_barrier =
4850 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004851 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4852 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4853 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854
4855 switch (value_type) {
4856 case Primitive::kPrimBoolean:
4857 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004858 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004859 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004860 if (value.IsRegister()) {
4861 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004863 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866 break;
4867 }
4868
4869 case Primitive::kPrimShort:
4870 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004871 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004872 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004873 if (value.IsRegister()) {
4874 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 DCHECK(value.IsConstant()) << value;
4877 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004879 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 break;
4881 }
4882
4883 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004884 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004885 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004886
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 if (!value.IsRegister()) {
4888 // Just setting null.
4889 DCHECK(instruction->InputAt(2)->IsNullConstant());
4890 DCHECK(value.IsConstant()) << value;
4891 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004892 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004894 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004895 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004896 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004897
4898 DCHECK(needs_write_barrier);
4899 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004900 // We cannot use a NearLabel for `done`, as its range may be too
4901 // short when Baker read barriers are enabled.
4902 Label done;
4903 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004904 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004905 Location temp_loc = locations->GetTemp(0);
4906 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004907 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004908 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4909 codegen_->AddSlowPath(slow_path);
4910 if (instruction->GetValueCanBeNull()) {
4911 __ testl(register_value, register_value);
4912 __ j(kNotEqual, &not_null);
4913 __ movl(address, Immediate(0));
4914 codegen_->MaybeRecordImplicitNullCheck(instruction);
4915 __ jmp(&done);
4916 __ Bind(&not_null);
4917 }
4918
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004919 // Note that when Baker read barriers are enabled, the type
4920 // checks are performed without read barriers. This is fine,
4921 // even in the case where a class object is in the from-space
4922 // after the flip, as a comparison involving such a type would
4923 // not produce a false positive; it may of course produce a
4924 // false negative, in which case we would take the ArraySet
4925 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004926
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004927 // /* HeapReference<Class> */ temp = array->klass_
4928 __ movl(temp, Address(array, class_offset));
4929 codegen_->MaybeRecordImplicitNullCheck(instruction);
4930 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004931
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004932 // /* HeapReference<Class> */ temp = temp->component_type_
4933 __ movl(temp, Address(temp, component_offset));
4934 // If heap poisoning is enabled, no need to unpoison `temp`
4935 // nor the object reference in `register_value->klass`, as
4936 // we are comparing two poisoned references.
4937 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004938
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004939 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4940 __ j(kEqual, &do_put);
4941 // If heap poisoning is enabled, the `temp` reference has
4942 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004943 __ MaybeUnpoisonHeapReference(temp);
4944
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004945 // If heap poisoning is enabled, no need to unpoison the
4946 // heap reference loaded below, as it is only used for a
4947 // comparison with null.
4948 __ cmpl(Address(temp, super_offset), Immediate(0));
4949 __ j(kNotEqual, slow_path->GetEntryLabel());
4950 __ Bind(&do_put);
4951 } else {
4952 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 }
4954 }
4955
4956 if (kPoisonHeapReferences) {
4957 __ movl(temp, register_value);
4958 __ PoisonHeapReference(temp);
4959 __ movl(address, temp);
4960 } else {
4961 __ movl(address, register_value);
4962 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004963 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 codegen_->MaybeRecordImplicitNullCheck(instruction);
4965 }
4966
4967 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4968 codegen_->MarkGCCard(
4969 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4970 __ Bind(&done);
4971
4972 if (slow_path != nullptr) {
4973 __ Bind(slow_path->GetExitLabel());
4974 }
4975
4976 break;
4977 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004978
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004979 case Primitive::kPrimInt: {
4980 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004981 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004982 if (value.IsRegister()) {
4983 __ movl(address, value.AsRegister<CpuRegister>());
4984 } else {
4985 DCHECK(value.IsConstant()) << value;
4986 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4987 __ movl(address, Immediate(v));
4988 }
4989 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004990 break;
4991 }
4992
4993 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004994 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004995 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004996 if (value.IsRegister()) {
4997 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004998 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005000 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005001 Address address_high =
5002 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005003 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004 }
5005 break;
5006 }
5007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005008 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005009 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005010 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005011 if (value.IsFpuRegister()) {
5012 __ movss(address, value.AsFpuRegister<XmmRegister>());
5013 } else {
5014 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005015 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005016 __ movl(address, Immediate(v));
5017 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005018 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005019 break;
5020 }
5021
5022 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005023 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005024 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005025 if (value.IsFpuRegister()) {
5026 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5027 codegen_->MaybeRecordImplicitNullCheck(instruction);
5028 } else {
5029 int64_t v =
5030 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005031 Address address_high =
5032 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005033 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5034 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005035 break;
5036 }
5037
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 case Primitive::kPrimVoid:
5039 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005040 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 }
5042}
5043
5044void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005045 LocationSummary* locations =
5046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005047 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005048 if (!instruction->IsEmittedAtUseSite()) {
5049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051}
5052
5053void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005054 if (instruction->IsEmittedAtUseSite()) {
5055 return;
5056 }
5057
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005059 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005060 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5061 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005062 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005063 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005064 // Mask out most significant bit in case the array is String's array of char.
5065 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005066 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005067 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068}
5069
5070void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005071 RegisterSet caller_saves = RegisterSet::Empty();
5072 InvokeRuntimeCallingConvention calling_convention;
5073 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5074 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5075 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005076 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005077 HInstruction* length = instruction->InputAt(1);
5078 if (!length->IsEmittedAtUseSite()) {
5079 locations->SetInAt(1, Location::RegisterOrConstant(length));
5080 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081}
5082
5083void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5084 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005085 Location index_loc = locations->InAt(0);
5086 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005087 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088
Mark Mendell99dbd682015-04-22 16:18:52 -04005089 if (length_loc.IsConstant()) {
5090 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5091 if (index_loc.IsConstant()) {
5092 // BCE will remove the bounds check if we are guarenteed to pass.
5093 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5094 if (index < 0 || index >= length) {
5095 codegen_->AddSlowPath(slow_path);
5096 __ jmp(slow_path->GetEntryLabel());
5097 } else {
5098 // Some optimization after BCE may have generated this, and we should not
5099 // generate a bounds check if it is a valid range.
5100 }
5101 return;
5102 }
5103
5104 // We have to reverse the jump condition because the length is the constant.
5105 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5106 __ cmpl(index_reg, Immediate(length));
5107 codegen_->AddSlowPath(slow_path);
5108 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005109 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005110 HInstruction* array_length = instruction->InputAt(1);
5111 if (array_length->IsEmittedAtUseSite()) {
5112 // Address the length field in the array.
5113 DCHECK(array_length->IsArrayLength());
5114 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5115 Location array_loc = array_length->GetLocations()->InAt(0);
5116 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005117 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005118 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5119 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005120 CpuRegister length_reg = CpuRegister(TMP);
5121 __ movl(length_reg, array_len);
5122 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005123 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005124 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005125 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005126 // Checking the bound for general case:
5127 // Array of char or String's array when the compression feature off.
5128 if (index_loc.IsConstant()) {
5129 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5130 __ cmpl(array_len, Immediate(value));
5131 } else {
5132 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5133 }
5134 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005135 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005136 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005137 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005138 }
5139 codegen_->AddSlowPath(slow_path);
5140 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142}
5143
5144void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5145 CpuRegister card,
5146 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005147 CpuRegister value,
5148 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005149 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005150 if (value_can_be_null) {
5151 __ testl(value, value);
5152 __ j(kEqual, &is_null);
5153 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005154 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005155 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005156 __ movq(temp, object);
5157 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005158 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005159 if (value_can_be_null) {
5160 __ Bind(&is_null);
5161 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005162}
5163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005164void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005165 LOG(FATAL) << "Unimplemented";
5166}
5167
5168void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005169 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5170}
5171
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005172void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005173 LocationSummary* locations =
5174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005175 // In suspend check slow path, usually there are no caller-save registers at all.
5176 // If SIMD instructions are present, however, we force spilling all live SIMD
5177 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005178 locations->SetCustomSlowPathCallerSaves(
5179 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005180}
5181
5182void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005183 HBasicBlock* block = instruction->GetBlock();
5184 if (block->GetLoopInformation() != nullptr) {
5185 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5186 // The back edge will generate the suspend check.
5187 return;
5188 }
5189 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5190 // The goto will generate the suspend check.
5191 return;
5192 }
5193 GenerateSuspendCheck(instruction, nullptr);
5194}
5195
5196void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5197 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005198 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005199 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5200 if (slow_path == nullptr) {
5201 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5202 instruction->SetSlowPath(slow_path);
5203 codegen_->AddSlowPath(slow_path);
5204 if (successor != nullptr) {
5205 DCHECK(successor->IsLoopHeader());
5206 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5207 }
5208 } else {
5209 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5210 }
5211
Andreas Gampe542451c2016-07-26 09:02:02 -07005212 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005213 /* no_rip */ true),
5214 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005215 if (successor == nullptr) {
5216 __ j(kNotEqual, slow_path->GetEntryLabel());
5217 __ Bind(slow_path->GetReturnLabel());
5218 } else {
5219 __ j(kEqual, codegen_->GetLabelOf(successor));
5220 __ jmp(slow_path->GetEntryLabel());
5221 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005222}
5223
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005224X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5225 return codegen_->GetAssembler();
5226}
5227
5228void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005229 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 Location source = move->GetSource();
5231 Location destination = move->GetDestination();
5232
5233 if (source.IsRegister()) {
5234 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005235 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005236 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005237 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005238 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005239 } else {
5240 DCHECK(destination.IsDoubleStackSlot());
5241 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005242 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005243 }
5244 } else if (source.IsStackSlot()) {
5245 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005246 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005247 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005248 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005249 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005250 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005251 } else {
5252 DCHECK(destination.IsStackSlot());
5253 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5254 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5255 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005256 } else if (source.IsDoubleStackSlot()) {
5257 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005258 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005259 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005261 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5262 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005263 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005264 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005265 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5266 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5267 }
Aart Bik5576f372017-03-23 16:17:37 -07005268 } else if (source.IsSIMDStackSlot()) {
5269 DCHECK(destination.IsFpuRegister());
5270 __ movups(destination.AsFpuRegister<XmmRegister>(),
5271 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005272 } else if (source.IsConstant()) {
5273 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005274 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5275 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005276 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005277 if (value == 0) {
5278 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5279 } else {
5280 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5281 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005282 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005283 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005284 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005285 }
5286 } else if (constant->IsLongConstant()) {
5287 int64_t value = constant->AsLongConstant()->GetValue();
5288 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005289 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005290 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005292 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005293 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005295 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005296 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005297 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005298 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005299 } else {
5300 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005301 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5303 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005304 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005306 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005307 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005309 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005310 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 } else {
5312 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005313 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005315 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 } else if (source.IsFpuRegister()) {
5317 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005318 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005319 } else if (destination.IsStackSlot()) {
5320 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005321 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005322 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005323 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005324 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005325 } else {
5326 DCHECK(destination.IsSIMDStackSlot());
5327 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5328 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005329 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005330 }
5331}
5332
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005333void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005334 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005335 __ movl(Address(CpuRegister(RSP), mem), reg);
5336 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005337}
5338
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005339void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005340 ScratchRegisterScope ensure_scratch(
5341 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5342
5343 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5344 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5345 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5346 Address(CpuRegister(RSP), mem2 + stack_offset));
5347 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5348 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5349 CpuRegister(ensure_scratch.GetRegister()));
5350}
5351
Mark Mendell8a1c7282015-06-29 15:41:28 -04005352void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5353 __ movq(CpuRegister(TMP), reg1);
5354 __ movq(reg1, reg2);
5355 __ movq(reg2, CpuRegister(TMP));
5356}
5357
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005358void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5359 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5360 __ movq(Address(CpuRegister(RSP), mem), reg);
5361 __ movq(reg, CpuRegister(TMP));
5362}
5363
5364void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5365 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005366 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005367
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005368 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5369 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5370 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5371 Address(CpuRegister(RSP), mem2 + stack_offset));
5372 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5373 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5374 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005375}
5376
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005377void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5378 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5379 __ movss(Address(CpuRegister(RSP), mem), reg);
5380 __ movd(reg, CpuRegister(TMP));
5381}
5382
5383void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5384 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5385 __ movsd(Address(CpuRegister(RSP), mem), reg);
5386 __ movd(reg, CpuRegister(TMP));
5387}
5388
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005389void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005390 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005391 Location source = move->GetSource();
5392 Location destination = move->GetDestination();
5393
5394 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005395 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005396 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005397 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005398 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005399 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005400 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005401 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5402 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005403 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005404 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005406 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5407 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005408 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005409 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5410 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5411 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005412 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005413 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005414 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005415 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005416 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005417 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005418 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005419 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005420 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005421 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005422 }
5423}
5424
5425
5426void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5427 __ pushq(CpuRegister(reg));
5428}
5429
5430
5431void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5432 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005433}
5434
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005435void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005436 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005437 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5438 Immediate(mirror::Class::kStatusInitialized));
5439 __ j(kLess, slow_path->GetEntryLabel());
5440 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005441 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005442}
5443
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005444HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5445 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005446 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005447 case HLoadClass::LoadKind::kInvalid:
5448 LOG(FATAL) << "UNREACHABLE";
5449 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005450 case HLoadClass::LoadKind::kReferrersClass:
5451 break;
5452 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5453 DCHECK(!GetCompilerOptions().GetCompilePic());
5454 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5455 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5456 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5457 DCHECK(GetCompilerOptions().GetCompilePic());
5458 break;
5459 case HLoadClass::LoadKind::kBootImageAddress:
5460 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005461 case HLoadClass::LoadKind::kBssEntry:
5462 DCHECK(!Runtime::Current()->UseJitCompilation());
5463 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005464 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005465 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005466 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005467 case HLoadClass::LoadKind::kDexCacheViaMethod:
5468 break;
5469 }
5470 return desired_class_load_kind;
5471}
5472
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005473void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005474 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5475 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005476 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005477 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005478 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005479 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005480 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 return;
5482 }
Vladimir Marko41559982017-01-06 14:04:23 +00005483 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005484
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005485 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5486 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005487 ? LocationSummary::kCallOnSlowPath
5488 : LocationSummary::kNoCall;
5489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005490 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005491 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005492 }
5493
Vladimir Marko41559982017-01-06 14:04:23 +00005494 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005495 locations->SetInAt(0, Location::RequiresRegister());
5496 }
5497 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005498 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5499 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5500 // Rely on the type resolution and/or initialization to save everything.
5501 // Custom calling convention: RAX serves as both input and output.
5502 RegisterSet caller_saves = RegisterSet::Empty();
5503 caller_saves.Add(Location::RegisterLocation(RAX));
5504 locations->SetCustomSlowPathCallerSaves(caller_saves);
5505 } else {
5506 // For non-Baker read barrier we have a temp-clobbering call.
5507 }
5508 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005509}
5510
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005511Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
5512 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005513 Handle<mirror::Class> handle) {
5514 jit_class_roots_.Overwrite(
5515 TypeReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005516 // Add a patch entry and return the label.
5517 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
5518 PatchInfo<Label>* info = &jit_class_patches_.back();
5519 return &info->label;
5520}
5521
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005522// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5523// move.
5524void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005525 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5526 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5527 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005528 return;
5529 }
Vladimir Marko41559982017-01-06 14:04:23 +00005530 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005531
Vladimir Marko41559982017-01-06 14:04:23 +00005532 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005533 Location out_loc = locations->Out();
5534 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005535
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005536 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5537 ? kWithoutReadBarrier
5538 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005539 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005540 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005541 case HLoadClass::LoadKind::kReferrersClass: {
5542 DCHECK(!cls->CanCallRuntime());
5543 DCHECK(!cls->MustGenerateClinitCheck());
5544 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5545 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5546 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005547 cls,
5548 out_loc,
5549 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005550 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005551 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005552 break;
5553 }
5554 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005555 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005556 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005558 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005559 break;
5560 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005561 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005562 uint32_t address = dchecked_integral_cast<uint32_t>(
5563 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5564 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005565 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566 break;
5567 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005568 case HLoadClass::LoadKind::kBssEntry: {
5569 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5570 /* no_rip */ false);
5571 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5572 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5573 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5574 generate_null_check = true;
5575 break;
5576 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005577 case HLoadClass::LoadKind::kJitTableAddress: {
5578 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5579 /* no_rip */ true);
5580 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005581 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005582 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005583 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 break;
5585 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005586 default:
5587 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5588 UNREACHABLE();
5589 }
5590
5591 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5592 DCHECK(cls->CanCallRuntime());
5593 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5594 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5595 codegen_->AddSlowPath(slow_path);
5596 if (generate_null_check) {
5597 __ testl(out, out);
5598 __ j(kEqual, slow_path->GetEntryLabel());
5599 }
5600 if (cls->MustGenerateClinitCheck()) {
5601 GenerateClassInitializationCheck(slow_path, out);
5602 } else {
5603 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005604 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005605 }
5606}
5607
5608void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5609 LocationSummary* locations =
5610 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5611 locations->SetInAt(0, Location::RequiresRegister());
5612 if (check->HasUses()) {
5613 locations->SetOut(Location::SameAsFirstInput());
5614 }
5615}
5616
5617void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005618 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005619 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005620 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005621 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005622 GenerateClassInitializationCheck(slow_path,
5623 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005624}
5625
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005626HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5627 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005628 switch (desired_string_load_kind) {
5629 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5630 DCHECK(!GetCompilerOptions().GetCompilePic());
5631 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5632 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5633 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5634 DCHECK(GetCompilerOptions().GetCompilePic());
5635 break;
5636 case HLoadString::LoadKind::kBootImageAddress:
5637 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005638 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005639 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005640 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005641 case HLoadString::LoadKind::kJitTableAddress:
5642 DCHECK(Runtime::Current()->UseJitCompilation());
5643 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005644 case HLoadString::LoadKind::kDexCacheViaMethod:
5645 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005646 }
5647 return desired_string_load_kind;
5648}
5649
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005650void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005651 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005653 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005654 locations->SetOut(Location::RegisterLocation(RAX));
5655 } else {
5656 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005657 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5658 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005659 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005660 // Custom calling convention: RAX serves as both input and output.
5661 RegisterSet caller_saves = RegisterSet::Empty();
5662 caller_saves.Add(Location::RegisterLocation(RAX));
5663 locations->SetCustomSlowPathCallerSaves(caller_saves);
5664 } else {
5665 // For non-Baker read barrier we have a temp-clobbering call.
5666 }
5667 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005668 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005669}
5670
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005671Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005672 dex::StringIndex dex_index,
5673 Handle<mirror::String> handle) {
5674 jit_string_roots_.Overwrite(
5675 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005676 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005677 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005678 PatchInfo<Label>* info = &jit_string_patches_.back();
5679 return &info->label;
5680}
5681
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005682// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5683// move.
5684void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005685 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005686 Location out_loc = locations->Out();
5687 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005688
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005689 switch (load->GetLoadKind()) {
5690 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005691 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005692 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005693 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005694 return; // No dex cache slow path.
5695 }
5696 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005697 uint32_t address = dchecked_integral_cast<uint32_t>(
5698 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5699 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005700 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005701 return; // No dex cache slow path.
5702 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005703 case HLoadString::LoadKind::kBssEntry: {
5704 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5705 /* no_rip */ false);
5706 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5707 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005708 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005709 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5710 codegen_->AddSlowPath(slow_path);
5711 __ testl(out, out);
5712 __ j(kEqual, slow_path->GetEntryLabel());
5713 __ Bind(slow_path->GetExitLabel());
5714 return;
5715 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005716 case HLoadString::LoadKind::kJitTableAddress: {
5717 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5718 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005719 Label* fixup_label = codegen_->NewJitRootStringPatch(
5720 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005721 // /* GcRoot<mirror::String> */ out = *address
5722 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5723 return;
5724 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005725 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005726 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005729 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005730 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005731 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005732 codegen_->InvokeRuntime(kQuickResolveString,
5733 load,
5734 load->GetDexPc());
5735 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005736}
5737
David Brazdilcb1c0552015-08-04 16:22:25 +01005738static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005739 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005740 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005741}
5742
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005743void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5744 LocationSummary* locations =
5745 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5746 locations->SetOut(Location::RequiresRegister());
5747}
5748
5749void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005750 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5751}
5752
5753void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5754 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5755}
5756
5757void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5758 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005759}
5760
5761void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5762 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005764 InvokeRuntimeCallingConvention calling_convention;
5765 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5766}
5767
5768void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005769 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005770 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005771}
5772
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005773static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5774 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005775 // We need a temporary for holding the iftable length.
5776 return true;
5777 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005778 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005779 !kUseBakerReadBarrier &&
5780 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005781 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5782 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5783}
5784
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005785static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5786 return kEmitCompilerReadBarrier &&
5787 !kUseBakerReadBarrier &&
5788 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5789 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5790 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5791}
5792
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005793void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005795 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005796 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005797 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005798 case TypeCheckKind::kExactCheck:
5799 case TypeCheckKind::kAbstractClassCheck:
5800 case TypeCheckKind::kClassHierarchyCheck:
5801 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005802 call_kind =
5803 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005804 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 break;
5806 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005807 case TypeCheckKind::kUnresolvedCheck:
5808 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 call_kind = LocationSummary::kCallOnSlowPath;
5810 break;
5811 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005812
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005814 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005815 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005816 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 locations->SetInAt(0, Location::RequiresRegister());
5818 locations->SetInAt(1, Location::Any());
5819 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5820 locations->SetOut(Location::RequiresRegister());
5821 // When read barriers are enabled, we need a temporary register for
5822 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005823 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005824 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005826}
5827
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005828void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005829 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005830 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005831 Location obj_loc = locations->InAt(0);
5832 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005833 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005834 Location out_loc = locations->Out();
5835 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005836 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005837 locations->GetTemp(0) :
5838 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005839 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5841 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5842 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005843 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005845
5846 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005847 // Avoid null check if we know obj is not null.
5848 if (instruction->MustDoNullCheck()) {
5849 __ testl(obj, obj);
5850 __ j(kEqual, &zero);
5851 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005853 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005854 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005855 // /* HeapReference<Class> */ out = obj->klass_
5856 GenerateReferenceLoadTwoRegisters(instruction,
5857 out_loc,
5858 obj_loc,
5859 class_offset,
5860 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005861 if (cls.IsRegister()) {
5862 __ cmpl(out, cls.AsRegister<CpuRegister>());
5863 } else {
5864 DCHECK(cls.IsStackSlot()) << cls;
5865 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5866 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005867 if (zero.IsLinked()) {
5868 // Classes must be equal for the instanceof to succeed.
5869 __ j(kNotEqual, &zero);
5870 __ movl(out, Immediate(1));
5871 __ jmp(&done);
5872 } else {
5873 __ setcc(kEqual, out);
5874 // setcc only sets the low byte.
5875 __ andl(out, Immediate(1));
5876 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 break;
5878 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005879
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005881 // /* HeapReference<Class> */ out = obj->klass_
5882 GenerateReferenceLoadTwoRegisters(instruction,
5883 out_loc,
5884 obj_loc,
5885 class_offset,
5886 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005887 // If the class is abstract, we eagerly fetch the super class of the
5888 // object to avoid doing a comparison we know will fail.
5889 NearLabel loop, success;
5890 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005892 GenerateReferenceLoadOneRegister(instruction,
5893 out_loc,
5894 super_offset,
5895 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005896 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 __ testl(out, out);
5898 // If `out` is null, we use it for the result, and jump to `done`.
5899 __ j(kEqual, &done);
5900 if (cls.IsRegister()) {
5901 __ cmpl(out, cls.AsRegister<CpuRegister>());
5902 } else {
5903 DCHECK(cls.IsStackSlot()) << cls;
5904 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5905 }
5906 __ j(kNotEqual, &loop);
5907 __ movl(out, Immediate(1));
5908 if (zero.IsLinked()) {
5909 __ jmp(&done);
5910 }
5911 break;
5912 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005915 // /* HeapReference<Class> */ out = obj->klass_
5916 GenerateReferenceLoadTwoRegisters(instruction,
5917 out_loc,
5918 obj_loc,
5919 class_offset,
5920 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921 // Walk over the class hierarchy to find a match.
5922 NearLabel loop, success;
5923 __ Bind(&loop);
5924 if (cls.IsRegister()) {
5925 __ cmpl(out, cls.AsRegister<CpuRegister>());
5926 } else {
5927 DCHECK(cls.IsStackSlot()) << cls;
5928 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5929 }
5930 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005932 GenerateReferenceLoadOneRegister(instruction,
5933 out_loc,
5934 super_offset,
5935 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005936 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 __ testl(out, out);
5938 __ j(kNotEqual, &loop);
5939 // If `out` is null, we use it for the result, and jump to `done`.
5940 __ jmp(&done);
5941 __ Bind(&success);
5942 __ movl(out, Immediate(1));
5943 if (zero.IsLinked()) {
5944 __ jmp(&done);
5945 }
5946 break;
5947 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005948
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005950 // /* HeapReference<Class> */ out = obj->klass_
5951 GenerateReferenceLoadTwoRegisters(instruction,
5952 out_loc,
5953 obj_loc,
5954 class_offset,
5955 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005956 // Do an exact check.
5957 NearLabel exact_check;
5958 if (cls.IsRegister()) {
5959 __ cmpl(out, cls.AsRegister<CpuRegister>());
5960 } else {
5961 DCHECK(cls.IsStackSlot()) << cls;
5962 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5963 }
5964 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005966 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005967 GenerateReferenceLoadOneRegister(instruction,
5968 out_loc,
5969 component_offset,
5970 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005971 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 __ testl(out, out);
5973 // If `out` is null, we use it for the result, and jump to `done`.
5974 __ j(kEqual, &done);
5975 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5976 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005977 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005978 __ movl(out, Immediate(1));
5979 __ jmp(&done);
5980 break;
5981 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005982
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005984 // No read barrier since the slow path will retry upon failure.
5985 // /* HeapReference<Class> */ out = obj->klass_
5986 GenerateReferenceLoadTwoRegisters(instruction,
5987 out_loc,
5988 obj_loc,
5989 class_offset,
5990 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005991 if (cls.IsRegister()) {
5992 __ cmpl(out, cls.AsRegister<CpuRegister>());
5993 } else {
5994 DCHECK(cls.IsStackSlot()) << cls;
5995 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5996 }
5997 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5999 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 codegen_->AddSlowPath(slow_path);
6001 __ j(kNotEqual, slow_path->GetEntryLabel());
6002 __ movl(out, Immediate(1));
6003 if (zero.IsLinked()) {
6004 __ jmp(&done);
6005 }
6006 break;
6007 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008
Calin Juravle98893e12015-10-02 21:05:03 +01006009 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010 case TypeCheckKind::kInterfaceCheck: {
6011 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006012 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013 // cases.
6014 //
6015 // We cannot directly call the InstanceofNonTrivial runtime
6016 // entry point without resorting to a type checking slow path
6017 // here (i.e. by calling InvokeRuntime directly), as it would
6018 // require to assign fixed registers for the inputs of this
6019 // HInstanceOf instruction (following the runtime calling
6020 // convention), which might be cluttered by the potential first
6021 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006022 //
6023 // TODO: Introduce a new runtime entry point taking the object
6024 // to test (instead of its class) as argument, and let it deal
6025 // with the read barrier issues. This will let us refactor this
6026 // case of the `switch` code as it was previously (with a direct
6027 // call to the runtime not using a type checking slow path).
6028 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006029 DCHECK(locations->OnlyCallsOnSlowPath());
6030 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6031 /* is_fatal */ false);
6032 codegen_->AddSlowPath(slow_path);
6033 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 if (zero.IsLinked()) {
6035 __ jmp(&done);
6036 }
6037 break;
6038 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006039 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006040
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006041 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006042 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006043 __ xorl(out, out);
6044 }
6045
6046 if (done.IsLinked()) {
6047 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006048 }
6049
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006050 if (slow_path != nullptr) {
6051 __ Bind(slow_path->GetExitLabel());
6052 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006053}
6054
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006055static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006057 case TypeCheckKind::kExactCheck:
6058 case TypeCheckKind::kAbstractClassCheck:
6059 case TypeCheckKind::kClassHierarchyCheck:
6060 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006061 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006062 case TypeCheckKind::kInterfaceCheck:
6063 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006064 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006065 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006066 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006068 LOG(FATAL) << "Unreachable";
6069 UNREACHABLE();
6070}
6071
6072void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6073 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6074 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6075 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6076 LocationSummary::CallKind call_kind = is_fatal_slow_path
6077 ? LocationSummary::kNoCall
6078 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6080 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006081 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6082 // Require a register for the interface check since there is a loop that compares the class to
6083 // a memory address.
6084 locations->SetInAt(1, Location::RequiresRegister());
6085 } else {
6086 locations->SetInAt(1, Location::Any());
6087 }
6088
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6090 locations->AddTemp(Location::RequiresRegister());
6091 // When read barriers are enabled, we need an additional temporary
6092 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006093 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006094 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006096}
6097
6098void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006099 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006100 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101 Location obj_loc = locations->InAt(0);
6102 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006103 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 Location temp_loc = locations->GetTemp(0);
6105 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006106 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006107 locations->GetTemp(1) :
6108 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006109 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6110 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6111 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6112 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6113 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6114 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006115 const uint32_t object_array_data_offset =
6116 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006118 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6119 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6120 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006122 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123 SlowPathCode* type_check_slow_path =
6124 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6125 is_type_check_slow_path_fatal);
6126 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006128
6129 NearLabel done;
6130 // Avoid null check if we know obj is not null.
6131 if (instruction->MustDoNullCheck()) {
6132 __ testl(obj, obj);
6133 __ j(kEqual, &done);
6134 }
6135
Roland Levillain0d5a2812015-11-13 10:07:31 +00006136 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 case TypeCheckKind::kExactCheck:
6138 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006139 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006140 GenerateReferenceLoadTwoRegisters(instruction,
6141 temp_loc,
6142 obj_loc,
6143 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006144 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 if (cls.IsRegister()) {
6146 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6147 } else {
6148 DCHECK(cls.IsStackSlot()) << cls;
6149 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6150 }
6151 // Jump to slow path for throwing the exception or doing a
6152 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154 break;
6155 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006156
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006158 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006159 GenerateReferenceLoadTwoRegisters(instruction,
6160 temp_loc,
6161 obj_loc,
6162 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006163 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 // If the class is abstract, we eagerly fetch the super class of the
6165 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006166 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006169 GenerateReferenceLoadOneRegister(instruction,
6170 temp_loc,
6171 super_offset,
6172 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006173 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006174
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006175 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6176 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006178 // Otherwise, compare the classes.
6179 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180 if (cls.IsRegister()) {
6181 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6182 } else {
6183 DCHECK(cls.IsStackSlot()) << cls;
6184 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6185 }
6186 __ j(kNotEqual, &loop);
6187 break;
6188 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006191 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006192 GenerateReferenceLoadTwoRegisters(instruction,
6193 temp_loc,
6194 obj_loc,
6195 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006196 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006198 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 __ Bind(&loop);
6200 if (cls.IsRegister()) {
6201 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6202 } else {
6203 DCHECK(cls.IsStackSlot()) << cls;
6204 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6205 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006206 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006209 GenerateReferenceLoadOneRegister(instruction,
6210 temp_loc,
6211 super_offset,
6212 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006213 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006214
6215 // If the class reference currently in `temp` is not null, jump
6216 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006218 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 break;
6222 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006225 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006226 GenerateReferenceLoadTwoRegisters(instruction,
6227 temp_loc,
6228 obj_loc,
6229 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006230 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006231 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006232 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006233 if (cls.IsRegister()) {
6234 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6235 } else {
6236 DCHECK(cls.IsStackSlot()) << cls;
6237 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6238 }
6239 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006240
6241 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006242 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006243 GenerateReferenceLoadOneRegister(instruction,
6244 temp_loc,
6245 component_offset,
6246 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006247 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248
6249 // If the component type is not null (i.e. the object is indeed
6250 // an array), jump to label `check_non_primitive_component_type`
6251 // to further check that this component type is not a primitive
6252 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006255 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006257 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258 break;
6259 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006260
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006261 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006262 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006263 //
6264 // We cannot directly call the CheckCast runtime entry point
6265 // without resorting to a type checking slow path here (i.e. by
6266 // calling InvokeRuntime directly), as it would require to
6267 // assign fixed registers for the inputs of this HInstanceOf
6268 // instruction (following the runtime calling convention), which
6269 // might be cluttered by the potential first read barrier
6270 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006271 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006272 break;
6273 }
6274
6275 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006276 // Fast path for the interface check. We always go slow path for heap poisoning since
6277 // unpoisoning cls would require an extra temp.
6278 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006279 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6280 // doing this.
6281 // /* HeapReference<Class> */ temp = obj->klass_
6282 GenerateReferenceLoadTwoRegisters(instruction,
6283 temp_loc,
6284 obj_loc,
6285 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006286 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006287
6288 // /* HeapReference<Class> */ temp = temp->iftable_
6289 GenerateReferenceLoadTwoRegisters(instruction,
6290 temp_loc,
6291 temp_loc,
6292 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006293 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006294 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006295 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006296 // Loop through the iftable and check if any class matches.
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006297 NearLabel start_loop;
6298 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006299 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006300 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006301 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6302 // Go to next interface if the classes do not match.
6303 __ cmpl(cls.AsRegister<CpuRegister>(),
6304 CodeGeneratorX86_64::ArrayAddress(temp,
6305 maybe_temp2_loc,
6306 TIMES_4,
6307 object_array_data_offset));
6308 __ j(kNotEqual, &start_loop); // Return if same class.
6309 } else {
6310 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006311 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 break;
6313 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006315 if (done.IsLinked()) {
6316 __ Bind(&done);
6317 }
6318
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320}
6321
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006322void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6323 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006324 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006325 InvokeRuntimeCallingConvention calling_convention;
6326 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6327}
6328
6329void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006330 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006331 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006332 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006333 if (instruction->IsEnter()) {
6334 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6335 } else {
6336 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6337 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006338}
6339
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006340void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6341void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6342void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6343
6344void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6345 LocationSummary* locations =
6346 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6347 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6348 || instruction->GetResultType() == Primitive::kPrimLong);
6349 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006350 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006351 locations->SetOut(Location::SameAsFirstInput());
6352}
6353
6354void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6355 HandleBitwiseOperation(instruction);
6356}
6357
6358void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6359 HandleBitwiseOperation(instruction);
6360}
6361
6362void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6363 HandleBitwiseOperation(instruction);
6364}
6365
6366void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6367 LocationSummary* locations = instruction->GetLocations();
6368 Location first = locations->InAt(0);
6369 Location second = locations->InAt(1);
6370 DCHECK(first.Equals(locations->Out()));
6371
6372 if (instruction->GetResultType() == Primitive::kPrimInt) {
6373 if (second.IsRegister()) {
6374 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006375 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006376 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006377 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006378 } else {
6379 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006380 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006381 }
6382 } else if (second.IsConstant()) {
6383 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6384 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006385 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006386 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006387 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006388 } else {
6389 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006390 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006391 }
6392 } else {
6393 Address address(CpuRegister(RSP), second.GetStackIndex());
6394 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006395 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006396 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006397 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006398 } else {
6399 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006400 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006401 }
6402 }
6403 } else {
6404 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006405 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6406 bool second_is_constant = false;
6407 int64_t value = 0;
6408 if (second.IsConstant()) {
6409 second_is_constant = true;
6410 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006411 }
Mark Mendell40741f32015-04-20 22:10:34 -04006412 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006413
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006414 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006415 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006416 if (is_int32_value) {
6417 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6418 } else {
6419 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6420 }
6421 } else if (second.IsDoubleStackSlot()) {
6422 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006423 } else {
6424 __ andq(first_reg, second.AsRegister<CpuRegister>());
6425 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006426 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006427 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006428 if (is_int32_value) {
6429 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6430 } else {
6431 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6432 }
6433 } else if (second.IsDoubleStackSlot()) {
6434 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006435 } else {
6436 __ orq(first_reg, second.AsRegister<CpuRegister>());
6437 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006438 } else {
6439 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006440 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006441 if (is_int32_value) {
6442 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6443 } else {
6444 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6445 }
6446 } else if (second.IsDoubleStackSlot()) {
6447 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006448 } else {
6449 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6450 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006451 }
6452 }
6453}
6454
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006455void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6456 HInstruction* instruction,
6457 Location out,
6458 uint32_t offset,
6459 Location maybe_temp,
6460 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006462 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006463 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006464 if (kUseBakerReadBarrier) {
6465 // Load with fast path based Baker's read barrier.
6466 // /* HeapReference<Object> */ out = *(out + offset)
6467 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006468 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006469 } else {
6470 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006471 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006472 // in the following move operation, as we will need it for the
6473 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006474 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006475 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006476 // /* HeapReference<Object> */ out = *(out + offset)
6477 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006478 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006479 }
6480 } else {
6481 // Plain load with no read barrier.
6482 // /* HeapReference<Object> */ out = *(out + offset)
6483 __ movl(out_reg, Address(out_reg, offset));
6484 __ MaybeUnpoisonHeapReference(out_reg);
6485 }
6486}
6487
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006488void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6489 HInstruction* instruction,
6490 Location out,
6491 Location obj,
6492 uint32_t offset,
6493 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006494 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6495 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006496 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006497 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006498 if (kUseBakerReadBarrier) {
6499 // Load with fast path based Baker's read barrier.
6500 // /* HeapReference<Object> */ out = *(obj + offset)
6501 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006502 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006503 } else {
6504 // Load with slow path based read barrier.
6505 // /* HeapReference<Object> */ out = *(obj + offset)
6506 __ movl(out_reg, Address(obj_reg, offset));
6507 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6508 }
6509 } else {
6510 // Plain load with no read barrier.
6511 // /* HeapReference<Object> */ out = *(obj + offset)
6512 __ movl(out_reg, Address(obj_reg, offset));
6513 __ MaybeUnpoisonHeapReference(out_reg);
6514 }
6515}
6516
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006517void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6518 HInstruction* instruction,
6519 Location root,
6520 const Address& address,
6521 Label* fixup_label,
6522 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006523 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006524 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006525 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006526 if (kUseBakerReadBarrier) {
6527 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6528 // Baker's read barrier are used:
6529 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006530 // root = obj.field;
6531 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6532 // if (temp != null) {
6533 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006534 // }
6535
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006536 // /* GcRoot<mirror::Object> */ root = *address
6537 __ movl(root_reg, address);
6538 if (fixup_label != nullptr) {
6539 __ Bind(fixup_label);
6540 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006541 static_assert(
6542 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6543 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6544 "have different sizes.");
6545 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6546 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6547 "have different sizes.");
6548
Vladimir Marko953437b2016-08-24 08:30:46 +00006549 // Slow path marking the GC root `root`.
6550 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006551 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006552 codegen_->AddSlowPath(slow_path);
6553
Roland Levillaind966ce72017-02-09 16:20:14 +00006554 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6555 const int32_t entry_point_offset =
6556 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
6557 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6558 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006559 __ j(kNotEqual, slow_path->GetEntryLabel());
6560 __ Bind(slow_path->GetExitLabel());
6561 } else {
6562 // GC root loaded through a slow path for read barriers other
6563 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006564 // /* GcRoot<mirror::Object>* */ root = address
6565 __ leaq(root_reg, address);
6566 if (fixup_label != nullptr) {
6567 __ Bind(fixup_label);
6568 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 // /* mirror::Object* */ root = root->Read()
6570 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6571 }
6572 } else {
6573 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006574 // /* GcRoot<mirror::Object> */ root = *address
6575 __ movl(root_reg, address);
6576 if (fixup_label != nullptr) {
6577 __ Bind(fixup_label);
6578 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006579 // Note that GC roots are not affected by heap poisoning, thus we
6580 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006581 }
6582}
6583
6584void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6585 Location ref,
6586 CpuRegister obj,
6587 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006588 bool needs_null_check) {
6589 DCHECK(kEmitCompilerReadBarrier);
6590 DCHECK(kUseBakerReadBarrier);
6591
6592 // /* HeapReference<Object> */ ref = *(obj + offset)
6593 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006594 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006595}
6596
6597void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6598 Location ref,
6599 CpuRegister obj,
6600 uint32_t data_offset,
6601 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006602 bool needs_null_check) {
6603 DCHECK(kEmitCompilerReadBarrier);
6604 DCHECK(kUseBakerReadBarrier);
6605
Roland Levillain3d312422016-06-23 13:53:42 +01006606 static_assert(
6607 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6608 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006609 // /* HeapReference<Object> */ ref =
6610 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006611 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006612 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006613}
6614
6615void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6616 Location ref,
6617 CpuRegister obj,
6618 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006619 bool needs_null_check,
6620 bool always_update_field,
6621 CpuRegister* temp1,
6622 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006623 DCHECK(kEmitCompilerReadBarrier);
6624 DCHECK(kUseBakerReadBarrier);
6625
6626 // In slow path based read barriers, the read barrier call is
6627 // inserted after the original load. However, in fast path based
6628 // Baker's read barriers, we need to perform the load of
6629 // mirror::Object::monitor_ *before* the original reference load.
6630 // This load-load ordering is required by the read barrier.
6631 // The fast path/slow path (for Baker's algorithm) should look like:
6632 //
6633 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6634 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6635 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006636 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006637 // if (is_gray) {
6638 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6639 // }
6640 //
6641 // Note: the original implementation in ReadBarrier::Barrier is
6642 // slightly more complex as:
6643 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006644 // the high-bits of rb_state, which are expected to be all zeroes
6645 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6646 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006647 // - it performs additional checks that we do not do here for
6648 // performance reasons.
6649
6650 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006651 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6652
Vladimir Marko953437b2016-08-24 08:30:46 +00006653 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006654 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6655 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006656 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6657 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6658 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6659
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006660 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006661 // ref = ReadBarrier::Mark(ref);
6662 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6663 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006664 if (needs_null_check) {
6665 MaybeRecordImplicitNullCheck(instruction);
6666 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006667
6668 // Load fence to prevent load-load reordering.
6669 // Note that this is a no-op, thanks to the x86-64 memory model.
6670 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6671
6672 // The actual reference load.
6673 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006674 __ movl(ref_reg, src); // Flags are unaffected.
6675
6676 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6677 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006678 SlowPathCode* slow_path;
6679 if (always_update_field) {
6680 DCHECK(temp1 != nullptr);
6681 DCHECK(temp2 != nullptr);
6682 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6683 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6684 } else {
6685 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6686 instruction, ref, /* unpoison_ref_before_marking */ true);
6687 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006688 AddSlowPath(slow_path);
6689
6690 // We have done the "if" of the gray bit check above, now branch based on the flags.
6691 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006692
6693 // Object* ref = ref_addr->AsMirrorPtr()
6694 __ MaybeUnpoisonHeapReference(ref_reg);
6695
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006696 __ Bind(slow_path->GetExitLabel());
6697}
6698
6699void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6700 Location out,
6701 Location ref,
6702 Location obj,
6703 uint32_t offset,
6704 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705 DCHECK(kEmitCompilerReadBarrier);
6706
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006707 // Insert a slow path based read barrier *after* the reference load.
6708 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709 // If heap poisoning is enabled, the unpoisoning of the loaded
6710 // reference will be carried out by the runtime within the slow
6711 // path.
6712 //
6713 // Note that `ref` currently does not get unpoisoned (when heap
6714 // poisoning is enabled), which is alright as the `ref` argument is
6715 // not used by the artReadBarrierSlow entry point.
6716 //
6717 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6718 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6719 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6720 AddSlowPath(slow_path);
6721
Roland Levillain0d5a2812015-11-13 10:07:31 +00006722 __ jmp(slow_path->GetEntryLabel());
6723 __ Bind(slow_path->GetExitLabel());
6724}
6725
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006726void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6727 Location out,
6728 Location ref,
6729 Location obj,
6730 uint32_t offset,
6731 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006733 // Baker's read barriers shall be handled by the fast path
6734 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6735 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736 // If heap poisoning is enabled, unpoisoning will be taken care of
6737 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006738 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 } else if (kPoisonHeapReferences) {
6740 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6741 }
6742}
6743
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006744void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6745 Location out,
6746 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747 DCHECK(kEmitCompilerReadBarrier);
6748
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006749 // Insert a slow path based read barrier *after* the GC root load.
6750 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006751 // Note that GC roots are not affected by heap poisoning, so we do
6752 // not need to do anything special for this here.
6753 SlowPathCode* slow_path =
6754 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6755 AddSlowPath(slow_path);
6756
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757 __ jmp(slow_path->GetEntryLabel());
6758 __ Bind(slow_path->GetExitLabel());
6759}
6760
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006761void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006762 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006763 LOG(FATAL) << "Unreachable";
6764}
6765
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006766void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006767 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006768 LOG(FATAL) << "Unreachable";
6769}
6770
Mark Mendellfe57faa2015-09-18 09:26:15 -04006771// Simple implementation of packed switch - generate cascaded compare/jumps.
6772void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6773 LocationSummary* locations =
6774 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6775 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006776 locations->AddTemp(Location::RequiresRegister());
6777 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006778}
6779
6780void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6781 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006782 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006783 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006784 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6785 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6786 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006787 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6788
6789 // Should we generate smaller inline compare/jumps?
6790 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6791 // Figure out the correct compare values and jump conditions.
6792 // Handle the first compare/branch as a special case because it might
6793 // jump to the default case.
6794 DCHECK_GT(num_entries, 2u);
6795 Condition first_condition;
6796 uint32_t index;
6797 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6798 if (lower_bound != 0) {
6799 first_condition = kLess;
6800 __ cmpl(value_reg_in, Immediate(lower_bound));
6801 __ j(first_condition, codegen_->GetLabelOf(default_block));
6802 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6803
6804 index = 1;
6805 } else {
6806 // Handle all the compare/jumps below.
6807 first_condition = kBelow;
6808 index = 0;
6809 }
6810
6811 // Handle the rest of the compare/jumps.
6812 for (; index + 1 < num_entries; index += 2) {
6813 int32_t compare_to_value = lower_bound + index + 1;
6814 __ cmpl(value_reg_in, Immediate(compare_to_value));
6815 // Jump to successors[index] if value < case_value[index].
6816 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6817 // Jump to successors[index + 1] if value == case_value[index + 1].
6818 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6819 }
6820
6821 if (index != num_entries) {
6822 // There are an odd number of entries. Handle the last one.
6823 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006824 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006825 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6826 }
6827
6828 // And the default for any other value.
6829 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6830 __ jmp(codegen_->GetLabelOf(default_block));
6831 }
6832 return;
6833 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006834
6835 // Remove the bias, if needed.
6836 Register value_reg_out = value_reg_in.AsRegister();
6837 if (lower_bound != 0) {
6838 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6839 value_reg_out = temp_reg.AsRegister();
6840 }
6841 CpuRegister value_reg(value_reg_out);
6842
6843 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006844 __ cmpl(value_reg, Immediate(num_entries - 1));
6845 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006846
Mark Mendell9c86b482015-09-18 13:36:07 -04006847 // We are in the range of the table.
6848 // Load the address of the jump table in the constant area.
6849 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006850
Mark Mendell9c86b482015-09-18 13:36:07 -04006851 // Load the (signed) offset from the jump table.
6852 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6853
6854 // Add the offset to the address of the table base.
6855 __ addq(temp_reg, base_reg);
6856
6857 // And jump.
6858 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006859}
6860
Aart Bikc5d47542016-01-27 17:00:35 -08006861void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6862 if (value == 0) {
6863 __ xorl(dest, dest);
6864 } else {
6865 __ movl(dest, Immediate(value));
6866 }
6867}
6868
Mark Mendell92e83bf2015-05-07 11:25:03 -04006869void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6870 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006871 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006872 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006873 } else if (IsUint<32>(value)) {
6874 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006875 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6876 } else {
6877 __ movq(dest, Immediate(value));
6878 }
6879}
6880
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006881void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6882 if (value == 0) {
6883 __ xorps(dest, dest);
6884 } else {
6885 __ movss(dest, LiteralInt32Address(value));
6886 }
6887}
6888
6889void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6890 if (value == 0) {
6891 __ xorpd(dest, dest);
6892 } else {
6893 __ movsd(dest, LiteralInt64Address(value));
6894 }
6895}
6896
6897void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6898 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6899}
6900
6901void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6902 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6903}
6904
Aart Bika19616e2016-02-01 18:57:58 -08006905void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6906 if (value == 0) {
6907 __ testl(dest, dest);
6908 } else {
6909 __ cmpl(dest, Immediate(value));
6910 }
6911}
6912
6913void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6914 if (IsInt<32>(value)) {
6915 if (value == 0) {
6916 __ testq(dest, dest);
6917 } else {
6918 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6919 }
6920 } else {
6921 // Value won't fit in an int.
6922 __ cmpq(dest, LiteralInt64Address(value));
6923 }
6924}
6925
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006926void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6927 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006928 GenerateIntCompare(lhs_reg, rhs);
6929}
6930
6931void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006932 if (rhs.IsConstant()) {
6933 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006934 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006935 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006936 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006937 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006938 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006939 }
6940}
6941
6942void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6943 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6944 if (rhs.IsConstant()) {
6945 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6946 Compare64BitValue(lhs_reg, value);
6947 } else if (rhs.IsDoubleStackSlot()) {
6948 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6949 } else {
6950 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6951 }
6952}
6953
6954Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6955 Location index,
6956 ScaleFactor scale,
6957 uint32_t data_offset) {
6958 return index.IsConstant() ?
6959 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6960 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6961}
6962
Mark Mendellcfa410b2015-05-25 16:02:44 -04006963void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6964 DCHECK(dest.IsDoubleStackSlot());
6965 if (IsInt<32>(value)) {
6966 // Can move directly as an int32 constant.
6967 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6968 Immediate(static_cast<int32_t>(value)));
6969 } else {
6970 Load64BitValue(CpuRegister(TMP), value);
6971 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6972 }
6973}
6974
Mark Mendell9c86b482015-09-18 13:36:07 -04006975/**
6976 * Class to handle late fixup of offsets into constant area.
6977 */
6978class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6979 public:
6980 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6981 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6982
6983 protected:
6984 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6985
6986 CodeGeneratorX86_64* codegen_;
6987
6988 private:
6989 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6990 // Patch the correct offset for the instruction. We use the address of the
6991 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6992 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6993 int32_t relative_position = constant_offset - pos;
6994
6995 // Patch in the right value.
6996 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6997 }
6998
6999 // Location in constant area that the fixup refers to.
7000 size_t offset_into_constant_area_;
7001};
7002
7003/**
7004 t * Class to handle late fixup of offsets to a jump table that will be created in the
7005 * constant area.
7006 */
7007class JumpTableRIPFixup : public RIPFixup {
7008 public:
7009 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7010 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7011
7012 void CreateJumpTable() {
7013 X86_64Assembler* assembler = codegen_->GetAssembler();
7014
7015 // Ensure that the reference to the jump table has the correct offset.
7016 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7017 SetOffset(offset_in_constant_table);
7018
7019 // Compute the offset from the start of the function to this jump table.
7020 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7021
7022 // Populate the jump table with the correct values for the jump table.
7023 int32_t num_entries = switch_instr_->GetNumEntries();
7024 HBasicBlock* block = switch_instr_->GetBlock();
7025 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7026 // The value that we want is the target offset - the position of the table.
7027 for (int32_t i = 0; i < num_entries; i++) {
7028 HBasicBlock* b = successors[i];
7029 Label* l = codegen_->GetLabelOf(b);
7030 DCHECK(l->IsBound());
7031 int32_t offset_to_block = l->Position() - current_table_offset;
7032 assembler->AppendInt32(offset_to_block);
7033 }
7034 }
7035
7036 private:
7037 const HPackedSwitch* switch_instr_;
7038};
7039
Mark Mendellf55c3e02015-03-26 21:07:46 -04007040void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7041 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007042 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007043 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7044 // 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 -04007045 assembler->Align(4, 0);
7046 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007047
7048 // Populate any jump tables.
7049 for (auto jump_table : fixups_to_jump_tables_) {
7050 jump_table->CreateJumpTable();
7051 }
7052
7053 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007054 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007055 }
7056
7057 // And finish up.
7058 CodeGenerator::Finalize(allocator);
7059}
7060
Mark Mendellf55c3e02015-03-26 21:07:46 -04007061Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7062 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7063 return Address::RIP(fixup);
7064}
7065
7066Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7067 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7068 return Address::RIP(fixup);
7069}
7070
7071Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7072 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7073 return Address::RIP(fixup);
7074}
7075
7076Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7077 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7078 return Address::RIP(fixup);
7079}
7080
Andreas Gampe85b62f22015-09-09 13:15:38 -07007081// TODO: trg as memory.
7082void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7083 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007084 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007085 return;
7086 }
7087
7088 DCHECK_NE(type, Primitive::kPrimVoid);
7089
7090 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7091 if (trg.Equals(return_loc)) {
7092 return;
7093 }
7094
7095 // Let the parallel move resolver take care of all of this.
7096 HParallelMove parallel_move(GetGraph()->GetArena());
7097 parallel_move.AddMove(return_loc, trg, type, nullptr);
7098 GetMoveResolver()->EmitNativeCode(&parallel_move);
7099}
7100
Mark Mendell9c86b482015-09-18 13:36:07 -04007101Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7102 // Create a fixup to be used to create and address the jump table.
7103 JumpTableRIPFixup* table_fixup =
7104 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7105
7106 // We have to populate the jump tables.
7107 fixups_to_jump_tables_.push_back(table_fixup);
7108 return Address::RIP(table_fixup);
7109}
7110
Mark Mendellea5af682015-10-22 17:35:49 -04007111void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7112 const Address& addr_high,
7113 int64_t v,
7114 HInstruction* instruction) {
7115 if (IsInt<32>(v)) {
7116 int32_t v_32 = v;
7117 __ movq(addr_low, Immediate(v_32));
7118 MaybeRecordImplicitNullCheck(instruction);
7119 } else {
7120 // Didn't fit in a register. Do it in pieces.
7121 int32_t low_v = Low32Bits(v);
7122 int32_t high_v = High32Bits(v);
7123 __ movl(addr_low, Immediate(low_v));
7124 MaybeRecordImplicitNullCheck(instruction);
7125 __ movl(addr_high, Immediate(high_v));
7126 }
7127}
7128
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007129void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7130 const uint8_t* roots_data,
7131 const PatchInfo<Label>& info,
7132 uint64_t index_in_table) const {
7133 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7134 uintptr_t address =
7135 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7136 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7137 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7138 dchecked_integral_cast<uint32_t>(address);
7139}
7140
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007141void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7142 for (const PatchInfo<Label>& info : jit_string_patches_) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007143 const auto& it = jit_string_roots_.find(
7144 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007145 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007146 PatchJitRootUse(code, roots_data, info, it->second);
7147 }
7148
7149 for (const PatchInfo<Label>& info : jit_class_patches_) {
7150 const auto& it = jit_class_roots_.find(
7151 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7152 DCHECK(it != jit_class_roots_.end());
7153 PatchJitRootUse(code, roots_data, info, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007154 }
7155}
7156
Roland Levillain4d027112015-07-01 15:41:14 +01007157#undef __
7158
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007159} // namespace x86_64
7160} // namespace art