blob: e8bfa66a589625364ca568db1091e16e574b9328 [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"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070025#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080026#include "intrinsics.h"
27#include "intrinsics_x86_64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010028#include "linker/linker_patch.h"
Andreas Gamped4901292017-05-30 18:41:34 -070029#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "mirror/object_reference.h"
33#include "thread.h"
34#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036#include "utils/x86_64/assembler_x86_64.h"
37#include "utils/x86_64/managed_register_x86_64.h"
38
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010039namespace art {
40
Roland Levillain0d5a2812015-11-13 10:07:31 +000041template<class MirrorType>
42class GcRoot;
43
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010044namespace x86_64 {
45
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000048// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
49// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
50// generates less code/data with a small num_entries.
51static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000053static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000054static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Mark Mendell24f2dfa2015-01-14 19:51:45 -050056static constexpr int kC2ConditionMask = 0x400;
57
Roland Levillain7cbd27f2016-08-11 23:53:33 +010058// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
59#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000064 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames2ed20af2015-03-06 13:55:35 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000067 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010073 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000074 instruction_,
75 instruction_->GetDexPc(),
76 this);
Roland Levillain888d0672015-11-23 18:53:50 +000077 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 }
79
Alexandre Rames8158f282015-08-07 10:26:17 +010080 bool IsFatal() const OVERRIDE { return true; }
81
Alexandre Rames9931f312015-06-19 14:47:01 +010082 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
83
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
86};
87
Andreas Gampe85b62f22015-09-09 13:15:38 -070088class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000089 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000090 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000091
Alexandre Rames2ed20af2015-03-06 13:55:35 +000092 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000093 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000094 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010095 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100109 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, DataType::Type type, bool is_div)
David Srbecky9cd6d372016-02-09 15:24:47 +0000110 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100114 if (type_ == DataType::Type::kInt32) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negl(cpu_reg_);
117 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400118 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 }
120
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000121 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 DCHECK_EQ(DataType::Type::kInt64, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 if (is_div_) {
124 __ negq(cpu_reg_);
125 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400126 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000128 }
Calin Juravled0d48522014-11-04 16:40:20 +0000129 __ jmp(GetExitLabel());
130 }
131
Alexandre Rames9931f312015-06-19 14:47:01 +0100132 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
133
Calin Juravled0d48522014-11-04 16:40:20 +0000134 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const CpuRegister cpu_reg_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 const DataType::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 const bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Andreas Gampe85b62f22015-09-09 13:15:38 -0700141class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100143 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700147 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000148 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700150 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100151 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000152 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700153 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 if (successor_ == nullptr) {
155 __ jmp(GetReturnLabel());
156 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000157 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 }
160
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 Label* GetReturnLabel() {
162 DCHECK(successor_ == nullptr);
163 return &return_label_;
164 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100166 HBasicBlock* GetSuccessor() const {
167 return successor_;
168 }
169
Alexandre Rames9931f312015-06-19 14:47:01 +0100170 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
171
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Andreas Gampe85b62f22015-09-09 13:15:38 -0700179class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000182 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000186 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000188 if (instruction_->CanThrowIntoCatchBlock()) {
189 // Live registers will be restored in the catch block if caught.
190 SaveLiveRegisters(codegen, instruction_->GetLocations());
191 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400192 // Are we using an array length from memory?
193 HInstruction* array_length = instruction_->InputAt(1);
194 Location length_loc = locations->InAt(1);
195 InvokeRuntimeCallingConvention calling_convention;
196 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
197 // Load the array length into our temporary.
198 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
199 Location array_loc = array_length->GetLocations()->InAt(0);
200 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
201 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
202 // Check for conflicts with index.
203 if (length_loc.Equals(locations->InAt(0))) {
204 // We know we aren't using parameter 2.
205 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
206 }
207 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700208 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100209 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700210 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400211 }
212
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000213 // We're moving two locations to locations that could overlap, so we need a parallel
214 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000215 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100216 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000217 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100218 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400219 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100220 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100221 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100222 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
223 ? kQuickThrowStringBounds
224 : kQuickThrowArrayBounds;
225 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100226 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000227 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100228 }
229
Alexandre Rames8158f282015-08-07 10:26:17 +0100230 bool IsFatal() const OVERRIDE { return true; }
231
Alexandre Rames9931f312015-06-19 14:47:01 +0100232 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
233
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100234 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100235 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
236};
237
Andreas Gampe85b62f22015-09-09 13:15:38 -0700238class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 LoadClassSlowPathX86_64(HLoadClass* cls,
241 HInstruction* at,
242 uint32_t dex_pc,
243 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
246 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000250 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000253 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254
Vladimir Markoea4c1262017-02-06 19:59:33 +0000255 // Custom calling convention: RAX serves as both input and output.
256 __ movl(CpuRegister(RAX), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100257 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000258 instruction_,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000259 dex_pc_,
260 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000261 if (do_clinit_) {
262 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
263 } else {
264 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
265 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100266
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000269 if (out.IsValid()) {
270 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000271 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 }
273
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000274 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000275 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
276 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
277 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
278 DCHECK(out.IsValid());
279 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
280 locations->Out().AsRegister<CpuRegister>());
281 Label* fixup_label = x86_64_codegen->NewTypeBssEntryPatch(cls_);
282 __ Bind(fixup_label);
283 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100284 __ jmp(GetExitLabel());
285 }
286
Alexandre Rames9931f312015-06-19 14:47:01 +0100287 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
288
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100289 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290 // The class this slow path will load.
291 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100292
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000293 // The dex PC of `at_`.
294 const uint32_t dex_pc_;
295
296 // Whether to initialize the class.
297 const bool do_clinit_;
298
299 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100300};
301
Vladimir Markoaad75c62016-10-03 08:46:48 +0000302class LoadStringSlowPathX86_64 : public SlowPathCode {
303 public:
304 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
305
306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
307 LocationSummary* locations = instruction_->GetLocations();
308 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
309
310 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
311 __ Bind(GetEntryLabel());
312 SaveLiveRegisters(codegen, locations);
313
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000314 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100315 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000317 x86_64_codegen->InvokeRuntime(kQuickResolveString,
318 instruction_,
319 instruction_->GetDexPc(),
320 this);
321 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
322 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
323 RestoreLiveRegisters(codegen, locations);
324
325 // Store the resolved String to the BSS entry.
326 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
327 locations->Out().AsRegister<CpuRegister>());
328 Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
329 __ Bind(fixup_label);
330
331 __ jmp(GetExitLabel());
332 }
333
334 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
335
336 private:
337 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
338};
339
Andreas Gampe85b62f22015-09-09 13:15:38 -0700340class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000342 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000343 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 DCHECK(instruction_->IsCheckCast()
349 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000350
Roland Levillain0d5a2812015-11-13 10:07:31 +0000351 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000354 if (!is_fatal_) {
355 SaveLiveRegisters(codegen, locations);
356 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000357
358 // We're moving two locations to locations that could overlap, so we need a parallel
359 // move resolver.
360 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800362 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100363 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800364 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800365 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100366 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000367 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100368 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800369 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000370 } else {
371 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800372 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
373 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000374 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 if (!is_fatal_) {
377 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000378 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000380
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000381 RestoreLiveRegisters(codegen, locations);
382 __ jmp(GetExitLabel());
383 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384 }
385
Alexandre Rames9931f312015-06-19 14:47:01 +0100386 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
387
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000388 bool IsFatal() const OVERRIDE { return is_fatal_; }
389
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000390 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000391 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000392
393 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
394};
395
Andreas Gampe85b62f22015-09-09 13:15:38 -0700396class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 public:
Aart Bik42249c32016-01-07 15:33:50 -0800398 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000399 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000402 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100404 LocationSummary* locations = instruction_->GetLocations();
405 SaveLiveRegisters(codegen, locations);
406 InvokeRuntimeCallingConvention calling_convention;
407 x86_64_codegen->Load32BitValue(
408 CpuRegister(calling_convention.GetRegisterAt(0)),
409 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100410 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100411 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700412 }
413
Alexandre Rames9931f312015-06-19 14:47:01 +0100414 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
415
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700416 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700417 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
418};
419
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100420class ArraySetSlowPathX86_64 : public SlowPathCode {
421 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000422 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423
424 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
425 LocationSummary* locations = instruction_->GetLocations();
426 __ Bind(GetEntryLabel());
427 SaveLiveRegisters(codegen, locations);
428
429 InvokeRuntimeCallingConvention calling_convention;
430 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
431 parallel_move.AddMove(
432 locations->InAt(0),
433 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100434 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435 nullptr);
436 parallel_move.AddMove(
437 locations->InAt(1),
438 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100439 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100440 nullptr);
441 parallel_move.AddMove(
442 locations->InAt(2),
443 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100444 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 nullptr);
446 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
447
Roland Levillain0d5a2812015-11-13 10:07:31 +0000448 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100449 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000450 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100451 RestoreLiveRegisters(codegen, locations);
452 __ jmp(GetExitLabel());
453 }
454
455 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
456
457 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100458 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
459};
460
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100461// Slow path marking an object reference `ref` during a read
462// barrier. The field `obj.field` in the object `obj` holding this
463// reference does not get updated by this slow path after marking (see
464// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
465//
466// This means that after the execution of this slow path, `ref` will
467// always be up-to-date, but `obj.field` may not; i.e., after the
468// flip, `ref` will be a to-space reference, but `obj.field` will
469// probably still be a from-space reference (unless it gets updated by
470// another thread, or if another thread installed another object
471// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000472class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
473 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100474 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
475 Location ref,
476 bool unpoison_ref_before_marking)
477 : SlowPathCode(instruction),
478 ref_(ref),
479 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000480 DCHECK(kEmitCompilerReadBarrier);
481 }
482
483 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
484
485 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
486 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
488 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000489 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 DCHECK(instruction_->IsInstanceFieldGet() ||
492 instruction_->IsStaticFieldGet() ||
493 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100494 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000495 instruction_->IsLoadClass() ||
496 instruction_->IsLoadString() ||
497 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100498 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100499 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
500 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000501 << "Unexpected instruction in read barrier marking slow path: "
502 << instruction_->DebugName();
503
504 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000506 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000508 }
Roland Levillain4359e612016-07-20 11:32:19 +0100509 // No need to save live registers; it's taken care of by the
510 // entrypoint. Also, there is no need to update the stack mask,
511 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000512 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 DCHECK_NE(ref_reg, RSP);
514 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // "Compact" slow path, saving two moves.
516 //
517 // Instead of using the standard runtime calling convention (input
518 // and output in R0):
519 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100521 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100522 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100523 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100524 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100525 // of a dedicated entrypoint:
526 //
527 // rX <- ReadBarrierMarkRegX(rX)
528 //
529 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100530 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100531 // This runtime call does not require a stack map.
532 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000533 __ jmp(GetExitLabel());
534 }
535
536 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100537 // The location (register) of the marked object reference.
538 const Location ref_;
539 // Should the reference in `ref_` be unpoisoned prior to marking it?
540 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000541
542 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
543};
544
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100545// Slow path marking an object reference `ref` during a read barrier,
546// and if needed, atomically updating the field `obj.field` in the
547// object `obj` holding this reference after marking (contrary to
548// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
549// `obj.field`).
550//
551// This means that after the execution of this slow path, both `ref`
552// and `obj.field` will be up-to-date; i.e., after the flip, both will
553// hold the same to-space reference (unless another thread installed
554// another object reference (different from `ref`) in `obj.field`).
555class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
556 public:
557 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
558 Location ref,
559 CpuRegister obj,
560 const Address& field_addr,
561 bool unpoison_ref_before_marking,
562 CpuRegister temp1,
563 CpuRegister temp2)
564 : SlowPathCode(instruction),
565 ref_(ref),
566 obj_(obj),
567 field_addr_(field_addr),
568 unpoison_ref_before_marking_(unpoison_ref_before_marking),
569 temp1_(temp1),
570 temp2_(temp2) {
571 DCHECK(kEmitCompilerReadBarrier);
572 }
573
574 const char* GetDescription() const OVERRIDE {
575 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
576 }
577
578 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
579 LocationSummary* locations = instruction_->GetLocations();
580 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
581 Register ref_reg = ref_cpu_reg.AsRegister();
582 DCHECK(locations->CanCall());
583 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
584 // This slow path is only used by the UnsafeCASObject intrinsic.
585 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
586 << "Unexpected instruction in read barrier marking and field updating slow path: "
587 << instruction_->DebugName();
588 DCHECK(instruction_->GetLocations()->Intrinsified());
589 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
590
591 __ Bind(GetEntryLabel());
592 if (unpoison_ref_before_marking_) {
593 // Object* ref = ref_addr->AsMirrorPtr()
594 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
595 }
596
597 // Save the old (unpoisoned) reference.
598 __ movl(temp1_, ref_cpu_reg);
599
600 // No need to save live registers; it's taken care of by the
601 // entrypoint. Also, there is no need to update the stack mask,
602 // as this runtime call will not trigger a garbage collection.
603 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
604 DCHECK_NE(ref_reg, RSP);
605 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
606 // "Compact" slow path, saving two moves.
607 //
608 // Instead of using the standard runtime calling convention (input
609 // and output in R0):
610 //
611 // RDI <- ref
612 // RAX <- ReadBarrierMark(RDI)
613 // ref <- RAX
614 //
615 // we just use rX (the register containing `ref`) as input and output
616 // of a dedicated entrypoint:
617 //
618 // rX <- ReadBarrierMarkRegX(rX)
619 //
620 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100621 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100622 // This runtime call does not require a stack map.
623 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
624
625 // If the new reference is different from the old reference,
626 // update the field in the holder (`*field_addr`).
627 //
628 // Note that this field could also hold a different object, if
629 // another thread had concurrently changed it. In that case, the
630 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
631 // operation below would abort the CAS, leaving the field as-is.
632 NearLabel done;
633 __ cmpl(temp1_, ref_cpu_reg);
634 __ j(kEqual, &done);
635
636 // Update the the holder's field atomically. This may fail if
637 // mutator updates before us, but it's OK. This is achived
638 // using a strong compare-and-set (CAS) operation with relaxed
639 // memory synchronization ordering, where the expected value is
640 // the old reference and the desired value is the new reference.
641 // This operation is implemented with a 32-bit LOCK CMPXLCHG
642 // instruction, which requires the expected value (the old
643 // reference) to be in EAX. Save RAX beforehand, and move the
644 // expected value (stored in `temp1_`) into EAX.
645 __ movq(temp2_, CpuRegister(RAX));
646 __ movl(CpuRegister(RAX), temp1_);
647
648 // Convenience aliases.
649 CpuRegister base = obj_;
650 CpuRegister expected = CpuRegister(RAX);
651 CpuRegister value = ref_cpu_reg;
652
653 bool base_equals_value = (base.AsRegister() == value.AsRegister());
654 Register value_reg = ref_reg;
655 if (kPoisonHeapReferences) {
656 if (base_equals_value) {
657 // If `base` and `value` are the same register location, move
658 // `value_reg` to a temporary register. This way, poisoning
659 // `value_reg` won't invalidate `base`.
660 value_reg = temp1_.AsRegister();
661 __ movl(CpuRegister(value_reg), base);
662 }
663
664 // Check that the register allocator did not assign the location
665 // of `expected` (RAX) to `value` nor to `base`, so that heap
666 // poisoning (when enabled) works as intended below.
667 // - If `value` were equal to `expected`, both references would
668 // be poisoned twice, meaning they would not be poisoned at
669 // all, as heap poisoning uses address negation.
670 // - If `base` were equal to `expected`, poisoning `expected`
671 // would invalidate `base`.
672 DCHECK_NE(value_reg, expected.AsRegister());
673 DCHECK_NE(base.AsRegister(), expected.AsRegister());
674
675 __ PoisonHeapReference(expected);
676 __ PoisonHeapReference(CpuRegister(value_reg));
677 }
678
679 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
680
681 // If heap poisoning is enabled, we need to unpoison the values
682 // that were poisoned earlier.
683 if (kPoisonHeapReferences) {
684 if (base_equals_value) {
685 // `value_reg` has been moved to a temporary register, no need
686 // to unpoison it.
687 } else {
688 __ UnpoisonHeapReference(CpuRegister(value_reg));
689 }
690 // No need to unpoison `expected` (RAX), as it is be overwritten below.
691 }
692
693 // Restore RAX.
694 __ movq(CpuRegister(RAX), temp2_);
695
696 __ Bind(&done);
697 __ jmp(GetExitLabel());
698 }
699
700 private:
701 // The location (register) of the marked object reference.
702 const Location ref_;
703 // The register containing the object holding the marked object reference field.
704 const CpuRegister obj_;
705 // The address of the marked reference field. The base of this address must be `obj_`.
706 const Address field_addr_;
707
708 // Should the reference in `ref_` be unpoisoned prior to marking it?
709 const bool unpoison_ref_before_marking_;
710
711 const CpuRegister temp1_;
712 const CpuRegister temp2_;
713
714 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
715};
716
Roland Levillain0d5a2812015-11-13 10:07:31 +0000717// Slow path generating a read barrier for a heap reference.
718class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
719 public:
720 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
721 Location out,
722 Location ref,
723 Location obj,
724 uint32_t offset,
725 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000726 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000727 out_(out),
728 ref_(ref),
729 obj_(obj),
730 offset_(offset),
731 index_(index) {
732 DCHECK(kEmitCompilerReadBarrier);
733 // If `obj` is equal to `out` or `ref`, it means the initial
734 // object has been overwritten by (or after) the heap object
735 // reference load to be instrumented, e.g.:
736 //
737 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000738 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000739 //
740 // In that case, we have lost the information about the original
741 // object, and the emitted read barrier cannot work properly.
742 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
743 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
744}
745
746 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
747 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
748 LocationSummary* locations = instruction_->GetLocations();
749 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
750 DCHECK(locations->CanCall());
751 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100752 DCHECK(instruction_->IsInstanceFieldGet() ||
753 instruction_->IsStaticFieldGet() ||
754 instruction_->IsArrayGet() ||
755 instruction_->IsInstanceOf() ||
756 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700757 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000758 << "Unexpected instruction in read barrier for heap reference slow path: "
759 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000760
761 __ Bind(GetEntryLabel());
762 SaveLiveRegisters(codegen, locations);
763
764 // We may have to change the index's value, but as `index_` is a
765 // constant member (like other "inputs" of this slow path),
766 // introduce a copy of it, `index`.
767 Location index = index_;
768 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100769 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000770 if (instruction_->IsArrayGet()) {
771 // Compute real offset and store it in index_.
772 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
773 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
774 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
775 // We are about to change the value of `index_reg` (see the
776 // calls to art::x86_64::X86_64Assembler::shll and
777 // art::x86_64::X86_64Assembler::AddImmediate below), but it
778 // has not been saved by the previous call to
779 // art::SlowPathCode::SaveLiveRegisters, as it is a
780 // callee-save register --
781 // art::SlowPathCode::SaveLiveRegisters does not consider
782 // callee-save registers, as it has been designed with the
783 // assumption that callee-save registers are supposed to be
784 // handled by the called function. So, as a callee-save
785 // register, `index_reg` _would_ eventually be saved onto
786 // the stack, but it would be too late: we would have
787 // changed its value earlier. Therefore, we manually save
788 // it here into another freely available register,
789 // `free_reg`, chosen of course among the caller-save
790 // registers (as a callee-save `free_reg` register would
791 // exhibit the same problem).
792 //
793 // Note we could have requested a temporary register from
794 // the register allocator instead; but we prefer not to, as
795 // this is a slow path, and we know we can find a
796 // caller-save register that is available.
797 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
798 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
799 index_reg = free_reg;
800 index = Location::RegisterLocation(index_reg);
801 } else {
802 // The initial register stored in `index_` has already been
803 // saved in the call to art::SlowPathCode::SaveLiveRegisters
804 // (as it is not a callee-save register), so we can freely
805 // use it.
806 }
807 // Shifting the index value contained in `index_reg` by the
808 // scale factor (2) cannot overflow in practice, as the
809 // runtime is unable to allocate object arrays with a size
810 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
811 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
812 static_assert(
813 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
814 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
815 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
816 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100817 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
818 // intrinsics, `index_` is not shifted by a scale factor of 2
819 // (as in the case of ArrayGet), as it is actually an offset
820 // to an object field within an object.
821 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 DCHECK(instruction_->GetLocations()->Intrinsified());
823 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
824 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
825 << instruction_->AsInvoke()->GetIntrinsic();
826 DCHECK_EQ(offset_, 0U);
827 DCHECK(index_.IsRegister());
828 }
829 }
830
831 // We're moving two or three locations to locations that could
832 // overlap, so we need a parallel move resolver.
833 InvokeRuntimeCallingConvention calling_convention;
834 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
835 parallel_move.AddMove(ref_,
836 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100837 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000838 nullptr);
839 parallel_move.AddMove(obj_,
840 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100841 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000842 nullptr);
843 if (index.IsValid()) {
844 parallel_move.AddMove(index,
845 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100846 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000847 nullptr);
848 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
849 } else {
850 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
851 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
852 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100853 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000854 instruction_,
855 instruction_->GetDexPc(),
856 this);
857 CheckEntrypointTypes<
858 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
859 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
860
861 RestoreLiveRegisters(codegen, locations);
862 __ jmp(GetExitLabel());
863 }
864
865 const char* GetDescription() const OVERRIDE {
866 return "ReadBarrierForHeapReferenceSlowPathX86_64";
867 }
868
869 private:
870 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
871 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
872 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
873 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
874 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
875 return static_cast<CpuRegister>(i);
876 }
877 }
878 // We shall never fail to find a free caller-save register, as
879 // there are more than two core caller-save registers on x86-64
880 // (meaning it is possible to find one which is different from
881 // `ref` and `obj`).
882 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
883 LOG(FATAL) << "Could not find a free caller-save register";
884 UNREACHABLE();
885 }
886
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887 const Location out_;
888 const Location ref_;
889 const Location obj_;
890 const uint32_t offset_;
891 // An additional location containing an index to an array.
892 // Only used for HArrayGet and the UnsafeGetObject &
893 // UnsafeGetObjectVolatile intrinsics.
894 const Location index_;
895
896 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
897};
898
899// Slow path generating a read barrier for a GC root.
900class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
901 public:
902 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000903 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000904 DCHECK(kEmitCompilerReadBarrier);
905 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000906
907 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
908 LocationSummary* locations = instruction_->GetLocations();
909 DCHECK(locations->CanCall());
910 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000911 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
912 << "Unexpected instruction in read barrier for GC root slow path: "
913 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000914
915 __ Bind(GetEntryLabel());
916 SaveLiveRegisters(codegen, locations);
917
918 InvokeRuntimeCallingConvention calling_convention;
919 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
920 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100921 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000922 instruction_,
923 instruction_->GetDexPc(),
924 this);
925 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
926 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
927
928 RestoreLiveRegisters(codegen, locations);
929 __ jmp(GetExitLabel());
930 }
931
932 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
933
934 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000935 const Location out_;
936 const Location root_;
937
938 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
939};
940
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100941#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100942// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
943#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100944
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
949 case kCondLT: return kLess;
950 case kCondLE: return kLessEqual;
951 case kCondGT: return kGreater;
952 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700953 case kCondB: return kBelow;
954 case kCondBE: return kBelowEqual;
955 case kCondA: return kAbove;
956 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700957 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100958 LOG(FATAL) << "Unreachable";
959 UNREACHABLE();
960}
961
Aart Bike9f37602015-10-09 11:15:55 -0700962// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100963inline Condition X86_64FPCondition(IfCondition cond) {
964 switch (cond) {
965 case kCondEQ: return kEqual;
966 case kCondNE: return kNotEqual;
967 case kCondLT: return kBelow;
968 case kCondLE: return kBelowEqual;
969 case kCondGT: return kAbove;
970 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700971 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100972 };
973 LOG(FATAL) << "Unreachable";
974 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700975}
976
Vladimir Markodc151b22015-10-15 18:02:30 +0100977HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
978 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100979 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000980 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100981}
982
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100983void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
984 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800985 // All registers are assumed to be correctly set up.
Vladimir Marko4ee8e292017-06-02 15:39:30 +0000986
Vladimir Marko58155012015-08-19 12:49:41 +0000987 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
988 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100989 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000990 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100991 uint32_t offset =
992 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
993 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000994 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100995 }
Vladimir Marko58155012015-08-19 12:49:41 +0000996 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000997 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000998 break;
Vladimir Marko65979462017-05-19 17:25:12 +0100999 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
1000 DCHECK(GetCompilerOptions().IsBootImage());
1001 __ leal(temp.AsRegister<CpuRegister>(),
1002 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
1003 RecordBootMethodPatch(invoke);
1004 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001005 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +00001006 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +00001007 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001008 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001009 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001010 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001011 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001012 __ Bind(NewMethodBssEntryPatch(
1013 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko58155012015-08-19 12:49:41 +00001014 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001015 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001016 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1017 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1018 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001019 }
Vladimir Marko58155012015-08-19 12:49:41 +00001020 }
1021
1022 switch (invoke->GetCodePtrLocation()) {
1023 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1024 __ call(&frame_entry_label_);
1025 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001026 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1027 // (callee_method + offset_of_quick_compiled_code)()
1028 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1029 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001030 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001031 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001032 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001033 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001034
1035 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001036}
1037
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001038void CodeGeneratorX86_64::GenerateVirtualCall(
1039 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001040 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1041 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1042 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001043
1044 // Use the calling convention instead of the location of the receiver, as
1045 // intrinsics may have put the receiver in a different register. In the intrinsics
1046 // slow path, the arguments have been moved to the right place, so here we are
1047 // guaranteed that the receiver is the first register of the calling convention.
1048 InvokeDexCallingConvention calling_convention;
1049 Register receiver = calling_convention.GetRegisterAt(0);
1050
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001051 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001052 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001053 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001054 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001055 // Instead of simply (possibly) unpoisoning `temp` here, we should
1056 // emit a read barrier for the previous class reference load.
1057 // However this is not required in practice, as this is an
1058 // intermediate/temporary reference and because the current
1059 // concurrent copying collector keeps the from-space memory
1060 // intact/accessible until the end of the marking phase (the
1061 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001062 __ MaybeUnpoisonHeapReference(temp);
1063 // temp = temp->GetMethodAt(method_offset);
1064 __ movq(temp, Address(temp, method_offset));
1065 // call temp->GetEntryPoint();
1066 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001067 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001068 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001069}
1070
Vladimir Marko65979462017-05-19 17:25:12 +01001071void CodeGeneratorX86_64::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
1072 boot_image_method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001073 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001074 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001075}
1076
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001077Label* CodeGeneratorX86_64::NewMethodBssEntryPatch(MethodReference target_method) {
1078 // Add a patch entry and return the label.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001079 method_bss_entry_patches_.emplace_back(*target_method.dex_file, target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001080 return &method_bss_entry_patches_.back().label;
1081}
1082
Vladimir Marko1998cd02017-01-13 13:02:58 +00001083void CodeGeneratorX86_64::RecordBootTypePatch(HLoadClass* load_class) {
1084 boot_image_type_patches_.emplace_back(load_class->GetDexFile(),
1085 load_class->GetTypeIndex().index_);
1086 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001087}
1088
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001089Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001090 type_bss_entry_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
1091 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001092}
1093
Vladimir Marko65979462017-05-19 17:25:12 +01001094void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01001095 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
1096 __ Bind(&string_patches_.back().label);
1097}
1098
Vladimir Markoaad75c62016-10-03 08:46:48 +00001099Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1100 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001101 string_bss_entry_patches_.emplace_back(
1102 load_string->GetDexFile(), load_string->GetStringIndex().index_);
1103 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001104}
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
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001110template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001111inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1112 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001113 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001114 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 Markod8dbc8d2017-09-20 13:37:47 +01001121void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001122 DCHECK(linker_patches->empty());
1123 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001124 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001125 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001126 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001127 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001128 string_patches_.size() +
1129 string_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001130 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001131 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001132 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1133 boot_image_method_patches_, linker_patches);
1134 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1135 boot_image_type_patches_, linker_patches);
1136 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1137 string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001138 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01001139 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001140 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1141 boot_image_type_patches_, linker_patches);
1142 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1143 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001144 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001145 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1146 method_bss_entry_patches_, linker_patches);
1147 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1148 type_bss_entry_patches_, linker_patches);
1149 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1150 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001151 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001152}
1153
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001154void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001155 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156}
1157
1158void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001159 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160}
1161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001162size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1163 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1164 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001165}
1166
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001167size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1168 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1169 return kX86_64WordSize;
1170}
1171
1172size_t CodeGeneratorX86_64::SaveFloatingPointRegister(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(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001175 } else {
1176 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1177 }
1178 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001179}
1180
1181size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001182 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001183 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001184 } else {
1185 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1186 }
1187 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001188}
1189
Calin Juravle175dc732015-08-25 15:42:32 +01001190void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1191 HInstruction* instruction,
1192 uint32_t dex_pc,
1193 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001194 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001195 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1196 if (EntrypointRequiresStackMap(entrypoint)) {
1197 RecordPcInfo(instruction, dex_pc, slow_path);
1198 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001199}
1200
Roland Levillaindec8f632016-07-22 17:10:06 +01001201void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1202 HInstruction* instruction,
1203 SlowPathCode* slow_path) {
1204 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001205 GenerateInvokeRuntime(entry_point_offset);
1206}
1207
1208void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001209 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1210}
1211
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001212static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001213// Use a fake return address register to mimic Quick.
1214static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001215CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001216 const X86_64InstructionSetFeatures& isa_features,
1217 const CompilerOptions& compiler_options,
1218 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001219 : CodeGenerator(graph,
1220 kNumberOfCpuRegisters,
1221 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001222 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001223 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1224 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001225 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001226 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1227 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001228 compiler_options,
1229 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001230 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001231 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001232 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001233 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001234 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001235 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001236 constant_area_start_(0),
Vladimir Marko65979462017-05-19 17:25:12 +01001237 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001238 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001239 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1240 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001241 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001242 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001243 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001244 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1245 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001246 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1247}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001249InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1250 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001251 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001252 assembler_(codegen->GetAssembler()),
1253 codegen_(codegen) {}
1254
David Brazdil58282f42016-01-14 12:45:10 +00001255void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001256 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001257 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001258
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001259 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001260 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001261}
1262
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001263static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001264 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001265}
David Srbecky9d8606d2015-04-12 09:35:32 +01001266
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001267static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001268 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001269}
1270
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001271void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001272 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001273 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001274 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001275 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001276 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001277
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001278 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001279 __ testq(CpuRegister(RAX), Address(
1280 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001281 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001282 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001283
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001284 if (HasEmptyFrame()) {
1285 return;
1286 }
1287
Nicolas Geoffray98893962015-01-21 12:32:32 +00001288 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001289 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001290 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001291 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001292 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1293 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001294 }
1295 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001296
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001297 int adjust = GetFrameSize() - GetCoreSpillSize();
1298 __ subq(CpuRegister(RSP), Immediate(adjust));
1299 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001300 uint32_t xmm_spill_location = GetFpuSpillStart();
1301 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001302
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001303 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1304 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001305 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1306 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1307 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001308 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001309 }
1310
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001311 // Save the current method if we need it. Note that we do not
1312 // do this in HCurrentMethod, as the instruction might have been removed
1313 // in the SSA graph.
1314 if (RequiresCurrentMethod()) {
1315 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1316 CpuRegister(kMethodRegisterArgument));
1317 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001318
1319 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1320 // Initialize should_deoptimize flag to 0.
1321 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1322 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001323}
1324
1325void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001326 __ cfi().RememberState();
1327 if (!HasEmptyFrame()) {
1328 uint32_t xmm_spill_location = GetFpuSpillStart();
1329 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1330 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1331 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1332 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1333 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1334 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1335 }
1336 }
1337
1338 int adjust = GetFrameSize() - GetCoreSpillSize();
1339 __ addq(CpuRegister(RSP), Immediate(adjust));
1340 __ cfi().AdjustCFAOffset(-adjust);
1341
1342 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1343 Register reg = kCoreCalleeSaves[i];
1344 if (allocated_registers_.ContainsCoreRegister(reg)) {
1345 __ popq(CpuRegister(reg));
1346 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1347 __ cfi().Restore(DWARFReg(reg));
1348 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001349 }
1350 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001351 __ ret();
1352 __ cfi().RestoreState();
1353 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001354}
1355
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001356void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1357 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001358}
1359
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001360void CodeGeneratorX86_64::Move(Location destination, Location source) {
1361 if (source.Equals(destination)) {
1362 return;
1363 }
1364 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001365 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001366 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001367 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001368 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001369 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001370 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001371 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1372 } else if (source.IsConstant()) {
1373 HConstant* constant = source.GetConstant();
1374 if (constant->IsLongConstant()) {
1375 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1376 } else {
1377 Load32BitValue(dest, GetInt32ValueOf(constant));
1378 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001379 } else {
1380 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001381 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001382 }
1383 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001384 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001385 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001386 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001387 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001388 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1389 } else if (source.IsConstant()) {
1390 HConstant* constant = source.GetConstant();
1391 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1392 if (constant->IsFloatConstant()) {
1393 Load32BitValue(dest, static_cast<int32_t>(value));
1394 } else {
1395 Load64BitValue(dest, value);
1396 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001397 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001398 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001399 } else {
1400 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001401 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001402 }
1403 } else if (destination.IsStackSlot()) {
1404 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001405 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001407 } else if (source.IsFpuRegister()) {
1408 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001409 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001410 } else if (source.IsConstant()) {
1411 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001412 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001413 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001414 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001415 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001416 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1417 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001418 }
1419 } else {
1420 DCHECK(destination.IsDoubleStackSlot());
1421 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001422 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001423 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 } else if (source.IsFpuRegister()) {
1425 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001426 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001427 } else if (source.IsConstant()) {
1428 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001429 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1430 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001431 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001432 } else {
1433 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001434 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1435 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001436 }
1437 }
1438}
1439
Calin Juravle175dc732015-08-25 15:42:32 +01001440void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1441 DCHECK(location.IsRegister());
1442 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1443}
1444
Calin Juravlee460d1d2015-09-29 04:52:17 +01001445void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001447 Move(dst, src);
1448}
1449
1450void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1451 if (location.IsRegister()) {
1452 locations->AddTemp(location);
1453 } else {
1454 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1455 }
1456}
1457
David Brazdilfc6a86a2015-06-26 10:33:45 +00001458void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001459 DCHECK(!successor->IsExitBlock());
1460
1461 HBasicBlock* block = got->GetBlock();
1462 HInstruction* previous = got->GetPrevious();
1463
1464 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001465 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001466 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1467 return;
1468 }
1469
1470 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1471 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1472 }
1473 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001474 __ jmp(codegen_->GetLabelOf(successor));
1475 }
1476}
1477
David Brazdilfc6a86a2015-06-26 10:33:45 +00001478void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1479 got->SetLocations(nullptr);
1480}
1481
1482void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1483 HandleGoto(got, got->GetSuccessor());
1484}
1485
1486void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1487 try_boundary->SetLocations(nullptr);
1488}
1489
1490void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1491 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1492 if (!successor->IsExitBlock()) {
1493 HandleGoto(try_boundary, successor);
1494 }
1495}
1496
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001497void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1498 exit->SetLocations(nullptr);
1499}
1500
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001501void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001502}
1503
Mark Mendell152408f2015-12-31 12:28:50 -05001504template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001505void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001506 LabelType* true_label,
1507 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001508 if (cond->IsFPConditionTrueIfNaN()) {
1509 __ j(kUnordered, true_label);
1510 } else if (cond->IsFPConditionFalseIfNaN()) {
1511 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001512 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001513 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001514}
1515
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001516void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001517 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001518
Mark Mendellc4701932015-04-10 13:18:51 -04001519 Location left = locations->InAt(0);
1520 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001521 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001522 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001523 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001524 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 case DataType::Type::kInt8:
1526 case DataType::Type::kUint16:
1527 case DataType::Type::kInt16:
1528 case DataType::Type::kInt32:
1529 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001530 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001531 break;
1532 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001533 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001534 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001535 break;
1536 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001537 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001538 if (right.IsFpuRegister()) {
1539 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1540 } else if (right.IsConstant()) {
1541 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1542 codegen_->LiteralFloatAddress(
1543 right.GetConstant()->AsFloatConstant()->GetValue()));
1544 } else {
1545 DCHECK(right.IsStackSlot());
1546 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1547 Address(CpuRegister(RSP), right.GetStackIndex()));
1548 }
Mark Mendellc4701932015-04-10 13:18:51 -04001549 break;
1550 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001551 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001552 if (right.IsFpuRegister()) {
1553 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1554 } else if (right.IsConstant()) {
1555 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1556 codegen_->LiteralDoubleAddress(
1557 right.GetConstant()->AsDoubleConstant()->GetValue()));
1558 } else {
1559 DCHECK(right.IsDoubleStackSlot());
1560 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1561 Address(CpuRegister(RSP), right.GetStackIndex()));
1562 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001563 break;
1564 }
1565 default:
1566 LOG(FATAL) << "Unexpected condition type " << type;
1567 }
1568}
1569
1570template<class LabelType>
1571void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1572 LabelType* true_target_in,
1573 LabelType* false_target_in) {
1574 // Generated branching requires both targets to be explicit. If either of the
1575 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1576 LabelType fallthrough_target;
1577 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1578 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1579
1580 // Generate the comparison to set the CC.
1581 GenerateCompareTest(condition);
1582
1583 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001585 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001586 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001587 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1588 break;
1589 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001590 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001591 GenerateFPJumps(condition, true_target, false_target);
1592 break;
1593 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001594 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001595 GenerateFPJumps(condition, true_target, false_target);
1596 break;
1597 }
1598 default:
1599 LOG(FATAL) << "Unexpected condition type " << type;
1600 }
1601
David Brazdil0debae72015-11-12 18:37:00 +00001602 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001603 __ jmp(false_target);
1604 }
David Brazdil0debae72015-11-12 18:37:00 +00001605
1606 if (fallthrough_target.IsLinked()) {
1607 __ Bind(&fallthrough_target);
1608 }
Mark Mendellc4701932015-04-10 13:18:51 -04001609}
1610
David Brazdil0debae72015-11-12 18:37:00 +00001611static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1612 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1613 // are set only strictly before `branch`. We can't use the eflags on long
1614 // conditions if they are materialized due to the complex branching.
1615 return cond->IsCondition() &&
1616 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001617 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001618}
1619
Mark Mendell152408f2015-12-31 12:28:50 -05001620template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001621void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001622 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001623 LabelType* true_target,
1624 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001625 HInstruction* cond = instruction->InputAt(condition_input_index);
1626
1627 if (true_target == nullptr && false_target == nullptr) {
1628 // Nothing to do. The code always falls through.
1629 return;
1630 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001631 // Constant condition, statically compared against "true" (integer value 1).
1632 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001633 if (true_target != nullptr) {
1634 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001635 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001636 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001637 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001638 if (false_target != nullptr) {
1639 __ jmp(false_target);
1640 }
1641 }
1642 return;
1643 }
1644
1645 // The following code generates these patterns:
1646 // (1) true_target == nullptr && false_target != nullptr
1647 // - opposite condition true => branch to false_target
1648 // (2) true_target != nullptr && false_target == nullptr
1649 // - condition true => branch to true_target
1650 // (3) true_target != nullptr && false_target != nullptr
1651 // - condition true => branch to true_target
1652 // - branch to false_target
1653 if (IsBooleanValueOrMaterializedCondition(cond)) {
1654 if (AreEflagsSetFrom(cond, instruction)) {
1655 if (true_target == nullptr) {
1656 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1657 } else {
1658 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1659 }
1660 } else {
1661 // Materialized condition, compare against 0.
1662 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1663 if (lhs.IsRegister()) {
1664 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1665 } else {
1666 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1667 }
1668 if (true_target == nullptr) {
1669 __ j(kEqual, false_target);
1670 } else {
1671 __ j(kNotEqual, true_target);
1672 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001673 }
1674 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001675 // Condition has not been materialized, use its inputs as the
1676 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001677 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001678
David Brazdil0debae72015-11-12 18:37:00 +00001679 // If this is a long or FP comparison that has been folded into
1680 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 DataType::Type type = condition->InputAt(0)->GetType();
1682 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001683 GenerateCompareTestAndBranch(condition, true_target, false_target);
1684 return;
1685 }
1686
1687 Location lhs = condition->GetLocations()->InAt(0);
1688 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001689 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001690 if (true_target == nullptr) {
1691 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1692 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001693 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001694 }
Dave Allison20dfc792014-06-16 20:44:29 -07001695 }
David Brazdil0debae72015-11-12 18:37:00 +00001696
1697 // If neither branch falls through (case 3), the conditional branch to `true_target`
1698 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1699 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001701 }
1702}
1703
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001704void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001705 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1706 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001707 locations->SetInAt(0, Location::Any());
1708 }
1709}
1710
1711void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001712 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1713 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1714 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1715 nullptr : codegen_->GetLabelOf(true_successor);
1716 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1717 nullptr : codegen_->GetLabelOf(false_successor);
1718 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001719}
1720
1721void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1722 LocationSummary* locations = new (GetGraph()->GetArena())
1723 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001724 InvokeRuntimeCallingConvention calling_convention;
1725 RegisterSet caller_saves = RegisterSet::Empty();
1726 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1727 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001728 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001729 locations->SetInAt(0, Location::Any());
1730 }
1731}
1732
1733void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001734 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001735 GenerateTestAndBranch<Label>(deoptimize,
1736 /* condition_input_index */ 0,
1737 slow_path->GetEntryLabel(),
1738 /* false_target */ nullptr);
1739}
1740
Mingyao Yang063fc772016-08-02 11:02:54 -07001741void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1742 LocationSummary* locations = new (GetGraph()->GetArena())
1743 LocationSummary(flag, LocationSummary::kNoCall);
1744 locations->SetOut(Location::RequiresRegister());
1745}
1746
1747void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1748 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1749 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1750}
1751
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001752static bool SelectCanUseCMOV(HSelect* select) {
1753 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001755 return false;
1756 }
1757
1758 // A FP condition doesn't generate the single CC that we need.
1759 HInstruction* condition = select->GetCondition();
1760 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001762 return false;
1763 }
1764
1765 // We can generate a CMOV for this Select.
1766 return true;
1767}
1768
David Brazdil74eb1b22015-12-14 11:44:01 +00001769void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1770 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001772 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001773 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001774 } else {
1775 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001776 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001777 if (select->InputAt(1)->IsConstant()) {
1778 locations->SetInAt(1, Location::RequiresRegister());
1779 } else {
1780 locations->SetInAt(1, Location::Any());
1781 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001782 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001783 locations->SetInAt(1, Location::Any());
1784 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001785 }
1786 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1787 locations->SetInAt(2, Location::RequiresRegister());
1788 }
1789 locations->SetOut(Location::SameAsFirstInput());
1790}
1791
1792void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1793 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001794 if (SelectCanUseCMOV(select)) {
1795 // If both the condition and the source types are integer, we can generate
1796 // a CMOV to implement Select.
1797 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001798 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001799 DCHECK(locations->InAt(0).Equals(locations->Out()));
1800
1801 HInstruction* select_condition = select->GetCondition();
1802 Condition cond = kNotEqual;
1803
1804 // Figure out how to test the 'condition'.
1805 if (select_condition->IsCondition()) {
1806 HCondition* condition = select_condition->AsCondition();
1807 if (!condition->IsEmittedAtUseSite()) {
1808 // This was a previously materialized condition.
1809 // Can we use the existing condition code?
1810 if (AreEflagsSetFrom(condition, select)) {
1811 // Materialization was the previous instruction. Condition codes are right.
1812 cond = X86_64IntegerCondition(condition->GetCondition());
1813 } else {
1814 // No, we have to recreate the condition code.
1815 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1816 __ testl(cond_reg, cond_reg);
1817 }
1818 } else {
1819 GenerateCompareTest(condition);
1820 cond = X86_64IntegerCondition(condition->GetCondition());
1821 }
1822 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001823 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001824 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1825 __ testl(cond_reg, cond_reg);
1826 }
1827
1828 // If the condition is true, overwrite the output, which already contains false.
1829 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001831 if (value_true_loc.IsRegister()) {
1832 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1833 } else {
1834 __ cmov(cond,
1835 value_false,
1836 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1837 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001838 } else {
1839 NearLabel false_target;
1840 GenerateTestAndBranch<NearLabel>(select,
1841 /* condition_input_index */ 2,
1842 /* true_target */ nullptr,
1843 &false_target);
1844 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1845 __ Bind(&false_target);
1846 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001847}
1848
David Srbecky0cf44932015-12-09 14:09:59 +00001849void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1850 new (GetGraph()->GetArena()) LocationSummary(info);
1851}
1852
David Srbeckyd28f4a02016-03-14 17:14:24 +00001853void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1854 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001855}
1856
1857void CodeGeneratorX86_64::GenerateNop() {
1858 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001859}
1860
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001861void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001862 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001863 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001864 // Handle the long/FP comparisons made in instruction simplification.
1865 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001866 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001867 locations->SetInAt(0, Location::RequiresRegister());
1868 locations->SetInAt(1, Location::Any());
1869 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 case DataType::Type::kFloat32:
1871 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001872 locations->SetInAt(0, Location::RequiresFpuRegister());
1873 locations->SetInAt(1, Location::Any());
1874 break;
1875 default:
1876 locations->SetInAt(0, Location::RequiresRegister());
1877 locations->SetInAt(1, Location::Any());
1878 break;
1879 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001880 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001881 locations->SetOut(Location::RequiresRegister());
1882 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001883}
1884
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001885void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001886 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001887 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001888 }
Mark Mendellc4701932015-04-10 13:18:51 -04001889
1890 LocationSummary* locations = cond->GetLocations();
1891 Location lhs = locations->InAt(0);
1892 Location rhs = locations->InAt(1);
1893 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001894 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001895
1896 switch (cond->InputAt(0)->GetType()) {
1897 default:
1898 // Integer case.
1899
1900 // Clear output register: setcc only sets the low byte.
1901 __ xorl(reg, reg);
1902
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001903 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001904 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001905 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001907 // Clear output register: setcc only sets the low byte.
1908 __ xorl(reg, reg);
1909
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001910 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001911 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001913 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001914 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1915 if (rhs.IsConstant()) {
1916 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1917 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1918 } else if (rhs.IsStackSlot()) {
1919 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1920 } else {
1921 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1922 }
1923 GenerateFPJumps(cond, &true_label, &false_label);
1924 break;
1925 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001926 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001927 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1928 if (rhs.IsConstant()) {
1929 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1930 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1931 } else if (rhs.IsDoubleStackSlot()) {
1932 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1933 } else {
1934 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1935 }
1936 GenerateFPJumps(cond, &true_label, &false_label);
1937 break;
1938 }
1939 }
1940
1941 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001942 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001943
Roland Levillain4fa13f62015-07-06 18:11:54 +01001944 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001945 __ Bind(&false_label);
1946 __ xorl(reg, reg);
1947 __ jmp(&done_label);
1948
Roland Levillain4fa13f62015-07-06 18:11:54 +01001949 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001950 __ Bind(&true_label);
1951 __ movl(reg, Immediate(1));
1952 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001997}
1998
1999void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002001}
2002
Aart Bike9f37602015-10-09 11:15:55 -07002003void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
2031void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002033}
2034
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002035void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002036 LocationSummary* locations =
2037 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002038 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002040 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002043 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kInt32:
2045 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002046 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002047 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2049 break;
2050 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kFloat32:
2052 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002053 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002054 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002055 locations->SetOut(Location::RequiresRegister());
2056 break;
2057 }
2058 default:
2059 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2060 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002061}
2062
2063void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002064 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002065 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002066 Location left = locations->InAt(0);
2067 Location right = locations->InAt(1);
2068
Mark Mendell0c9497d2015-08-21 09:30:05 -04002069 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002071 Condition less_cond = kLess;
2072
Calin Juravleddb7df22014-11-25 20:56:51 +00002073 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002075 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002078 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002079 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002080 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002081 break;
2082 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002084 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002085 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002086 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002087 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002088 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2089 if (right.IsConstant()) {
2090 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2091 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2092 } else if (right.IsStackSlot()) {
2093 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2094 } else {
2095 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2096 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002097 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002098 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002099 break;
2100 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002101 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002102 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2103 if (right.IsConstant()) {
2104 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2105 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2106 } else if (right.IsDoubleStackSlot()) {
2107 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2108 } else {
2109 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2110 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002111 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002112 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002113 break;
2114 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002115 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002116 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002117 }
Aart Bika19616e2016-02-01 18:57:58 -08002118
Calin Juravleddb7df22014-11-25 20:56:51 +00002119 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002120 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002121 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002122
Calin Juravle91debbc2014-11-26 19:01:09 +00002123 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002124 __ movl(out, Immediate(1));
2125 __ jmp(&done);
2126
2127 __ Bind(&less);
2128 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002129
2130 __ Bind(&done);
2131}
2132
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002133void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002134 LocationSummary* locations =
2135 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002136 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002137}
2138
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002139void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002140 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002141}
2142
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002143void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2144 LocationSummary* locations =
2145 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2146 locations->SetOut(Location::ConstantLocation(constant));
2147}
2148
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002149void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002150 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002151}
2152
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002153void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002154 LocationSummary* locations =
2155 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002156 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002157}
2158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002159void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002160 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002161}
2162
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002163void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2164 LocationSummary* locations =
2165 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2166 locations->SetOut(Location::ConstantLocation(constant));
2167}
2168
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002169void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002170 // Will be generated at use site.
2171}
2172
2173void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2174 LocationSummary* locations =
2175 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2176 locations->SetOut(Location::ConstantLocation(constant));
2177}
2178
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002179void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2180 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181 // Will be generated at use site.
2182}
2183
Igor Murashkind01745e2017-04-05 16:40:31 -07002184void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2185 constructor_fence->SetLocations(nullptr);
2186}
2187
2188void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2189 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2190 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2191}
2192
Calin Juravle27df7582015-04-17 19:12:31 +01002193void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2194 memory_barrier->SetLocations(nullptr);
2195}
2196
2197void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002198 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002199}
2200
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002201void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2202 ret->SetLocations(nullptr);
2203}
2204
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002205void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002206 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207}
2208
2209void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002210 LocationSummary* locations =
2211 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002213 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002215 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002216 case DataType::Type::kInt8:
2217 case DataType::Type::kUint16:
2218 case DataType::Type::kInt16:
2219 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002221 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002222 break;
2223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 case DataType::Type::kFloat32:
2225 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002226 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002227 break;
2228
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002229 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002231 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002232}
2233
2234void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2235 if (kIsDebugBuild) {
2236 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002237 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002239 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 case DataType::Type::kInt8:
2241 case DataType::Type::kUint16:
2242 case DataType::Type::kInt16:
2243 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002246 break;
2247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002248 case DataType::Type::kFloat32:
2249 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002250 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002251 XMM0);
2252 break;
2253
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002254 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002255 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002256 }
2257 }
2258 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002259}
2260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002262 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002263 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002265 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 case DataType::Type::kInt8:
2267 case DataType::Type::kUint16:
2268 case DataType::Type::kInt16:
2269 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002271 return Location::RegisterLocation(RAX);
2272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002274 return Location::NoLocation();
2275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kFloat64:
2277 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002278 return Location::FpuRegisterLocation(XMM0);
2279 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002280
2281 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002282}
2283
2284Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2285 return Location::RegisterLocation(kMethodRegisterArgument);
2286}
2287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002289 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002290 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002292 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 case DataType::Type::kInt8:
2294 case DataType::Type::kUint16:
2295 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002296 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002297 uint32_t index = gp_index_++;
2298 stack_index_++;
2299 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002300 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002301 } else {
2302 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2303 }
2304 }
2305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002307 uint32_t index = gp_index_;
2308 stack_index_ += 2;
2309 if (index < calling_convention.GetNumberOfRegisters()) {
2310 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002312 } else {
2313 gp_index_ += 2;
2314 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2315 }
2316 }
2317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002319 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002320 stack_index_++;
2321 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002323 } else {
2324 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2325 }
2326 }
2327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002329 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002330 stack_index_ += 2;
2331 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002332 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002333 } else {
2334 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2335 }
2336 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002339 LOG(FATAL) << "Unexpected parameter type " << type;
2340 break;
2341 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002342 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002343}
2344
Calin Juravle175dc732015-08-25 15:42:32 +01002345void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2346 // The trampoline uses the same calling convention as dex calling conventions,
2347 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2348 // the method_idx.
2349 HandleInvoke(invoke);
2350}
2351
2352void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2353 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2354}
2355
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002356void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002357 // Explicit clinit checks triggered by static invokes must have been pruned by
2358 // art::PrepareForRegisterAllocation.
2359 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002360
Mark Mendellfb8d2792015-03-31 22:16:59 -04002361 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002362 if (intrinsic.TryDispatch(invoke)) {
2363 return;
2364 }
2365
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002366 HandleInvoke(invoke);
2367}
2368
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002369static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2370 if (invoke->GetLocations()->Intrinsified()) {
2371 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2372 intrinsic.Dispatch(invoke);
2373 return true;
2374 }
2375 return false;
2376}
2377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002378void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002379 // Explicit clinit checks triggered by static invokes must have been pruned by
2380 // art::PrepareForRegisterAllocation.
2381 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002382
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002383 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2384 return;
2385 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002386
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002387 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002388 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002389 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002390}
2391
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002392void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002393 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002394 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002395}
2396
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002397void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002398 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002399 if (intrinsic.TryDispatch(invoke)) {
2400 return;
2401 }
2402
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403 HandleInvoke(invoke);
2404}
2405
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002406void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002407 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2408 return;
2409 }
2410
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002411 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002412 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002413}
2414
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002415void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2416 HandleInvoke(invoke);
2417 // Add the hidden argument.
2418 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2419}
2420
2421void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2422 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002423 LocationSummary* locations = invoke->GetLocations();
2424 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2425 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002426 Location receiver = locations->InAt(0);
2427 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2428
Roland Levillain0d5a2812015-11-13 10:07:31 +00002429 // Set the hidden argument. This is safe to do this here, as RAX
2430 // won't be modified thereafter, before the `call` instruction.
2431 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002432 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002433
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434 if (receiver.IsStackSlot()) {
2435 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002436 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002437 __ movl(temp, Address(temp, class_offset));
2438 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002439 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002442 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002443 // Instead of simply (possibly) unpoisoning `temp` here, we should
2444 // emit a read barrier for the previous class reference load.
2445 // However this is not required in practice, as this is an
2446 // intermediate/temporary reference and because the current
2447 // concurrent copying collector keeps the from-space memory
2448 // intact/accessible until the end of the marking phase (the
2449 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002450 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002451 // temp = temp->GetAddressOfIMT()
2452 __ movq(temp,
2453 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2454 // temp = temp->GetImtEntryAt(method_offset);
2455 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002456 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002457 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002458 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002459 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002460 __ call(Address(
2461 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002462
2463 DCHECK(!codegen_->IsLeafMethod());
2464 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2465}
2466
Orion Hodsonac141392017-01-13 11:53:47 +00002467void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2468 HandleInvoke(invoke);
2469}
2470
2471void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2472 codegen_->GenerateInvokePolymorphicCall(invoke);
2473}
2474
Roland Levillain88cb1752014-10-20 16:36:47 +01002475void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2476 LocationSummary* locations =
2477 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2478 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 case DataType::Type::kInt32:
2480 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002481 locations->SetInAt(0, Location::RequiresRegister());
2482 locations->SetOut(Location::SameAsFirstInput());
2483 break;
2484
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485 case DataType::Type::kFloat32:
2486 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002487 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002488 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002489 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002490 break;
2491
2492 default:
2493 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2494 }
2495}
2496
2497void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2498 LocationSummary* locations = neg->GetLocations();
2499 Location out = locations->Out();
2500 Location in = locations->InAt(0);
2501 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002502 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002503 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002504 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002506 break;
2507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002509 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002510 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002512 break;
2513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002514 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002515 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002516 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002517 // Implement float negation with an exclusive or with value
2518 // 0x80000000 (mask for bit 31, representing the sign of a
2519 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002520 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002522 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002523 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002524
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002526 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002527 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002528 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002529 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002530 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002531 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002532 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002533 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002534 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002535
2536 default:
2537 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2538 }
2539}
2540
Roland Levillaindff1f282014-11-05 14:15:05 +00002541void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2542 LocationSummary* locations =
2543 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 DataType::Type result_type = conversion->GetResultType();
2545 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002546 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2547 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002548
Roland Levillaindff1f282014-11-05 14:15:05 +00002549 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002550 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002552 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002554 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2555 locations->SetInAt(0, Location::Any());
2556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002557 break;
2558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002560 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002562 locations->SetInAt(0, Location::Any());
2563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2564 break;
2565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002567 locations->SetInAt(0, Location::RequiresFpuRegister());
2568 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002569 break;
2570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002572 locations->SetInAt(0, Location::RequiresFpuRegister());
2573 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002574 break;
2575
2576 default:
2577 LOG(FATAL) << "Unexpected type conversion from " << input_type
2578 << " to " << result_type;
2579 }
2580 break;
2581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002583 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002584 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002585 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002586 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002587 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kInt16:
2589 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002590 // TODO: We would benefit from a (to-be-implemented)
2591 // Location::RegisterOrStackSlot requirement for this input.
2592 locations->SetInAt(0, Location::RequiresRegister());
2593 locations->SetOut(Location::RequiresRegister());
2594 break;
2595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002597 locations->SetInAt(0, Location::RequiresFpuRegister());
2598 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002599 break;
2600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002602 locations->SetInAt(0, Location::RequiresFpuRegister());
2603 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002604 break;
2605
2606 default:
2607 LOG(FATAL) << "Unexpected type conversion from " << input_type
2608 << " to " << result_type;
2609 }
2610 break;
2611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002613 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002615 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002617 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 case DataType::Type::kInt16:
2619 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002620 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002621 locations->SetOut(Location::RequiresFpuRegister());
2622 break;
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002625 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002626 locations->SetOut(Location::RequiresFpuRegister());
2627 break;
2628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002630 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002631 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002632 break;
2633
2634 default:
2635 LOG(FATAL) << "Unexpected type conversion from " << input_type
2636 << " to " << result_type;
2637 };
2638 break;
2639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002641 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002643 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002644 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002645 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 case DataType::Type::kInt16:
2647 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002648 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002649 locations->SetOut(Location::RequiresFpuRegister());
2650 break;
2651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002653 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002654 locations->SetOut(Location::RequiresFpuRegister());
2655 break;
2656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002658 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002659 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002660 break;
2661
2662 default:
2663 LOG(FATAL) << "Unexpected type conversion from " << input_type
2664 << " to " << result_type;
2665 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 break;
2667
2668 default:
2669 LOG(FATAL) << "Unexpected type conversion from " << input_type
2670 << " to " << result_type;
2671 }
2672}
2673
2674void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2675 LocationSummary* locations = conversion->GetLocations();
2676 Location out = locations->Out();
2677 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002678 DataType::Type result_type = conversion->GetResultType();
2679 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002680 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2681 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002682 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002683 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002684 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002685 case DataType::Type::kInt8:
2686 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kInt16:
2688 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002689 case DataType::Type::kInt64:
2690 if (in.IsRegister()) {
2691 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2692 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2693 __ movzxb(out.AsRegister<CpuRegister>(),
2694 Address(CpuRegister(RSP), in.GetStackIndex()));
2695 } else {
2696 __ movl(out.AsRegister<CpuRegister>(),
2697 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2698 }
2699 break;
2700
2701 default:
2702 LOG(FATAL) << "Unexpected type conversion from " << input_type
2703 << " to " << result_type;
2704 }
2705 break;
2706
2707 case DataType::Type::kInt8:
2708 switch (input_type) {
2709 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002711 case DataType::Type::kInt16:
2712 case DataType::Type::kInt32:
2713 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002714 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002716 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002718 Address(CpuRegister(RSP), in.GetStackIndex()));
2719 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002721 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002722 }
2723 break;
2724
2725 default:
2726 LOG(FATAL) << "Unexpected type conversion from " << input_type
2727 << " to " << result_type;
2728 }
2729 break;
2730
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002731 case DataType::Type::kUint16:
2732 switch (input_type) {
2733 case DataType::Type::kInt8:
2734 case DataType::Type::kInt16:
2735 case DataType::Type::kInt32:
2736 case DataType::Type::kInt64:
2737 if (in.IsRegister()) {
2738 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2739 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2740 __ movzxw(out.AsRegister<CpuRegister>(),
2741 Address(CpuRegister(RSP), in.GetStackIndex()));
2742 } else {
2743 __ movl(out.AsRegister<CpuRegister>(),
2744 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2745 }
2746 break;
2747
2748 default:
2749 LOG(FATAL) << "Unexpected type conversion from " << input_type
2750 << " to " << result_type;
2751 }
2752 break;
2753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002755 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002757 case DataType::Type::kInt32:
2758 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002759 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002761 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002763 Address(CpuRegister(RSP), in.GetStackIndex()));
2764 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002766 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002767 }
2768 break;
2769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774 break;
2775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002777 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002778 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002779 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002780 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002781 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002783 Address(CpuRegister(RSP), in.GetStackIndex()));
2784 } else {
2785 DCHECK(in.IsConstant());
2786 DCHECK(in.GetConstant()->IsLongConstant());
2787 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002788 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002789 }
2790 break;
2791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002793 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2794 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002795 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002796
2797 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002798 // if input >= (float)INT_MAX goto done
2799 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002800 __ j(kAboveEqual, &done);
2801 // if input == NaN goto nan
2802 __ j(kUnordered, &nan);
2803 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002804 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002805 __ jmp(&done);
2806 __ Bind(&nan);
2807 // output = 0
2808 __ xorl(output, output);
2809 __ Bind(&done);
2810 break;
2811 }
2812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002814 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2815 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002816 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002817
2818 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002819 // if input >= (double)INT_MAX goto done
2820 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002821 __ j(kAboveEqual, &done);
2822 // if input == NaN goto nan
2823 __ j(kUnordered, &nan);
2824 // output = double-to-int-truncate(input)
2825 __ cvttsd2si(output, input);
2826 __ jmp(&done);
2827 __ Bind(&nan);
2828 // output = 0
2829 __ xorl(output, output);
2830 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002831 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002832 }
Roland Levillain946e1432014-11-11 17:35:19 +00002833
2834 default:
2835 LOG(FATAL) << "Unexpected type conversion from " << input_type
2836 << " to " << result_type;
2837 }
2838 break;
2839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002841 switch (input_type) {
2842 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002844 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002846 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 case DataType::Type::kInt16:
2848 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002849 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002851 break;
2852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002854 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2855 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002856 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002857
Mark Mendell92e83bf2015-05-07 11:25:03 -04002858 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002859 // if input >= (float)LONG_MAX goto done
2860 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002861 __ j(kAboveEqual, &done);
2862 // if input == NaN goto nan
2863 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002864 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002865 __ cvttss2si(output, input, true);
2866 __ jmp(&done);
2867 __ Bind(&nan);
2868 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002869 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002870 __ Bind(&done);
2871 break;
2872 }
2873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002875 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2876 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002877 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002878
Mark Mendell92e83bf2015-05-07 11:25:03 -04002879 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002880 // if input >= (double)LONG_MAX goto done
2881 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002882 __ j(kAboveEqual, &done);
2883 // if input == NaN goto nan
2884 __ j(kUnordered, &nan);
2885 // output = double-to-long-truncate(input)
2886 __ cvttsd2si(output, input, true);
2887 __ jmp(&done);
2888 __ Bind(&nan);
2889 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002890 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002891 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002892 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002893 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002894
2895 default:
2896 LOG(FATAL) << "Unexpected type conversion from " << input_type
2897 << " to " << result_type;
2898 }
2899 break;
2900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002902 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002904 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002906 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 case DataType::Type::kInt16:
2908 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002909 if (in.IsRegister()) {
2910 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2911 } else if (in.IsConstant()) {
2912 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2913 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002914 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002915 } else {
2916 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2917 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2918 }
Roland Levillaincff13742014-11-17 14:32:17 +00002919 break;
2920
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002921 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002922 if (in.IsRegister()) {
2923 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2924 } else if (in.IsConstant()) {
2925 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2926 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002927 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002928 } else {
2929 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2930 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2931 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002932 break;
2933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002935 if (in.IsFpuRegister()) {
2936 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2937 } else if (in.IsConstant()) {
2938 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2939 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002940 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002941 } else {
2942 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2943 Address(CpuRegister(RSP), in.GetStackIndex()));
2944 }
Roland Levillaincff13742014-11-17 14:32:17 +00002945 break;
2946
2947 default:
2948 LOG(FATAL) << "Unexpected type conversion from " << input_type
2949 << " to " << result_type;
2950 };
2951 break;
2952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002954 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002956 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002958 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 case DataType::Type::kInt16:
2960 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002961 if (in.IsRegister()) {
2962 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2963 } else if (in.IsConstant()) {
2964 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2965 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002966 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002967 } else {
2968 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2969 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2970 }
Roland Levillaincff13742014-11-17 14:32:17 +00002971 break;
2972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002974 if (in.IsRegister()) {
2975 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2976 } else if (in.IsConstant()) {
2977 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2978 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002979 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002980 } else {
2981 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2982 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2983 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002984 break;
2985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002987 if (in.IsFpuRegister()) {
2988 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2989 } else if (in.IsConstant()) {
2990 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2991 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002992 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002993 } else {
2994 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2995 Address(CpuRegister(RSP), in.GetStackIndex()));
2996 }
Roland Levillaincff13742014-11-17 14:32:17 +00002997 break;
2998
2999 default:
3000 LOG(FATAL) << "Unexpected type conversion from " << input_type
3001 << " to " << result_type;
3002 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003003 break;
3004
3005 default:
3006 LOG(FATAL) << "Unexpected type conversion from " << input_type
3007 << " to " << result_type;
3008 }
3009}
3010
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003012 LocationSummary* locations =
3013 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003014 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003016 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003017 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003019 break;
3020 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003023 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003024 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003025 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003027 break;
3028 }
3029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003030 case DataType::Type::kFloat64:
3031 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003032 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003033 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003034 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003036 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037
3038 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003039 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003040 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003041}
3042
3043void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3044 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 Location first = locations->InAt(0);
3046 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003047 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003048
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003050 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003052 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3053 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003054 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3055 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003056 } else {
3057 __ leal(out.AsRegister<CpuRegister>(), Address(
3058 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3059 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003060 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003061 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3062 __ addl(out.AsRegister<CpuRegister>(),
3063 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3064 } else {
3065 __ leal(out.AsRegister<CpuRegister>(), Address(
3066 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3067 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003068 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003069 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003070 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003071 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003072 break;
3073 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003076 if (second.IsRegister()) {
3077 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3078 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003079 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3080 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003081 } else {
3082 __ leaq(out.AsRegister<CpuRegister>(), Address(
3083 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3084 }
3085 } else {
3086 DCHECK(second.IsConstant());
3087 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3088 int32_t int32_value = Low32Bits(value);
3089 DCHECK_EQ(int32_value, value);
3090 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3091 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3092 } else {
3093 __ leaq(out.AsRegister<CpuRegister>(), Address(
3094 first.AsRegister<CpuRegister>(), int32_value));
3095 }
3096 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003097 break;
3098 }
3099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003101 if (second.IsFpuRegister()) {
3102 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3103 } else if (second.IsConstant()) {
3104 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003105 codegen_->LiteralFloatAddress(
3106 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003107 } else {
3108 DCHECK(second.IsStackSlot());
3109 __ addss(first.AsFpuRegister<XmmRegister>(),
3110 Address(CpuRegister(RSP), second.GetStackIndex()));
3111 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003112 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003113 }
3114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003116 if (second.IsFpuRegister()) {
3117 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3118 } else if (second.IsConstant()) {
3119 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003120 codegen_->LiteralDoubleAddress(
3121 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003122 } else {
3123 DCHECK(second.IsDoubleStackSlot());
3124 __ addsd(first.AsFpuRegister<XmmRegister>(),
3125 Address(CpuRegister(RSP), second.GetStackIndex()));
3126 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003127 break;
3128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129
3130 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003131 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003132 }
3133}
3134
3135void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003136 LocationSummary* locations =
3137 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003138 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003140 locations->SetInAt(0, Location::RequiresRegister());
3141 locations->SetInAt(1, Location::Any());
3142 locations->SetOut(Location::SameAsFirstInput());
3143 break;
3144 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003146 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003147 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003148 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003149 break;
3150 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003151 case DataType::Type::kFloat32:
3152 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003153 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003154 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003155 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003156 break;
Calin Juravle11351682014-10-23 15:38:15 +01003157 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003158 default:
Calin Juravle11351682014-10-23 15:38:15 +01003159 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003160 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003161}
3162
3163void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3164 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003165 Location first = locations->InAt(0);
3166 Location second = locations->InAt(1);
3167 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003168 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003169 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003170 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003171 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003172 } else if (second.IsConstant()) {
3173 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003174 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003175 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003176 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003177 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003178 break;
3179 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003181 if (second.IsConstant()) {
3182 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3183 DCHECK(IsInt<32>(value));
3184 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3185 } else {
3186 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3187 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003188 break;
3189 }
3190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003192 if (second.IsFpuRegister()) {
3193 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3194 } else if (second.IsConstant()) {
3195 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003196 codegen_->LiteralFloatAddress(
3197 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003198 } else {
3199 DCHECK(second.IsStackSlot());
3200 __ subss(first.AsFpuRegister<XmmRegister>(),
3201 Address(CpuRegister(RSP), second.GetStackIndex()));
3202 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003203 break;
Calin Juravle11351682014-10-23 15:38:15 +01003204 }
3205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003206 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003207 if (second.IsFpuRegister()) {
3208 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3209 } else if (second.IsConstant()) {
3210 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003211 codegen_->LiteralDoubleAddress(
3212 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003213 } else {
3214 DCHECK(second.IsDoubleStackSlot());
3215 __ subsd(first.AsFpuRegister<XmmRegister>(),
3216 Address(CpuRegister(RSP), second.GetStackIndex()));
3217 }
Calin Juravle11351682014-10-23 15:38:15 +01003218 break;
3219 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003220
3221 default:
Calin Juravle11351682014-10-23 15:38:15 +01003222 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003223 }
3224}
3225
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3227 LocationSummary* locations =
3228 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3229 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003231 locations->SetInAt(0, Location::RequiresRegister());
3232 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003233 if (mul->InputAt(1)->IsIntConstant()) {
3234 // Can use 3 operand multiply.
3235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3236 } else {
3237 locations->SetOut(Location::SameAsFirstInput());
3238 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003239 break;
3240 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003242 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003243 locations->SetInAt(1, Location::Any());
3244 if (mul->InputAt(1)->IsLongConstant() &&
3245 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003246 // Can use 3 operand multiply.
3247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3248 } else {
3249 locations->SetOut(Location::SameAsFirstInput());
3250 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251 break;
3252 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kFloat32:
3254 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003255 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003256 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003257 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003258 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003259 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003260
3261 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003262 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003263 }
3264}
3265
3266void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3267 LocationSummary* locations = mul->GetLocations();
3268 Location first = locations->InAt(0);
3269 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003270 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003272 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003273 // The constant may have ended up in a register, so test explicitly to avoid
3274 // problems where the output may not be the same as the first operand.
3275 if (mul->InputAt(1)->IsIntConstant()) {
3276 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3277 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3278 } else if (second.IsRegister()) {
3279 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003280 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003281 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003282 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003283 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003284 __ imull(first.AsRegister<CpuRegister>(),
3285 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003286 }
3287 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003288 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003289 // The constant may have ended up in a register, so test explicitly to avoid
3290 // problems where the output may not be the same as the first operand.
3291 if (mul->InputAt(1)->IsLongConstant()) {
3292 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3293 if (IsInt<32>(value)) {
3294 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3295 Immediate(static_cast<int32_t>(value)));
3296 } else {
3297 // Have to use the constant area.
3298 DCHECK(first.Equals(out));
3299 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3300 }
3301 } else if (second.IsRegister()) {
3302 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003303 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003304 } else {
3305 DCHECK(second.IsDoubleStackSlot());
3306 DCHECK(first.Equals(out));
3307 __ imulq(first.AsRegister<CpuRegister>(),
3308 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003309 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 break;
3311 }
3312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003314 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003315 if (second.IsFpuRegister()) {
3316 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3317 } else if (second.IsConstant()) {
3318 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003319 codegen_->LiteralFloatAddress(
3320 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003321 } else {
3322 DCHECK(second.IsStackSlot());
3323 __ mulss(first.AsFpuRegister<XmmRegister>(),
3324 Address(CpuRegister(RSP), second.GetStackIndex()));
3325 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003326 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003327 }
3328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003330 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003331 if (second.IsFpuRegister()) {
3332 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3333 } else if (second.IsConstant()) {
3334 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003335 codegen_->LiteralDoubleAddress(
3336 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003337 } else {
3338 DCHECK(second.IsDoubleStackSlot());
3339 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3340 Address(CpuRegister(RSP), second.GetStackIndex()));
3341 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003342 break;
3343 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003344
3345 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003346 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003347 }
3348}
3349
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003350void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3351 uint32_t stack_adjustment, bool is_float) {
3352 if (source.IsStackSlot()) {
3353 DCHECK(is_float);
3354 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3355 } else if (source.IsDoubleStackSlot()) {
3356 DCHECK(!is_float);
3357 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3358 } else {
3359 // Write the value to the temporary location on the stack and load to FP stack.
3360 if (is_float) {
3361 Location stack_temp = Location::StackSlot(temp_offset);
3362 codegen_->Move(stack_temp, source);
3363 __ flds(Address(CpuRegister(RSP), temp_offset));
3364 } else {
3365 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3366 codegen_->Move(stack_temp, source);
3367 __ fldl(Address(CpuRegister(RSP), temp_offset));
3368 }
3369 }
3370}
3371
3372void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003373 DataType::Type type = rem->GetResultType();
3374 bool is_float = type == DataType::Type::kFloat32;
3375 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003376 LocationSummary* locations = rem->GetLocations();
3377 Location first = locations->InAt(0);
3378 Location second = locations->InAt(1);
3379 Location out = locations->Out();
3380
3381 // Create stack space for 2 elements.
3382 // TODO: enhance register allocator to ask for stack temporaries.
3383 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3384
3385 // Load the values to the FP stack in reverse order, using temporaries if needed.
3386 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3387 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3388
3389 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003390 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 __ Bind(&retry);
3392 __ fprem();
3393
3394 // Move FP status to AX.
3395 __ fstsw();
3396
3397 // And see if the argument reduction is complete. This is signaled by the
3398 // C2 FPU flag bit set to 0.
3399 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3400 __ j(kNotEqual, &retry);
3401
3402 // We have settled on the final value. Retrieve it into an XMM register.
3403 // Store FP top of stack to real stack.
3404 if (is_float) {
3405 __ fsts(Address(CpuRegister(RSP), 0));
3406 } else {
3407 __ fstl(Address(CpuRegister(RSP), 0));
3408 }
3409
3410 // Pop the 2 items from the FP stack.
3411 __ fucompp();
3412
3413 // Load the value from the stack into an XMM register.
3414 DCHECK(out.IsFpuRegister()) << out;
3415 if (is_float) {
3416 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3417 } else {
3418 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3419 }
3420
3421 // And remove the temporary stack space we allocated.
3422 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3423}
3424
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3426 DCHECK(instruction->IsDiv() || instruction->IsRem());
3427
3428 LocationSummary* locations = instruction->GetLocations();
3429 Location second = locations->InAt(1);
3430 DCHECK(second.IsConstant());
3431
3432 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3433 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003434 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435
3436 DCHECK(imm == 1 || imm == -1);
3437
3438 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 if (instruction->IsRem()) {
3441 __ xorl(output_register, output_register);
3442 } else {
3443 __ movl(output_register, input_register);
3444 if (imm == -1) {
3445 __ negl(output_register);
3446 }
3447 }
3448 break;
3449 }
3450
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003451 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003452 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003453 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003454 } else {
3455 __ movq(output_register, input_register);
3456 if (imm == -1) {
3457 __ negq(output_register);
3458 }
3459 }
3460 break;
3461 }
3462
3463 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003464 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003465 }
3466}
3467
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003468void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 LocationSummary* locations = instruction->GetLocations();
3470 Location second = locations->InAt(1);
3471
3472 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3473 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3474
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003475 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003476 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3477 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003478
3479 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003481 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003482 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483 __ testl(numerator, numerator);
3484 __ cmov(kGreaterEqual, tmp, numerator);
3485 int shift = CTZ(imm);
3486 __ sarl(tmp, Immediate(shift));
3487
3488 if (imm < 0) {
3489 __ negl(tmp);
3490 }
3491
3492 __ movl(output_register, tmp);
3493 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003494 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3496
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003497 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 __ addq(rdx, numerator);
3499 __ testq(numerator, numerator);
3500 __ cmov(kGreaterEqual, rdx, numerator);
3501 int shift = CTZ(imm);
3502 __ sarq(rdx, Immediate(shift));
3503
3504 if (imm < 0) {
3505 __ negq(rdx);
3506 }
3507
3508 __ movq(output_register, rdx);
3509 }
3510}
3511
3512void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3513 DCHECK(instruction->IsDiv() || instruction->IsRem());
3514
3515 LocationSummary* locations = instruction->GetLocations();
3516 Location second = locations->InAt(1);
3517
3518 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3519 : locations->GetTemp(0).AsRegister<CpuRegister>();
3520 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3521 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3522 : locations->Out().AsRegister<CpuRegister>();
3523 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3524
3525 DCHECK_EQ(RAX, eax.AsRegister());
3526 DCHECK_EQ(RDX, edx.AsRegister());
3527 if (instruction->IsDiv()) {
3528 DCHECK_EQ(RAX, out.AsRegister());
3529 } else {
3530 DCHECK_EQ(RDX, out.AsRegister());
3531 }
3532
3533 int64_t magic;
3534 int shift;
3535
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003536 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3539
3540 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3541
3542 __ movl(numerator, eax);
3543
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 __ movl(eax, Immediate(magic));
3545 __ imull(numerator);
3546
3547 if (imm > 0 && magic < 0) {
3548 __ addl(edx, numerator);
3549 } else if (imm < 0 && magic > 0) {
3550 __ subl(edx, numerator);
3551 }
3552
3553 if (shift != 0) {
3554 __ sarl(edx, Immediate(shift));
3555 }
3556
3557 __ movl(eax, edx);
3558 __ shrl(edx, Immediate(31));
3559 __ addl(edx, eax);
3560
3561 if (instruction->IsRem()) {
3562 __ movl(eax, numerator);
3563 __ imull(edx, Immediate(imm));
3564 __ subl(eax, edx);
3565 __ movl(edx, eax);
3566 } else {
3567 __ movl(eax, edx);
3568 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003569 } else {
3570 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573
3574 CpuRegister rax = eax;
3575 CpuRegister rdx = edx;
3576
3577 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3578
3579 // Save the numerator.
3580 __ movq(numerator, rax);
3581
3582 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003583 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584
3585 // RDX:RAX = magic * numerator
3586 __ imulq(numerator);
3587
3588 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003589 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 __ addq(rdx, numerator);
3591 } else if (imm < 0 && magic > 0) {
3592 // RDX -= numerator
3593 __ subq(rdx, numerator);
3594 }
3595
3596 // Shift if needed.
3597 if (shift != 0) {
3598 __ sarq(rdx, Immediate(shift));
3599 }
3600
3601 // RDX += 1 if RDX < 0
3602 __ movq(rax, rdx);
3603 __ shrq(rdx, Immediate(63));
3604 __ addq(rdx, rax);
3605
3606 if (instruction->IsRem()) {
3607 __ movq(rax, numerator);
3608
3609 if (IsInt<32>(imm)) {
3610 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3611 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003612 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 }
3614
3615 __ subq(rax, rdx);
3616 __ movq(rdx, rax);
3617 } else {
3618 __ movq(rax, rdx);
3619 }
3620 }
3621}
3622
Calin Juravlebacfec32014-11-14 15:54:36 +00003623void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3624 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 DataType::Type type = instruction->GetResultType();
3626 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003627
3628 bool is_div = instruction->IsDiv();
3629 LocationSummary* locations = instruction->GetLocations();
3630
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003631 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3632 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003633
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003635 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003636
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003637 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003638 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003639
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003640 if (imm == 0) {
3641 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3642 } else if (imm == 1 || imm == -1) {
3643 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003644 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003645 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003646 } else {
3647 DCHECK(imm <= -2 || imm >= 2);
3648 GenerateDivRemWithAnyConstant(instruction);
3649 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003651 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003652 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003653 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003654 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003655
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003656 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3657 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3658 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3659 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003661 __ cmpl(second_reg, Immediate(-1));
3662 __ j(kEqual, slow_path->GetEntryLabel());
3663 // edx:eax <- sign-extended of eax
3664 __ cdq();
3665 // eax = quotient, edx = remainder
3666 __ idivl(second_reg);
3667 } else {
3668 __ cmpq(second_reg, Immediate(-1));
3669 __ j(kEqual, slow_path->GetEntryLabel());
3670 // rdx:rax <- sign-extended of rax
3671 __ cqo();
3672 // rax = quotient, rdx = remainder
3673 __ idivq(second_reg);
3674 }
3675 __ Bind(slow_path->GetExitLabel());
3676 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003677}
3678
Calin Juravle7c4954d2014-10-28 16:57:40 +00003679void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3680 LocationSummary* locations =
3681 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3682 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003683 case DataType::Type::kInt32:
3684 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003685 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003686 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003687 locations->SetOut(Location::SameAsFirstInput());
3688 // Intel uses edx:eax as the dividend.
3689 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003690 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3691 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3692 // output and request another temp.
3693 if (div->InputAt(1)->IsConstant()) {
3694 locations->AddTemp(Location::RequiresRegister());
3695 }
Calin Juravled0d48522014-11-04 16:40:20 +00003696 break;
3697 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003699 case DataType::Type::kFloat32:
3700 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003701 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003702 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003703 locations->SetOut(Location::SameAsFirstInput());
3704 break;
3705 }
3706
3707 default:
3708 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3709 }
3710}
3711
3712void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3713 LocationSummary* locations = div->GetLocations();
3714 Location first = locations->InAt(0);
3715 Location second = locations->InAt(1);
3716 DCHECK(first.Equals(locations->Out()));
3717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003719 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 case DataType::Type::kInt32:
3721 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003722 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003723 break;
3724 }
3725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003727 if (second.IsFpuRegister()) {
3728 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3729 } else if (second.IsConstant()) {
3730 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003731 codegen_->LiteralFloatAddress(
3732 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003733 } else {
3734 DCHECK(second.IsStackSlot());
3735 __ divss(first.AsFpuRegister<XmmRegister>(),
3736 Address(CpuRegister(RSP), second.GetStackIndex()));
3737 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003738 break;
3739 }
3740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003741 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003742 if (second.IsFpuRegister()) {
3743 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3744 } else if (second.IsConstant()) {
3745 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003746 codegen_->LiteralDoubleAddress(
3747 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003748 } else {
3749 DCHECK(second.IsDoubleStackSlot());
3750 __ divsd(first.AsFpuRegister<XmmRegister>(),
3751 Address(CpuRegister(RSP), second.GetStackIndex()));
3752 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003753 break;
3754 }
3755
3756 default:
3757 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3758 }
3759}
3760
Calin Juravlebacfec32014-11-14 15:54:36 +00003761void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003762 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003763 LocationSummary* locations =
3764 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003765
3766 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kInt32:
3768 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003769 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003771 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3772 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003773 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3774 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3775 // output and request another temp.
3776 if (rem->InputAt(1)->IsConstant()) {
3777 locations->AddTemp(Location::RequiresRegister());
3778 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 break;
3780 }
3781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 case DataType::Type::kFloat32:
3783 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003784 locations->SetInAt(0, Location::Any());
3785 locations->SetInAt(1, Location::Any());
3786 locations->SetOut(Location::RequiresFpuRegister());
3787 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003788 break;
3789 }
3790
3791 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003792 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003793 }
3794}
3795
3796void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003797 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003799 case DataType::Type::kInt32:
3800 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 GenerateDivRemIntegral(rem);
3802 break;
3803 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kFloat32:
3805 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003806 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003807 break;
3808 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 default:
3810 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3811 }
3812}
3813
Calin Juravled0d48522014-11-04 16:40:20 +00003814void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003815 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003816 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003817}
3818
3819void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003820 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003821 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3822 codegen_->AddSlowPath(slow_path);
3823
3824 LocationSummary* locations = instruction->GetLocations();
3825 Location value = locations->InAt(0);
3826
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003827 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003828 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003829 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kInt8:
3831 case DataType::Type::kUint16:
3832 case DataType::Type::kInt16:
3833 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003834 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 __ j(kEqual, slow_path->GetEntryLabel());
3837 } else if (value.IsStackSlot()) {
3838 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3839 __ j(kEqual, slow_path->GetEntryLabel());
3840 } else {
3841 DCHECK(value.IsConstant()) << value;
3842 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003843 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003844 }
3845 }
3846 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003847 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003849 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003851 __ j(kEqual, slow_path->GetEntryLabel());
3852 } else if (value.IsDoubleStackSlot()) {
3853 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3854 __ j(kEqual, slow_path->GetEntryLabel());
3855 } else {
3856 DCHECK(value.IsConstant()) << value;
3857 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003858 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003859 }
3860 }
3861 break;
3862 }
3863 default:
3864 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003865 }
Calin Juravled0d48522014-11-04 16:40:20 +00003866}
3867
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3869 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3870
3871 LocationSummary* locations =
3872 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3873
3874 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003875 case DataType::Type::kInt32:
3876 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 locations->SetInAt(0, Location::RequiresRegister());
3878 // The shift count needs to be in CL.
3879 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3880 locations->SetOut(Location::SameAsFirstInput());
3881 break;
3882 }
3883 default:
3884 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3885 }
3886}
3887
3888void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3889 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3890
3891 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003893 Location second = locations->InAt(1);
3894
3895 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003897 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003899 if (op->IsShl()) {
3900 __ shll(first_reg, second_reg);
3901 } else if (op->IsShr()) {
3902 __ sarl(first_reg, second_reg);
3903 } else {
3904 __ shrl(first_reg, second_reg);
3905 }
3906 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003907 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 if (op->IsShl()) {
3909 __ shll(first_reg, imm);
3910 } else if (op->IsShr()) {
3911 __ sarl(first_reg, imm);
3912 } else {
3913 __ shrl(first_reg, imm);
3914 }
3915 }
3916 break;
3917 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003918 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003919 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003921 if (op->IsShl()) {
3922 __ shlq(first_reg, second_reg);
3923 } else if (op->IsShr()) {
3924 __ sarq(first_reg, second_reg);
3925 } else {
3926 __ shrq(first_reg, second_reg);
3927 }
3928 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003929 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003930 if (op->IsShl()) {
3931 __ shlq(first_reg, imm);
3932 } else if (op->IsShr()) {
3933 __ sarq(first_reg, imm);
3934 } else {
3935 __ shrq(first_reg, imm);
3936 }
3937 }
3938 break;
3939 }
3940 default:
3941 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003942 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003943 }
3944}
3945
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003946void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3947 LocationSummary* locations =
3948 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3949
3950 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003951 case DataType::Type::kInt32:
3952 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003953 locations->SetInAt(0, Location::RequiresRegister());
3954 // The shift count needs to be in CL (unless it is a constant).
3955 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3956 locations->SetOut(Location::SameAsFirstInput());
3957 break;
3958 }
3959 default:
3960 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3961 UNREACHABLE();
3962 }
3963}
3964
3965void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3966 LocationSummary* locations = ror->GetLocations();
3967 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3968 Location second = locations->InAt(1);
3969
3970 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003971 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003972 if (second.IsRegister()) {
3973 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3974 __ rorl(first_reg, second_reg);
3975 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003976 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003977 __ rorl(first_reg, imm);
3978 }
3979 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003980 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003981 if (second.IsRegister()) {
3982 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3983 __ rorq(first_reg, second_reg);
3984 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003985 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003986 __ rorq(first_reg, imm);
3987 }
3988 break;
3989 default:
3990 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3991 UNREACHABLE();
3992 }
3993}
3994
Calin Juravle9aec02f2014-11-18 23:06:35 +00003995void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3996 HandleShift(shl);
3997}
3998
3999void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4000 HandleShift(shl);
4001}
4002
4003void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4004 HandleShift(shr);
4005}
4006
4007void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4008 HandleShift(shr);
4009}
4010
4011void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4012 HandleShift(ushr);
4013}
4014
4015void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4016 HandleShift(ushr);
4017}
4018
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004019void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004020 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004022 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004023 if (instruction->IsStringAlloc()) {
4024 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4025 } else {
4026 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004027 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004028 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004029}
4030
4031void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004032 // Note: if heap poisoning is enabled, the entry point takes cares
4033 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004034 if (instruction->IsStringAlloc()) {
4035 // String is allocated through StringFactory. Call NewEmptyString entry point.
4036 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004037 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004038 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4039 __ call(Address(temp, code_offset.SizeValue()));
4040 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4041 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004042 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004043 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004044 DCHECK(!codegen_->IsLeafMethod());
4045 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004046}
4047
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004048void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4049 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004051 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004052 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004053 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4054 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004055}
4056
4057void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004058 // Note: if heap poisoning is enabled, the entry point takes cares
4059 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004060 QuickEntrypointEnum entrypoint =
4061 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4062 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004063 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004064 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004065}
4066
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004067void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004070 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4071 if (location.IsStackSlot()) {
4072 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4073 } else if (location.IsDoubleStackSlot()) {
4074 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4075 }
4076 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004077}
4078
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004079void InstructionCodeGeneratorX86_64::VisitParameterValue(
4080 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004081 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004082}
4083
4084void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4085 LocationSummary* locations =
4086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4087 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4088}
4089
4090void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4091 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4092 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004093}
4094
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004095void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4096 LocationSummary* locations =
4097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4098 locations->SetInAt(0, Location::RequiresRegister());
4099 locations->SetOut(Location::RequiresRegister());
4100}
4101
4102void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4103 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004104 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004105 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004106 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004107 __ movq(locations->Out().AsRegister<CpuRegister>(),
4108 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004109 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004110 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004111 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004112 __ movq(locations->Out().AsRegister<CpuRegister>(),
4113 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4114 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004115 __ movq(locations->Out().AsRegister<CpuRegister>(),
4116 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004117 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004118}
4119
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004120void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004121 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004122 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004123 locations->SetInAt(0, Location::RequiresRegister());
4124 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004125}
4126
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004127void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4128 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004129 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4130 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004131 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004132 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004133 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004134 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004135 break;
4136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004137 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004139 break;
4140
4141 default:
4142 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4143 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004144}
4145
David Brazdil66d126e2015-04-03 16:02:44 +01004146void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4147 LocationSummary* locations =
4148 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4149 locations->SetInAt(0, Location::RequiresRegister());
4150 locations->SetOut(Location::SameAsFirstInput());
4151}
4152
4153void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004154 LocationSummary* locations = bool_not->GetLocations();
4155 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4156 locations->Out().AsRegister<CpuRegister>().AsRegister());
4157 Location out = locations->Out();
4158 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4159}
4160
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004161void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004162 LocationSummary* locations =
4163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004164 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004165 locations->SetInAt(i, Location::Any());
4166 }
4167 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004168}
4169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004170void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004171 LOG(FATAL) << "Unimplemented";
4172}
4173
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004174void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004175 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004176 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004177 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004178 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4179 */
4180 switch (kind) {
4181 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004182 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004183 break;
4184 }
4185 case MemBarrierKind::kAnyStore:
4186 case MemBarrierKind::kLoadAny:
4187 case MemBarrierKind::kStoreStore: {
4188 // nop
4189 break;
4190 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004191 case MemBarrierKind::kNTStoreStore:
4192 // Non-Temporal Store/Store needs an explicit fence.
4193 MemoryFence(/* non-temporal */ true);
4194 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004195 }
4196}
4197
4198void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4199 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4200
Roland Levillain0d5a2812015-11-13 10:07:31 +00004201 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004203 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004204 new (GetGraph()->GetArena()) LocationSummary(instruction,
4205 object_field_get_with_read_barrier ?
4206 LocationSummary::kCallOnSlowPath :
4207 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004208 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004209 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004210 }
Calin Juravle52c48962014-12-16 17:02:57 +00004211 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004213 locations->SetOut(Location::RequiresFpuRegister());
4214 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004215 // The output overlaps for an object field get when read barriers
4216 // are enabled: we do not want the move to overwrite the object's
4217 // location, as we need it to emit the read barrier.
4218 locations->SetOut(
4219 Location::RequiresRegister(),
4220 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004221 }
Calin Juravle52c48962014-12-16 17:02:57 +00004222}
4223
4224void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4225 const FieldInfo& field_info) {
4226 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4227
4228 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004229 Location base_loc = locations->InAt(0);
4230 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004231 Location out = locations->Out();
4232 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004233 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004234 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4235
4236 switch (field_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004237 case DataType::Type::kBool:
4238 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004239 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4240 break;
4241 }
4242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004243 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004244 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4245 break;
4246 }
4247
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004248 case DataType::Type::kUint16: {
4249 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004250 break;
4251 }
4252
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004253 case DataType::Type::kInt16: {
4254 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004255 break;
4256 }
4257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004258 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004259 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4260 break;
4261 }
4262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004263 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004264 // /* HeapReference<Object> */ out = *(base + offset)
4265 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004266 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004267 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004268 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004269 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004270 if (is_volatile) {
4271 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4272 }
4273 } else {
4274 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4275 codegen_->MaybeRecordImplicitNullCheck(instruction);
4276 if (is_volatile) {
4277 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4278 }
4279 // If read barriers are enabled, emit read barriers other than
4280 // Baker's using a slow path (and also unpoison the loaded
4281 // reference, if heap poisoning is enabled).
4282 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4283 }
4284 break;
4285 }
4286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004287 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004288 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4289 break;
4290 }
4291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004292 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004293 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4294 break;
4295 }
4296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004297 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004298 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4299 break;
4300 }
4301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004302 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004303 LOG(FATAL) << "Unreachable type " << field_type;
4304 UNREACHABLE();
4305 }
4306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004307 if (field_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004308 // Potential implicit null checks, in the case of reference
4309 // fields, are handled in the previous switch statement.
4310 } else {
4311 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004312 }
Roland Levillain4d027112015-07-01 15:41:14 +01004313
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004314 if (is_volatile) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004315 if (field_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004316 // Memory barriers, in the case of references, are also handled
4317 // in the previous switch statement.
4318 } else {
4319 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4320 }
Roland Levillain4d027112015-07-01 15:41:14 +01004321 }
Calin Juravle52c48962014-12-16 17:02:57 +00004322}
4323
4324void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4325 const FieldInfo& field_info) {
4326 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4327
4328 LocationSummary* locations =
4329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004331 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004332 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004333 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004334
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004335 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004336 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004337 if (is_volatile) {
4338 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4339 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4340 } else {
4341 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4342 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004343 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004344 if (is_volatile) {
4345 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4346 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4347 } else {
4348 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4349 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004350 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004351 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004352 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004353 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004354 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004356 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004357 locations->AddTemp(Location::RequiresRegister());
4358 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359}
4360
Calin Juravle52c48962014-12-16 17:02:57 +00004361void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004362 const FieldInfo& field_info,
4363 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004364 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4365
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004366 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004367 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4368 Location value = locations->InAt(1);
4369 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004370 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004371 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4372
4373 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004374 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004375 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
Mark Mendellea5af682015-10-22 17:35:49 -04004377 bool maybe_record_implicit_null_check_done = false;
4378
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004379 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004380 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004381 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004382 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004383 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004384 __ movb(Address(base, offset),
4385 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004386 } else {
4387 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4388 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389 break;
4390 }
4391
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004392 case DataType::Type::kUint16:
4393 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004394 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004395 __ movw(Address(base, offset),
4396 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004397 } else {
4398 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4399 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 break;
4401 }
4402
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004403 case DataType::Type::kInt32:
4404 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004405 if (value.IsConstant()) {
4406 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004407 // `field_type == DataType::Type::kReference` implies `v == 0`.
4408 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004409 // Note: if heap poisoning is enabled, no need to poison
4410 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004411 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004412 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004413 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004414 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4415 __ movl(temp, value.AsRegister<CpuRegister>());
4416 __ PoisonHeapReference(temp);
4417 __ movl(Address(base, offset), temp);
4418 } else {
4419 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4420 }
Mark Mendell40741f32015-04-20 22:10:34 -04004421 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004422 break;
4423 }
4424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004425 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004426 if (value.IsConstant()) {
4427 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004428 codegen_->MoveInt64ToAddress(Address(base, offset),
4429 Address(base, offset + sizeof(int32_t)),
4430 v,
4431 instruction);
4432 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004433 } else {
4434 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4435 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436 break;
4437 }
4438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004439 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004440 if (value.IsConstant()) {
4441 int32_t v =
4442 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4443 __ movl(Address(base, offset), Immediate(v));
4444 } else {
4445 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4446 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004447 break;
4448 }
4449
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004450 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004451 if (value.IsConstant()) {
4452 int64_t v =
4453 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4454 codegen_->MoveInt64ToAddress(Address(base, offset),
4455 Address(base, offset + sizeof(int32_t)),
4456 v,
4457 instruction);
4458 maybe_record_implicit_null_check_done = true;
4459 } else {
4460 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4461 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004462 break;
4463 }
4464
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004465 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004467 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468 }
Calin Juravle52c48962014-12-16 17:02:57 +00004469
Mark Mendellea5af682015-10-22 17:35:49 -04004470 if (!maybe_record_implicit_null_check_done) {
4471 codegen_->MaybeRecordImplicitNullCheck(instruction);
4472 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004473
4474 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4475 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4476 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004477 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004478 }
4479
Calin Juravle52c48962014-12-16 17:02:57 +00004480 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004481 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004482 }
4483}
4484
4485void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4486 HandleFieldSet(instruction, instruction->GetFieldInfo());
4487}
4488
4489void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004490 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004491}
4492
4493void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004494 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004495}
4496
4497void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004498 HandleFieldGet(instruction, instruction->GetFieldInfo());
4499}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500
Calin Juravle52c48962014-12-16 17:02:57 +00004501void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4502 HandleFieldGet(instruction);
4503}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504
Calin Juravle52c48962014-12-16 17:02:57 +00004505void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4506 HandleFieldGet(instruction, instruction->GetFieldInfo());
4507}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508
Calin Juravle52c48962014-12-16 17:02:57 +00004509void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4510 HandleFieldSet(instruction, instruction->GetFieldInfo());
4511}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004512
Calin Juravle52c48962014-12-16 17:02:57 +00004513void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004514 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004515}
4516
Calin Juravlee460d1d2015-09-29 04:52:17 +01004517void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4518 HUnresolvedInstanceFieldGet* instruction) {
4519 FieldAccessCallingConventionX86_64 calling_convention;
4520 codegen_->CreateUnresolvedFieldLocationSummary(
4521 instruction, instruction->GetFieldType(), calling_convention);
4522}
4523
4524void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4525 HUnresolvedInstanceFieldGet* instruction) {
4526 FieldAccessCallingConventionX86_64 calling_convention;
4527 codegen_->GenerateUnresolvedFieldAccess(instruction,
4528 instruction->GetFieldType(),
4529 instruction->GetFieldIndex(),
4530 instruction->GetDexPc(),
4531 calling_convention);
4532}
4533
4534void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4535 HUnresolvedInstanceFieldSet* instruction) {
4536 FieldAccessCallingConventionX86_64 calling_convention;
4537 codegen_->CreateUnresolvedFieldLocationSummary(
4538 instruction, instruction->GetFieldType(), calling_convention);
4539}
4540
4541void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4542 HUnresolvedInstanceFieldSet* instruction) {
4543 FieldAccessCallingConventionX86_64 calling_convention;
4544 codegen_->GenerateUnresolvedFieldAccess(instruction,
4545 instruction->GetFieldType(),
4546 instruction->GetFieldIndex(),
4547 instruction->GetDexPc(),
4548 calling_convention);
4549}
4550
4551void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4552 HUnresolvedStaticFieldGet* instruction) {
4553 FieldAccessCallingConventionX86_64 calling_convention;
4554 codegen_->CreateUnresolvedFieldLocationSummary(
4555 instruction, instruction->GetFieldType(), calling_convention);
4556}
4557
4558void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4559 HUnresolvedStaticFieldGet* instruction) {
4560 FieldAccessCallingConventionX86_64 calling_convention;
4561 codegen_->GenerateUnresolvedFieldAccess(instruction,
4562 instruction->GetFieldType(),
4563 instruction->GetFieldIndex(),
4564 instruction->GetDexPc(),
4565 calling_convention);
4566}
4567
4568void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4569 HUnresolvedStaticFieldSet* instruction) {
4570 FieldAccessCallingConventionX86_64 calling_convention;
4571 codegen_->CreateUnresolvedFieldLocationSummary(
4572 instruction, instruction->GetFieldType(), calling_convention);
4573}
4574
4575void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4576 HUnresolvedStaticFieldSet* instruction) {
4577 FieldAccessCallingConventionX86_64 calling_convention;
4578 codegen_->GenerateUnresolvedFieldAccess(instruction,
4579 instruction->GetFieldType(),
4580 instruction->GetFieldIndex(),
4581 instruction->GetDexPc(),
4582 calling_convention);
4583}
4584
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004586 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4587 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4588 ? Location::RequiresRegister()
4589 : Location::Any();
4590 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591}
4592
Calin Juravle2ae48182016-03-16 14:05:09 +00004593void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4594 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004595 return;
4596 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004597 LocationSummary* locations = instruction->GetLocations();
4598 Location obj = locations->InAt(0);
4599
4600 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004601 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004602}
4603
Calin Juravle2ae48182016-03-16 14:05:09 +00004604void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004605 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004606 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607
4608 LocationSummary* locations = instruction->GetLocations();
4609 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004610
4611 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004612 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004613 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004615 } else {
4616 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004617 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004618 __ jmp(slow_path->GetEntryLabel());
4619 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004620 }
4621 __ j(kEqual, slow_path->GetEntryLabel());
4622}
4623
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004624void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004625 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004626}
4627
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004629 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004630 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004631 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004632 new (GetGraph()->GetArena()) LocationSummary(instruction,
4633 object_array_get_with_read_barrier ?
4634 LocationSummary::kCallOnSlowPath :
4635 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004636 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004637 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004638 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004639 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004640 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004642 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4643 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004644 // The output overlaps for an object array get when read barriers
4645 // are enabled: we do not want the move to overwrite the array's
4646 // location, as we need it to emit the read barrier.
4647 locations->SetOut(
4648 Location::RequiresRegister(),
4649 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004650 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651}
4652
4653void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4654 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004655 Location obj_loc = locations->InAt(0);
4656 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004657 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004658 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004659 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004661 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004662 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004663 case DataType::Type::kBool:
4664 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004665 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004666 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 break;
4668 }
4669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004671 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004672 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 break;
4674 }
4675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004676 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004677 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004678 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4679 // Branch cases into compressed and uncompressed for each index's type.
4680 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4681 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004682 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004683 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004684 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4685 "Expecting 0=compressed, 1=uncompressed");
4686 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004687 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4688 __ jmp(&done);
4689 __ Bind(&not_compressed);
4690 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4691 __ Bind(&done);
4692 } else {
4693 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4694 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695 break;
4696 }
4697
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004698 case DataType::Type::kInt16: {
4699 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4700 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4701 break;
4702 }
4703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004704 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004705 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004706 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707 break;
4708 }
4709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004710 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004711 static_assert(
4712 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4713 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004714 // /* HeapReference<Object> */ out =
4715 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4716 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004717 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004718 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004719 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004720 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004721 } else {
4722 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004723 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4724 codegen_->MaybeRecordImplicitNullCheck(instruction);
4725 // If read barriers are enabled, emit read barriers other than
4726 // Baker's using a slow path (and also unpoison the loaded
4727 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004728 if (index.IsConstant()) {
4729 uint32_t offset =
4730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004731 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4732 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004733 codegen_->MaybeGenerateReadBarrierSlow(
4734 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4735 }
4736 }
4737 break;
4738 }
4739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004741 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004742 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 break;
4744 }
4745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004746 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004747 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004748 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004749 break;
4750 }
4751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004752 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004753 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004754 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004755 break;
4756 }
4757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004758 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004759 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004760 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761 }
Roland Levillain4d027112015-07-01 15:41:14 +01004762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004763 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004764 // Potential implicit null checks, in the case of reference
4765 // arrays, are handled in the previous switch statement.
4766 } else {
4767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004768 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004769}
4770
4771void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004772 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004773
4774 bool needs_write_barrier =
4775 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004776 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004777
Nicolas Geoffray39468442014-09-02 15:17:15 +01004778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004779 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004780 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004781 LocationSummary::kCallOnSlowPath :
4782 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004783
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004784 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004785 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004786 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04004787 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004788 } else {
4789 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4790 }
4791
4792 if (needs_write_barrier) {
4793 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004794 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004795 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004796 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797}
4798
4799void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4800 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004801 Location array_loc = locations->InAt(0);
4802 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004803 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004804 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004806 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004807 bool needs_write_barrier =
4808 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004809 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4810 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4811 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004812
4813 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004814 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004815 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004816 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004817 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004818 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004819 if (value.IsRegister()) {
4820 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004822 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004823 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004825 break;
4826 }
4827
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004828 case DataType::Type::kUint16:
4829 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004831 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 if (value.IsRegister()) {
4833 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004834 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01004836 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004838 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004839 break;
4840 }
4841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004842 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004843 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004844 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004845
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 if (!value.IsRegister()) {
4847 // Just setting null.
4848 DCHECK(instruction->InputAt(2)->IsNullConstant());
4849 DCHECK(value.IsConstant()) << value;
4850 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004852 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004853 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004854 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004855 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856
4857 DCHECK(needs_write_barrier);
4858 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004859 // We cannot use a NearLabel for `done`, as its range may be too
4860 // short when Baker read barriers are enabled.
4861 Label done;
4862 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004863 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004864 Location temp_loc = locations->GetTemp(0);
4865 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004866 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004867 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4868 codegen_->AddSlowPath(slow_path);
4869 if (instruction->GetValueCanBeNull()) {
4870 __ testl(register_value, register_value);
4871 __ j(kNotEqual, &not_null);
4872 __ movl(address, Immediate(0));
4873 codegen_->MaybeRecordImplicitNullCheck(instruction);
4874 __ jmp(&done);
4875 __ Bind(&not_null);
4876 }
4877
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004878 // Note that when Baker read barriers are enabled, the type
4879 // checks are performed without read barriers. This is fine,
4880 // even in the case where a class object is in the from-space
4881 // after the flip, as a comparison involving such a type would
4882 // not produce a false positive; it may of course produce a
4883 // false negative, in which case we would take the ArraySet
4884 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004885
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004886 // /* HeapReference<Class> */ temp = array->klass_
4887 __ movl(temp, Address(array, class_offset));
4888 codegen_->MaybeRecordImplicitNullCheck(instruction);
4889 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004890
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004891 // /* HeapReference<Class> */ temp = temp->component_type_
4892 __ movl(temp, Address(temp, component_offset));
4893 // If heap poisoning is enabled, no need to unpoison `temp`
4894 // nor the object reference in `register_value->klass`, as
4895 // we are comparing two poisoned references.
4896 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004897
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004898 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4899 __ j(kEqual, &do_put);
4900 // If heap poisoning is enabled, the `temp` reference has
4901 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004902 __ MaybeUnpoisonHeapReference(temp);
4903
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004904 // If heap poisoning is enabled, no need to unpoison the
4905 // heap reference loaded below, as it is only used for a
4906 // comparison with null.
4907 __ cmpl(Address(temp, super_offset), Immediate(0));
4908 __ j(kNotEqual, slow_path->GetEntryLabel());
4909 __ Bind(&do_put);
4910 } else {
4911 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004912 }
4913 }
4914
4915 if (kPoisonHeapReferences) {
4916 __ movl(temp, register_value);
4917 __ PoisonHeapReference(temp);
4918 __ movl(address, temp);
4919 } else {
4920 __ movl(address, register_value);
4921 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004922 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004923 codegen_->MaybeRecordImplicitNullCheck(instruction);
4924 }
4925
4926 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4927 codegen_->MarkGCCard(
4928 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4929 __ Bind(&done);
4930
4931 if (slow_path != nullptr) {
4932 __ Bind(slow_path->GetExitLabel());
4933 }
4934
4935 break;
4936 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004938 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004939 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004940 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004941 if (value.IsRegister()) {
4942 __ movl(address, value.AsRegister<CpuRegister>());
4943 } else {
4944 DCHECK(value.IsConstant()) << value;
4945 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4946 __ movl(address, Immediate(v));
4947 }
4948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004949 break;
4950 }
4951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004952 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004954 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004955 if (value.IsRegister()) {
4956 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004958 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004959 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004960 Address address_high =
4961 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004962 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004963 }
4964 break;
4965 }
4966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004967 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004968 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004969 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004970 if (value.IsFpuRegister()) {
4971 __ movss(address, value.AsFpuRegister<XmmRegister>());
4972 } else {
4973 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004974 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004975 __ movl(address, Immediate(v));
4976 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004977 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004978 break;
4979 }
4980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004981 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004982 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004983 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004984 if (value.IsFpuRegister()) {
4985 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4986 codegen_->MaybeRecordImplicitNullCheck(instruction);
4987 } else {
4988 int64_t v =
4989 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004990 Address address_high =
4991 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004992 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4993 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004994 break;
4995 }
4996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004998 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004999 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005000 }
5001}
5002
5003void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005004 LocationSummary* locations =
5005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005006 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005007 if (!instruction->IsEmittedAtUseSite()) {
5008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5009 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010}
5011
5012void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005013 if (instruction->IsEmittedAtUseSite()) {
5014 return;
5015 }
5016
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005017 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005018 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005019 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5020 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005023 // Mask out most significant bit in case the array is String's array of char.
5024 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005025 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005026 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005027}
5028
5029void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005030 RegisterSet caller_saves = RegisterSet::Empty();
5031 InvokeRuntimeCallingConvention calling_convention;
5032 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5033 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5034 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005035 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005036 HInstruction* length = instruction->InputAt(1);
5037 if (!length->IsEmittedAtUseSite()) {
5038 locations->SetInAt(1, Location::RegisterOrConstant(length));
5039 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040}
5041
5042void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5043 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005044 Location index_loc = locations->InAt(0);
5045 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005046 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047
Mark Mendell99dbd682015-04-22 16:18:52 -04005048 if (length_loc.IsConstant()) {
5049 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5050 if (index_loc.IsConstant()) {
5051 // BCE will remove the bounds check if we are guarenteed to pass.
5052 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5053 if (index < 0 || index >= length) {
5054 codegen_->AddSlowPath(slow_path);
5055 __ jmp(slow_path->GetEntryLabel());
5056 } else {
5057 // Some optimization after BCE may have generated this, and we should not
5058 // generate a bounds check if it is a valid range.
5059 }
5060 return;
5061 }
5062
5063 // We have to reverse the jump condition because the length is the constant.
5064 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5065 __ cmpl(index_reg, Immediate(length));
5066 codegen_->AddSlowPath(slow_path);
5067 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005068 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005069 HInstruction* array_length = instruction->InputAt(1);
5070 if (array_length->IsEmittedAtUseSite()) {
5071 // Address the length field in the array.
5072 DCHECK(array_length->IsArrayLength());
5073 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5074 Location array_loc = array_length->GetLocations()->InAt(0);
5075 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005076 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005077 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5078 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005079 CpuRegister length_reg = CpuRegister(TMP);
5080 __ movl(length_reg, array_len);
5081 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005082 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005083 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005084 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005085 // Checking the bound for general case:
5086 // Array of char or String's array when the compression feature off.
5087 if (index_loc.IsConstant()) {
5088 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5089 __ cmpl(array_len, Immediate(value));
5090 } else {
5091 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5092 }
5093 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005094 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005095 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005096 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005097 }
5098 codegen_->AddSlowPath(slow_path);
5099 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005100 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101}
5102
5103void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5104 CpuRegister card,
5105 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005106 CpuRegister value,
5107 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005108 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005109 if (value_can_be_null) {
5110 __ testl(value, value);
5111 __ j(kEqual, &is_null);
5112 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005113 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005114 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 __ movq(temp, object);
5116 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005117 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005118 if (value_can_be_null) {
5119 __ Bind(&is_null);
5120 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121}
5122
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005123void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005124 LOG(FATAL) << "Unimplemented";
5125}
5126
5127void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005128 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5129}
5130
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005131void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005132 LocationSummary* locations =
5133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005134 // In suspend check slow path, usually there are no caller-save registers at all.
5135 // If SIMD instructions are present, however, we force spilling all live SIMD
5136 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005137 locations->SetCustomSlowPathCallerSaves(
5138 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005139}
5140
5141void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005142 HBasicBlock* block = instruction->GetBlock();
5143 if (block->GetLoopInformation() != nullptr) {
5144 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5145 // The back edge will generate the suspend check.
5146 return;
5147 }
5148 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5149 // The goto will generate the suspend check.
5150 return;
5151 }
5152 GenerateSuspendCheck(instruction, nullptr);
5153}
5154
5155void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5156 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005157 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005158 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5159 if (slow_path == nullptr) {
5160 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5161 instruction->SetSlowPath(slow_path);
5162 codegen_->AddSlowPath(slow_path);
5163 if (successor != nullptr) {
5164 DCHECK(successor->IsLoopHeader());
5165 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5166 }
5167 } else {
5168 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5169 }
5170
Andreas Gampe542451c2016-07-26 09:02:02 -07005171 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005172 /* no_rip */ true),
5173 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005174 if (successor == nullptr) {
5175 __ j(kNotEqual, slow_path->GetEntryLabel());
5176 __ Bind(slow_path->GetReturnLabel());
5177 } else {
5178 __ j(kEqual, codegen_->GetLabelOf(successor));
5179 __ jmp(slow_path->GetEntryLabel());
5180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005181}
5182
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005183X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5184 return codegen_->GetAssembler();
5185}
5186
5187void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005188 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005189 Location source = move->GetSource();
5190 Location destination = move->GetDestination();
5191
5192 if (source.IsRegister()) {
5193 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005194 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005195 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005196 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005197 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005198 } else {
5199 DCHECK(destination.IsDoubleStackSlot());
5200 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005201 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005202 }
5203 } else if (source.IsStackSlot()) {
5204 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005205 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005206 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005208 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005210 } else {
5211 DCHECK(destination.IsStackSlot());
5212 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5213 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5214 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005215 } else if (source.IsDoubleStackSlot()) {
5216 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005218 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005220 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5221 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005222 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005223 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005224 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5225 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5226 }
Aart Bik5576f372017-03-23 16:17:37 -07005227 } else if (source.IsSIMDStackSlot()) {
5228 DCHECK(destination.IsFpuRegister());
5229 __ movups(destination.AsFpuRegister<XmmRegister>(),
5230 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005231 } else if (source.IsConstant()) {
5232 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005233 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5234 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005235 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005236 if (value == 0) {
5237 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5238 } else {
5239 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5240 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005241 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005242 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005243 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005244 }
5245 } else if (constant->IsLongConstant()) {
5246 int64_t value = constant->AsLongConstant()->GetValue();
5247 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005248 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005249 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005250 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005251 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005252 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005253 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005254 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005255 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005256 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005257 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005258 } else {
5259 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005260 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005261 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5262 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005263 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005264 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005265 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005266 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005267 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005268 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005269 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005270 } else {
5271 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005272 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005273 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005274 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005275 } else if (source.IsFpuRegister()) {
5276 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005277 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005278 } else if (destination.IsStackSlot()) {
5279 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005281 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005282 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005283 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005284 } else {
5285 DCHECK(destination.IsSIMDStackSlot());
5286 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5287 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005289 }
5290}
5291
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005292void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005293 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005294 __ movl(Address(CpuRegister(RSP), mem), reg);
5295 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005296}
5297
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005298void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005299 ScratchRegisterScope ensure_scratch(
5300 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5301
5302 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5303 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5304 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5305 Address(CpuRegister(RSP), mem2 + stack_offset));
5306 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5307 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5308 CpuRegister(ensure_scratch.GetRegister()));
5309}
5310
Mark Mendell8a1c7282015-06-29 15:41:28 -04005311void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5312 __ movq(CpuRegister(TMP), reg1);
5313 __ movq(reg1, reg2);
5314 __ movq(reg2, CpuRegister(TMP));
5315}
5316
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005317void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5318 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5319 __ movq(Address(CpuRegister(RSP), mem), reg);
5320 __ movq(reg, CpuRegister(TMP));
5321}
5322
5323void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5324 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005325 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005326
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005327 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5328 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5329 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5330 Address(CpuRegister(RSP), mem2 + stack_offset));
5331 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5332 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5333 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005334}
5335
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005336void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5337 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5338 __ movss(Address(CpuRegister(RSP), mem), reg);
5339 __ movd(reg, CpuRegister(TMP));
5340}
5341
5342void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5343 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5344 __ movsd(Address(CpuRegister(RSP), mem), reg);
5345 __ movd(reg, CpuRegister(TMP));
5346}
5347
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005348void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005349 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005350 Location source = move->GetSource();
5351 Location destination = move->GetDestination();
5352
5353 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005354 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005355 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005356 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005357 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005358 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005359 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005360 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5361 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005362 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005363 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005364 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005365 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5366 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005367 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005368 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5369 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5370 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005371 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005372 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005373 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005374 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005375 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005376 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005377 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005378 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005379 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005380 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005381 }
5382}
5383
5384
5385void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5386 __ pushq(CpuRegister(reg));
5387}
5388
5389
5390void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5391 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005392}
5393
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005394void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005395 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005396 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5397 Immediate(mirror::Class::kStatusInitialized));
5398 __ j(kLess, slow_path->GetEntryLabel());
5399 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005400 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005401}
5402
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005403HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5404 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005405 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005406 case HLoadClass::LoadKind::kInvalid:
5407 LOG(FATAL) << "UNREACHABLE";
5408 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005409 case HLoadClass::LoadKind::kReferrersClass:
5410 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005411 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005412 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005413 case HLoadClass::LoadKind::kBssEntry:
5414 DCHECK(!Runtime::Current()->UseJitCompilation());
5415 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005416 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005417 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005418 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005419 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005420 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005421 break;
5422 }
5423 return desired_class_load_kind;
5424}
5425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005426void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005427 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005428 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005429 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005430 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005431 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005432 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005433 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005434 return;
5435 }
Vladimir Marko41559982017-01-06 14:04:23 +00005436 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005438 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5439 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 ? LocationSummary::kCallOnSlowPath
5441 : LocationSummary::kNoCall;
5442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005443 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005444 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005445 }
5446
Vladimir Marko41559982017-01-06 14:04:23 +00005447 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005448 locations->SetInAt(0, Location::RequiresRegister());
5449 }
5450 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005451 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5452 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5453 // Rely on the type resolution and/or initialization to save everything.
5454 // Custom calling convention: RAX serves as both input and output.
5455 RegisterSet caller_saves = RegisterSet::Empty();
5456 caller_saves.Add(Location::RegisterLocation(RAX));
5457 locations->SetCustomSlowPathCallerSaves(caller_saves);
5458 } else {
5459 // For non-Baker read barrier we have a temp-clobbering call.
5460 }
5461 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005462}
5463
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005464Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
5465 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005466 Handle<mirror::Class> handle) {
5467 jit_class_roots_.Overwrite(
5468 TypeReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005469 // Add a patch entry and return the label.
5470 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
5471 PatchInfo<Label>* info = &jit_class_patches_.back();
5472 return &info->label;
5473}
5474
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005475// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5476// move.
5477void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005478 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005479 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005480 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005481 return;
5482 }
Vladimir Marko41559982017-01-06 14:04:23 +00005483 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005484
Vladimir Marko41559982017-01-06 14:04:23 +00005485 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005486 Location out_loc = locations->Out();
5487 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005488
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005489 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5490 ? kWithoutReadBarrier
5491 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005493 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005494 case HLoadClass::LoadKind::kReferrersClass: {
5495 DCHECK(!cls->CanCallRuntime());
5496 DCHECK(!cls->MustGenerateClinitCheck());
5497 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5498 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5499 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005500 cls,
5501 out_loc,
5502 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005503 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005504 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005505 break;
5506 }
5507 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005508 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005509 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005510 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005511 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005512 break;
5513 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005514 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005515 uint32_t address = dchecked_integral_cast<uint32_t>(
5516 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5517 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005518 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005519 break;
5520 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005521 case HLoadClass::LoadKind::kBootImageClassTable: {
5522 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5523 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5524 codegen_->RecordBootTypePatch(cls);
5525 // Extract the reference from the slot data, i.e. clear the hash bits.
5526 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
5527 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
5528 if (masked_hash != 0) {
5529 __ subl(out, Immediate(masked_hash));
5530 }
5531 break;
5532 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005533 case HLoadClass::LoadKind::kBssEntry: {
5534 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5535 /* no_rip */ false);
5536 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5537 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5538 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5539 generate_null_check = true;
5540 break;
5541 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005542 case HLoadClass::LoadKind::kJitTableAddress: {
5543 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5544 /* no_rip */ true);
5545 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005546 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005548 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005549 break;
5550 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 default:
5552 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5553 UNREACHABLE();
5554 }
5555
5556 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5557 DCHECK(cls->CanCallRuntime());
5558 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5559 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5560 codegen_->AddSlowPath(slow_path);
5561 if (generate_null_check) {
5562 __ testl(out, out);
5563 __ j(kEqual, slow_path->GetEntryLabel());
5564 }
5565 if (cls->MustGenerateClinitCheck()) {
5566 GenerateClassInitializationCheck(slow_path, out);
5567 } else {
5568 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005569 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570 }
5571}
5572
5573void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5574 LocationSummary* locations =
5575 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5576 locations->SetInAt(0, Location::RequiresRegister());
5577 if (check->HasUses()) {
5578 locations->SetOut(Location::SameAsFirstInput());
5579 }
5580}
5581
5582void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005583 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005584 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005585 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005586 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005587 GenerateClassInitializationCheck(slow_path,
5588 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005589}
5590
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005591HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5592 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005593 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005594 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005595 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005596 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005597 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005598 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005599 case HLoadString::LoadKind::kJitTableAddress:
5600 DCHECK(Runtime::Current()->UseJitCompilation());
5601 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005602 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005603 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005604 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005605 }
5606 return desired_string_load_kind;
5607}
5608
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005609void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005610 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005612 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005613 locations->SetOut(Location::RegisterLocation(RAX));
5614 } else {
5615 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005616 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5617 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005618 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005619 // Custom calling convention: RAX serves as both input and output.
5620 RegisterSet caller_saves = RegisterSet::Empty();
5621 caller_saves.Add(Location::RegisterLocation(RAX));
5622 locations->SetCustomSlowPathCallerSaves(caller_saves);
5623 } else {
5624 // For non-Baker read barrier we have a temp-clobbering call.
5625 }
5626 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005627 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005628}
5629
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005630Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005631 dex::StringIndex dex_index,
5632 Handle<mirror::String> handle) {
5633 jit_string_roots_.Overwrite(
5634 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005635 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005636 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005637 PatchInfo<Label>* info = &jit_string_patches_.back();
5638 return &info->label;
5639}
5640
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005641// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5642// move.
5643void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005644 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005645 Location out_loc = locations->Out();
5646 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005647
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005648 switch (load->GetLoadKind()) {
5649 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005650 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005651 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005652 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005653 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005654 }
5655 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005656 uint32_t address = dchecked_integral_cast<uint32_t>(
5657 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5658 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005659 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005660 return;
5661 }
5662 case HLoadString::LoadKind::kBootImageInternTable: {
5663 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5664 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5665 codegen_->RecordBootStringPatch(load);
5666 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005667 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005668 case HLoadString::LoadKind::kBssEntry: {
5669 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5670 /* no_rip */ false);
5671 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5672 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005673 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005674 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5675 codegen_->AddSlowPath(slow_path);
5676 __ testl(out, out);
5677 __ j(kEqual, slow_path->GetEntryLabel());
5678 __ Bind(slow_path->GetExitLabel());
5679 return;
5680 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005681 case HLoadString::LoadKind::kJitTableAddress: {
5682 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5683 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005684 Label* fixup_label = codegen_->NewJitRootStringPatch(
5685 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005686 // /* GcRoot<mirror::String> */ out = *address
5687 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5688 return;
5689 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005690 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005691 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005692 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005694 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005695 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005696 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005697 codegen_->InvokeRuntime(kQuickResolveString,
5698 load,
5699 load->GetDexPc());
5700 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005701}
5702
David Brazdilcb1c0552015-08-04 16:22:25 +01005703static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005704 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005705 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005706}
5707
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005708void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5709 LocationSummary* locations =
5710 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5711 locations->SetOut(Location::RequiresRegister());
5712}
5713
5714void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005715 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5716}
5717
5718void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5719 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5720}
5721
5722void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5723 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005724}
5725
5726void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5727 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005729 InvokeRuntimeCallingConvention calling_convention;
5730 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5731}
5732
5733void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005734 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005735 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005736}
5737
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005738static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5739 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005740 // We need a temporary for holding the iftable length.
5741 return true;
5742 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005743 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005744 !kUseBakerReadBarrier &&
5745 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005746 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5747 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5748}
5749
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005750static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5751 return kEmitCompilerReadBarrier &&
5752 !kUseBakerReadBarrier &&
5753 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5754 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5755 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5756}
5757
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005758void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005759 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005760 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005761 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005762 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005763 case TypeCheckKind::kExactCheck:
5764 case TypeCheckKind::kAbstractClassCheck:
5765 case TypeCheckKind::kClassHierarchyCheck:
5766 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005767 call_kind =
5768 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005769 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005770 break;
5771 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005772 case TypeCheckKind::kUnresolvedCheck:
5773 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005774 call_kind = LocationSummary::kCallOnSlowPath;
5775 break;
5776 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005779 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005780 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005781 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005782 locations->SetInAt(0, Location::RequiresRegister());
5783 locations->SetInAt(1, Location::Any());
5784 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5785 locations->SetOut(Location::RequiresRegister());
5786 // When read barriers are enabled, we need a temporary register for
5787 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005788 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005789 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005791}
5792
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005793void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005794 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005795 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796 Location obj_loc = locations->InAt(0);
5797 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005798 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 Location out_loc = locations->Out();
5800 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005801 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005802 locations->GetTemp(0) :
5803 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005804 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5806 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5807 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005808 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005810
5811 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005812 // Avoid null check if we know obj is not null.
5813 if (instruction->MustDoNullCheck()) {
5814 __ testl(obj, obj);
5815 __ j(kEqual, &zero);
5816 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005817
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005818 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005820 // /* HeapReference<Class> */ out = obj->klass_
5821 GenerateReferenceLoadTwoRegisters(instruction,
5822 out_loc,
5823 obj_loc,
5824 class_offset,
5825 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 if (cls.IsRegister()) {
5827 __ cmpl(out, cls.AsRegister<CpuRegister>());
5828 } else {
5829 DCHECK(cls.IsStackSlot()) << cls;
5830 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5831 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005832 if (zero.IsLinked()) {
5833 // Classes must be equal for the instanceof to succeed.
5834 __ j(kNotEqual, &zero);
5835 __ movl(out, Immediate(1));
5836 __ jmp(&done);
5837 } else {
5838 __ setcc(kEqual, out);
5839 // setcc only sets the low byte.
5840 __ andl(out, Immediate(1));
5841 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842 break;
5843 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005844
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005846 // /* HeapReference<Class> */ out = obj->klass_
5847 GenerateReferenceLoadTwoRegisters(instruction,
5848 out_loc,
5849 obj_loc,
5850 class_offset,
5851 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852 // If the class is abstract, we eagerly fetch the super class of the
5853 // object to avoid doing a comparison we know will fail.
5854 NearLabel loop, success;
5855 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005857 GenerateReferenceLoadOneRegister(instruction,
5858 out_loc,
5859 super_offset,
5860 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005861 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 __ testl(out, out);
5863 // If `out` is null, we use it for the result, and jump to `done`.
5864 __ j(kEqual, &done);
5865 if (cls.IsRegister()) {
5866 __ cmpl(out, cls.AsRegister<CpuRegister>());
5867 } else {
5868 DCHECK(cls.IsStackSlot()) << cls;
5869 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5870 }
5871 __ j(kNotEqual, &loop);
5872 __ movl(out, Immediate(1));
5873 if (zero.IsLinked()) {
5874 __ jmp(&done);
5875 }
5876 break;
5877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005880 // /* HeapReference<Class> */ out = obj->klass_
5881 GenerateReferenceLoadTwoRegisters(instruction,
5882 out_loc,
5883 obj_loc,
5884 class_offset,
5885 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 // Walk over the class hierarchy to find a match.
5887 NearLabel loop, success;
5888 __ Bind(&loop);
5889 if (cls.IsRegister()) {
5890 __ cmpl(out, cls.AsRegister<CpuRegister>());
5891 } else {
5892 DCHECK(cls.IsStackSlot()) << cls;
5893 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5894 }
5895 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005896 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005897 GenerateReferenceLoadOneRegister(instruction,
5898 out_loc,
5899 super_offset,
5900 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005901 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 __ testl(out, out);
5903 __ j(kNotEqual, &loop);
5904 // If `out` is null, we use it for the result, and jump to `done`.
5905 __ jmp(&done);
5906 __ Bind(&success);
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::kArrayObjectCheck: {
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 Geoffrayabfcf182015-09-21 18:41:21 +01005921 // Do an exact check.
5922 NearLabel exact_check;
5923 if (cls.IsRegister()) {
5924 __ cmpl(out, cls.AsRegister<CpuRegister>());
5925 } else {
5926 DCHECK(cls.IsStackSlot()) << cls;
5927 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5928 }
5929 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005932 GenerateReferenceLoadOneRegister(instruction,
5933 out_loc,
5934 component_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 // If `out` is null, we use it for the result, and jump to `done`.
5939 __ j(kEqual, &done);
5940 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5941 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005942 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 __ movl(out, Immediate(1));
5944 __ jmp(&done);
5945 break;
5946 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005949 // No read barrier since the slow path will retry upon failure.
5950 // /* HeapReference<Class> */ out = obj->klass_
5951 GenerateReferenceLoadTwoRegisters(instruction,
5952 out_loc,
5953 obj_loc,
5954 class_offset,
5955 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 if (cls.IsRegister()) {
5957 __ cmpl(out, cls.AsRegister<CpuRegister>());
5958 } else {
5959 DCHECK(cls.IsStackSlot()) << cls;
5960 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5961 }
5962 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005963 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5964 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005965 codegen_->AddSlowPath(slow_path);
5966 __ j(kNotEqual, slow_path->GetEntryLabel());
5967 __ movl(out, Immediate(1));
5968 if (zero.IsLinked()) {
5969 __ jmp(&done);
5970 }
5971 break;
5972 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005973
Calin Juravle98893e12015-10-02 21:05:03 +01005974 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975 case TypeCheckKind::kInterfaceCheck: {
5976 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005977 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 // cases.
5979 //
5980 // We cannot directly call the InstanceofNonTrivial runtime
5981 // entry point without resorting to a type checking slow path
5982 // here (i.e. by calling InvokeRuntime directly), as it would
5983 // require to assign fixed registers for the inputs of this
5984 // HInstanceOf instruction (following the runtime calling
5985 // convention), which might be cluttered by the potential first
5986 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005987 //
5988 // TODO: Introduce a new runtime entry point taking the object
5989 // to test (instead of its class) as argument, and let it deal
5990 // with the read barrier issues. This will let us refactor this
5991 // case of the `switch` code as it was previously (with a direct
5992 // call to the runtime not using a type checking slow path).
5993 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 DCHECK(locations->OnlyCallsOnSlowPath());
5995 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5996 /* is_fatal */ false);
5997 codegen_->AddSlowPath(slow_path);
5998 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 if (zero.IsLinked()) {
6000 __ jmp(&done);
6001 }
6002 break;
6003 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006004 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006005
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006007 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006008 __ xorl(out, out);
6009 }
6010
6011 if (done.IsLinked()) {
6012 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006013 }
6014
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006015 if (slow_path != nullptr) {
6016 __ Bind(slow_path->GetExitLabel());
6017 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006018}
6019
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006020static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006021 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 case TypeCheckKind::kExactCheck:
6023 case TypeCheckKind::kAbstractClassCheck:
6024 case TypeCheckKind::kClassHierarchyCheck:
6025 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006026 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006027 case TypeCheckKind::kInterfaceCheck:
6028 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006030 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006031 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006032 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006033 LOG(FATAL) << "Unreachable";
6034 UNREACHABLE();
6035}
6036
6037void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6038 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6039 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6040 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6041 LocationSummary::CallKind call_kind = is_fatal_slow_path
6042 ? LocationSummary::kNoCall
6043 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6045 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006046 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6047 // Require a register for the interface check since there is a loop that compares the class to
6048 // a memory address.
6049 locations->SetInAt(1, Location::RequiresRegister());
6050 } else {
6051 locations->SetInAt(1, Location::Any());
6052 }
6053
Roland Levillain0d5a2812015-11-13 10:07:31 +00006054 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6055 locations->AddTemp(Location::RequiresRegister());
6056 // When read barriers are enabled, we need an additional temporary
6057 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006058 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006059 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006060 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006061}
6062
6063void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006064 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006065 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006066 Location obj_loc = locations->InAt(0);
6067 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006068 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 Location temp_loc = locations->GetTemp(0);
6070 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006071 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006072 locations->GetTemp(1) :
6073 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006074 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6075 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6076 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6077 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6078 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6079 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006080 const uint32_t object_array_data_offset =
6081 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006082
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006083 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6084 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6085 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006087 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006088 SlowPathCode* type_check_slow_path =
6089 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6090 is_type_check_slow_path_fatal);
6091 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006092
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006093
6094 NearLabel done;
6095 // Avoid null check if we know obj is not null.
6096 if (instruction->MustDoNullCheck()) {
6097 __ testl(obj, obj);
6098 __ j(kEqual, &done);
6099 }
6100
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102 case TypeCheckKind::kExactCheck:
6103 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006104 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006105 GenerateReferenceLoadTwoRegisters(instruction,
6106 temp_loc,
6107 obj_loc,
6108 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006109 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 if (cls.IsRegister()) {
6111 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6112 } else {
6113 DCHECK(cls.IsStackSlot()) << cls;
6114 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6115 }
6116 // Jump to slow path for throwing the exception or doing a
6117 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006118 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006119 break;
6120 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006123 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006124 GenerateReferenceLoadTwoRegisters(instruction,
6125 temp_loc,
6126 obj_loc,
6127 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006128 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 // If the class is abstract, we eagerly fetch the super class of the
6130 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006131 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006132 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006133 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006134 GenerateReferenceLoadOneRegister(instruction,
6135 temp_loc,
6136 super_offset,
6137 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006138 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006140 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6141 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006143 // Otherwise, compare the classes.
6144 __ j(kZero, type_check_slow_path->GetEntryLabel());
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 __ j(kNotEqual, &loop);
6152 break;
6153 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006156 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006157 GenerateReferenceLoadTwoRegisters(instruction,
6158 temp_loc,
6159 obj_loc,
6160 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006161 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006162 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006163 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 __ Bind(&loop);
6165 if (cls.IsRegister()) {
6166 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6167 } else {
6168 DCHECK(cls.IsStackSlot()) << cls;
6169 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6170 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006171 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006172
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006174 GenerateReferenceLoadOneRegister(instruction,
6175 temp_loc,
6176 super_offset,
6177 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006178 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006179
6180 // If the class reference currently in `temp` is not null, jump
6181 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006183 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006184 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006186 break;
6187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006190 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006191 GenerateReferenceLoadTwoRegisters(instruction,
6192 temp_loc,
6193 obj_loc,
6194 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006195 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006196 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006198 if (cls.IsRegister()) {
6199 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6200 } else {
6201 DCHECK(cls.IsStackSlot()) << cls;
6202 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6203 }
6204 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205
6206 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006208 GenerateReferenceLoadOneRegister(instruction,
6209 temp_loc,
6210 component_offset,
6211 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006212 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213
6214 // If the component type is not null (i.e. the object is indeed
6215 // an array), jump to label `check_non_primitive_component_type`
6216 // to further check that this component type is not a primitive
6217 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006220 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006222 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 break;
6224 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006226 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006227 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006228 //
6229 // We cannot directly call the CheckCast runtime entry point
6230 // without resorting to a type checking slow path here (i.e. by
6231 // calling InvokeRuntime directly), as it would require to
6232 // assign fixed registers for the inputs of this HInstanceOf
6233 // instruction (following the runtime calling convention), which
6234 // might be cluttered by the potential first read barrier
6235 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006236 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006237 break;
6238 }
6239
6240 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006241 // Fast path for the interface check. We always go slow path for heap poisoning since
6242 // unpoisoning cls would require an extra temp.
6243 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006244 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6245 // doing this.
6246 // /* HeapReference<Class> */ temp = obj->klass_
6247 GenerateReferenceLoadTwoRegisters(instruction,
6248 temp_loc,
6249 obj_loc,
6250 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006251 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006252
6253 // /* HeapReference<Class> */ temp = temp->iftable_
6254 GenerateReferenceLoadTwoRegisters(instruction,
6255 temp_loc,
6256 temp_loc,
6257 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006258 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006259 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006260 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006261 // Loop through the iftable and check if any class matches.
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006262 NearLabel start_loop;
6263 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006264 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006265 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006266 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6267 // Go to next interface if the classes do not match.
6268 __ cmpl(cls.AsRegister<CpuRegister>(),
6269 CodeGeneratorX86_64::ArrayAddress(temp,
6270 maybe_temp2_loc,
6271 TIMES_4,
6272 object_array_data_offset));
6273 __ j(kNotEqual, &start_loop); // Return if same class.
6274 } else {
6275 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006276 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006277 break;
6278 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006279
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006280 if (done.IsLinked()) {
6281 __ Bind(&done);
6282 }
6283
Roland Levillain0d5a2812015-11-13 10:07:31 +00006284 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006285}
6286
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006287void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6288 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006290 InvokeRuntimeCallingConvention calling_convention;
6291 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6292}
6293
6294void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006295 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006296 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006297 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006298 if (instruction->IsEnter()) {
6299 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6300 } else {
6301 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6302 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006303}
6304
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006305void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6306void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6307void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6308
6309void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6310 LocationSummary* locations =
6311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6313 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006314 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006315 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006316 locations->SetOut(Location::SameAsFirstInput());
6317}
6318
6319void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6320 HandleBitwiseOperation(instruction);
6321}
6322
6323void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6324 HandleBitwiseOperation(instruction);
6325}
6326
6327void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6328 HandleBitwiseOperation(instruction);
6329}
6330
6331void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6332 LocationSummary* locations = instruction->GetLocations();
6333 Location first = locations->InAt(0);
6334 Location second = locations->InAt(1);
6335 DCHECK(first.Equals(locations->Out()));
6336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006338 if (second.IsRegister()) {
6339 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006340 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006341 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006342 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006343 } else {
6344 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006345 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006346 }
6347 } else if (second.IsConstant()) {
6348 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6349 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006350 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006351 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006352 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006353 } else {
6354 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006355 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006356 }
6357 } else {
6358 Address address(CpuRegister(RSP), second.GetStackIndex());
6359 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006360 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006361 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006362 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006363 } else {
6364 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006365 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006366 }
6367 }
6368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006370 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6371 bool second_is_constant = false;
6372 int64_t value = 0;
6373 if (second.IsConstant()) {
6374 second_is_constant = true;
6375 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006376 }
Mark Mendell40741f32015-04-20 22:10:34 -04006377 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006378
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006379 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006380 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006381 if (is_int32_value) {
6382 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6383 } else {
6384 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6385 }
6386 } else if (second.IsDoubleStackSlot()) {
6387 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006388 } else {
6389 __ andq(first_reg, second.AsRegister<CpuRegister>());
6390 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006391 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006392 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006393 if (is_int32_value) {
6394 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6395 } else {
6396 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6397 }
6398 } else if (second.IsDoubleStackSlot()) {
6399 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006400 } else {
6401 __ orq(first_reg, second.AsRegister<CpuRegister>());
6402 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006403 } else {
6404 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006405 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006406 if (is_int32_value) {
6407 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6408 } else {
6409 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6410 }
6411 } else if (second.IsDoubleStackSlot()) {
6412 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006413 } else {
6414 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6415 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006416 }
6417 }
6418}
6419
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006420void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6421 HInstruction* instruction,
6422 Location out,
6423 uint32_t offset,
6424 Location maybe_temp,
6425 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006426 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006427 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006428 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006429 if (kUseBakerReadBarrier) {
6430 // Load with fast path based Baker's read barrier.
6431 // /* HeapReference<Object> */ out = *(out + offset)
6432 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006433 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006434 } else {
6435 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006436 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006437 // in the following move operation, as we will need it for the
6438 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006439 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006440 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006441 // /* HeapReference<Object> */ out = *(out + offset)
6442 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006443 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006444 }
6445 } else {
6446 // Plain load with no read barrier.
6447 // /* HeapReference<Object> */ out = *(out + offset)
6448 __ movl(out_reg, Address(out_reg, offset));
6449 __ MaybeUnpoisonHeapReference(out_reg);
6450 }
6451}
6452
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006453void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6454 HInstruction* instruction,
6455 Location out,
6456 Location obj,
6457 uint32_t offset,
6458 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006459 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6460 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006461 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006462 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006463 if (kUseBakerReadBarrier) {
6464 // Load with fast path based Baker's read barrier.
6465 // /* HeapReference<Object> */ out = *(obj + offset)
6466 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006467 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006468 } else {
6469 // Load with slow path based read barrier.
6470 // /* HeapReference<Object> */ out = *(obj + offset)
6471 __ movl(out_reg, Address(obj_reg, offset));
6472 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6473 }
6474 } else {
6475 // Plain load with no read barrier.
6476 // /* HeapReference<Object> */ out = *(obj + offset)
6477 __ movl(out_reg, Address(obj_reg, offset));
6478 __ MaybeUnpoisonHeapReference(out_reg);
6479 }
6480}
6481
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006482void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6483 HInstruction* instruction,
6484 Location root,
6485 const Address& address,
6486 Label* fixup_label,
6487 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006488 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006489 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006490 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006491 if (kUseBakerReadBarrier) {
6492 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6493 // Baker's read barrier are used:
6494 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006495 // root = obj.field;
6496 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6497 // if (temp != null) {
6498 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006499 // }
6500
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006501 // /* GcRoot<mirror::Object> */ root = *address
6502 __ movl(root_reg, address);
6503 if (fixup_label != nullptr) {
6504 __ Bind(fixup_label);
6505 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006506 static_assert(
6507 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6508 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6509 "have different sizes.");
6510 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6511 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6512 "have different sizes.");
6513
Vladimir Marko953437b2016-08-24 08:30:46 +00006514 // Slow path marking the GC root `root`.
6515 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006516 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006517 codegen_->AddSlowPath(slow_path);
6518
Roland Levillaind966ce72017-02-09 16:20:14 +00006519 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6520 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006521 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006522 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6523 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006524 __ j(kNotEqual, slow_path->GetEntryLabel());
6525 __ Bind(slow_path->GetExitLabel());
6526 } else {
6527 // GC root loaded through a slow path for read barriers other
6528 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006529 // /* GcRoot<mirror::Object>* */ root = address
6530 __ leaq(root_reg, address);
6531 if (fixup_label != nullptr) {
6532 __ Bind(fixup_label);
6533 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006534 // /* mirror::Object* */ root = root->Read()
6535 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6536 }
6537 } else {
6538 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006539 // /* GcRoot<mirror::Object> */ root = *address
6540 __ movl(root_reg, address);
6541 if (fixup_label != nullptr) {
6542 __ Bind(fixup_label);
6543 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006544 // Note that GC roots are not affected by heap poisoning, thus we
6545 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006546 }
6547}
6548
6549void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6550 Location ref,
6551 CpuRegister obj,
6552 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006553 bool needs_null_check) {
6554 DCHECK(kEmitCompilerReadBarrier);
6555 DCHECK(kUseBakerReadBarrier);
6556
6557 // /* HeapReference<Object> */ ref = *(obj + offset)
6558 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006559 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006560}
6561
6562void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6563 Location ref,
6564 CpuRegister obj,
6565 uint32_t data_offset,
6566 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006567 bool needs_null_check) {
6568 DCHECK(kEmitCompilerReadBarrier);
6569 DCHECK(kUseBakerReadBarrier);
6570
Roland Levillain3d312422016-06-23 13:53:42 +01006571 static_assert(
6572 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6573 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006574 // /* HeapReference<Object> */ ref =
6575 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006576 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006577 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006578}
6579
6580void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6581 Location ref,
6582 CpuRegister obj,
6583 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006584 bool needs_null_check,
6585 bool always_update_field,
6586 CpuRegister* temp1,
6587 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006588 DCHECK(kEmitCompilerReadBarrier);
6589 DCHECK(kUseBakerReadBarrier);
6590
6591 // In slow path based read barriers, the read barrier call is
6592 // inserted after the original load. However, in fast path based
6593 // Baker's read barriers, we need to perform the load of
6594 // mirror::Object::monitor_ *before* the original reference load.
6595 // This load-load ordering is required by the read barrier.
6596 // The fast path/slow path (for Baker's algorithm) should look like:
6597 //
6598 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6599 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6600 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006601 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006602 // if (is_gray) {
6603 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6604 // }
6605 //
6606 // Note: the original implementation in ReadBarrier::Barrier is
6607 // slightly more complex as:
6608 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006609 // the high-bits of rb_state, which are expected to be all zeroes
6610 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6611 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006612 // - it performs additional checks that we do not do here for
6613 // performance reasons.
6614
6615 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006616 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6617
Vladimir Marko953437b2016-08-24 08:30:46 +00006618 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006619 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6620 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006621 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6622 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6623 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6624
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006625 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006626 // ref = ReadBarrier::Mark(ref);
6627 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6628 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006629 if (needs_null_check) {
6630 MaybeRecordImplicitNullCheck(instruction);
6631 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006632
6633 // Load fence to prevent load-load reordering.
6634 // Note that this is a no-op, thanks to the x86-64 memory model.
6635 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6636
6637 // The actual reference load.
6638 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006639 __ movl(ref_reg, src); // Flags are unaffected.
6640
6641 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6642 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006643 SlowPathCode* slow_path;
6644 if (always_update_field) {
6645 DCHECK(temp1 != nullptr);
6646 DCHECK(temp2 != nullptr);
6647 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6648 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6649 } else {
6650 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6651 instruction, ref, /* unpoison_ref_before_marking */ true);
6652 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006653 AddSlowPath(slow_path);
6654
6655 // We have done the "if" of the gray bit check above, now branch based on the flags.
6656 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006657
6658 // Object* ref = ref_addr->AsMirrorPtr()
6659 __ MaybeUnpoisonHeapReference(ref_reg);
6660
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006661 __ Bind(slow_path->GetExitLabel());
6662}
6663
6664void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6665 Location out,
6666 Location ref,
6667 Location obj,
6668 uint32_t offset,
6669 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 DCHECK(kEmitCompilerReadBarrier);
6671
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006672 // Insert a slow path based read barrier *after* the reference load.
6673 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674 // If heap poisoning is enabled, the unpoisoning of the loaded
6675 // reference will be carried out by the runtime within the slow
6676 // path.
6677 //
6678 // Note that `ref` currently does not get unpoisoned (when heap
6679 // poisoning is enabled), which is alright as the `ref` argument is
6680 // not used by the artReadBarrierSlow entry point.
6681 //
6682 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6683 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6684 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6685 AddSlowPath(slow_path);
6686
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 __ jmp(slow_path->GetEntryLabel());
6688 __ Bind(slow_path->GetExitLabel());
6689}
6690
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006691void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6692 Location out,
6693 Location ref,
6694 Location obj,
6695 uint32_t offset,
6696 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006698 // Baker's read barriers shall be handled by the fast path
6699 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6700 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 // If heap poisoning is enabled, unpoisoning will be taken care of
6702 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006703 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 } else if (kPoisonHeapReferences) {
6705 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6706 }
6707}
6708
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006709void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6710 Location out,
6711 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 DCHECK(kEmitCompilerReadBarrier);
6713
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006714 // Insert a slow path based read barrier *after* the GC root load.
6715 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 // Note that GC roots are not affected by heap poisoning, so we do
6717 // not need to do anything special for this here.
6718 SlowPathCode* slow_path =
6719 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
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 Levillain4b8f1ec2015-08-26 18:34:03 +01006726void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006727 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006728 LOG(FATAL) << "Unreachable";
6729}
6730
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006731void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006732 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006733 LOG(FATAL) << "Unreachable";
6734}
6735
Mark Mendellfe57faa2015-09-18 09:26:15 -04006736// Simple implementation of packed switch - generate cascaded compare/jumps.
6737void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6738 LocationSummary* locations =
6739 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6740 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006741 locations->AddTemp(Location::RequiresRegister());
6742 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006743}
6744
6745void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6746 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006747 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006748 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006749 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6750 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6751 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006752 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6753
6754 // Should we generate smaller inline compare/jumps?
6755 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6756 // Figure out the correct compare values and jump conditions.
6757 // Handle the first compare/branch as a special case because it might
6758 // jump to the default case.
6759 DCHECK_GT(num_entries, 2u);
6760 Condition first_condition;
6761 uint32_t index;
6762 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6763 if (lower_bound != 0) {
6764 first_condition = kLess;
6765 __ cmpl(value_reg_in, Immediate(lower_bound));
6766 __ j(first_condition, codegen_->GetLabelOf(default_block));
6767 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6768
6769 index = 1;
6770 } else {
6771 // Handle all the compare/jumps below.
6772 first_condition = kBelow;
6773 index = 0;
6774 }
6775
6776 // Handle the rest of the compare/jumps.
6777 for (; index + 1 < num_entries; index += 2) {
6778 int32_t compare_to_value = lower_bound + index + 1;
6779 __ cmpl(value_reg_in, Immediate(compare_to_value));
6780 // Jump to successors[index] if value < case_value[index].
6781 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6782 // Jump to successors[index + 1] if value == case_value[index + 1].
6783 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6784 }
6785
6786 if (index != num_entries) {
6787 // There are an odd number of entries. Handle the last one.
6788 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006789 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006790 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6791 }
6792
6793 // And the default for any other value.
6794 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6795 __ jmp(codegen_->GetLabelOf(default_block));
6796 }
6797 return;
6798 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006799
6800 // Remove the bias, if needed.
6801 Register value_reg_out = value_reg_in.AsRegister();
6802 if (lower_bound != 0) {
6803 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6804 value_reg_out = temp_reg.AsRegister();
6805 }
6806 CpuRegister value_reg(value_reg_out);
6807
6808 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006809 __ cmpl(value_reg, Immediate(num_entries - 1));
6810 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006811
Mark Mendell9c86b482015-09-18 13:36:07 -04006812 // We are in the range of the table.
6813 // Load the address of the jump table in the constant area.
6814 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006815
Mark Mendell9c86b482015-09-18 13:36:07 -04006816 // Load the (signed) offset from the jump table.
6817 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6818
6819 // Add the offset to the address of the table base.
6820 __ addq(temp_reg, base_reg);
6821
6822 // And jump.
6823 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006824}
6825
Aart Bikc5d47542016-01-27 17:00:35 -08006826void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6827 if (value == 0) {
6828 __ xorl(dest, dest);
6829 } else {
6830 __ movl(dest, Immediate(value));
6831 }
6832}
6833
Mark Mendell92e83bf2015-05-07 11:25:03 -04006834void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6835 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006836 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006837 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006838 } else if (IsUint<32>(value)) {
6839 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006840 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6841 } else {
6842 __ movq(dest, Immediate(value));
6843 }
6844}
6845
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006846void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6847 if (value == 0) {
6848 __ xorps(dest, dest);
6849 } else {
6850 __ movss(dest, LiteralInt32Address(value));
6851 }
6852}
6853
6854void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6855 if (value == 0) {
6856 __ xorpd(dest, dest);
6857 } else {
6858 __ movsd(dest, LiteralInt64Address(value));
6859 }
6860}
6861
6862void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6863 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6864}
6865
6866void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6867 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6868}
6869
Aart Bika19616e2016-02-01 18:57:58 -08006870void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6871 if (value == 0) {
6872 __ testl(dest, dest);
6873 } else {
6874 __ cmpl(dest, Immediate(value));
6875 }
6876}
6877
6878void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6879 if (IsInt<32>(value)) {
6880 if (value == 0) {
6881 __ testq(dest, dest);
6882 } else {
6883 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6884 }
6885 } else {
6886 // Value won't fit in an int.
6887 __ cmpq(dest, LiteralInt64Address(value));
6888 }
6889}
6890
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006891void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6892 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006893 GenerateIntCompare(lhs_reg, rhs);
6894}
6895
6896void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006897 if (rhs.IsConstant()) {
6898 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006899 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006900 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006901 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006902 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006903 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006904 }
6905}
6906
6907void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6908 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6909 if (rhs.IsConstant()) {
6910 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6911 Compare64BitValue(lhs_reg, value);
6912 } else if (rhs.IsDoubleStackSlot()) {
6913 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6914 } else {
6915 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6916 }
6917}
6918
6919Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6920 Location index,
6921 ScaleFactor scale,
6922 uint32_t data_offset) {
6923 return index.IsConstant() ?
6924 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6925 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6926}
6927
Mark Mendellcfa410b2015-05-25 16:02:44 -04006928void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6929 DCHECK(dest.IsDoubleStackSlot());
6930 if (IsInt<32>(value)) {
6931 // Can move directly as an int32 constant.
6932 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6933 Immediate(static_cast<int32_t>(value)));
6934 } else {
6935 Load64BitValue(CpuRegister(TMP), value);
6936 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6937 }
6938}
6939
Mark Mendell9c86b482015-09-18 13:36:07 -04006940/**
6941 * Class to handle late fixup of offsets into constant area.
6942 */
6943class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6944 public:
6945 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6946 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6947
6948 protected:
6949 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6950
6951 CodeGeneratorX86_64* codegen_;
6952
6953 private:
6954 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6955 // Patch the correct offset for the instruction. We use the address of the
6956 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6957 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6958 int32_t relative_position = constant_offset - pos;
6959
6960 // Patch in the right value.
6961 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6962 }
6963
6964 // Location in constant area that the fixup refers to.
6965 size_t offset_into_constant_area_;
6966};
6967
6968/**
6969 t * Class to handle late fixup of offsets to a jump table that will be created in the
6970 * constant area.
6971 */
6972class JumpTableRIPFixup : public RIPFixup {
6973 public:
6974 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6975 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6976
6977 void CreateJumpTable() {
6978 X86_64Assembler* assembler = codegen_->GetAssembler();
6979
6980 // Ensure that the reference to the jump table has the correct offset.
6981 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6982 SetOffset(offset_in_constant_table);
6983
6984 // Compute the offset from the start of the function to this jump table.
6985 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6986
6987 // Populate the jump table with the correct values for the jump table.
6988 int32_t num_entries = switch_instr_->GetNumEntries();
6989 HBasicBlock* block = switch_instr_->GetBlock();
6990 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6991 // The value that we want is the target offset - the position of the table.
6992 for (int32_t i = 0; i < num_entries; i++) {
6993 HBasicBlock* b = successors[i];
6994 Label* l = codegen_->GetLabelOf(b);
6995 DCHECK(l->IsBound());
6996 int32_t offset_to_block = l->Position() - current_table_offset;
6997 assembler->AppendInt32(offset_to_block);
6998 }
6999 }
7000
7001 private:
7002 const HPackedSwitch* switch_instr_;
7003};
7004
Mark Mendellf55c3e02015-03-26 21:07:46 -04007005void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7006 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007007 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007008 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7009 // 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 -04007010 assembler->Align(4, 0);
7011 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007012
7013 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007014 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007015 jump_table->CreateJumpTable();
7016 }
7017
7018 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007019 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007020 }
7021
7022 // And finish up.
7023 CodeGenerator::Finalize(allocator);
7024}
7025
Mark Mendellf55c3e02015-03-26 21:07:46 -04007026Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7027 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7028 return Address::RIP(fixup);
7029}
7030
7031Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7032 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7033 return Address::RIP(fixup);
7034}
7035
7036Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7037 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7038 return Address::RIP(fixup);
7039}
7040
7041Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7042 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7043 return Address::RIP(fixup);
7044}
7045
Andreas Gampe85b62f22015-09-09 13:15:38 -07007046// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007047void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007048 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007049 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007050 return;
7051 }
7052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007053 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007054
7055 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7056 if (trg.Equals(return_loc)) {
7057 return;
7058 }
7059
7060 // Let the parallel move resolver take care of all of this.
7061 HParallelMove parallel_move(GetGraph()->GetArena());
7062 parallel_move.AddMove(return_loc, trg, type, nullptr);
7063 GetMoveResolver()->EmitNativeCode(&parallel_move);
7064}
7065
Mark Mendell9c86b482015-09-18 13:36:07 -04007066Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7067 // Create a fixup to be used to create and address the jump table.
7068 JumpTableRIPFixup* table_fixup =
7069 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7070
7071 // We have to populate the jump tables.
7072 fixups_to_jump_tables_.push_back(table_fixup);
7073 return Address::RIP(table_fixup);
7074}
7075
Mark Mendellea5af682015-10-22 17:35:49 -04007076void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7077 const Address& addr_high,
7078 int64_t v,
7079 HInstruction* instruction) {
7080 if (IsInt<32>(v)) {
7081 int32_t v_32 = v;
7082 __ movq(addr_low, Immediate(v_32));
7083 MaybeRecordImplicitNullCheck(instruction);
7084 } else {
7085 // Didn't fit in a register. Do it in pieces.
7086 int32_t low_v = Low32Bits(v);
7087 int32_t high_v = High32Bits(v);
7088 __ movl(addr_low, Immediate(low_v));
7089 MaybeRecordImplicitNullCheck(instruction);
7090 __ movl(addr_high, Immediate(high_v));
7091 }
7092}
7093
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007094void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7095 const uint8_t* roots_data,
7096 const PatchInfo<Label>& info,
7097 uint64_t index_in_table) const {
7098 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7099 uintptr_t address =
7100 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7101 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7102 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7103 dchecked_integral_cast<uint32_t>(address);
7104}
7105
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007106void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7107 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007108 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007109 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007110 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007111 uint64_t index_in_table = it->second;
7112 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007113 }
7114
7115 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007116 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007117 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7118 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007119 uint64_t index_in_table = it->second;
7120 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007121 }
7122}
7123
Roland Levillain4d027112015-07-01 15:41:14 +01007124#undef __
7125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007126} // namespace x86_64
7127} // namespace art