blob: 781f2724a5f748c666ada3449261cf2f8595a2e0 [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"
Vladimir Markoeebb8212018-06-05 14:57:24 +010025#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027#include "intrinsics.h"
28#include "intrinsics_x86_64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010029#include "linker/linker_patch.h"
Andreas Gamped4901292017-05-30 18:41:34 -070030#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070031#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033#include "mirror/object_reference.h"
34#include "thread.h"
35#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010037#include "utils/x86_64/assembler_x86_64.h"
38#include "utils/x86_64/managed_register_x86_64.h"
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace art {
41
Roland Levillain0d5a2812015-11-13 10:07:31 +000042template<class MirrorType>
43class GcRoot;
44
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010045namespace x86_64 {
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000049// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
50// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
51// generates less code/data with a small num_entries.
52static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010053
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000054static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000055static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Mark Mendell24f2dfa2015-01-14 19:51:45 -050057static constexpr int kC2ConditionMask = 0x400;
58
Vladimir Marko3232dbb2018-07-25 15:42:46 +010059static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
60 // Custom calling convention: RAX serves as both input and output.
61 RegisterSet caller_saves = RegisterSet::Empty();
62 caller_saves.Add(Location::RegisterLocation(RAX));
63 return caller_saves;
64}
65
Roland Levillain7cbd27f2016-08-11 23:53:33 +010066// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
67#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070068#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
Andreas Gampe85b62f22015-09-09 13:15:38 -070070class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000072 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010074 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +000075 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000077 if (instruction_->CanThrowIntoCatchBlock()) {
78 // Live registers will be restored in the catch block if caught.
79 SaveLiveRegisters(codegen, instruction_->GetLocations());
80 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010081 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000082 instruction_,
83 instruction_->GetDexPc(),
84 this);
Roland Levillain888d0672015-11-23 18:53:50 +000085 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010088 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010090 const char* GetDescription() const override { return "NullCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010091
Nicolas Geoffraye5038322014-07-04 09:41:32 +010092 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
94};
95
Andreas Gampe85b62f22015-09-09 13:15:38 -070096class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000097 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000098 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100100 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000101 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000102 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100103 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000104 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000105 }
106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100107 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100109 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100110
Calin Juravled0d48522014-11-04 16:40:20 +0000111 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000112 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
113};
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000116 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100117 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, DataType::Type type, bool is_div)
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100120 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000121 __ Bind(GetEntryLabel());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 if (type_ == DataType::Type::kInt32) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 if (is_div_) {
124 __ negl(cpu_reg_);
125 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400126 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 }
128
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000129 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130 DCHECK_EQ(DataType::Type::kInt64, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000131 if (is_div_) {
132 __ negq(cpu_reg_);
133 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400134 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000136 }
Calin Juravled0d48522014-11-04 16:40:20 +0000137 __ jmp(GetExitLabel());
138 }
139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100140 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100141
Calin Juravled0d48522014-11-04 16:40:20 +0000142 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000143 const CpuRegister cpu_reg_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100144 const DataType::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000145 const bool is_div_;
146 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000147};
148
Andreas Gampe85b62f22015-09-09 13:15:38 -0700149class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100151 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000152 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100154 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700155 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000156 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700158 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100159 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000160 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700161 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 if (successor_ == nullptr) {
163 __ jmp(GetReturnLabel());
164 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000165 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 }
168
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100169 Label* GetReturnLabel() {
170 DCHECK(successor_ == nullptr);
171 return &return_label_;
172 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100174 HBasicBlock* GetSuccessor() const {
175 return successor_;
176 }
177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100178 const char* GetDescription() const override { return "SuspendCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100179
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100181 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182 Label return_label_;
183
184 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
185};
186
Andreas Gampe85b62f22015-09-09 13:15:38 -0700187class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100189 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000190 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100192 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100193 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000194 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100195 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000196 if (instruction_->CanThrowIntoCatchBlock()) {
197 // Live registers will be restored in the catch block if caught.
198 SaveLiveRegisters(codegen, instruction_->GetLocations());
199 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400200 // Are we using an array length from memory?
201 HInstruction* array_length = instruction_->InputAt(1);
202 Location length_loc = locations->InAt(1);
203 InvokeRuntimeCallingConvention calling_convention;
204 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
205 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100206 HArrayLength* length = array_length->AsArrayLength();
Nicolas Geoffray003444a2017-10-17 10:58:42 +0100207 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400208 Location array_loc = array_length->GetLocations()->InAt(0);
209 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
210 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
211 // Check for conflicts with index.
212 if (length_loc.Equals(locations->InAt(0))) {
213 // We know we aren't using parameter 2.
214 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
215 }
216 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100217 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100218 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700219 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400220 }
221
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000222 // We're moving two locations to locations that could overlap, so we need a parallel
223 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000224 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000226 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100227 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400228 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100229 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100230 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100231 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
232 ? kQuickThrowStringBounds
233 : kQuickThrowArrayBounds;
234 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100235 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000236 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100237 }
238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100239 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100241 const char* GetDescription() const override { return "BoundsCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100242
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100243 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100244 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
245};
246
Andreas Gampe85b62f22015-09-09 13:15:38 -0700247class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100249 LoadClassSlowPathX86_64(HLoadClass* cls, HInstruction* at)
250 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100252 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100255 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000256 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100257 Location out = locations->Out();
258 const uint32_t dex_pc = instruction_->GetDexPc();
259 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
260 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
261
Roland Levillain0d5a2812015-11-13 10:07:31 +0000262 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100263 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265
Vladimir Markoea4c1262017-02-06 19:59:33 +0000266 // Custom calling convention: RAX serves as both input and output.
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100267 if (must_resolve_type) {
268 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_64_codegen->GetGraph()->GetDexFile()));
269 dex::TypeIndex type_index = cls_->GetTypeIndex();
270 __ movl(CpuRegister(RAX), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100271 x86_64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
272 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100273 // If we also must_do_clinit, the resolved type is now in the correct register.
274 } else {
275 DCHECK(must_do_clinit);
276 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
277 x86_64_codegen->Move(Location::RegisterLocation(RAX), source);
278 }
279 if (must_do_clinit) {
280 x86_64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000282 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100283
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000284 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 if (out.IsValid()) {
286 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000287 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000288 }
289
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100291 __ jmp(GetExitLabel());
292 }
293
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100294 const char* GetDescription() const override { return "LoadClassSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100295
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100296 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000297 // The class this slow path will load.
298 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100299
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000300 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100301};
302
Vladimir Markoaad75c62016-10-03 08:46:48 +0000303class LoadStringSlowPathX86_64 : public SlowPathCode {
304 public:
305 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100307 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308 LocationSummary* locations = instruction_->GetLocations();
309 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
310
311 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
312 __ Bind(GetEntryLabel());
313 SaveLiveRegisters(codegen, locations);
314
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000315 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100316 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000317 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000318 x86_64_codegen->InvokeRuntime(kQuickResolveString,
319 instruction_,
320 instruction_->GetDexPc(),
321 this);
322 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
323 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
324 RestoreLiveRegisters(codegen, locations);
325
Vladimir Markoaad75c62016-10-03 08:46:48 +0000326 __ jmp(GetExitLabel());
327 }
328
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100329 const char* GetDescription() const override { return "LoadStringSlowPathX86_64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000330
331 private:
332 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
333};
334
Andreas Gampe85b62f22015-09-09 13:15:38 -0700335class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000338 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100340 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100342 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 DCHECK(instruction_->IsCheckCast()
344 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Roland Levillain0d5a2812015-11-13 10:07:31 +0000346 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000349 if (kPoisonHeapReferences &&
350 instruction_->IsCheckCast() &&
351 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
352 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
353 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<CpuRegister>());
354 }
355
Vladimir Marko87584542017-12-12 17:47:52 +0000356 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 SaveLiveRegisters(codegen, locations);
358 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359
360 // We're moving two locations to locations that could overlap, so we need a parallel
361 // move resolver.
362 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800364 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800366 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800367 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100368 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100370 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800371 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000372 } else {
373 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800374 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
375 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000376 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000377
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000378 if (!is_fatal_) {
379 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000380 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000381 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000382
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000383 RestoreLiveRegisters(codegen, locations);
384 __ jmp(GetExitLabel());
385 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386 }
387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100388 const char* GetDescription() const override { return "TypeCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100390 bool IsFatal() const override { return is_fatal_; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000391
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000392 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000393 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000394
395 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
396};
397
Andreas Gampe85b62f22015-09-09 13:15:38 -0700398class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 public:
Aart Bik42249c32016-01-07 15:33:50 -0800400 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000401 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100403 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000404 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700405 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100406 LocationSummary* locations = instruction_->GetLocations();
407 SaveLiveRegisters(codegen, locations);
408 InvokeRuntimeCallingConvention calling_convention;
409 x86_64_codegen->Load32BitValue(
410 CpuRegister(calling_convention.GetRegisterAt(0)),
411 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100412 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100413 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700414 }
415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100416 const char* GetDescription() const override { return "DeoptimizationSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100417
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700418 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700419 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
420};
421
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422class ArraySetSlowPathX86_64 : public SlowPathCode {
423 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000424 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100426 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 LocationSummary* locations = instruction_->GetLocations();
428 __ Bind(GetEntryLabel());
429 SaveLiveRegisters(codegen, locations);
430
431 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100432 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 parallel_move.AddMove(
434 locations->InAt(0),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100436 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(1),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100441 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100442 nullptr);
443 parallel_move.AddMove(
444 locations->InAt(2),
445 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100446 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100447 nullptr);
448 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
449
Roland Levillain0d5a2812015-11-13 10:07:31 +0000450 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100451 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000452 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100453 RestoreLiveRegisters(codegen, locations);
454 __ jmp(GetExitLabel());
455 }
456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100457 const char* GetDescription() const override { return "ArraySetSlowPathX86_64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100458
459 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100460 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
461};
462
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100463// Slow path marking an object reference `ref` during a read
464// barrier. The field `obj.field` in the object `obj` holding this
465// reference does not get updated by this slow path after marking (see
466// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
467//
468// This means that after the execution of this slow path, `ref` will
469// always be up-to-date, but `obj.field` may not; i.e., after the
470// flip, `ref` will be a to-space reference, but `obj.field` will
471// probably still be a from-space reference (unless it gets updated by
472// another thread, or if another thread installed another object
473// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000474class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
475 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100476 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
477 Location ref,
478 bool unpoison_ref_before_marking)
479 : SlowPathCode(instruction),
480 ref_(ref),
481 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000482 DCHECK(kEmitCompilerReadBarrier);
483 }
484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100485 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86_64"; }
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100487 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000488 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
490 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100492 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000493 DCHECK(instruction_->IsInstanceFieldGet() ||
494 instruction_->IsStaticFieldGet() ||
495 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100496 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000497 instruction_->IsLoadClass() ||
498 instruction_->IsLoadString() ||
499 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100500 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100501 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
502 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000503 << "Unexpected instruction in read barrier marking slow path: "
504 << instruction_->DebugName();
505
506 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000508 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100509 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000510 }
Roland Levillain4359e612016-07-20 11:32:19 +0100511 // No need to save live registers; it's taken care of by the
512 // entrypoint. Also, there is no need to update the stack mask,
513 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000514 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 DCHECK_NE(ref_reg, RSP);
516 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // "Compact" slow path, saving two moves.
518 //
519 // Instead of using the standard runtime calling convention (input
520 // and output in R0):
521 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100522 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100523 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100524 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100525 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100526 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100527 // of a dedicated entrypoint:
528 //
529 // rX <- ReadBarrierMarkRegX(rX)
530 //
531 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100532 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100533 // This runtime call does not require a stack map.
534 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000535 __ jmp(GetExitLabel());
536 }
537
538 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100539 // The location (register) of the marked object reference.
540 const Location ref_;
541 // Should the reference in `ref_` be unpoisoned prior to marking it?
542 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000543
544 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
545};
546
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100547// Slow path marking an object reference `ref` during a read barrier,
548// and if needed, atomically updating the field `obj.field` in the
549// object `obj` holding this reference after marking (contrary to
550// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
551// `obj.field`).
552//
553// This means that after the execution of this slow path, both `ref`
554// and `obj.field` will be up-to-date; i.e., after the flip, both will
555// hold the same to-space reference (unless another thread installed
556// another object reference (different from `ref`) in `obj.field`).
557class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
558 public:
559 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
560 Location ref,
561 CpuRegister obj,
562 const Address& field_addr,
563 bool unpoison_ref_before_marking,
564 CpuRegister temp1,
565 CpuRegister temp2)
566 : SlowPathCode(instruction),
567 ref_(ref),
568 obj_(obj),
569 field_addr_(field_addr),
570 unpoison_ref_before_marking_(unpoison_ref_before_marking),
571 temp1_(temp1),
572 temp2_(temp2) {
573 DCHECK(kEmitCompilerReadBarrier);
574 }
575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100576 const char* GetDescription() const override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100577 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
578 }
579
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100580 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100581 LocationSummary* locations = instruction_->GetLocations();
582 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
583 Register ref_reg = ref_cpu_reg.AsRegister();
584 DCHECK(locations->CanCall());
585 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
586 // This slow path is only used by the UnsafeCASObject intrinsic.
587 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
588 << "Unexpected instruction in read barrier marking and field updating slow path: "
589 << instruction_->DebugName();
590 DCHECK(instruction_->GetLocations()->Intrinsified());
591 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
592
593 __ Bind(GetEntryLabel());
594 if (unpoison_ref_before_marking_) {
595 // Object* ref = ref_addr->AsMirrorPtr()
596 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
597 }
598
599 // Save the old (unpoisoned) reference.
600 __ movl(temp1_, ref_cpu_reg);
601
602 // No need to save live registers; it's taken care of by the
603 // entrypoint. Also, there is no need to update the stack mask,
604 // as this runtime call will not trigger a garbage collection.
605 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
606 DCHECK_NE(ref_reg, RSP);
607 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
608 // "Compact" slow path, saving two moves.
609 //
610 // Instead of using the standard runtime calling convention (input
611 // and output in R0):
612 //
613 // RDI <- ref
614 // RAX <- ReadBarrierMark(RDI)
615 // ref <- RAX
616 //
617 // we just use rX (the register containing `ref`) as input and output
618 // of a dedicated entrypoint:
619 //
620 // rX <- ReadBarrierMarkRegX(rX)
621 //
622 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100623 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100624 // This runtime call does not require a stack map.
625 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
626
627 // If the new reference is different from the old reference,
628 // update the field in the holder (`*field_addr`).
629 //
630 // Note that this field could also hold a different object, if
631 // another thread had concurrently changed it. In that case, the
632 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
633 // operation below would abort the CAS, leaving the field as-is.
634 NearLabel done;
635 __ cmpl(temp1_, ref_cpu_reg);
636 __ j(kEqual, &done);
637
638 // Update the the holder's field atomically. This may fail if
639 // mutator updates before us, but it's OK. This is achived
640 // using a strong compare-and-set (CAS) operation with relaxed
641 // memory synchronization ordering, where the expected value is
642 // the old reference and the desired value is the new reference.
643 // This operation is implemented with a 32-bit LOCK CMPXLCHG
644 // instruction, which requires the expected value (the old
645 // reference) to be in EAX. Save RAX beforehand, and move the
646 // expected value (stored in `temp1_`) into EAX.
647 __ movq(temp2_, CpuRegister(RAX));
648 __ movl(CpuRegister(RAX), temp1_);
649
650 // Convenience aliases.
651 CpuRegister base = obj_;
652 CpuRegister expected = CpuRegister(RAX);
653 CpuRegister value = ref_cpu_reg;
654
655 bool base_equals_value = (base.AsRegister() == value.AsRegister());
656 Register value_reg = ref_reg;
657 if (kPoisonHeapReferences) {
658 if (base_equals_value) {
659 // If `base` and `value` are the same register location, move
660 // `value_reg` to a temporary register. This way, poisoning
661 // `value_reg` won't invalidate `base`.
662 value_reg = temp1_.AsRegister();
663 __ movl(CpuRegister(value_reg), base);
664 }
665
666 // Check that the register allocator did not assign the location
667 // of `expected` (RAX) to `value` nor to `base`, so that heap
668 // poisoning (when enabled) works as intended below.
669 // - If `value` were equal to `expected`, both references would
670 // be poisoned twice, meaning they would not be poisoned at
671 // all, as heap poisoning uses address negation.
672 // - If `base` were equal to `expected`, poisoning `expected`
673 // would invalidate `base`.
674 DCHECK_NE(value_reg, expected.AsRegister());
675 DCHECK_NE(base.AsRegister(), expected.AsRegister());
676
677 __ PoisonHeapReference(expected);
678 __ PoisonHeapReference(CpuRegister(value_reg));
679 }
680
681 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
682
683 // If heap poisoning is enabled, we need to unpoison the values
684 // that were poisoned earlier.
685 if (kPoisonHeapReferences) {
686 if (base_equals_value) {
687 // `value_reg` has been moved to a temporary register, no need
688 // to unpoison it.
689 } else {
690 __ UnpoisonHeapReference(CpuRegister(value_reg));
691 }
692 // No need to unpoison `expected` (RAX), as it is be overwritten below.
693 }
694
695 // Restore RAX.
696 __ movq(CpuRegister(RAX), temp2_);
697
698 __ Bind(&done);
699 __ jmp(GetExitLabel());
700 }
701
702 private:
703 // The location (register) of the marked object reference.
704 const Location ref_;
705 // The register containing the object holding the marked object reference field.
706 const CpuRegister obj_;
707 // The address of the marked reference field. The base of this address must be `obj_`.
708 const Address field_addr_;
709
710 // Should the reference in `ref_` be unpoisoned prior to marking it?
711 const bool unpoison_ref_before_marking_;
712
713 const CpuRegister temp1_;
714 const CpuRegister temp2_;
715
716 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
717};
718
Roland Levillain0d5a2812015-11-13 10:07:31 +0000719// Slow path generating a read barrier for a heap reference.
720class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
721 public:
722 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
723 Location out,
724 Location ref,
725 Location obj,
726 uint32_t offset,
727 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000728 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000729 out_(out),
730 ref_(ref),
731 obj_(obj),
732 offset_(offset),
733 index_(index) {
734 DCHECK(kEmitCompilerReadBarrier);
735 // If `obj` is equal to `out` or `ref`, it means the initial
736 // object has been overwritten by (or after) the heap object
737 // reference load to be instrumented, e.g.:
738 //
739 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000740 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000741 //
742 // In that case, we have lost the information about the original
743 // object, and the emitted read barrier cannot work properly.
744 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
745 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
746}
747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100748 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000749 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
750 LocationSummary* locations = instruction_->GetLocations();
751 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
752 DCHECK(locations->CanCall());
753 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100754 DCHECK(instruction_->IsInstanceFieldGet() ||
755 instruction_->IsStaticFieldGet() ||
756 instruction_->IsArrayGet() ||
757 instruction_->IsInstanceOf() ||
758 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700759 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000760 << "Unexpected instruction in read barrier for heap reference slow path: "
761 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000762
763 __ Bind(GetEntryLabel());
764 SaveLiveRegisters(codegen, locations);
765
766 // We may have to change the index's value, but as `index_` is a
767 // constant member (like other "inputs" of this slow path),
768 // introduce a copy of it, `index`.
769 Location index = index_;
770 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100771 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000772 if (instruction_->IsArrayGet()) {
773 // Compute real offset and store it in index_.
774 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
775 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
776 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
777 // We are about to change the value of `index_reg` (see the
778 // calls to art::x86_64::X86_64Assembler::shll and
779 // art::x86_64::X86_64Assembler::AddImmediate below), but it
780 // has not been saved by the previous call to
781 // art::SlowPathCode::SaveLiveRegisters, as it is a
782 // callee-save register --
783 // art::SlowPathCode::SaveLiveRegisters does not consider
784 // callee-save registers, as it has been designed with the
785 // assumption that callee-save registers are supposed to be
786 // handled by the called function. So, as a callee-save
787 // register, `index_reg` _would_ eventually be saved onto
788 // the stack, but it would be too late: we would have
789 // changed its value earlier. Therefore, we manually save
790 // it here into another freely available register,
791 // `free_reg`, chosen of course among the caller-save
792 // registers (as a callee-save `free_reg` register would
793 // exhibit the same problem).
794 //
795 // Note we could have requested a temporary register from
796 // the register allocator instead; but we prefer not to, as
797 // this is a slow path, and we know we can find a
798 // caller-save register that is available.
799 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
800 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
801 index_reg = free_reg;
802 index = Location::RegisterLocation(index_reg);
803 } else {
804 // The initial register stored in `index_` has already been
805 // saved in the call to art::SlowPathCode::SaveLiveRegisters
806 // (as it is not a callee-save register), so we can freely
807 // use it.
808 }
809 // Shifting the index value contained in `index_reg` by the
810 // scale factor (2) cannot overflow in practice, as the
811 // runtime is unable to allocate object arrays with a size
812 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
813 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
814 static_assert(
815 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
816 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
817 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
818 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100819 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
820 // intrinsics, `index_` is not shifted by a scale factor of 2
821 // (as in the case of ArrayGet), as it is actually an offset
822 // to an object field within an object.
823 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000824 DCHECK(instruction_->GetLocations()->Intrinsified());
825 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
826 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
827 << instruction_->AsInvoke()->GetIntrinsic();
828 DCHECK_EQ(offset_, 0U);
829 DCHECK(index_.IsRegister());
830 }
831 }
832
833 // We're moving two or three locations to locations that could
834 // overlap, so we need a parallel move resolver.
835 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100836 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000837 parallel_move.AddMove(ref_,
838 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 nullptr);
841 parallel_move.AddMove(obj_,
842 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100843 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000844 nullptr);
845 if (index.IsValid()) {
846 parallel_move.AddMove(index,
847 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100848 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849 nullptr);
850 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
851 } else {
852 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
853 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
854 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100855 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000856 instruction_,
857 instruction_->GetDexPc(),
858 this);
859 CheckEntrypointTypes<
860 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
861 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
862
863 RestoreLiveRegisters(codegen, locations);
864 __ jmp(GetExitLabel());
865 }
866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100867 const char* GetDescription() const override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 return "ReadBarrierForHeapReferenceSlowPathX86_64";
869 }
870
871 private:
872 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
873 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
874 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
875 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
876 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
877 return static_cast<CpuRegister>(i);
878 }
879 }
880 // We shall never fail to find a free caller-save register, as
881 // there are more than two core caller-save registers on x86-64
882 // (meaning it is possible to find one which is different from
883 // `ref` and `obj`).
884 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
885 LOG(FATAL) << "Could not find a free caller-save register";
886 UNREACHABLE();
887 }
888
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889 const Location out_;
890 const Location ref_;
891 const Location obj_;
892 const uint32_t offset_;
893 // An additional location containing an index to an array.
894 // Only used for HArrayGet and the UnsafeGetObject &
895 // UnsafeGetObjectVolatile intrinsics.
896 const Location index_;
897
898 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
899};
900
901// Slow path generating a read barrier for a GC root.
902class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
903 public:
904 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000905 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000906 DCHECK(kEmitCompilerReadBarrier);
907 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000908
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100909 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000910 LocationSummary* locations = instruction_->GetLocations();
911 DCHECK(locations->CanCall());
912 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000913 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
914 << "Unexpected instruction in read barrier for GC root slow path: "
915 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000916
917 __ Bind(GetEntryLabel());
918 SaveLiveRegisters(codegen, locations);
919
920 InvokeRuntimeCallingConvention calling_convention;
921 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
922 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100923 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000924 instruction_,
925 instruction_->GetDexPc(),
926 this);
927 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
928 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
929
930 RestoreLiveRegisters(codegen, locations);
931 __ jmp(GetExitLabel());
932 }
933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100934 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86_64"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000935
936 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000937 const Location out_;
938 const Location root_;
939
940 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
941};
942
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100943#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100944// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
945#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100946
Roland Levillain4fa13f62015-07-06 18:11:54 +0100947inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700948 switch (cond) {
949 case kCondEQ: return kEqual;
950 case kCondNE: return kNotEqual;
951 case kCondLT: return kLess;
952 case kCondLE: return kLessEqual;
953 case kCondGT: return kGreater;
954 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700955 case kCondB: return kBelow;
956 case kCondBE: return kBelowEqual;
957 case kCondA: return kAbove;
958 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700959 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 LOG(FATAL) << "Unreachable";
961 UNREACHABLE();
962}
963
Aart Bike9f37602015-10-09 11:15:55 -0700964// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100965inline Condition X86_64FPCondition(IfCondition cond) {
966 switch (cond) {
967 case kCondEQ: return kEqual;
968 case kCondNE: return kNotEqual;
969 case kCondLT: return kBelow;
970 case kCondLE: return kBelowEqual;
971 case kCondGT: return kAbove;
972 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700973 default: break; // should not happen
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800974 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100975 LOG(FATAL) << "Unreachable";
976 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700977}
978
Vladimir Markodc151b22015-10-15 18:02:30 +0100979HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
980 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100981 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000982 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100983}
984
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100985void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
986 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800987 // All registers are assumed to be correctly set up.
Vladimir Marko4ee8e292017-06-02 15:39:30 +0000988
Vladimir Marko58155012015-08-19 12:49:41 +0000989 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
990 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100991 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000992 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100993 uint32_t offset =
994 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
995 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000996 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100997 }
Vladimir Marko58155012015-08-19 12:49:41 +0000998 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000999 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00001000 break;
Vladimir Marko65979462017-05-19 17:25:12 +01001001 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
1002 DCHECK(GetCompilerOptions().IsBootImage());
1003 __ leal(temp.AsRegister<CpuRegister>(),
1004 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001005 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01001006 break;
Vladimir Markob066d432018-01-03 13:14:37 +00001007 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
1008 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1009 __ movl(temp.AsRegister<CpuRegister>(),
1010 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001011 RecordBootImageRelRoPatch(GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00001012 break;
1013 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001014 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001015 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001016 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001017 RecordMethodBssEntryPatch(invoke);
Vladimir Marko58155012015-08-19 12:49:41 +00001018 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001019 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001020 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
1021 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
1022 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001023 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1024 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1025 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001026 }
Vladimir Marko58155012015-08-19 12:49:41 +00001027 }
1028
1029 switch (invoke->GetCodePtrLocation()) {
1030 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1031 __ call(&frame_entry_label_);
1032 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001033 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1034 // (callee_method + offset_of_quick_compiled_code)()
1035 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1036 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001037 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001038 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001039 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001040 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001041
1042 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001043}
1044
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001045void CodeGeneratorX86_64::GenerateVirtualCall(
1046 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001047 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1048 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1049 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001050
1051 // Use the calling convention instead of the location of the receiver, as
1052 // intrinsics may have put the receiver in a different register. In the intrinsics
1053 // slow path, the arguments have been moved to the right place, so here we are
1054 // guaranteed that the receiver is the first register of the calling convention.
1055 InvokeDexCallingConvention calling_convention;
1056 Register receiver = calling_convention.GetRegisterAt(0);
1057
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001058 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001059 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001060 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001061 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001062 // Instead of simply (possibly) unpoisoning `temp` here, we should
1063 // emit a read barrier for the previous class reference load.
1064 // However this is not required in practice, as this is an
1065 // intermediate/temporary reference and because the current
1066 // concurrent copying collector keeps the from-space memory
1067 // intact/accessible until the end of the marking phase (the
1068 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001069 __ MaybeUnpoisonHeapReference(temp);
1070 // temp = temp->GetMethodAt(method_offset);
1071 __ movq(temp, Address(temp, method_offset));
1072 // call temp->GetEntryPoint();
1073 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001074 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001075 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001076}
1077
Vladimir Marko6fd16062018-06-26 11:02:04 +01001078void CodeGeneratorX86_64::RecordBootImageIntrinsicPatch(uint32_t intrinsic_data) {
1079 boot_image_intrinsic_patches_.emplace_back(/* target_dex_file */ nullptr, intrinsic_data);
1080 __ Bind(&boot_image_intrinsic_patches_.back().label);
1081}
1082
Vladimir Markob066d432018-01-03 13:14:37 +00001083void CodeGeneratorX86_64::RecordBootImageRelRoPatch(uint32_t boot_image_offset) {
1084 boot_image_method_patches_.emplace_back(/* target_dex_file */ nullptr, boot_image_offset);
1085 __ Bind(&boot_image_method_patches_.back().label);
1086}
1087
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001088void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
1089 boot_image_method_patches_.emplace_back(
1090 invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001091 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001092}
1093
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001094void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
1095 method_bss_entry_patches_.emplace_back(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
1096 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001097}
1098
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001099void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1100 boot_image_type_patches_.emplace_back(
1101 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001102 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001103}
1104
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001105Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001106 type_bss_entry_patches_.emplace_back(
1107 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001108 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001109}
1110
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001111void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1112 boot_image_string_patches_.emplace_back(
1113 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1114 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001115}
1116
Vladimir Markoaad75c62016-10-03 08:46:48 +00001117Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1118 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001119 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001120 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001121 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001122}
1123
Vladimir Marko6fd16062018-06-26 11:02:04 +01001124void CodeGeneratorX86_64::LoadBootImageAddress(CpuRegister reg, uint32_t boot_image_reference) {
1125 if (GetCompilerOptions().IsBootImage()) {
1126 __ leal(reg, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
1127 RecordBootImageIntrinsicPatch(boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01001128 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01001129 __ movl(reg, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko6fd16062018-06-26 11:02:04 +01001130 RecordBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001131 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001132 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001133 gc::Heap* heap = Runtime::Current()->GetHeap();
1134 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001135 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001136 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
1137 }
1138}
1139
Vladimir Marko6fd16062018-06-26 11:02:04 +01001140void CodeGeneratorX86_64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1141 uint32_t boot_image_offset) {
1142 DCHECK(invoke->IsStatic());
1143 InvokeRuntimeCallingConvention calling_convention;
1144 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
1145 if (GetCompilerOptions().IsBootImage()) {
1146 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1147 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1148 __ leal(argument,
1149 Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
1150 MethodReference target_method = invoke->GetTargetMethod();
1151 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1152 boot_image_type_patches_.emplace_back(target_method.dex_file, type_idx.index_);
1153 __ Bind(&boot_image_type_patches_.back().label);
1154 } else {
1155 LoadBootImageAddress(argument, boot_image_offset);
1156 }
1157 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1158 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1159}
1160
Vladimir Markoaad75c62016-10-03 08:46:48 +00001161// The label points to the end of the "movl" or another instruction but the literal offset
1162// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1163constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1164
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001165template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001166inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1167 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001168 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001169 for (const PatchInfo<Label>& info : infos) {
1170 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1171 linker_patches->push_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001172 Factory(literal_offset, info.target_dex_file, info.label.Position(), info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001173 }
1174}
1175
Vladimir Marko6fd16062018-06-26 11:02:04 +01001176template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1177linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1178 const DexFile* target_dex_file,
1179 uint32_t pc_insn_offset,
1180 uint32_t boot_image_offset) {
1181 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1182 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001183}
1184
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001185void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001186 DCHECK(linker_patches->empty());
1187 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001188 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001189 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001190 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001191 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001192 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001193 string_bss_entry_patches_.size() +
1194 boot_image_intrinsic_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001195 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001196 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001197 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1198 boot_image_method_patches_, linker_patches);
1199 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1200 boot_image_type_patches_, linker_patches);
1201 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001202 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001203 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1204 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001205 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001206 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001207 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001208 DCHECK(boot_image_type_patches_.empty());
1209 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001210 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001211 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001212 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1213 method_bss_entry_patches_, linker_patches);
1214 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1215 type_bss_entry_patches_, linker_patches);
1216 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1217 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001218 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001219}
1220
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001222 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001223}
1224
1225void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001226 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001227}
1228
Vladimir Markoa0431112018-06-25 09:32:54 +01001229const X86_64InstructionSetFeatures& CodeGeneratorX86_64::GetInstructionSetFeatures() const {
1230 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86_64InstructionSetFeatures();
1231}
1232
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001233size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1234 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1235 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001236}
1237
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001238size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1239 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1240 return kX86_64WordSize;
1241}
1242
1243size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001244 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001245 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001246 } else {
1247 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1248 }
1249 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001250}
1251
1252size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001253 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001254 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001255 } else {
1256 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1257 }
1258 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001259}
1260
Calin Juravle175dc732015-08-25 15:42:32 +01001261void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1262 HInstruction* instruction,
1263 uint32_t dex_pc,
1264 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001265 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001266 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1267 if (EntrypointRequiresStackMap(entrypoint)) {
1268 RecordPcInfo(instruction, dex_pc, slow_path);
1269 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001270}
1271
Roland Levillaindec8f632016-07-22 17:10:06 +01001272void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1273 HInstruction* instruction,
1274 SlowPathCode* slow_path) {
1275 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001276 GenerateInvokeRuntime(entry_point_offset);
1277}
1278
1279void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001280 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1281}
1282
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001283static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001284// Use a fake return address register to mimic Quick.
1285static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001286CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001287 const CompilerOptions& compiler_options,
1288 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001289 : CodeGenerator(graph,
1290 kNumberOfCpuRegisters,
1291 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001292 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001293 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1294 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001295 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001296 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1297 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001298 compiler_options,
1299 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001300 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001301 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001302 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001303 move_resolver_(graph->GetAllocator(), this),
1304 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001305 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001306 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1307 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1308 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1309 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001310 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001311 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001312 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001313 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1314 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1315 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001316 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1317}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001318
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001319InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1320 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001321 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001322 assembler_(codegen->GetAssembler()),
1323 codegen_(codegen) {}
1324
David Brazdil58282f42016-01-14 12:45:10 +00001325void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001326 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001327 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001328
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001329 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001330 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001331}
1332
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001333static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001334 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001335}
David Srbecky9d8606d2015-04-12 09:35:32 +01001336
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001337static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001338 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001339}
1340
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001341void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001342 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001343 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001344 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001345 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001346 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001347
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001348 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1349 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1350 ArtMethod::HotnessCountOffset().Int32Value()),
1351 Immediate(1));
1352 }
1353
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001354 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001355 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1356 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001357 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001358 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001359
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001360 if (HasEmptyFrame()) {
1361 return;
1362 }
1363
Nicolas Geoffray98893962015-01-21 12:32:32 +00001364 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001365 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001366 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001367 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001368 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1369 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001370 }
1371 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001372
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001373 int adjust = GetFrameSize() - GetCoreSpillSize();
1374 __ subq(CpuRegister(RSP), Immediate(adjust));
1375 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001376 uint32_t xmm_spill_location = GetFpuSpillStart();
1377 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001378
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001379 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1380 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001381 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1382 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1383 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001384 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001385 }
1386
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001387 // Save the current method if we need it. Note that we do not
1388 // do this in HCurrentMethod, as the instruction might have been removed
1389 // in the SSA graph.
1390 if (RequiresCurrentMethod()) {
1391 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1392 CpuRegister(kMethodRegisterArgument));
1393 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001394
1395 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1396 // Initialize should_deoptimize flag to 0.
1397 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1398 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001399}
1400
1401void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001402 __ cfi().RememberState();
1403 if (!HasEmptyFrame()) {
1404 uint32_t xmm_spill_location = GetFpuSpillStart();
1405 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1406 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1407 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1408 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1409 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1410 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1411 }
1412 }
1413
1414 int adjust = GetFrameSize() - GetCoreSpillSize();
1415 __ addq(CpuRegister(RSP), Immediate(adjust));
1416 __ cfi().AdjustCFAOffset(-adjust);
1417
1418 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1419 Register reg = kCoreCalleeSaves[i];
1420 if (allocated_registers_.ContainsCoreRegister(reg)) {
1421 __ popq(CpuRegister(reg));
1422 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1423 __ cfi().Restore(DWARFReg(reg));
1424 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001425 }
1426 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001427 __ ret();
1428 __ cfi().RestoreState();
1429 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001430}
1431
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001432void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1433 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001434}
1435
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001436void CodeGeneratorX86_64::Move(Location destination, Location source) {
1437 if (source.Equals(destination)) {
1438 return;
1439 }
1440 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001441 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001442 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001443 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001444 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001445 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001446 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001447 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1448 } else if (source.IsConstant()) {
1449 HConstant* constant = source.GetConstant();
1450 if (constant->IsLongConstant()) {
1451 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1452 } else {
1453 Load32BitValue(dest, GetInt32ValueOf(constant));
1454 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455 } else {
1456 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001457 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001458 }
1459 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001460 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001462 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001463 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001464 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1465 } else if (source.IsConstant()) {
1466 HConstant* constant = source.GetConstant();
1467 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1468 if (constant->IsFloatConstant()) {
1469 Load32BitValue(dest, static_cast<int32_t>(value));
1470 } else {
1471 Load64BitValue(dest, value);
1472 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001473 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001474 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001475 } else {
1476 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001477 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001478 }
1479 } else if (destination.IsStackSlot()) {
1480 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001482 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001483 } else if (source.IsFpuRegister()) {
1484 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001485 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001486 } else if (source.IsConstant()) {
1487 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001488 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001489 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001490 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001491 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001492 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1493 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001494 }
1495 } else {
1496 DCHECK(destination.IsDoubleStackSlot());
1497 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001498 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001499 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001500 } else if (source.IsFpuRegister()) {
1501 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001503 } else if (source.IsConstant()) {
1504 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001505 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1506 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001507 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508 } else {
1509 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001510 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1511 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001512 }
1513 }
1514}
1515
Calin Juravle175dc732015-08-25 15:42:32 +01001516void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1517 DCHECK(location.IsRegister());
1518 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1519}
1520
Calin Juravlee460d1d2015-09-29 04:52:17 +01001521void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001522 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001523 Move(dst, src);
1524}
1525
1526void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1527 if (location.IsRegister()) {
1528 locations->AddTemp(location);
1529 } else {
1530 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1531 }
1532}
1533
David Brazdilfc6a86a2015-06-26 10:33:45 +00001534void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001535 if (successor->IsExitBlock()) {
1536 DCHECK(got->GetPrevious()->AlwaysThrows());
1537 return; // no code needed
1538 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001539
1540 HBasicBlock* block = got->GetBlock();
1541 HInstruction* previous = got->GetPrevious();
1542
1543 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001544 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001545 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1546 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1547 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1548 Immediate(1));
1549 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001550 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1551 return;
1552 }
1553
1554 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1555 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1556 }
1557 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001558 __ jmp(codegen_->GetLabelOf(successor));
1559 }
1560}
1561
David Brazdilfc6a86a2015-06-26 10:33:45 +00001562void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1563 got->SetLocations(nullptr);
1564}
1565
1566void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1567 HandleGoto(got, got->GetSuccessor());
1568}
1569
1570void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1571 try_boundary->SetLocations(nullptr);
1572}
1573
1574void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1575 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1576 if (!successor->IsExitBlock()) {
1577 HandleGoto(try_boundary, successor);
1578 }
1579}
1580
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001581void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1582 exit->SetLocations(nullptr);
1583}
1584
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001585void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001586}
1587
Mark Mendell152408f2015-12-31 12:28:50 -05001588template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001589void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001590 LabelType* true_label,
1591 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001592 if (cond->IsFPConditionTrueIfNaN()) {
1593 __ j(kUnordered, true_label);
1594 } else if (cond->IsFPConditionFalseIfNaN()) {
1595 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001596 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001597 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001598}
1599
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001600void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001601 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001602
Mark Mendellc4701932015-04-10 13:18:51 -04001603 Location left = locations->InAt(0);
1604 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001605 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001606 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001608 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 case DataType::Type::kInt8:
1610 case DataType::Type::kUint16:
1611 case DataType::Type::kInt16:
1612 case DataType::Type::kInt32:
1613 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001614 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001615 break;
1616 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001617 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001618 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001619 break;
1620 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001621 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001622 if (right.IsFpuRegister()) {
1623 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1624 } else if (right.IsConstant()) {
1625 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1626 codegen_->LiteralFloatAddress(
1627 right.GetConstant()->AsFloatConstant()->GetValue()));
1628 } else {
1629 DCHECK(right.IsStackSlot());
1630 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1631 Address(CpuRegister(RSP), right.GetStackIndex()));
1632 }
Mark Mendellc4701932015-04-10 13:18:51 -04001633 break;
1634 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001636 if (right.IsFpuRegister()) {
1637 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1638 } else if (right.IsConstant()) {
1639 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1640 codegen_->LiteralDoubleAddress(
1641 right.GetConstant()->AsDoubleConstant()->GetValue()));
1642 } else {
1643 DCHECK(right.IsDoubleStackSlot());
1644 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1645 Address(CpuRegister(RSP), right.GetStackIndex()));
1646 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001647 break;
1648 }
1649 default:
1650 LOG(FATAL) << "Unexpected condition type " << type;
1651 }
1652}
1653
1654template<class LabelType>
1655void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1656 LabelType* true_target_in,
1657 LabelType* false_target_in) {
1658 // Generated branching requires both targets to be explicit. If either of the
1659 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1660 LabelType fallthrough_target;
1661 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1662 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1663
1664 // Generate the comparison to set the CC.
1665 GenerateCompareTest(condition);
1666
1667 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001668 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001669 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001670 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001671 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1672 break;
1673 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001675 GenerateFPJumps(condition, true_target, false_target);
1676 break;
1677 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001679 GenerateFPJumps(condition, true_target, false_target);
1680 break;
1681 }
1682 default:
1683 LOG(FATAL) << "Unexpected condition type " << type;
1684 }
1685
David Brazdil0debae72015-11-12 18:37:00 +00001686 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001687 __ jmp(false_target);
1688 }
David Brazdil0debae72015-11-12 18:37:00 +00001689
1690 if (fallthrough_target.IsLinked()) {
1691 __ Bind(&fallthrough_target);
1692 }
Mark Mendellc4701932015-04-10 13:18:51 -04001693}
1694
David Brazdil0debae72015-11-12 18:37:00 +00001695static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1696 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1697 // are set only strictly before `branch`. We can't use the eflags on long
1698 // conditions if they are materialized due to the complex branching.
1699 return cond->IsCondition() &&
1700 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001701 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001702}
1703
Mark Mendell152408f2015-12-31 12:28:50 -05001704template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001705void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001706 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001707 LabelType* true_target,
1708 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001709 HInstruction* cond = instruction->InputAt(condition_input_index);
1710
1711 if (true_target == nullptr && false_target == nullptr) {
1712 // Nothing to do. The code always falls through.
1713 return;
1714 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001715 // Constant condition, statically compared against "true" (integer value 1).
1716 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001717 if (true_target != nullptr) {
1718 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001719 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001720 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001721 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001722 if (false_target != nullptr) {
1723 __ jmp(false_target);
1724 }
1725 }
1726 return;
1727 }
1728
1729 // The following code generates these patterns:
1730 // (1) true_target == nullptr && false_target != nullptr
1731 // - opposite condition true => branch to false_target
1732 // (2) true_target != nullptr && false_target == nullptr
1733 // - condition true => branch to true_target
1734 // (3) true_target != nullptr && false_target != nullptr
1735 // - condition true => branch to true_target
1736 // - branch to false_target
1737 if (IsBooleanValueOrMaterializedCondition(cond)) {
1738 if (AreEflagsSetFrom(cond, instruction)) {
1739 if (true_target == nullptr) {
1740 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1741 } else {
1742 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1743 }
1744 } else {
1745 // Materialized condition, compare against 0.
1746 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1747 if (lhs.IsRegister()) {
1748 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1749 } else {
1750 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1751 }
1752 if (true_target == nullptr) {
1753 __ j(kEqual, false_target);
1754 } else {
1755 __ j(kNotEqual, true_target);
1756 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001757 }
1758 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001759 // Condition has not been materialized, use its inputs as the
1760 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001761 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001762
David Brazdil0debae72015-11-12 18:37:00 +00001763 // If this is a long or FP comparison that has been folded into
1764 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001765 DataType::Type type = condition->InputAt(0)->GetType();
1766 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001767 GenerateCompareTestAndBranch(condition, true_target, false_target);
1768 return;
1769 }
1770
1771 Location lhs = condition->GetLocations()->InAt(0);
1772 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001773 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001774 if (true_target == nullptr) {
1775 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1776 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001777 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001778 }
Dave Allison20dfc792014-06-16 20:44:29 -07001779 }
David Brazdil0debae72015-11-12 18:37:00 +00001780
1781 // If neither branch falls through (case 3), the conditional branch to `true_target`
1782 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1783 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001784 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001785 }
1786}
1787
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001788void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001789 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001790 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001791 locations->SetInAt(0, Location::Any());
1792 }
1793}
1794
1795void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001796 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1797 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1798 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1799 nullptr : codegen_->GetLabelOf(true_successor);
1800 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1801 nullptr : codegen_->GetLabelOf(false_successor);
1802 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001803}
1804
1805void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001806 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001807 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001808 InvokeRuntimeCallingConvention calling_convention;
1809 RegisterSet caller_saves = RegisterSet::Empty();
1810 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1811 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001812 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001813 locations->SetInAt(0, Location::Any());
1814 }
1815}
1816
1817void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001818 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001819 GenerateTestAndBranch<Label>(deoptimize,
1820 /* condition_input_index */ 0,
1821 slow_path->GetEntryLabel(),
1822 /* false_target */ nullptr);
1823}
1824
Mingyao Yang063fc772016-08-02 11:02:54 -07001825void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001826 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001827 LocationSummary(flag, LocationSummary::kNoCall);
1828 locations->SetOut(Location::RequiresRegister());
1829}
1830
1831void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1832 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1833 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1834}
1835
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001836static bool SelectCanUseCMOV(HSelect* select) {
1837 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001839 return false;
1840 }
1841
1842 // A FP condition doesn't generate the single CC that we need.
1843 HInstruction* condition = select->GetCondition();
1844 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001845 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001846 return false;
1847 }
1848
1849 // We can generate a CMOV for this Select.
1850 return true;
1851}
1852
David Brazdil74eb1b22015-12-14 11:44:01 +00001853void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001854 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001855 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001856 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001857 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001858 } else {
1859 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001860 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001861 if (select->InputAt(1)->IsConstant()) {
1862 locations->SetInAt(1, Location::RequiresRegister());
1863 } else {
1864 locations->SetInAt(1, Location::Any());
1865 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001866 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001867 locations->SetInAt(1, Location::Any());
1868 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001869 }
1870 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1871 locations->SetInAt(2, Location::RequiresRegister());
1872 }
1873 locations->SetOut(Location::SameAsFirstInput());
1874}
1875
1876void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1877 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001878 if (SelectCanUseCMOV(select)) {
1879 // If both the condition and the source types are integer, we can generate
1880 // a CMOV to implement Select.
1881 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001882 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001883 DCHECK(locations->InAt(0).Equals(locations->Out()));
1884
1885 HInstruction* select_condition = select->GetCondition();
1886 Condition cond = kNotEqual;
1887
1888 // Figure out how to test the 'condition'.
1889 if (select_condition->IsCondition()) {
1890 HCondition* condition = select_condition->AsCondition();
1891 if (!condition->IsEmittedAtUseSite()) {
1892 // This was a previously materialized condition.
1893 // Can we use the existing condition code?
1894 if (AreEflagsSetFrom(condition, select)) {
1895 // Materialization was the previous instruction. Condition codes are right.
1896 cond = X86_64IntegerCondition(condition->GetCondition());
1897 } else {
1898 // No, we have to recreate the condition code.
1899 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1900 __ testl(cond_reg, cond_reg);
1901 }
1902 } else {
1903 GenerateCompareTest(condition);
1904 cond = X86_64IntegerCondition(condition->GetCondition());
1905 }
1906 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001907 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001908 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1909 __ testl(cond_reg, cond_reg);
1910 }
1911
1912 // If the condition is true, overwrite the output, which already contains false.
1913 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001915 if (value_true_loc.IsRegister()) {
1916 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1917 } else {
1918 __ cmov(cond,
1919 value_false,
1920 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1921 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001922 } else {
1923 NearLabel false_target;
1924 GenerateTestAndBranch<NearLabel>(select,
1925 /* condition_input_index */ 2,
1926 /* true_target */ nullptr,
1927 &false_target);
1928 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1929 __ Bind(&false_target);
1930 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001931}
1932
David Srbecky0cf44932015-12-09 14:09:59 +00001933void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001934 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001935}
1936
David Srbeckyd28f4a02016-03-14 17:14:24 +00001937void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1938 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001939}
1940
1941void CodeGeneratorX86_64::GenerateNop() {
1942 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001943}
1944
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001946 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001947 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001948 // Handle the long/FP comparisons made in instruction simplification.
1949 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001950 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001951 locations->SetInAt(0, Location::RequiresRegister());
1952 locations->SetInAt(1, Location::Any());
1953 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001954 case DataType::Type::kFloat32:
1955 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001956 locations->SetInAt(0, Location::RequiresFpuRegister());
1957 locations->SetInAt(1, Location::Any());
1958 break;
1959 default:
1960 locations->SetInAt(0, Location::RequiresRegister());
1961 locations->SetInAt(1, Location::Any());
1962 break;
1963 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001964 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001965 locations->SetOut(Location::RequiresRegister());
1966 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967}
1968
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001970 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001971 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001972 }
Mark Mendellc4701932015-04-10 13:18:51 -04001973
1974 LocationSummary* locations = cond->GetLocations();
1975 Location lhs = locations->InAt(0);
1976 Location rhs = locations->InAt(1);
1977 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001978 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001979
1980 switch (cond->InputAt(0)->GetType()) {
1981 default:
1982 // Integer case.
1983
1984 // Clear output register: setcc only sets the low byte.
1985 __ xorl(reg, reg);
1986
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001987 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001988 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001989 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001991 // Clear output register: setcc only sets the low byte.
1992 __ xorl(reg, reg);
1993
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001994 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001995 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001996 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001998 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1999 if (rhs.IsConstant()) {
2000 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
2001 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
2002 } else if (rhs.IsStackSlot()) {
2003 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2004 } else {
2005 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2006 }
2007 GenerateFPJumps(cond, &true_label, &false_label);
2008 break;
2009 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002011 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2012 if (rhs.IsConstant()) {
2013 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
2014 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
2015 } else if (rhs.IsDoubleStackSlot()) {
2016 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2017 } else {
2018 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2019 }
2020 GenerateFPJumps(cond, &true_label, &false_label);
2021 break;
2022 }
2023 }
2024
2025 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002026 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002027
Roland Levillain4fa13f62015-07-06 18:11:54 +01002028 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002029 __ Bind(&false_label);
2030 __ xorl(reg, reg);
2031 __ jmp(&done_label);
2032
Roland Levillain4fa13f62015-07-06 18:11:54 +01002033 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002034 __ Bind(&true_label);
2035 __ movl(reg, Immediate(1));
2036 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002037}
2038
2039void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002040 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002041}
2042
2043void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002044 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002045}
2046
2047void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002049}
2050
2051void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002052 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002053}
2054
2055void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002056 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002057}
2058
2059void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002060 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002061}
2062
2063void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002064 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002065}
2066
2067void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002068 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002069}
2070
2071void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002072 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002073}
2074
2075void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002076 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002077}
2078
2079void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002081}
2082
2083void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085}
2086
Aart Bike9f37602015-10-09 11:15:55 -07002087void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002088 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002089}
2090
2091void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002093}
2094
2095void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002097}
2098
2099void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002100 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002101}
2102
2103void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002105}
2106
2107void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002108 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002109}
2110
2111void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002112 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002113}
2114
2115void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002117}
2118
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002119void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002121 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002122 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002124 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002125 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002127 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kInt32:
2129 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002130 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002131 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002132 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2133 break;
2134 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 case DataType::Type::kFloat32:
2136 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002137 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002138 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002139 locations->SetOut(Location::RequiresRegister());
2140 break;
2141 }
2142 default:
2143 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2144 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002145}
2146
2147void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002148 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002149 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002150 Location left = locations->InAt(0);
2151 Location right = locations->InAt(1);
2152
Mark Mendell0c9497d2015-08-21 09:30:05 -04002153 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002155 Condition less_cond = kLess;
2156
Calin Juravleddb7df22014-11-25 20:56:51 +00002157 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002158 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002159 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002162 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002164 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002165 break;
2166 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002168 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002169 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002172 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2173 if (right.IsConstant()) {
2174 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2175 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2176 } else if (right.IsStackSlot()) {
2177 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2178 } else {
2179 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2180 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002181 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002182 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002183 break;
2184 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002185 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002186 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2187 if (right.IsConstant()) {
2188 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2189 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2190 } else if (right.IsDoubleStackSlot()) {
2191 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2192 } else {
2193 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2194 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002195 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002196 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002197 break;
2198 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002199 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002200 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002201 }
Aart Bika19616e2016-02-01 18:57:58 -08002202
Calin Juravleddb7df22014-11-25 20:56:51 +00002203 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002204 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002205 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002206
Calin Juravle91debbc2014-11-26 19:01:09 +00002207 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002208 __ movl(out, Immediate(1));
2209 __ jmp(&done);
2210
2211 __ Bind(&less);
2212 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002213
2214 __ Bind(&done);
2215}
2216
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002217void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002218 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002219 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002220 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002221}
2222
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002223void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002224 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225}
2226
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002227void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2228 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002229 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002230 locations->SetOut(Location::ConstantLocation(constant));
2231}
2232
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002233void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002234 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002235}
2236
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002238 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002239 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002240 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002241}
2242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002243void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002244 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002245}
2246
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002247void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2248 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002249 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002250 locations->SetOut(Location::ConstantLocation(constant));
2251}
2252
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002253void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002254 // Will be generated at use site.
2255}
2256
2257void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2258 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002259 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002260 locations->SetOut(Location::ConstantLocation(constant));
2261}
2262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002263void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2264 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002265 // Will be generated at use site.
2266}
2267
Igor Murashkind01745e2017-04-05 16:40:31 -07002268void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2269 constructor_fence->SetLocations(nullptr);
2270}
2271
2272void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2273 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2274 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2275}
2276
Calin Juravle27df7582015-04-17 19:12:31 +01002277void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2278 memory_barrier->SetLocations(nullptr);
2279}
2280
2281void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002282 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002283}
2284
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002285void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2286 ret->SetLocations(nullptr);
2287}
2288
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002289void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002290 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002291}
2292
2293void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002294 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002295 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002296 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002297 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002299 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt8:
2301 case DataType::Type::kUint16:
2302 case DataType::Type::kInt16:
2303 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002304 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002305 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002306 break;
2307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kFloat32:
2309 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002310 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 break;
2312
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002313 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002314 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002315 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002316}
2317
2318void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2319 if (kIsDebugBuild) {
2320 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002321 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002323 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kInt8:
2325 case DataType::Type::kUint16:
2326 case DataType::Type::kInt16:
2327 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002330 break;
2331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 case DataType::Type::kFloat32:
2333 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002334 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002335 XMM0);
2336 break;
2337
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002338 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002339 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002340 }
2341 }
2342 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002343}
2344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002346 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002347 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002349 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002350 case DataType::Type::kInt8:
2351 case DataType::Type::kUint16:
2352 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002353 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002355 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002357 return Location::RegisterLocation(RAX);
2358
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002360 return Location::NoLocation();
2361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 case DataType::Type::kFloat64:
2363 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002364 return Location::FpuRegisterLocation(XMM0);
2365 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002366
2367 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002368}
2369
2370Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2371 return Location::RegisterLocation(kMethodRegisterArgument);
2372}
2373
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002374Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002375 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002376 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002378 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kInt8:
2380 case DataType::Type::kUint16:
2381 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002382 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002383 uint32_t index = gp_index_++;
2384 stack_index_++;
2385 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002386 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002387 } else {
2388 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2389 }
2390 }
2391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002392 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002393 uint32_t index = gp_index_;
2394 stack_index_ += 2;
2395 if (index < calling_convention.GetNumberOfRegisters()) {
2396 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002397 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002398 } else {
2399 gp_index_ += 2;
2400 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2401 }
2402 }
2403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002405 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002406 stack_index_++;
2407 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002408 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002409 } else {
2410 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2411 }
2412 }
2413
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002414 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002415 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002416 stack_index_ += 2;
2417 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002418 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002419 } else {
2420 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2421 }
2422 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002423
Aart Bik66c158e2018-01-31 12:55:04 -08002424 case DataType::Type::kUint32:
2425 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002427 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002428 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002429 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002430 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002431}
2432
Calin Juravle175dc732015-08-25 15:42:32 +01002433void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2434 // The trampoline uses the same calling convention as dex calling conventions,
2435 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2436 // the method_idx.
2437 HandleInvoke(invoke);
2438}
2439
2440void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2441 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2442}
2443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002444void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002445 // Explicit clinit checks triggered by static invokes must have been pruned by
2446 // art::PrepareForRegisterAllocation.
2447 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002448
Mark Mendellfb8d2792015-03-31 22:16:59 -04002449 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002450 if (intrinsic.TryDispatch(invoke)) {
2451 return;
2452 }
2453
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002454 HandleInvoke(invoke);
2455}
2456
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002457static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2458 if (invoke->GetLocations()->Intrinsified()) {
2459 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2460 intrinsic.Dispatch(invoke);
2461 return true;
2462 }
2463 return false;
2464}
2465
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002466void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002467 // Explicit clinit checks triggered by static invokes must have been pruned by
2468 // art::PrepareForRegisterAllocation.
2469 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002470
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002471 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2472 return;
2473 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002474
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002475 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002476 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002477 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002478}
2479
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002480void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002481 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002482 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002483}
2484
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002485void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002486 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002487 if (intrinsic.TryDispatch(invoke)) {
2488 return;
2489 }
2490
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002491 HandleInvoke(invoke);
2492}
2493
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002494void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002495 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2496 return;
2497 }
2498
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002499 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002500 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002501}
2502
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002503void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2504 HandleInvoke(invoke);
2505 // Add the hidden argument.
2506 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2507}
2508
2509void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2510 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002511 LocationSummary* locations = invoke->GetLocations();
2512 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2513 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002514 Location receiver = locations->InAt(0);
2515 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2516
Roland Levillain0d5a2812015-11-13 10:07:31 +00002517 // Set the hidden argument. This is safe to do this here, as RAX
2518 // won't be modified thereafter, before the `call` instruction.
2519 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002520 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002521
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002522 if (receiver.IsStackSlot()) {
2523 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002524 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002525 __ movl(temp, Address(temp, class_offset));
2526 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002527 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002529 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002530 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002531 // Instead of simply (possibly) unpoisoning `temp` here, we should
2532 // emit a read barrier for the previous class reference load.
2533 // However this is not required in practice, as this is an
2534 // intermediate/temporary reference and because the current
2535 // concurrent copying collector keeps the from-space memory
2536 // intact/accessible until the end of the marking phase (the
2537 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002538 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002539 // temp = temp->GetAddressOfIMT()
2540 __ movq(temp,
2541 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2542 // temp = temp->GetImtEntryAt(method_offset);
2543 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002544 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002545 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002546 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002547 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002548 __ call(Address(
2549 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002550
2551 DCHECK(!codegen_->IsLeafMethod());
2552 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2553}
2554
Orion Hodsonac141392017-01-13 11:53:47 +00002555void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2556 HandleInvoke(invoke);
2557}
2558
2559void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2560 codegen_->GenerateInvokePolymorphicCall(invoke);
2561}
2562
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002563void LocationsBuilderX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2564 HandleInvoke(invoke);
2565}
2566
2567void InstructionCodeGeneratorX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2568 codegen_->GenerateInvokeCustomCall(invoke);
2569}
2570
Roland Levillain88cb1752014-10-20 16:36:47 +01002571void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2572 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002573 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002574 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 case DataType::Type::kInt32:
2576 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002577 locations->SetInAt(0, Location::RequiresRegister());
2578 locations->SetOut(Location::SameAsFirstInput());
2579 break;
2580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002581 case DataType::Type::kFloat32:
2582 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002583 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002584 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002585 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002586 break;
2587
2588 default:
2589 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2590 }
2591}
2592
2593void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2594 LocationSummary* locations = neg->GetLocations();
2595 Location out = locations->Out();
2596 Location in = locations->InAt(0);
2597 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002599 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002600 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002602 break;
2603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002604 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002605 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002606 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002608 break;
2609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002611 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002612 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002613 // Implement float negation with an exclusive or with value
2614 // 0x80000000 (mask for bit 31, representing the sign of a
2615 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002616 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002617 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002618 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002619 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002622 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002623 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002624 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002625 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002626 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002627 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002629 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002630 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002631
2632 default:
2633 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2634 }
2635}
2636
Roland Levillaindff1f282014-11-05 14:15:05 +00002637void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2638 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002639 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 DataType::Type result_type = conversion->GetResultType();
2641 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002642 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2643 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002644
Roland Levillaindff1f282014-11-05 14:15:05 +00002645 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002646 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002648 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002650 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2651 locations->SetInAt(0, Location::Any());
2652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002653 break;
2654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002656 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002658 locations->SetInAt(0, Location::Any());
2659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2660 break;
2661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002663 locations->SetInAt(0, Location::RequiresFpuRegister());
2664 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002668 locations->SetInAt(0, Location::RequiresFpuRegister());
2669 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 }
2676 break;
2677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002678 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002679 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002681 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002683 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 case DataType::Type::kInt16:
2685 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002686 // TODO: We would benefit from a (to-be-implemented)
2687 // Location::RegisterOrStackSlot requirement for this input.
2688 locations->SetInAt(0, Location::RequiresRegister());
2689 locations->SetOut(Location::RequiresRegister());
2690 break;
2691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002693 locations->SetInAt(0, Location::RequiresFpuRegister());
2694 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002695 break;
2696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002698 locations->SetInAt(0, Location::RequiresFpuRegister());
2699 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 break;
2701
2702 default:
2703 LOG(FATAL) << "Unexpected type conversion from " << input_type
2704 << " to " << result_type;
2705 }
2706 break;
2707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002709 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002711 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002713 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kInt16:
2715 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002716 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002717 locations->SetOut(Location::RequiresFpuRegister());
2718 break;
2719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002721 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002722 locations->SetOut(Location::RequiresFpuRegister());
2723 break;
2724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002726 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002727 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002728 break;
2729
2730 default:
2731 LOG(FATAL) << "Unexpected type conversion from " << input_type
2732 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002733 }
Roland Levillaincff13742014-11-17 14:32:17 +00002734 break;
2735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002737 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002739 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002741 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 case DataType::Type::kInt16:
2743 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002744 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002745 locations->SetOut(Location::RequiresFpuRegister());
2746 break;
2747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002749 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002750 locations->SetOut(Location::RequiresFpuRegister());
2751 break;
2752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002754 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002755 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002756 break;
2757
2758 default:
2759 LOG(FATAL) << "Unexpected type conversion from " << input_type
2760 << " to " << result_type;
2761 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 }
2768}
2769
2770void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2771 LocationSummary* locations = conversion->GetLocations();
2772 Location out = locations->Out();
2773 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 DataType::Type result_type = conversion->GetResultType();
2775 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002776 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2777 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002778 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002779 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002780 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002781 case DataType::Type::kInt8:
2782 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 case DataType::Type::kInt16:
2784 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002785 case DataType::Type::kInt64:
2786 if (in.IsRegister()) {
2787 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2788 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2789 __ movzxb(out.AsRegister<CpuRegister>(),
2790 Address(CpuRegister(RSP), in.GetStackIndex()));
2791 } else {
2792 __ movl(out.AsRegister<CpuRegister>(),
2793 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2794 }
2795 break;
2796
2797 default:
2798 LOG(FATAL) << "Unexpected type conversion from " << input_type
2799 << " to " << result_type;
2800 }
2801 break;
2802
2803 case DataType::Type::kInt8:
2804 switch (input_type) {
2805 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002807 case DataType::Type::kInt16:
2808 case DataType::Type::kInt32:
2809 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002810 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002812 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002814 Address(CpuRegister(RSP), in.GetStackIndex()));
2815 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002817 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002818 }
2819 break;
2820
2821 default:
2822 LOG(FATAL) << "Unexpected type conversion from " << input_type
2823 << " to " << result_type;
2824 }
2825 break;
2826
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002827 case DataType::Type::kUint16:
2828 switch (input_type) {
2829 case DataType::Type::kInt8:
2830 case DataType::Type::kInt16:
2831 case DataType::Type::kInt32:
2832 case DataType::Type::kInt64:
2833 if (in.IsRegister()) {
2834 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2835 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2836 __ movzxw(out.AsRegister<CpuRegister>(),
2837 Address(CpuRegister(RSP), in.GetStackIndex()));
2838 } else {
2839 __ movl(out.AsRegister<CpuRegister>(),
2840 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2841 }
2842 break;
2843
2844 default:
2845 LOG(FATAL) << "Unexpected type conversion from " << input_type
2846 << " to " << result_type;
2847 }
2848 break;
2849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002851 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002852 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002853 case DataType::Type::kInt32:
2854 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002855 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002857 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002859 Address(CpuRegister(RSP), in.GetStackIndex()));
2860 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002861 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002862 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002863 }
2864 break;
2865
2866 default:
2867 LOG(FATAL) << "Unexpected type conversion from " << input_type
2868 << " to " << result_type;
2869 }
2870 break;
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002873 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002875 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002877 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002878 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002879 Address(CpuRegister(RSP), in.GetStackIndex()));
2880 } else {
2881 DCHECK(in.IsConstant());
2882 DCHECK(in.GetConstant()->IsLongConstant());
2883 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002885 }
2886 break;
2887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002889 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2890 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002891 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002892
2893 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002894 // if input >= (float)INT_MAX goto done
2895 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002896 __ j(kAboveEqual, &done);
2897 // if input == NaN goto nan
2898 __ j(kUnordered, &nan);
2899 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002900 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002901 __ jmp(&done);
2902 __ Bind(&nan);
2903 // output = 0
2904 __ xorl(output, output);
2905 __ Bind(&done);
2906 break;
2907 }
2908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002909 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002910 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2911 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002912 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002913
2914 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002915 // if input >= (double)INT_MAX goto done
2916 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002917 __ j(kAboveEqual, &done);
2918 // if input == NaN goto nan
2919 __ j(kUnordered, &nan);
2920 // output = double-to-int-truncate(input)
2921 __ cvttsd2si(output, input);
2922 __ jmp(&done);
2923 __ Bind(&nan);
2924 // output = 0
2925 __ xorl(output, output);
2926 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002927 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002928 }
Roland Levillain946e1432014-11-11 17:35:19 +00002929
2930 default:
2931 LOG(FATAL) << "Unexpected type conversion from " << input_type
2932 << " to " << result_type;
2933 }
2934 break;
2935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002937 switch (input_type) {
2938 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002940 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002942 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002943 case DataType::Type::kInt16:
2944 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002945 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002946 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002947 break;
2948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002949 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002950 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2951 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002952 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002953
Mark Mendell92e83bf2015-05-07 11:25:03 -04002954 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002955 // if input >= (float)LONG_MAX goto done
2956 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002957 __ j(kAboveEqual, &done);
2958 // if input == NaN goto nan
2959 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002960 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002961 __ cvttss2si(output, input, true);
2962 __ jmp(&done);
2963 __ Bind(&nan);
2964 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002965 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002966 __ Bind(&done);
2967 break;
2968 }
2969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002971 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2972 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002973 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002974
Mark Mendell92e83bf2015-05-07 11:25:03 -04002975 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002976 // if input >= (double)LONG_MAX goto done
2977 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002978 __ j(kAboveEqual, &done);
2979 // if input == NaN goto nan
2980 __ j(kUnordered, &nan);
2981 // output = double-to-long-truncate(input)
2982 __ cvttsd2si(output, input, true);
2983 __ jmp(&done);
2984 __ Bind(&nan);
2985 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002986 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002987 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002988 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002989 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002990
2991 default:
2992 LOG(FATAL) << "Unexpected type conversion from " << input_type
2993 << " to " << result_type;
2994 }
2995 break;
2996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002998 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002999 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003000 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003002 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kInt16:
3004 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003005 if (in.IsRegister()) {
3006 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3007 } else if (in.IsConstant()) {
3008 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3009 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003010 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003011 } else {
3012 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3013 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3014 }
Roland Levillaincff13742014-11-17 14:32:17 +00003015 break;
3016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003018 if (in.IsRegister()) {
3019 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3020 } else if (in.IsConstant()) {
3021 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3022 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06003023 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003024 } else {
3025 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3026 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3027 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003028 break;
3029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003030 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04003031 if (in.IsFpuRegister()) {
3032 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3033 } else if (in.IsConstant()) {
3034 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
3035 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003036 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003037 } else {
3038 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
3039 Address(CpuRegister(RSP), in.GetStackIndex()));
3040 }
Roland Levillaincff13742014-11-17 14:32:17 +00003041 break;
3042
3043 default:
3044 LOG(FATAL) << "Unexpected type conversion from " << input_type
3045 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003046 }
Roland Levillaincff13742014-11-17 14:32:17 +00003047 break;
3048
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003049 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003050 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003052 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003054 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 case DataType::Type::kInt16:
3056 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003057 if (in.IsRegister()) {
3058 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3059 } else if (in.IsConstant()) {
3060 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3061 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003062 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003063 } else {
3064 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3065 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3066 }
Roland Levillaincff13742014-11-17 14:32:17 +00003067 break;
3068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003069 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003070 if (in.IsRegister()) {
3071 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3072 } else if (in.IsConstant()) {
3073 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3074 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003075 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003076 } else {
3077 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3078 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3079 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003080 break;
3081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003082 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003083 if (in.IsFpuRegister()) {
3084 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3085 } else if (in.IsConstant()) {
3086 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3087 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003088 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003089 } else {
3090 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3091 Address(CpuRegister(RSP), in.GetStackIndex()));
3092 }
Roland Levillaincff13742014-11-17 14:32:17 +00003093 break;
3094
3095 default:
3096 LOG(FATAL) << "Unexpected type conversion from " << input_type
3097 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003098 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003099 break;
3100
3101 default:
3102 LOG(FATAL) << "Unexpected type conversion from " << input_type
3103 << " to " << result_type;
3104 }
3105}
3106
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003107void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003108 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003109 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003110 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003112 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003113 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003115 break;
3116 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003118 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003119 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003120 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003121 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003122 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 break;
3124 }
3125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kFloat64:
3127 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003128 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003129 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003130 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003131 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003132 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003133
3134 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003135 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003136 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003137}
3138
3139void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3140 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003141 Location first = locations->InAt(0);
3142 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003143 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003144
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003145 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003147 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003148 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3149 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003150 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3151 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003152 } else {
3153 __ leal(out.AsRegister<CpuRegister>(), Address(
3154 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3155 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003156 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003157 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3158 __ addl(out.AsRegister<CpuRegister>(),
3159 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3160 } else {
3161 __ leal(out.AsRegister<CpuRegister>(), Address(
3162 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3163 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003164 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003165 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003166 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003167 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003168 break;
3169 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003172 if (second.IsRegister()) {
3173 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3174 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003175 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3176 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003177 } else {
3178 __ leaq(out.AsRegister<CpuRegister>(), Address(
3179 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3180 }
3181 } else {
3182 DCHECK(second.IsConstant());
3183 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3184 int32_t int32_value = Low32Bits(value);
3185 DCHECK_EQ(int32_value, value);
3186 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3187 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3188 } else {
3189 __ leaq(out.AsRegister<CpuRegister>(), Address(
3190 first.AsRegister<CpuRegister>(), int32_value));
3191 }
3192 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003193 break;
3194 }
3195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003197 if (second.IsFpuRegister()) {
3198 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3199 } else if (second.IsConstant()) {
3200 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003201 codegen_->LiteralFloatAddress(
3202 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003203 } else {
3204 DCHECK(second.IsStackSlot());
3205 __ addss(first.AsFpuRegister<XmmRegister>(),
3206 Address(CpuRegister(RSP), second.GetStackIndex()));
3207 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003208 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003209 }
3210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003212 if (second.IsFpuRegister()) {
3213 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3214 } else if (second.IsConstant()) {
3215 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003216 codegen_->LiteralDoubleAddress(
3217 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003218 } else {
3219 DCHECK(second.IsDoubleStackSlot());
3220 __ addsd(first.AsFpuRegister<XmmRegister>(),
3221 Address(CpuRegister(RSP), second.GetStackIndex()));
3222 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003223 break;
3224 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003225
3226 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003227 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003228 }
3229}
3230
3231void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003233 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003234 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003235 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003236 locations->SetInAt(0, Location::RequiresRegister());
3237 locations->SetInAt(1, Location::Any());
3238 locations->SetOut(Location::SameAsFirstInput());
3239 break;
3240 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003242 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003243 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003244 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003245 break;
3246 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 case DataType::Type::kFloat32:
3248 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003249 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003250 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003251 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003252 break;
Calin Juravle11351682014-10-23 15:38:15 +01003253 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003254 default:
Calin Juravle11351682014-10-23 15:38:15 +01003255 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003256 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003257}
3258
3259void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3260 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003261 Location first = locations->InAt(0);
3262 Location second = locations->InAt(1);
3263 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003264 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003266 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003267 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003268 } else if (second.IsConstant()) {
3269 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003271 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003272 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003273 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003274 break;
3275 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003277 if (second.IsConstant()) {
3278 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3279 DCHECK(IsInt<32>(value));
3280 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3281 } else {
3282 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3283 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003284 break;
3285 }
3286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003288 if (second.IsFpuRegister()) {
3289 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3290 } else if (second.IsConstant()) {
3291 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003292 codegen_->LiteralFloatAddress(
3293 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003294 } else {
3295 DCHECK(second.IsStackSlot());
3296 __ subss(first.AsFpuRegister<XmmRegister>(),
3297 Address(CpuRegister(RSP), second.GetStackIndex()));
3298 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003299 break;
Calin Juravle11351682014-10-23 15:38:15 +01003300 }
3301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003302 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003303 if (second.IsFpuRegister()) {
3304 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3305 } else if (second.IsConstant()) {
3306 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003307 codegen_->LiteralDoubleAddress(
3308 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003309 } else {
3310 DCHECK(second.IsDoubleStackSlot());
3311 __ subsd(first.AsFpuRegister<XmmRegister>(),
3312 Address(CpuRegister(RSP), second.GetStackIndex()));
3313 }
Calin Juravle11351682014-10-23 15:38:15 +01003314 break;
3315 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003316
3317 default:
Calin Juravle11351682014-10-23 15:38:15 +01003318 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003319 }
3320}
3321
Calin Juravle34bacdf2014-10-07 20:23:36 +01003322void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003324 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003325 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003326 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003327 locations->SetInAt(0, Location::RequiresRegister());
3328 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003329 if (mul->InputAt(1)->IsIntConstant()) {
3330 // Can use 3 operand multiply.
3331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3332 } else {
3333 locations->SetOut(Location::SameAsFirstInput());
3334 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003335 break;
3336 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003337 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003338 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003339 locations->SetInAt(1, Location::Any());
3340 if (mul->InputAt(1)->IsLongConstant() &&
3341 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003342 // Can use 3 operand multiply.
3343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3344 } else {
3345 locations->SetOut(Location::SameAsFirstInput());
3346 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003347 break;
3348 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003349 case DataType::Type::kFloat32:
3350 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003351 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003352 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003353 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003355 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003356
3357 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003358 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003359 }
3360}
3361
3362void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3363 LocationSummary* locations = mul->GetLocations();
3364 Location first = locations->InAt(0);
3365 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003366 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003367 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003368 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003369 // The constant may have ended up in a register, so test explicitly to avoid
3370 // problems where the output may not be the same as the first operand.
3371 if (mul->InputAt(1)->IsIntConstant()) {
3372 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3373 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3374 } else if (second.IsRegister()) {
3375 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003376 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003377 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003378 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003379 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003380 __ imull(first.AsRegister<CpuRegister>(),
3381 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003382 }
3383 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003384 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003385 // The constant may have ended up in a register, so test explicitly to avoid
3386 // problems where the output may not be the same as the first operand.
3387 if (mul->InputAt(1)->IsLongConstant()) {
3388 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3389 if (IsInt<32>(value)) {
3390 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3391 Immediate(static_cast<int32_t>(value)));
3392 } else {
3393 // Have to use the constant area.
3394 DCHECK(first.Equals(out));
3395 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3396 }
3397 } else if (second.IsRegister()) {
3398 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003399 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003400 } else {
3401 DCHECK(second.IsDoubleStackSlot());
3402 DCHECK(first.Equals(out));
3403 __ imulq(first.AsRegister<CpuRegister>(),
3404 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003405 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003406 break;
3407 }
3408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003410 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003411 if (second.IsFpuRegister()) {
3412 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3413 } else if (second.IsConstant()) {
3414 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003415 codegen_->LiteralFloatAddress(
3416 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003417 } else {
3418 DCHECK(second.IsStackSlot());
3419 __ mulss(first.AsFpuRegister<XmmRegister>(),
3420 Address(CpuRegister(RSP), second.GetStackIndex()));
3421 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003422 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003423 }
3424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003425 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003426 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003427 if (second.IsFpuRegister()) {
3428 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3429 } else if (second.IsConstant()) {
3430 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003431 codegen_->LiteralDoubleAddress(
3432 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003433 } else {
3434 DCHECK(second.IsDoubleStackSlot());
3435 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3436 Address(CpuRegister(RSP), second.GetStackIndex()));
3437 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003438 break;
3439 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003440
3441 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003442 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003443 }
3444}
3445
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003446void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3447 uint32_t stack_adjustment, bool is_float) {
3448 if (source.IsStackSlot()) {
3449 DCHECK(is_float);
3450 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3451 } else if (source.IsDoubleStackSlot()) {
3452 DCHECK(!is_float);
3453 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3454 } else {
3455 // Write the value to the temporary location on the stack and load to FP stack.
3456 if (is_float) {
3457 Location stack_temp = Location::StackSlot(temp_offset);
3458 codegen_->Move(stack_temp, source);
3459 __ flds(Address(CpuRegister(RSP), temp_offset));
3460 } else {
3461 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3462 codegen_->Move(stack_temp, source);
3463 __ fldl(Address(CpuRegister(RSP), temp_offset));
3464 }
3465 }
3466}
3467
3468void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 DataType::Type type = rem->GetResultType();
3470 bool is_float = type == DataType::Type::kFloat32;
3471 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003472 LocationSummary* locations = rem->GetLocations();
3473 Location first = locations->InAt(0);
3474 Location second = locations->InAt(1);
3475 Location out = locations->Out();
3476
3477 // Create stack space for 2 elements.
3478 // TODO: enhance register allocator to ask for stack temporaries.
3479 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3480
3481 // Load the values to the FP stack in reverse order, using temporaries if needed.
3482 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3483 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3484
3485 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003486 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003487 __ Bind(&retry);
3488 __ fprem();
3489
3490 // Move FP status to AX.
3491 __ fstsw();
3492
3493 // And see if the argument reduction is complete. This is signaled by the
3494 // C2 FPU flag bit set to 0.
3495 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3496 __ j(kNotEqual, &retry);
3497
3498 // We have settled on the final value. Retrieve it into an XMM register.
3499 // Store FP top of stack to real stack.
3500 if (is_float) {
3501 __ fsts(Address(CpuRegister(RSP), 0));
3502 } else {
3503 __ fstl(Address(CpuRegister(RSP), 0));
3504 }
3505
3506 // Pop the 2 items from the FP stack.
3507 __ fucompp();
3508
3509 // Load the value from the stack into an XMM register.
3510 DCHECK(out.IsFpuRegister()) << out;
3511 if (is_float) {
3512 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3513 } else {
3514 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3515 }
3516
3517 // And remove the temporary stack space we allocated.
3518 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3519}
3520
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3522 DCHECK(instruction->IsDiv() || instruction->IsRem());
3523
3524 LocationSummary* locations = instruction->GetLocations();
3525 Location second = locations->InAt(1);
3526 DCHECK(second.IsConstant());
3527
3528 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3529 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003530 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531
3532 DCHECK(imm == 1 || imm == -1);
3533
3534 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003535 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 if (instruction->IsRem()) {
3537 __ xorl(output_register, output_register);
3538 } else {
3539 __ movl(output_register, input_register);
3540 if (imm == -1) {
3541 __ negl(output_register);
3542 }
3543 }
3544 break;
3545 }
3546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003547 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003549 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550 } else {
3551 __ movq(output_register, input_register);
3552 if (imm == -1) {
3553 __ negq(output_register);
3554 }
3555 }
3556 break;
3557 }
3558
3559 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003560 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561 }
3562}
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303563void InstructionCodeGeneratorX86_64::RemByPowerOfTwo(HRem* instruction) {
3564 LocationSummary* locations = instruction->GetLocations();
3565 Location second = locations->InAt(1);
3566 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3567 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3568 int64_t imm = Int64FromConstant(second.GetConstant());
3569 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3570 uint64_t abs_imm = AbsOrMin(imm);
3571 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3572 if (instruction->GetResultType() == DataType::Type::kInt32) {
3573 NearLabel done;
3574 __ movl(out, numerator);
3575 __ andl(out, Immediate(abs_imm-1));
3576 __ j(Condition::kZero, &done);
3577 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3578 __ testl(numerator, numerator);
3579 __ cmov(Condition::kLess, out, tmp, false);
3580 __ Bind(&done);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303582 } else {
3583 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3584 codegen_->Load64BitValue(tmp, abs_imm - 1);
3585 NearLabel done;
3586
3587 __ movq(out, numerator);
3588 __ andq(out, tmp);
3589 __ j(Condition::kZero, &done);
3590 __ movq(tmp, numerator);
3591 __ sarq(tmp, Immediate(63));
3592 __ shlq(tmp, Immediate(WhichPowerOf2(abs_imm)));
3593 __ orq(out, tmp);
3594 __ Bind(&done);
3595 }
3596}
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003597void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598 LocationSummary* locations = instruction->GetLocations();
3599 Location second = locations->InAt(1);
3600
3601 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3602 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3603
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003604 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003605 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3606 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607
3608 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003610 if (instruction->GetResultType() == DataType::Type::kInt32) {
Atul Bajaj1cc73292018-11-15 11:36:53 +05303611 // When denominator is equal to 2, we can add signed bit and numerator to tmp.
3612 // Below we are using addl instruction instead of cmov which give us 1 cycle benefit.
3613 if (abs_imm == 2) {
3614 __ leal(tmp, Address(numerator, 0));
3615 __ shrl(tmp, Immediate(31));
3616 __ addl(tmp, numerator);
3617 } else {
3618 __ leal(tmp, Address(numerator, abs_imm - 1));
3619 __ testl(numerator, numerator);
3620 __ cmov(kGreaterEqual, tmp, numerator);
3621 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 int shift = CTZ(imm);
3623 __ sarl(tmp, Immediate(shift));
3624
3625 if (imm < 0) {
3626 __ negl(tmp);
3627 }
3628
3629 __ movl(output_register, tmp);
3630 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
Atul Bajaj1cc73292018-11-15 11:36:53 +05303633 if (abs_imm == 2) {
3634 __ movq(rdx, numerator);
3635 __ shrq(rdx, Immediate(63));
3636 __ addq(rdx, numerator);
3637 } else {
3638 codegen_->Load64BitValue(rdx, abs_imm - 1);
3639 __ addq(rdx, numerator);
3640 __ testq(numerator, numerator);
3641 __ cmov(kGreaterEqual, rdx, numerator);
3642 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003643 int shift = CTZ(imm);
3644 __ sarq(rdx, Immediate(shift));
3645
3646 if (imm < 0) {
3647 __ negq(rdx);
3648 }
3649
3650 __ movq(output_register, rdx);
3651 }
3652}
3653
3654void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3655 DCHECK(instruction->IsDiv() || instruction->IsRem());
3656
3657 LocationSummary* locations = instruction->GetLocations();
3658 Location second = locations->InAt(1);
3659
3660 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3661 : locations->GetTemp(0).AsRegister<CpuRegister>();
3662 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3663 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3664 : locations->Out().AsRegister<CpuRegister>();
3665 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3666
3667 DCHECK_EQ(RAX, eax.AsRegister());
3668 DCHECK_EQ(RDX, edx.AsRegister());
3669 if (instruction->IsDiv()) {
3670 DCHECK_EQ(RAX, out.AsRegister());
3671 } else {
3672 DCHECK_EQ(RDX, out.AsRegister());
3673 }
3674
3675 int64_t magic;
3676 int shift;
3677
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003678 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003679 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003680 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3681
3682 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3683
3684 __ movl(numerator, eax);
3685
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003686 __ movl(eax, Immediate(magic));
3687 __ imull(numerator);
3688
3689 if (imm > 0 && magic < 0) {
3690 __ addl(edx, numerator);
3691 } else if (imm < 0 && magic > 0) {
3692 __ subl(edx, numerator);
3693 }
3694
3695 if (shift != 0) {
3696 __ sarl(edx, Immediate(shift));
3697 }
3698
3699 __ movl(eax, edx);
3700 __ shrl(edx, Immediate(31));
3701 __ addl(edx, eax);
3702
3703 if (instruction->IsRem()) {
3704 __ movl(eax, numerator);
3705 __ imull(edx, Immediate(imm));
3706 __ subl(eax, edx);
3707 __ movl(edx, eax);
3708 } else {
3709 __ movl(eax, edx);
3710 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003711 } else {
3712 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003715
3716 CpuRegister rax = eax;
3717 CpuRegister rdx = edx;
3718
3719 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3720
3721 // Save the numerator.
3722 __ movq(numerator, rax);
3723
3724 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003725 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003726
3727 // RDX:RAX = magic * numerator
3728 __ imulq(numerator);
3729
3730 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003731 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003732 __ addq(rdx, numerator);
3733 } else if (imm < 0 && magic > 0) {
3734 // RDX -= numerator
3735 __ subq(rdx, numerator);
3736 }
3737
3738 // Shift if needed.
3739 if (shift != 0) {
3740 __ sarq(rdx, Immediate(shift));
3741 }
3742
3743 // RDX += 1 if RDX < 0
3744 __ movq(rax, rdx);
3745 __ shrq(rdx, Immediate(63));
3746 __ addq(rdx, rax);
3747
3748 if (instruction->IsRem()) {
3749 __ movq(rax, numerator);
3750
3751 if (IsInt<32>(imm)) {
3752 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3753 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003754 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003755 }
3756
3757 __ subq(rax, rdx);
3758 __ movq(rdx, rax);
3759 } else {
3760 __ movq(rax, rdx);
3761 }
3762 }
3763}
3764
Calin Juravlebacfec32014-11-14 15:54:36 +00003765void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3766 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 DataType::Type type = instruction->GetResultType();
3768 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003769
3770 bool is_div = instruction->IsDiv();
3771 LocationSummary* locations = instruction->GetLocations();
3772
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003773 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3774 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003775
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003777 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003778
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003780 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003781
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003782 if (imm == 0) {
3783 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3784 } else if (imm == 1 || imm == -1) {
3785 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303786 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3787 if (is_div) {
3788 DivByPowerOfTwo(instruction->AsDiv());
3789 } else {
3790 RemByPowerOfTwo(instruction->AsRem());
3791 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003792 } else {
3793 DCHECK(imm <= -2 || imm >= 2);
3794 GenerateDivRemWithAnyConstant(instruction);
3795 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003796 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003797 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003798 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003799 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003800 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003801
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003802 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3803 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3804 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3805 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003807 __ cmpl(second_reg, Immediate(-1));
3808 __ j(kEqual, slow_path->GetEntryLabel());
3809 // edx:eax <- sign-extended of eax
3810 __ cdq();
3811 // eax = quotient, edx = remainder
3812 __ idivl(second_reg);
3813 } else {
3814 __ cmpq(second_reg, Immediate(-1));
3815 __ j(kEqual, slow_path->GetEntryLabel());
3816 // rdx:rax <- sign-extended of rax
3817 __ cqo();
3818 // rax = quotient, rdx = remainder
3819 __ idivq(second_reg);
3820 }
3821 __ Bind(slow_path->GetExitLabel());
3822 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003823}
3824
Calin Juravle7c4954d2014-10-28 16:57:40 +00003825void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3826 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003827 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003828 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kInt32:
3830 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003831 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003832 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003833 locations->SetOut(Location::SameAsFirstInput());
3834 // Intel uses edx:eax as the dividend.
3835 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003836 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3837 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3838 // output and request another temp.
3839 if (div->InputAt(1)->IsConstant()) {
3840 locations->AddTemp(Location::RequiresRegister());
3841 }
Calin Juravled0d48522014-11-04 16:40:20 +00003842 break;
3843 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003845 case DataType::Type::kFloat32:
3846 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003847 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003848 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003849 locations->SetOut(Location::SameAsFirstInput());
3850 break;
3851 }
3852
3853 default:
3854 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3855 }
3856}
3857
3858void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3859 LocationSummary* locations = div->GetLocations();
3860 Location first = locations->InAt(0);
3861 Location second = locations->InAt(1);
3862 DCHECK(first.Equals(locations->Out()));
3863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003865 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt32:
3867 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003868 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003869 break;
3870 }
3871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003873 if (second.IsFpuRegister()) {
3874 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3875 } else if (second.IsConstant()) {
3876 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003877 codegen_->LiteralFloatAddress(
3878 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003879 } else {
3880 DCHECK(second.IsStackSlot());
3881 __ divss(first.AsFpuRegister<XmmRegister>(),
3882 Address(CpuRegister(RSP), second.GetStackIndex()));
3883 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003884 break;
3885 }
3886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003887 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003888 if (second.IsFpuRegister()) {
3889 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3890 } else if (second.IsConstant()) {
3891 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003892 codegen_->LiteralDoubleAddress(
3893 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003894 } else {
3895 DCHECK(second.IsDoubleStackSlot());
3896 __ divsd(first.AsFpuRegister<XmmRegister>(),
3897 Address(CpuRegister(RSP), second.GetStackIndex()));
3898 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003899 break;
3900 }
3901
3902 default:
3903 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3904 }
3905}
3906
Calin Juravlebacfec32014-11-14 15:54:36 +00003907void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003908 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003909 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003910 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003911
3912 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003913 case DataType::Type::kInt32:
3914 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003915 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003916 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003917 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3918 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003919 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3920 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3921 // output and request another temp.
3922 if (rem->InputAt(1)->IsConstant()) {
3923 locations->AddTemp(Location::RequiresRegister());
3924 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003925 break;
3926 }
3927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kFloat32:
3929 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003930 locations->SetInAt(0, Location::Any());
3931 locations->SetInAt(1, Location::Any());
3932 locations->SetOut(Location::RequiresFpuRegister());
3933 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003934 break;
3935 }
3936
3937 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003938 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003939 }
3940}
3941
3942void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003943 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003944 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 case DataType::Type::kInt32:
3946 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003947 GenerateDivRemIntegral(rem);
3948 break;
3949 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003950 case DataType::Type::kFloat32:
3951 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003952 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003953 break;
3954 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003955 default:
3956 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3957 }
3958}
3959
Aart Bik1f8d51b2018-02-15 10:42:37 -08003960static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3961 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3962 switch (minmax->GetResultType()) {
3963 case DataType::Type::kInt32:
3964 case DataType::Type::kInt64:
3965 locations->SetInAt(0, Location::RequiresRegister());
3966 locations->SetInAt(1, Location::RequiresRegister());
3967 locations->SetOut(Location::SameAsFirstInput());
3968 break;
3969 case DataType::Type::kFloat32:
3970 case DataType::Type::kFloat64:
3971 locations->SetInAt(0, Location::RequiresFpuRegister());
3972 locations->SetInAt(1, Location::RequiresFpuRegister());
3973 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
3974 // the second input to be the output (we can simply swap inputs).
3975 locations->SetOut(Location::SameAsFirstInput());
3976 break;
3977 default:
3978 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3979 }
3980}
3981
Aart Bik351df3e2018-03-07 11:54:57 -08003982void InstructionCodeGeneratorX86_64::GenerateMinMaxInt(LocationSummary* locations,
3983 bool is_min,
3984 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003985 Location op1_loc = locations->InAt(0);
3986 Location op2_loc = locations->InAt(1);
3987
3988 // Shortcut for same input locations.
3989 if (op1_loc.Equals(op2_loc)) {
3990 // Can return immediately, as op1_loc == out_loc.
3991 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3992 // a copy here.
3993 DCHECK(locations->Out().Equals(op1_loc));
3994 return;
3995 }
3996
3997 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3998 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
3999
4000 // (out := op1)
4001 // out <=? op2
4002 // if out is min jmp done
4003 // out := op2
4004 // done:
4005
4006 if (type == DataType::Type::kInt64) {
4007 __ cmpq(out, op2);
4008 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ true);
4009 } else {
4010 DCHECK_EQ(type, DataType::Type::kInt32);
4011 __ cmpl(out, op2);
4012 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ false);
4013 }
4014}
4015
4016void InstructionCodeGeneratorX86_64::GenerateMinMaxFP(LocationSummary* locations,
4017 bool is_min,
4018 DataType::Type type) {
4019 Location op1_loc = locations->InAt(0);
4020 Location op2_loc = locations->InAt(1);
4021 Location out_loc = locations->Out();
4022 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4023
4024 // Shortcut for same input locations.
4025 if (op1_loc.Equals(op2_loc)) {
4026 DCHECK(out_loc.Equals(op1_loc));
4027 return;
4028 }
4029
4030 // (out := op1)
4031 // out <=? op2
4032 // if Nan jmp Nan_label
4033 // if out is min jmp done
4034 // if op2 is min jmp op2_label
4035 // handle -0/+0
4036 // jmp done
4037 // Nan_label:
4038 // out := NaN
4039 // op2_label:
4040 // out := op2
4041 // done:
4042 //
4043 // This removes one jmp, but needs to copy one input (op1) to out.
4044 //
4045 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
4046
4047 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4048
4049 NearLabel nan, done, op2_label;
4050 if (type == DataType::Type::kFloat64) {
4051 __ ucomisd(out, op2);
4052 } else {
4053 DCHECK_EQ(type, DataType::Type::kFloat32);
4054 __ ucomiss(out, op2);
4055 }
4056
4057 __ j(Condition::kParityEven, &nan);
4058
4059 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4060 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4061
4062 // Handle 0.0/-0.0.
4063 if (is_min) {
4064 if (type == DataType::Type::kFloat64) {
4065 __ orpd(out, op2);
4066 } else {
4067 __ orps(out, op2);
4068 }
4069 } else {
4070 if (type == DataType::Type::kFloat64) {
4071 __ andpd(out, op2);
4072 } else {
4073 __ andps(out, op2);
4074 }
4075 }
4076 __ jmp(&done);
4077
4078 // NaN handling.
4079 __ Bind(&nan);
4080 if (type == DataType::Type::kFloat64) {
4081 __ movsd(out, codegen_->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
4082 } else {
4083 __ movss(out, codegen_->LiteralInt32Address(INT32_C(0x7FC00000)));
4084 }
4085 __ jmp(&done);
4086
4087 // out := op2;
4088 __ Bind(&op2_label);
4089 if (type == DataType::Type::kFloat64) {
4090 __ movsd(out, op2);
4091 } else {
4092 __ movss(out, op2);
4093 }
4094
4095 // Done.
4096 __ Bind(&done);
4097}
4098
Aart Bik351df3e2018-03-07 11:54:57 -08004099void InstructionCodeGeneratorX86_64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4100 DataType::Type type = minmax->GetResultType();
4101 switch (type) {
4102 case DataType::Type::kInt32:
4103 case DataType::Type::kInt64:
4104 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4105 break;
4106 case DataType::Type::kFloat32:
4107 case DataType::Type::kFloat64:
4108 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4109 break;
4110 default:
4111 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4112 }
4113}
4114
Aart Bik1f8d51b2018-02-15 10:42:37 -08004115void LocationsBuilderX86_64::VisitMin(HMin* min) {
4116 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4117}
4118
4119void InstructionCodeGeneratorX86_64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004120 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004121}
4122
4123void LocationsBuilderX86_64::VisitMax(HMax* max) {
4124 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4125}
4126
4127void InstructionCodeGeneratorX86_64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004128 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004129}
4130
Aart Bik3dad3412018-02-28 12:01:46 -08004131void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
4132 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4133 switch (abs->GetResultType()) {
4134 case DataType::Type::kInt32:
4135 case DataType::Type::kInt64:
4136 locations->SetInAt(0, Location::RequiresRegister());
4137 locations->SetOut(Location::SameAsFirstInput());
4138 locations->AddTemp(Location::RequiresRegister());
4139 break;
4140 case DataType::Type::kFloat32:
4141 case DataType::Type::kFloat64:
4142 locations->SetInAt(0, Location::RequiresFpuRegister());
4143 locations->SetOut(Location::SameAsFirstInput());
4144 locations->AddTemp(Location::RequiresFpuRegister());
4145 break;
4146 default:
4147 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4148 }
4149}
4150
4151void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
4152 LocationSummary* locations = abs->GetLocations();
4153 switch (abs->GetResultType()) {
4154 case DataType::Type::kInt32: {
4155 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4156 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4157 // Create mask.
4158 __ movl(mask, out);
4159 __ sarl(mask, Immediate(31));
4160 // Add mask.
4161 __ addl(out, mask);
4162 __ xorl(out, mask);
4163 break;
4164 }
4165 case DataType::Type::kInt64: {
4166 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4167 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4168 // Create mask.
4169 __ movq(mask, out);
4170 __ sarq(mask, Immediate(63));
4171 // Add mask.
4172 __ addq(out, mask);
4173 __ xorq(out, mask);
4174 break;
4175 }
4176 case DataType::Type::kFloat32: {
4177 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4178 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4179 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
4180 __ andps(out, mask);
4181 break;
4182 }
4183 case DataType::Type::kFloat64: {
4184 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4185 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4186 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
4187 __ andpd(out, mask);
4188 break;
4189 }
4190 default:
4191 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4192 }
4193}
4194
Calin Juravled0d48522014-11-04 16:40:20 +00004195void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004196 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004197 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00004198}
4199
4200void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004201 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004202 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004203 codegen_->AddSlowPath(slow_path);
4204
4205 LocationSummary* locations = instruction->GetLocations();
4206 Location value = locations->InAt(0);
4207
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004208 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004209 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004210 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004211 case DataType::Type::kInt8:
4212 case DataType::Type::kUint16:
4213 case DataType::Type::kInt16:
4214 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004215 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004216 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004217 __ j(kEqual, slow_path->GetEntryLabel());
4218 } else if (value.IsStackSlot()) {
4219 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4220 __ j(kEqual, slow_path->GetEntryLabel());
4221 } else {
4222 DCHECK(value.IsConstant()) << value;
4223 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004224 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004225 }
4226 }
4227 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004228 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004229 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004230 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004231 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004232 __ j(kEqual, slow_path->GetEntryLabel());
4233 } else if (value.IsDoubleStackSlot()) {
4234 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4235 __ j(kEqual, slow_path->GetEntryLabel());
4236 } else {
4237 DCHECK(value.IsConstant()) << value;
4238 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004239 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004240 }
4241 }
4242 break;
4243 }
4244 default:
4245 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004246 }
Calin Juravled0d48522014-11-04 16:40:20 +00004247}
4248
Calin Juravle9aec02f2014-11-18 23:06:35 +00004249void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
4250 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4251
4252 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004253 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004254
4255 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004256 case DataType::Type::kInt32:
4257 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004258 locations->SetInAt(0, Location::RequiresRegister());
4259 // The shift count needs to be in CL.
4260 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
4261 locations->SetOut(Location::SameAsFirstInput());
4262 break;
4263 }
4264 default:
4265 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4266 }
4267}
4268
4269void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
4270 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4271
4272 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004274 Location second = locations->InAt(1);
4275
4276 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004277 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004278 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004279 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004280 if (op->IsShl()) {
4281 __ shll(first_reg, second_reg);
4282 } else if (op->IsShr()) {
4283 __ sarl(first_reg, second_reg);
4284 } else {
4285 __ shrl(first_reg, second_reg);
4286 }
4287 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004288 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004289 if (op->IsShl()) {
4290 __ shll(first_reg, imm);
4291 } else if (op->IsShr()) {
4292 __ sarl(first_reg, imm);
4293 } else {
4294 __ shrl(first_reg, imm);
4295 }
4296 }
4297 break;
4298 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004299 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004300 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004301 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004302 if (op->IsShl()) {
4303 __ shlq(first_reg, second_reg);
4304 } else if (op->IsShr()) {
4305 __ sarq(first_reg, second_reg);
4306 } else {
4307 __ shrq(first_reg, second_reg);
4308 }
4309 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004310 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004311 if (op->IsShl()) {
4312 __ shlq(first_reg, imm);
4313 } else if (op->IsShr()) {
4314 __ sarq(first_reg, imm);
4315 } else {
4316 __ shrq(first_reg, imm);
4317 }
4318 }
4319 break;
4320 }
4321 default:
4322 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004323 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004324 }
4325}
4326
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004327void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4328 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004329 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004330
4331 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004332 case DataType::Type::kInt32:
4333 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004334 locations->SetInAt(0, Location::RequiresRegister());
4335 // The shift count needs to be in CL (unless it is a constant).
4336 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4337 locations->SetOut(Location::SameAsFirstInput());
4338 break;
4339 }
4340 default:
4341 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4342 UNREACHABLE();
4343 }
4344}
4345
4346void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4347 LocationSummary* locations = ror->GetLocations();
4348 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4349 Location second = locations->InAt(1);
4350
4351 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004352 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004353 if (second.IsRegister()) {
4354 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4355 __ rorl(first_reg, second_reg);
4356 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004357 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004358 __ rorl(first_reg, imm);
4359 }
4360 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004361 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004362 if (second.IsRegister()) {
4363 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4364 __ rorq(first_reg, second_reg);
4365 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004366 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004367 __ rorq(first_reg, imm);
4368 }
4369 break;
4370 default:
4371 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4372 UNREACHABLE();
4373 }
4374}
4375
Calin Juravle9aec02f2014-11-18 23:06:35 +00004376void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4377 HandleShift(shl);
4378}
4379
4380void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4381 HandleShift(shl);
4382}
4383
4384void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4385 HandleShift(shr);
4386}
4387
4388void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4389 HandleShift(shr);
4390}
4391
4392void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4393 HandleShift(ushr);
4394}
4395
4396void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4397 HandleShift(ushr);
4398}
4399
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004400void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004401 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4402 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004403 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07004404 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004405 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004406}
4407
4408void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004409 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4410 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4411 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004412}
4413
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004414void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004415 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4416 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004417 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004418 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004419 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4420 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004421}
4422
4423void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004424 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4425 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004426 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004427 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004428 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004429}
4430
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004431void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004432 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004433 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004434 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4435 if (location.IsStackSlot()) {
4436 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4437 } else if (location.IsDoubleStackSlot()) {
4438 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4439 }
4440 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004441}
4442
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004443void InstructionCodeGeneratorX86_64::VisitParameterValue(
4444 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004445 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004446}
4447
4448void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4449 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004450 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004451 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4452}
4453
4454void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4455 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4456 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004457}
4458
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004459void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4460 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004461 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004462 locations->SetInAt(0, Location::RequiresRegister());
4463 locations->SetOut(Location::RequiresRegister());
4464}
4465
4466void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4467 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004468 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004469 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004470 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004471 __ movq(locations->Out().AsRegister<CpuRegister>(),
4472 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004473 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004474 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004475 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004476 __ movq(locations->Out().AsRegister<CpuRegister>(),
4477 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4478 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004479 __ movq(locations->Out().AsRegister<CpuRegister>(),
4480 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004481 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004482}
4483
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004484void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004485 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004486 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004487 locations->SetInAt(0, Location::RequiresRegister());
4488 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004489}
4490
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004491void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4492 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004493 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4494 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004495 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004496 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004497 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004498 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004499 break;
4500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004501 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004502 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004503 break;
4504
4505 default:
4506 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4507 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004508}
4509
David Brazdil66d126e2015-04-03 16:02:44 +01004510void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4511 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004512 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004513 locations->SetInAt(0, Location::RequiresRegister());
4514 locations->SetOut(Location::SameAsFirstInput());
4515}
4516
4517void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004518 LocationSummary* locations = bool_not->GetLocations();
4519 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4520 locations->Out().AsRegister<CpuRegister>().AsRegister());
4521 Location out = locations->Out();
4522 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4523}
4524
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004525void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004526 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004527 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004528 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004529 locations->SetInAt(i, Location::Any());
4530 }
4531 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004532}
4533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004534void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004535 LOG(FATAL) << "Unimplemented";
4536}
4537
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004538void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004539 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004540 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004541 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004542 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4543 */
4544 switch (kind) {
4545 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004546 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004547 break;
4548 }
4549 case MemBarrierKind::kAnyStore:
4550 case MemBarrierKind::kLoadAny:
4551 case MemBarrierKind::kStoreStore: {
4552 // nop
4553 break;
4554 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004555 case MemBarrierKind::kNTStoreStore:
4556 // Non-Temporal Store/Store needs an explicit fence.
4557 MemoryFence(/* non-temporal */ true);
4558 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004559 }
4560}
4561
4562void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4563 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4564
Roland Levillain0d5a2812015-11-13 10:07:31 +00004565 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004566 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004567 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004568 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4569 object_field_get_with_read_barrier
4570 ? LocationSummary::kCallOnSlowPath
4571 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004572 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004573 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004574 }
Calin Juravle52c48962014-12-16 17:02:57 +00004575 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004576 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004577 locations->SetOut(Location::RequiresFpuRegister());
4578 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004579 // The output overlaps for an object field get when read barriers
4580 // are enabled: we do not want the move to overwrite the object's
4581 // location, as we need it to emit the read barrier.
4582 locations->SetOut(
4583 Location::RequiresRegister(),
4584 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004585 }
Calin Juravle52c48962014-12-16 17:02:57 +00004586}
4587
4588void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4589 const FieldInfo& field_info) {
4590 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4591
4592 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004593 Location base_loc = locations->InAt(0);
4594 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004595 Location out = locations->Out();
4596 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004597 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4598 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004599 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4600
Vladimir Marko61b92282017-10-11 13:23:17 +01004601 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004602 case DataType::Type::kBool:
4603 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004604 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4605 break;
4606 }
4607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004608 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004609 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4610 break;
4611 }
4612
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004613 case DataType::Type::kUint16: {
4614 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004615 break;
4616 }
4617
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004618 case DataType::Type::kInt16: {
4619 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004620 break;
4621 }
4622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004623 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004624 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4625 break;
4626 }
4627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004628 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004629 // /* HeapReference<Object> */ out = *(base + offset)
4630 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004631 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004632 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004633 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004634 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004635 if (is_volatile) {
4636 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4637 }
4638 } else {
4639 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4640 codegen_->MaybeRecordImplicitNullCheck(instruction);
4641 if (is_volatile) {
4642 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4643 }
4644 // If read barriers are enabled, emit read barriers other than
4645 // Baker's using a slow path (and also unpoison the loaded
4646 // reference, if heap poisoning is enabled).
4647 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4648 }
4649 break;
4650 }
4651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004652 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004653 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4654 break;
4655 }
4656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004657 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004658 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4659 break;
4660 }
4661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004662 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004663 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4664 break;
4665 }
4666
Aart Bik66c158e2018-01-31 12:55:04 -08004667 case DataType::Type::kUint32:
4668 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004669 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004670 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004671 UNREACHABLE();
4672 }
4673
Vladimir Marko61b92282017-10-11 13:23:17 +01004674 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004675 // Potential implicit null checks, in the case of reference
4676 // fields, are handled in the previous switch statement.
4677 } else {
4678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004679 }
Roland Levillain4d027112015-07-01 15:41:14 +01004680
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004681 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004682 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004683 // Memory barriers, in the case of references, are also handled
4684 // in the previous switch statement.
4685 } else {
4686 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4687 }
Roland Levillain4d027112015-07-01 15:41:14 +01004688 }
Calin Juravle52c48962014-12-16 17:02:57 +00004689}
4690
4691void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4692 const FieldInfo& field_info) {
4693 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4694
4695 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004696 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004697 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004698 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004699 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004700 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004701
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004702 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004703 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004704 if (is_volatile) {
4705 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4706 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4707 } else {
4708 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4709 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004710 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004711 if (is_volatile) {
4712 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4713 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4714 } else {
4715 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4716 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004717 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004718 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004719 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004720 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004721 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004722 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004723 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004724 locations->AddTemp(Location::RequiresRegister());
4725 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004726}
4727
Calin Juravle52c48962014-12-16 17:02:57 +00004728void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 const FieldInfo& field_info,
4730 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004731 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4732
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004734 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4735 Location value = locations->InAt(1);
4736 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004738 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4739
4740 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004741 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004742 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004743
Mark Mendellea5af682015-10-22 17:35:49 -04004744 bool maybe_record_implicit_null_check_done = false;
4745
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004746 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004748 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004749 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004750 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004751 __ movb(Address(base, offset),
4752 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004753 } else {
4754 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4755 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004756 break;
4757 }
4758
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004759 case DataType::Type::kUint16:
4760 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004761 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004762 __ movw(Address(base, offset),
4763 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004764 } else {
4765 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4766 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004767 break;
4768 }
4769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004770 case DataType::Type::kInt32:
4771 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004772 if (value.IsConstant()) {
4773 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004774 // `field_type == DataType::Type::kReference` implies `v == 0`.
4775 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004776 // Note: if heap poisoning is enabled, no need to poison
4777 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004778 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004779 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004781 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4782 __ movl(temp, value.AsRegister<CpuRegister>());
4783 __ PoisonHeapReference(temp);
4784 __ movl(Address(base, offset), temp);
4785 } else {
4786 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4787 }
Mark Mendell40741f32015-04-20 22:10:34 -04004788 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
4790 }
4791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004793 if (value.IsConstant()) {
4794 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004795 codegen_->MoveInt64ToAddress(Address(base, offset),
4796 Address(base, offset + sizeof(int32_t)),
4797 v,
4798 instruction);
4799 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004800 } else {
4801 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4802 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004803 break;
4804 }
4805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004806 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004807 if (value.IsConstant()) {
4808 int32_t v =
4809 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4810 __ movl(Address(base, offset), Immediate(v));
4811 } else {
4812 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4813 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004814 break;
4815 }
4816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004817 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004818 if (value.IsConstant()) {
4819 int64_t v =
4820 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4821 codegen_->MoveInt64ToAddress(Address(base, offset),
4822 Address(base, offset + sizeof(int32_t)),
4823 v,
4824 instruction);
4825 maybe_record_implicit_null_check_done = true;
4826 } else {
4827 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4828 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004829 break;
4830 }
4831
Aart Bik66c158e2018-01-31 12:55:04 -08004832 case DataType::Type::kUint32:
4833 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004835 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004836 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004837 }
Calin Juravle52c48962014-12-16 17:02:57 +00004838
Mark Mendellea5af682015-10-22 17:35:49 -04004839 if (!maybe_record_implicit_null_check_done) {
4840 codegen_->MaybeRecordImplicitNullCheck(instruction);
4841 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004842
4843 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4844 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4845 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004846 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004847 }
4848
Calin Juravle52c48962014-12-16 17:02:57 +00004849 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004850 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004851 }
4852}
4853
4854void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4855 HandleFieldSet(instruction, instruction->GetFieldInfo());
4856}
4857
4858void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004859 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004860}
4861
4862void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004863 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004864}
4865
4866void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004867 HandleFieldGet(instruction, instruction->GetFieldInfo());
4868}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004869
Calin Juravle52c48962014-12-16 17:02:57 +00004870void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4871 HandleFieldGet(instruction);
4872}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004873
Calin Juravle52c48962014-12-16 17:02:57 +00004874void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4875 HandleFieldGet(instruction, instruction->GetFieldInfo());
4876}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004877
Calin Juravle52c48962014-12-16 17:02:57 +00004878void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4879 HandleFieldSet(instruction, instruction->GetFieldInfo());
4880}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004881
Calin Juravle52c48962014-12-16 17:02:57 +00004882void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004883 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004884}
4885
Calin Juravlee460d1d2015-09-29 04:52:17 +01004886void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4887 HUnresolvedInstanceFieldGet* instruction) {
4888 FieldAccessCallingConventionX86_64 calling_convention;
4889 codegen_->CreateUnresolvedFieldLocationSummary(
4890 instruction, instruction->GetFieldType(), calling_convention);
4891}
4892
4893void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4894 HUnresolvedInstanceFieldGet* instruction) {
4895 FieldAccessCallingConventionX86_64 calling_convention;
4896 codegen_->GenerateUnresolvedFieldAccess(instruction,
4897 instruction->GetFieldType(),
4898 instruction->GetFieldIndex(),
4899 instruction->GetDexPc(),
4900 calling_convention);
4901}
4902
4903void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4904 HUnresolvedInstanceFieldSet* instruction) {
4905 FieldAccessCallingConventionX86_64 calling_convention;
4906 codegen_->CreateUnresolvedFieldLocationSummary(
4907 instruction, instruction->GetFieldType(), calling_convention);
4908}
4909
4910void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4911 HUnresolvedInstanceFieldSet* instruction) {
4912 FieldAccessCallingConventionX86_64 calling_convention;
4913 codegen_->GenerateUnresolvedFieldAccess(instruction,
4914 instruction->GetFieldType(),
4915 instruction->GetFieldIndex(),
4916 instruction->GetDexPc(),
4917 calling_convention);
4918}
4919
4920void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4921 HUnresolvedStaticFieldGet* instruction) {
4922 FieldAccessCallingConventionX86_64 calling_convention;
4923 codegen_->CreateUnresolvedFieldLocationSummary(
4924 instruction, instruction->GetFieldType(), calling_convention);
4925}
4926
4927void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4928 HUnresolvedStaticFieldGet* instruction) {
4929 FieldAccessCallingConventionX86_64 calling_convention;
4930 codegen_->GenerateUnresolvedFieldAccess(instruction,
4931 instruction->GetFieldType(),
4932 instruction->GetFieldIndex(),
4933 instruction->GetDexPc(),
4934 calling_convention);
4935}
4936
4937void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4938 HUnresolvedStaticFieldSet* instruction) {
4939 FieldAccessCallingConventionX86_64 calling_convention;
4940 codegen_->CreateUnresolvedFieldLocationSummary(
4941 instruction, instruction->GetFieldType(), calling_convention);
4942}
4943
4944void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4945 HUnresolvedStaticFieldSet* instruction) {
4946 FieldAccessCallingConventionX86_64 calling_convention;
4947 codegen_->GenerateUnresolvedFieldAccess(instruction,
4948 instruction->GetFieldType(),
4949 instruction->GetFieldIndex(),
4950 instruction->GetDexPc(),
4951 calling_convention);
4952}
4953
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004955 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4956 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4957 ? Location::RequiresRegister()
4958 : Location::Any();
4959 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960}
4961
Calin Juravle2ae48182016-03-16 14:05:09 +00004962void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4963 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004964 return;
4965 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004966 LocationSummary* locations = instruction->GetLocations();
4967 Location obj = locations->InAt(0);
4968
4969 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004970 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004971}
4972
Calin Juravle2ae48182016-03-16 14:05:09 +00004973void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004974 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004975 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976
4977 LocationSummary* locations = instruction->GetLocations();
4978 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979
4980 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004981 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004982 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004983 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004984 } else {
4985 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004986 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004987 __ jmp(slow_path->GetEntryLabel());
4988 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989 }
4990 __ j(kEqual, slow_path->GetEntryLabel());
4991}
4992
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004993void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004994 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004995}
4996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004997void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004998 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004999 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005000 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005001 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5002 object_array_get_with_read_barrier
5003 ? LocationSummary::kCallOnSlowPath
5004 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005005 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005006 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005007 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005008 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005010 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005011 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5012 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005013 // The output overlaps for an object array get when read barriers
5014 // are enabled: we do not want the move to overwrite the array's
5015 // location, as we need it to emit the read barrier.
5016 locations->SetOut(
5017 Location::RequiresRegister(),
5018 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005019 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020}
5021
5022void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
5023 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005024 Location obj_loc = locations->InAt(0);
5025 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005027 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005028 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005030 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01005031 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005032 case DataType::Type::kBool:
5033 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005034 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005035 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 break;
5037 }
5038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005039 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005040 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005041 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 break;
5043 }
5044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005045 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005046 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07005047 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5048 // Branch cases into compressed and uncompressed for each index's type.
5049 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5050 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005051 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005052 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005053 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5054 "Expecting 0=compressed, 1=uncompressed");
5055 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005056 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
5057 __ jmp(&done);
5058 __ Bind(&not_compressed);
5059 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5060 __ Bind(&done);
5061 } else {
5062 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5063 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 break;
5065 }
5066
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005067 case DataType::Type::kInt16: {
5068 CpuRegister out = out_loc.AsRegister<CpuRegister>();
5069 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5070 break;
5071 }
5072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005073 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005074 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005075 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 break;
5077 }
5078
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005080 static_assert(
5081 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5082 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005083 // /* HeapReference<Object> */ out =
5084 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5085 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005086 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005087 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005088 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005089 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005090 } else {
5091 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005092 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
5093 codegen_->MaybeRecordImplicitNullCheck(instruction);
5094 // If read barriers are enabled, emit read barriers other than
5095 // Baker's using a slow path (and also unpoison the loaded
5096 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005097 if (index.IsConstant()) {
5098 uint32_t offset =
5099 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005100 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5101 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005102 codegen_->MaybeGenerateReadBarrierSlow(
5103 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5104 }
5105 }
5106 break;
5107 }
5108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005109 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005110 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005111 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 break;
5113 }
5114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005115 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005116 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005117 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005118 break;
5119 }
5120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005122 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005123 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005124 break;
5125 }
5126
Aart Bik66c158e2018-01-31 12:55:04 -08005127 case DataType::Type::kUint32:
5128 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005129 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005130 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005131 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 }
Roland Levillain4d027112015-07-01 15:41:14 +01005133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005134 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005135 // Potential implicit null checks, in the case of reference
5136 // arrays, are handled in the previous switch statement.
5137 } else {
5138 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140}
5141
5142void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005143 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144
5145 bool needs_write_barrier =
5146 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005147 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005148
Vladimir Markoca6fff82017-10-03 14:49:14 +01005149 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005150 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005151 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005152 LocationSummary::kCallOnSlowPath :
5153 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005154
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005155 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04005156 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005157 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04005158 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 } else {
5160 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5161 }
5162
5163 if (needs_write_barrier) {
5164 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005165 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005166 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005168}
5169
5170void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
5171 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005172 Location array_loc = locations->InAt(0);
5173 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005175 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005177 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005178 bool needs_write_barrier =
5179 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005180 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5181 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5182 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183
5184 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005186 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005187 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005188 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005189 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005190 if (value.IsRegister()) {
5191 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005193 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005194 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005195 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 break;
5197 }
5198
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005199 case DataType::Type::kUint16:
5200 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005201 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005202 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005203 if (value.IsRegister()) {
5204 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005206 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01005207 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 break;
5211 }
5212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005213 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005214 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005215 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005216
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005217 if (!value.IsRegister()) {
5218 // Just setting null.
5219 DCHECK(instruction->InputAt(2)->IsNullConstant());
5220 DCHECK(value.IsConstant()) << value;
5221 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005223 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005224 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005225 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005226 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005227
5228 DCHECK(needs_write_barrier);
5229 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005230 // We cannot use a NearLabel for `done`, as its range may be too
5231 // short when Baker read barriers are enabled.
5232 Label done;
5233 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005234 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005235 Location temp_loc = locations->GetTemp(0);
5236 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005237 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005238 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 codegen_->AddSlowPath(slow_path);
5240 if (instruction->GetValueCanBeNull()) {
5241 __ testl(register_value, register_value);
5242 __ j(kNotEqual, &not_null);
5243 __ movl(address, Immediate(0));
5244 codegen_->MaybeRecordImplicitNullCheck(instruction);
5245 __ jmp(&done);
5246 __ Bind(&not_null);
5247 }
5248
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005249 // Note that when Baker read barriers are enabled, the type
5250 // checks are performed without read barriers. This is fine,
5251 // even in the case where a class object is in the from-space
5252 // after the flip, as a comparison involving such a type would
5253 // not produce a false positive; it may of course produce a
5254 // false negative, in which case we would take the ArraySet
5255 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005256
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005257 // /* HeapReference<Class> */ temp = array->klass_
5258 __ movl(temp, Address(array, class_offset));
5259 codegen_->MaybeRecordImplicitNullCheck(instruction);
5260 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005261
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005262 // /* HeapReference<Class> */ temp = temp->component_type_
5263 __ movl(temp, Address(temp, component_offset));
5264 // If heap poisoning is enabled, no need to unpoison `temp`
5265 // nor the object reference in `register_value->klass`, as
5266 // we are comparing two poisoned references.
5267 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005268
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005269 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5270 __ j(kEqual, &do_put);
5271 // If heap poisoning is enabled, the `temp` reference has
5272 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005273 __ MaybeUnpoisonHeapReference(temp);
5274
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005275 // If heap poisoning is enabled, no need to unpoison the
5276 // heap reference loaded below, as it is only used for a
5277 // comparison with null.
5278 __ cmpl(Address(temp, super_offset), Immediate(0));
5279 __ j(kNotEqual, slow_path->GetEntryLabel());
5280 __ Bind(&do_put);
5281 } else {
5282 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 }
5284 }
5285
5286 if (kPoisonHeapReferences) {
5287 __ movl(temp, register_value);
5288 __ PoisonHeapReference(temp);
5289 __ movl(address, temp);
5290 } else {
5291 __ movl(address, register_value);
5292 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005293 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 codegen_->MaybeRecordImplicitNullCheck(instruction);
5295 }
5296
5297 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5298 codegen_->MarkGCCard(
5299 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
5300 __ Bind(&done);
5301
5302 if (slow_path != nullptr) {
5303 __ Bind(slow_path->GetExitLabel());
5304 }
5305
5306 break;
5307 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005309 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005311 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 if (value.IsRegister()) {
5313 __ movl(address, value.AsRegister<CpuRegister>());
5314 } else {
5315 DCHECK(value.IsConstant()) << value;
5316 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5317 __ movl(address, Immediate(v));
5318 }
5319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 break;
5321 }
5322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005325 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 if (value.IsRegister()) {
5327 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005328 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005331 Address address_high =
5332 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005333 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 }
5335 break;
5336 }
5337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005338 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005339 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005340 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005341 if (value.IsFpuRegister()) {
5342 __ movss(address, value.AsFpuRegister<XmmRegister>());
5343 } else {
5344 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005345 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005346 __ movl(address, Immediate(v));
5347 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005348 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005349 break;
5350 }
5351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005352 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005354 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005355 if (value.IsFpuRegister()) {
5356 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5357 codegen_->MaybeRecordImplicitNullCheck(instruction);
5358 } else {
5359 int64_t v =
5360 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005361 Address address_high =
5362 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005363 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5364 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005365 break;
5366 }
5367
Aart Bik66c158e2018-01-31 12:55:04 -08005368 case DataType::Type::kUint32:
5369 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005370 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005372 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 }
5374}
5375
5376void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005377 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005378 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005379 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005380 if (!instruction->IsEmittedAtUseSite()) {
5381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5382 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383}
5384
5385void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005386 if (instruction->IsEmittedAtUseSite()) {
5387 return;
5388 }
5389
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005391 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005392 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5393 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005395 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005396 // Mask out most significant bit in case the array is String's array of char.
5397 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005398 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005399 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400}
5401
5402void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005403 RegisterSet caller_saves = RegisterSet::Empty();
5404 InvokeRuntimeCallingConvention calling_convention;
5405 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5406 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5407 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005408 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005409 HInstruction* length = instruction->InputAt(1);
5410 if (!length->IsEmittedAtUseSite()) {
5411 locations->SetInAt(1, Location::RegisterOrConstant(length));
5412 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413}
5414
5415void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5416 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005417 Location index_loc = locations->InAt(0);
5418 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005419 SlowPathCode* slow_path =
5420 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421
Mark Mendell99dbd682015-04-22 16:18:52 -04005422 if (length_loc.IsConstant()) {
5423 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5424 if (index_loc.IsConstant()) {
5425 // BCE will remove the bounds check if we are guarenteed to pass.
5426 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5427 if (index < 0 || index >= length) {
5428 codegen_->AddSlowPath(slow_path);
5429 __ jmp(slow_path->GetEntryLabel());
5430 } else {
5431 // Some optimization after BCE may have generated this, and we should not
5432 // generate a bounds check if it is a valid range.
5433 }
5434 return;
5435 }
5436
5437 // We have to reverse the jump condition because the length is the constant.
5438 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5439 __ cmpl(index_reg, Immediate(length));
5440 codegen_->AddSlowPath(slow_path);
5441 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005442 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005443 HInstruction* array_length = instruction->InputAt(1);
5444 if (array_length->IsEmittedAtUseSite()) {
5445 // Address the length field in the array.
5446 DCHECK(array_length->IsArrayLength());
5447 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5448 Location array_loc = array_length->GetLocations()->InAt(0);
5449 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005450 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005451 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5452 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005453 CpuRegister length_reg = CpuRegister(TMP);
5454 __ movl(length_reg, array_len);
5455 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005456 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005457 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005458 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005459 // Checking the bound for general case:
5460 // Array of char or String's array when the compression feature off.
5461 if (index_loc.IsConstant()) {
5462 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5463 __ cmpl(array_len, Immediate(value));
5464 } else {
5465 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5466 }
5467 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005468 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005469 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005470 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005471 }
5472 codegen_->AddSlowPath(slow_path);
5473 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005474 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005475}
5476
5477void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5478 CpuRegister card,
5479 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005480 CpuRegister value,
5481 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005482 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005483 if (value_can_be_null) {
5484 __ testl(value, value);
5485 __ j(kEqual, &is_null);
5486 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005487 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005488 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005489 /* no_rip */ true));
Roland Levillainc73f0522018-08-14 15:16:50 +01005490 // Calculate the offset (in the card table) of the card corresponding to
5491 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492 __ movq(temp, object);
5493 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005494 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5495 // `object`'s card.
5496 //
5497 // Register `card` contains the address of the card table. Note that the card
5498 // table's base is biased during its creation so that it always starts at an
5499 // address whose least-significant byte is equal to `kCardDirty` (see
5500 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5501 // below writes the `kCardDirty` (byte) value into the `object`'s card
5502 // (located at `card + object >> kCardShift`).
5503 //
5504 // This dual use of the value in register `card` (1. to calculate the location
5505 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5506 // (no need to explicitly load `kCardDirty` as an immediate value).
Roland Levillain4d027112015-07-01 15:41:14 +01005507 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005508 if (value_can_be_null) {
5509 __ Bind(&is_null);
5510 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005511}
5512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005513void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005514 LOG(FATAL) << "Unimplemented";
5515}
5516
5517void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005518 if (instruction->GetNext()->IsSuspendCheck() &&
5519 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5520 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5521 // The back edge will generate the suspend check.
5522 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5523 }
5524
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005525 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5526}
5527
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005528void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005529 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5530 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005531 // In suspend check slow path, usually there are no caller-save registers at all.
5532 // If SIMD instructions are present, however, we force spilling all live SIMD
5533 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005534 locations->SetCustomSlowPathCallerSaves(
5535 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005536}
5537
5538void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005539 HBasicBlock* block = instruction->GetBlock();
5540 if (block->GetLoopInformation() != nullptr) {
5541 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5542 // The back edge will generate the suspend check.
5543 return;
5544 }
5545 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5546 // The goto will generate the suspend check.
5547 return;
5548 }
5549 GenerateSuspendCheck(instruction, nullptr);
5550}
5551
5552void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5553 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005554 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005555 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5556 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005557 slow_path =
5558 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005559 instruction->SetSlowPath(slow_path);
5560 codegen_->AddSlowPath(slow_path);
5561 if (successor != nullptr) {
5562 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005563 }
5564 } else {
5565 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5566 }
5567
Andreas Gampe542451c2016-07-26 09:02:02 -07005568 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005569 /* no_rip */ true),
5570 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005571 if (successor == nullptr) {
5572 __ j(kNotEqual, slow_path->GetEntryLabel());
5573 __ Bind(slow_path->GetReturnLabel());
5574 } else {
5575 __ j(kEqual, codegen_->GetLabelOf(successor));
5576 __ jmp(slow_path->GetEntryLabel());
5577 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005578}
5579
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005580X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5581 return codegen_->GetAssembler();
5582}
5583
5584void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005585 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005586 Location source = move->GetSource();
5587 Location destination = move->GetDestination();
5588
5589 if (source.IsRegister()) {
5590 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005591 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005592 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005593 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005594 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005595 } else {
5596 DCHECK(destination.IsDoubleStackSlot());
5597 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005598 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005599 }
5600 } else if (source.IsStackSlot()) {
5601 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005602 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005603 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005604 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005605 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005606 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005607 } else {
5608 DCHECK(destination.IsStackSlot());
5609 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5610 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5611 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005612 } else if (source.IsDoubleStackSlot()) {
5613 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005614 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005615 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005616 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005617 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5618 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005619 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005620 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005621 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5622 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5623 }
Aart Bik5576f372017-03-23 16:17:37 -07005624 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005625 if (destination.IsFpuRegister()) {
5626 __ movups(destination.AsFpuRegister<XmmRegister>(),
5627 Address(CpuRegister(RSP), source.GetStackIndex()));
5628 } else {
5629 DCHECK(destination.IsSIMDStackSlot());
5630 size_t high = kX86_64WordSize;
5631 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5632 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5633 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5634 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5635 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005636 } else if (source.IsConstant()) {
5637 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005638 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5639 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005640 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005641 if (value == 0) {
5642 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5643 } else {
5644 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5645 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005646 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005647 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005648 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005649 }
5650 } else if (constant->IsLongConstant()) {
5651 int64_t value = constant->AsLongConstant()->GetValue();
5652 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005653 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005654 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005655 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005656 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005657 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005658 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005659 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005660 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005661 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005662 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005663 } else {
5664 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005665 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005666 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5667 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005668 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005669 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005670 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005671 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005672 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005673 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005674 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005675 } else {
5676 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005677 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005678 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005679 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005680 } else if (source.IsFpuRegister()) {
5681 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005682 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005683 } else if (destination.IsStackSlot()) {
5684 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005685 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005686 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005687 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005688 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005689 } else {
5690 DCHECK(destination.IsSIMDStackSlot());
5691 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5692 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005693 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005694 }
5695}
5696
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005697void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005698 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005699 __ movl(Address(CpuRegister(RSP), mem), reg);
5700 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005701}
5702
Mark Mendell8a1c7282015-06-29 15:41:28 -04005703void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5704 __ movq(CpuRegister(TMP), reg1);
5705 __ movq(reg1, reg2);
5706 __ movq(reg2, CpuRegister(TMP));
5707}
5708
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005709void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5710 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5711 __ movq(Address(CpuRegister(RSP), mem), reg);
5712 __ movq(reg, CpuRegister(TMP));
5713}
5714
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005715void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5716 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5717 __ movss(Address(CpuRegister(RSP), mem), reg);
5718 __ movd(reg, CpuRegister(TMP));
5719}
5720
5721void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5722 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5723 __ movsd(Address(CpuRegister(RSP), mem), reg);
5724 __ movd(reg, CpuRegister(TMP));
5725}
5726
Aart Bikcfe50bb2017-12-12 14:54:12 -08005727void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5728 size_t extra_slot = 2 * kX86_64WordSize;
5729 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5730 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5731 ExchangeMemory64(0, mem + extra_slot, 2);
5732 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5733 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5734}
5735
5736void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5737 ScratchRegisterScope ensure_scratch(
5738 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5739
5740 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5741 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5742 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5743 Address(CpuRegister(RSP), mem2 + stack_offset));
5744 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5745 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5746 CpuRegister(ensure_scratch.GetRegister()));
5747}
5748
5749void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5750 ScratchRegisterScope ensure_scratch(
5751 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5752
5753 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5754
5755 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5756 for (int i = 0; i < num_of_qwords; i++) {
5757 __ movq(CpuRegister(TMP),
5758 Address(CpuRegister(RSP), mem1 + stack_offset));
5759 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5760 Address(CpuRegister(RSP), mem2 + stack_offset));
5761 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5762 CpuRegister(TMP));
5763 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5764 CpuRegister(ensure_scratch.GetRegister()));
5765 stack_offset += kX86_64WordSize;
5766 }
5767}
5768
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005769void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005770 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005771 Location source = move->GetSource();
5772 Location destination = move->GetDestination();
5773
5774 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005775 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005776 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005777 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005778 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005779 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005780 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005781 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005782 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005783 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005784 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005785 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005786 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005787 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005788 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005789 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5790 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5791 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005792 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005793 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005794 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005795 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005796 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005797 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005798 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005799 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005800 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5801 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5802 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5803 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5804 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5805 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005806 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005807 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005808 }
5809}
5810
5811
5812void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5813 __ pushq(CpuRegister(reg));
5814}
5815
5816
5817void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5818 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005819}
5820
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005821void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005822 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005823 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5824 const size_t status_byte_offset =
5825 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5826 constexpr uint32_t shifted_initialized_value =
5827 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5828
5829 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005830 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005831 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005832 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005833}
5834
Vladimir Marko175e7862018-03-27 09:03:13 +00005835void InstructionCodeGeneratorX86_64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
5836 CpuRegister temp) {
5837 uint32_t path_to_root = check->GetBitstringPathToRoot();
5838 uint32_t mask = check->GetBitstringMask();
5839 DCHECK(IsPowerOfTwo(mask + 1));
5840 size_t mask_bits = WhichPowerOf2(mask + 1);
5841
5842 if (mask_bits == 16u) {
5843 // Compare the bitstring in memory.
5844 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
5845 } else {
5846 // /* uint32_t */ temp = temp->status_
5847 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
5848 // Compare the bitstring bits using SUB.
5849 __ subl(temp, Immediate(path_to_root));
5850 // Shift out bits that do not contribute to the comparison.
5851 __ shll(temp, Immediate(32u - mask_bits));
5852 }
5853}
5854
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5856 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005857 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005858 case HLoadClass::LoadKind::kInvalid:
5859 LOG(FATAL) << "UNREACHABLE";
5860 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005861 case HLoadClass::LoadKind::kReferrersClass:
5862 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005863 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005864 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005865 case HLoadClass::LoadKind::kBssEntry:
5866 DCHECK(!Runtime::Current()->UseJitCompilation());
5867 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005868 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005869 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005870 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005871 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005872 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005873 break;
5874 }
5875 return desired_class_load_kind;
5876}
5877
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005878void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005879 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005880 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005881 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005882 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005883 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005884 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005885 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005886 return;
5887 }
Vladimir Marko41559982017-01-06 14:04:23 +00005888 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005889
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005890 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5891 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005892 ? LocationSummary::kCallOnSlowPath
5893 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005894 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005895 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005896 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005897 }
5898
Vladimir Marko41559982017-01-06 14:04:23 +00005899 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005900 locations->SetInAt(0, Location::RequiresRegister());
5901 }
5902 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005903 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5904 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5905 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005906 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005907 } else {
5908 // For non-Baker read barrier we have a temp-clobbering call.
5909 }
5910 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005911}
5912
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005913Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005914 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005915 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005916 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005917 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005918 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005919 PatchInfo<Label>* info = &jit_class_patches_.back();
5920 return &info->label;
5921}
5922
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005923// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5924// move.
5925void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005926 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005927 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005928 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005929 return;
5930 }
Vladimir Marko41559982017-01-06 14:04:23 +00005931 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005932
Vladimir Marko41559982017-01-06 14:04:23 +00005933 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934 Location out_loc = locations->Out();
5935 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005937 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5938 ? kWithoutReadBarrier
5939 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005940 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005941 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005942 case HLoadClass::LoadKind::kReferrersClass: {
5943 DCHECK(!cls->CanCallRuntime());
5944 DCHECK(!cls->MustGenerateClinitCheck());
5945 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5946 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5947 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005948 cls,
5949 out_loc,
5950 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005951 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005952 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005953 break;
5954 }
5955 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005956 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005957 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005958 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005959 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005960 break;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005961 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005962 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5963 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005964 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005965 break;
5966 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005967 case HLoadClass::LoadKind::kBssEntry: {
5968 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5969 /* no_rip */ false);
5970 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5971 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5972 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5973 generate_null_check = true;
5974 break;
5975 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005976 case HLoadClass::LoadKind::kJitBootImageAddress: {
5977 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5978 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5979 DCHECK_NE(address, 0u);
5980 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
5981 break;
5982 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005983 case HLoadClass::LoadKind::kJitTableAddress: {
5984 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5985 /* no_rip */ true);
5986 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005987 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005988 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005989 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005990 break;
5991 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005992 default:
5993 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5994 UNREACHABLE();
5995 }
5996
5997 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5998 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005999 SlowPathCode* slow_path =
6000 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006001 codegen_->AddSlowPath(slow_path);
6002 if (generate_null_check) {
6003 __ testl(out, out);
6004 __ j(kEqual, slow_path->GetEntryLabel());
6005 }
6006 if (cls->MustGenerateClinitCheck()) {
6007 GenerateClassInitializationCheck(slow_path, out);
6008 } else {
6009 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006010 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006011 }
6012}
6013
6014void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
6015 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006016 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006017 locations->SetInAt(0, Location::RequiresRegister());
6018 if (check->HasUses()) {
6019 locations->SetOut(Location::SameAsFirstInput());
6020 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006021 // Rely on the type initialization to save everything we need.
6022 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006023}
6024
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006025void LocationsBuilderX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6026 // Custom calling convention: RAX serves as both input and output.
6027 Location location = Location::RegisterLocation(RAX);
6028 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6029}
6030
6031void InstructionCodeGeneratorX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6032 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6033}
6034
Orion Hodson18259d72018-04-12 11:18:23 +01006035void LocationsBuilderX86_64::VisitLoadMethodType(HLoadMethodType* load) {
6036 // Custom calling convention: RAX serves as both input and output.
6037 Location location = Location::RegisterLocation(RAX);
6038 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6039}
6040
6041void InstructionCodeGeneratorX86_64::VisitLoadMethodType(HLoadMethodType* load) {
6042 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6043}
6044
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006046 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006047 SlowPathCode* slow_path =
6048 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006049 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006050 GenerateClassInitializationCheck(slow_path,
6051 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006052}
6053
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006054HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
6055 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006056 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006057 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006058 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006059 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006060 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006061 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006062 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006063 case HLoadString::LoadKind::kJitTableAddress:
6064 DCHECK(Runtime::Current()->UseJitCompilation());
6065 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006066 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006067 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006068 }
6069 return desired_string_load_kind;
6070}
6071
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006072void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006073 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006074 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006075 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006076 locations->SetOut(Location::RegisterLocation(RAX));
6077 } else {
6078 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006079 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
6080 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006081 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006082 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006083 } else {
6084 // For non-Baker read barrier we have a temp-clobbering call.
6085 }
6086 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006087 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006088}
6089
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006090Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006091 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006092 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006093 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006094 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006095 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006096 PatchInfo<Label>* info = &jit_string_patches_.back();
6097 return &info->label;
6098}
6099
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006100// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6101// move.
6102void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006103 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 Location out_loc = locations->Out();
6105 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006107 switch (load->GetLoadKind()) {
6108 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006109 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006110 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006111 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006112 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006113 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006114 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006115 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6116 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006117 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006118 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006119 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006120 case HLoadString::LoadKind::kBssEntry: {
6121 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6122 /* no_rip */ false);
6123 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6124 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006125 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006126 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006127 codegen_->AddSlowPath(slow_path);
6128 __ testl(out, out);
6129 __ j(kEqual, slow_path->GetEntryLabel());
6130 __ Bind(slow_path->GetExitLabel());
6131 return;
6132 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006133 case HLoadString::LoadKind::kJitBootImageAddress: {
6134 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6135 DCHECK_NE(address, 0u);
6136 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
6137 return;
6138 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006139 case HLoadString::LoadKind::kJitTableAddress: {
6140 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6141 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006142 Label* fixup_label = codegen_->NewJitRootStringPatch(
6143 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006144 // /* GcRoot<mirror::String> */ out = *address
6145 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6146 return;
6147 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006148 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006149 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006150 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006151
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006152 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006153 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006154 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006155 codegen_->InvokeRuntime(kQuickResolveString,
6156 load,
6157 load->GetDexPc());
6158 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006159}
6160
David Brazdilcb1c0552015-08-04 16:22:25 +01006161static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006162 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006163 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01006164}
6165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006166void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
6167 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006168 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006169 locations->SetOut(Location::RequiresRegister());
6170}
6171
6172void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006173 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
6174}
6175
6176void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006177 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006178}
6179
6180void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6181 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006182}
6183
6184void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006185 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6186 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006187 InvokeRuntimeCallingConvention calling_convention;
6188 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6189}
6190
6191void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006192 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006193 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006194}
6195
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006196// Temp is used for read barrier.
6197static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6198 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006199 !kUseBakerReadBarrier &&
6200 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006201 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006202 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6203 return 1;
6204 }
6205 return 0;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006206}
6207
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006208// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6209// interface pointer, the current interface is compared in memory.
6210// The other checks have one temp for loading the object's class.
6211static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6212 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6213 return 2;
6214 }
6215 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006216}
6217
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006218void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006219 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006221 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 case TypeCheckKind::kExactCheck:
6224 case TypeCheckKind::kAbstractClassCheck:
6225 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006226 case TypeCheckKind::kArrayObjectCheck: {
6227 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6228 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6229 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006230 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006231 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006233 case TypeCheckKind::kUnresolvedCheck:
6234 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006235 call_kind = LocationSummary::kCallOnSlowPath;
6236 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006237 case TypeCheckKind::kBitstringCheck:
6238 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006240
Vladimir Markoca6fff82017-10-03 14:49:14 +01006241 LocationSummary* locations =
6242 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006243 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006244 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006245 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006247 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6248 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6249 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6250 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6251 } else {
6252 locations->SetInAt(1, Location::Any());
6253 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
6255 locations->SetOut(Location::RequiresRegister());
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006256 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006257}
6258
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006259void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006260 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006261 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 Location obj_loc = locations->InAt(0);
6263 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006264 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 Location out_loc = locations->Out();
6266 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006267 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6268 DCHECK_LE(num_temps, 1u);
6269 Location maybe_temp_loc = (num_temps >= 1u) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006270 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6272 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6273 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006274 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006275 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006276
6277 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006278 // Avoid null check if we know obj is not null.
6279 if (instruction->MustDoNullCheck()) {
6280 __ testl(obj, obj);
6281 __ j(kEqual, &zero);
6282 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006283
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006284 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006286 ReadBarrierOption read_barrier_option =
6287 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006288 // /* HeapReference<Class> */ out = obj->klass_
6289 GenerateReferenceLoadTwoRegisters(instruction,
6290 out_loc,
6291 obj_loc,
6292 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006293 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006294 if (cls.IsRegister()) {
6295 __ cmpl(out, cls.AsRegister<CpuRegister>());
6296 } else {
6297 DCHECK(cls.IsStackSlot()) << cls;
6298 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6299 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006300 if (zero.IsLinked()) {
6301 // Classes must be equal for the instanceof to succeed.
6302 __ j(kNotEqual, &zero);
6303 __ movl(out, Immediate(1));
6304 __ jmp(&done);
6305 } else {
6306 __ setcc(kEqual, out);
6307 // setcc only sets the low byte.
6308 __ andl(out, Immediate(1));
6309 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 break;
6311 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006314 ReadBarrierOption read_barrier_option =
6315 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006316 // /* HeapReference<Class> */ out = obj->klass_
6317 GenerateReferenceLoadTwoRegisters(instruction,
6318 out_loc,
6319 obj_loc,
6320 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006321 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322 // If the class is abstract, we eagerly fetch the super class of the
6323 // object to avoid doing a comparison we know will fail.
6324 NearLabel loop, success;
6325 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006326 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006327 GenerateReferenceLoadOneRegister(instruction,
6328 out_loc,
6329 super_offset,
6330 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006331 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 __ testl(out, out);
6333 // If `out` is null, we use it for the result, and jump to `done`.
6334 __ j(kEqual, &done);
6335 if (cls.IsRegister()) {
6336 __ cmpl(out, cls.AsRegister<CpuRegister>());
6337 } else {
6338 DCHECK(cls.IsStackSlot()) << cls;
6339 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6340 }
6341 __ j(kNotEqual, &loop);
6342 __ movl(out, Immediate(1));
6343 if (zero.IsLinked()) {
6344 __ jmp(&done);
6345 }
6346 break;
6347 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006350 ReadBarrierOption read_barrier_option =
6351 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006352 // /* HeapReference<Class> */ out = obj->klass_
6353 GenerateReferenceLoadTwoRegisters(instruction,
6354 out_loc,
6355 obj_loc,
6356 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006357 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006358 // Walk over the class hierarchy to find a match.
6359 NearLabel loop, success;
6360 __ Bind(&loop);
6361 if (cls.IsRegister()) {
6362 __ cmpl(out, cls.AsRegister<CpuRegister>());
6363 } else {
6364 DCHECK(cls.IsStackSlot()) << cls;
6365 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6366 }
6367 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006369 GenerateReferenceLoadOneRegister(instruction,
6370 out_loc,
6371 super_offset,
6372 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006373 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006374 __ testl(out, out);
6375 __ j(kNotEqual, &loop);
6376 // If `out` is null, we use it for the result, and jump to `done`.
6377 __ jmp(&done);
6378 __ Bind(&success);
6379 __ movl(out, Immediate(1));
6380 if (zero.IsLinked()) {
6381 __ jmp(&done);
6382 }
6383 break;
6384 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006387 ReadBarrierOption read_barrier_option =
6388 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006389 // /* HeapReference<Class> */ out = obj->klass_
6390 GenerateReferenceLoadTwoRegisters(instruction,
6391 out_loc,
6392 obj_loc,
6393 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006394 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006395 // Do an exact check.
6396 NearLabel exact_check;
6397 if (cls.IsRegister()) {
6398 __ cmpl(out, cls.AsRegister<CpuRegister>());
6399 } else {
6400 DCHECK(cls.IsStackSlot()) << cls;
6401 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6402 }
6403 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006406 GenerateReferenceLoadOneRegister(instruction,
6407 out_loc,
6408 component_offset,
6409 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006410 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411 __ testl(out, out);
6412 // If `out` is null, we use it for the result, and jump to `done`.
6413 __ j(kEqual, &done);
6414 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6415 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006416 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 __ movl(out, Immediate(1));
6418 __ jmp(&done);
6419 break;
6420 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006423 // No read barrier since the slow path will retry upon failure.
6424 // /* HeapReference<Class> */ out = obj->klass_
6425 GenerateReferenceLoadTwoRegisters(instruction,
6426 out_loc,
6427 obj_loc,
6428 class_offset,
6429 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 if (cls.IsRegister()) {
6431 __ cmpl(out, cls.AsRegister<CpuRegister>());
6432 } else {
6433 DCHECK(cls.IsStackSlot()) << cls;
6434 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6435 }
6436 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006437 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6438 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 codegen_->AddSlowPath(slow_path);
6440 __ j(kNotEqual, slow_path->GetEntryLabel());
6441 __ movl(out, Immediate(1));
6442 if (zero.IsLinked()) {
6443 __ jmp(&done);
6444 }
6445 break;
6446 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447
Calin Juravle98893e12015-10-02 21:05:03 +01006448 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 case TypeCheckKind::kInterfaceCheck: {
6450 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006451 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006452 // cases.
6453 //
6454 // We cannot directly call the InstanceofNonTrivial runtime
6455 // entry point without resorting to a type checking slow path
6456 // here (i.e. by calling InvokeRuntime directly), as it would
6457 // require to assign fixed registers for the inputs of this
6458 // HInstanceOf instruction (following the runtime calling
6459 // convention), which might be cluttered by the potential first
6460 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461 //
6462 // TODO: Introduce a new runtime entry point taking the object
6463 // to test (instead of its class) as argument, and let it deal
6464 // with the read barrier issues. This will let us refactor this
6465 // case of the `switch` code as it was previously (with a direct
6466 // call to the runtime not using a type checking slow path).
6467 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006469 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6470 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471 codegen_->AddSlowPath(slow_path);
6472 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 if (zero.IsLinked()) {
6474 __ jmp(&done);
6475 }
6476 break;
6477 }
Vladimir Marko175e7862018-03-27 09:03:13 +00006478
6479 case TypeCheckKind::kBitstringCheck: {
6480 // /* HeapReference<Class> */ temp = obj->klass_
6481 GenerateReferenceLoadTwoRegisters(instruction,
6482 out_loc,
6483 obj_loc,
6484 class_offset,
6485 kWithoutReadBarrier);
6486
6487 GenerateBitstringTypeCheckCompare(instruction, out);
6488 if (zero.IsLinked()) {
6489 __ j(kNotEqual, &zero);
6490 __ movl(out, Immediate(1));
6491 __ jmp(&done);
6492 } else {
6493 __ setcc(kEqual, out);
6494 // setcc only sets the low byte.
6495 __ andl(out, Immediate(1));
6496 }
6497 break;
6498 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006499 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006500
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006501 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006502 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 __ xorl(out, out);
6504 }
6505
6506 if (done.IsLinked()) {
6507 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006508 }
6509
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006510 if (slow_path != nullptr) {
6511 __ Bind(slow_path->GetExitLabel());
6512 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006513}
6514
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006515void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006516 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006517 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006518 LocationSummary* locations =
6519 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006521 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6522 // Require a register for the interface check since there is a loop that compares the class to
6523 // a memory address.
6524 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006525 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6526 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6527 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6528 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006529 } else {
6530 locations->SetInAt(1, Location::Any());
6531 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006532 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
6533 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006534}
6535
6536void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006537 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006538 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 Location obj_loc = locations->InAt(0);
6540 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006541 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542 Location temp_loc = locations->GetTemp(0);
6543 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006544 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6545 DCHECK_GE(num_temps, 1u);
6546 DCHECK_LE(num_temps, 2u);
6547 Location maybe_temp2_loc = (num_temps >= 2u) ? locations->GetTemp(1) : Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006548 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6549 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6550 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6551 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6552 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6553 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006554 const uint32_t object_array_data_offset =
6555 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006556
Vladimir Marko87584542017-12-12 17:47:52 +00006557 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006559 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6560 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006563
6564 NearLabel done;
6565 // Avoid null check if we know obj is not null.
6566 if (instruction->MustDoNullCheck()) {
6567 __ testl(obj, obj);
6568 __ j(kEqual, &done);
6569 }
6570
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 case TypeCheckKind::kExactCheck:
6573 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006574 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006575 GenerateReferenceLoadTwoRegisters(instruction,
6576 temp_loc,
6577 obj_loc,
6578 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006579 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006580 if (cls.IsRegister()) {
6581 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6582 } else {
6583 DCHECK(cls.IsStackSlot()) << cls;
6584 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6585 }
6586 // Jump to slow path for throwing the exception or doing a
6587 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 break;
6590 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006593 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006594 GenerateReferenceLoadTwoRegisters(instruction,
6595 temp_loc,
6596 obj_loc,
6597 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006598 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006599 // If the class is abstract, we eagerly fetch the super class of the
6600 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006601 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006602 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006604 GenerateReferenceLoadOneRegister(instruction,
6605 temp_loc,
6606 super_offset,
6607 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006608 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006609
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006610 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6611 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006612 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006613 // Otherwise, compare the classes.
6614 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 if (cls.IsRegister()) {
6616 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6617 } else {
6618 DCHECK(cls.IsStackSlot()) << cls;
6619 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6620 }
6621 __ j(kNotEqual, &loop);
6622 break;
6623 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006625 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006626 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006627 GenerateReferenceLoadTwoRegisters(instruction,
6628 temp_loc,
6629 obj_loc,
6630 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006631 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006632 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006633 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634 __ Bind(&loop);
6635 if (cls.IsRegister()) {
6636 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6637 } else {
6638 DCHECK(cls.IsStackSlot()) << cls;
6639 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6640 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006641 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006642
Roland Levillain0d5a2812015-11-13 10:07:31 +00006643 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006644 GenerateReferenceLoadOneRegister(instruction,
6645 temp_loc,
6646 super_offset,
6647 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006648 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649
6650 // If the class reference currently in `temp` is not null, jump
6651 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006652 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006653 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006654 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006655 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 break;
6657 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006658
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006659 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006660 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006661 GenerateReferenceLoadTwoRegisters(instruction,
6662 temp_loc,
6663 obj_loc,
6664 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006665 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006666 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006668 if (cls.IsRegister()) {
6669 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6670 } else {
6671 DCHECK(cls.IsStackSlot()) << cls;
6672 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6673 }
6674 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006675
6676 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006677 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006678 GenerateReferenceLoadOneRegister(instruction,
6679 temp_loc,
6680 component_offset,
6681 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006682 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683
6684 // If the component type is not null (i.e. the object is indeed
6685 // an array), jump to label `check_non_primitive_component_type`
6686 // to further check that this component type is not a primitive
6687 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006688 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006690 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006691 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006692 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006693 break;
6694 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006696 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006697 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 //
6699 // We cannot directly call the CheckCast runtime entry point
6700 // without resorting to a type checking slow path here (i.e. by
6701 // calling InvokeRuntime directly), as it would require to
6702 // assign fixed registers for the inputs of this HInstanceOf
6703 // instruction (following the runtime calling convention), which
6704 // might be cluttered by the potential first read barrier
6705 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006706 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006707 break;
6708 }
6709
Vladimir Marko175e7862018-03-27 09:03:13 +00006710 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006711 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6712 // We can not get false positives by doing this.
6713 // /* HeapReference<Class> */ temp = obj->klass_
6714 GenerateReferenceLoadTwoRegisters(instruction,
6715 temp_loc,
6716 obj_loc,
6717 class_offset,
6718 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006719
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006720 // /* HeapReference<Class> */ temp = temp->iftable_
6721 GenerateReferenceLoadTwoRegisters(instruction,
6722 temp_loc,
6723 temp_loc,
6724 iftable_offset,
6725 kWithoutReadBarrier);
6726 // Iftable is never null.
6727 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6728 // Maybe poison the `cls` for direct comparison with memory.
6729 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6730 // Loop through the iftable and check if any class matches.
6731 NearLabel start_loop;
6732 __ Bind(&start_loop);
6733 // Need to subtract first to handle the empty array case.
6734 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6735 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6736 // Go to next interface if the classes do not match.
6737 __ cmpl(cls.AsRegister<CpuRegister>(),
6738 CodeGeneratorX86_64::ArrayAddress(temp,
6739 maybe_temp2_loc,
6740 TIMES_4,
6741 object_array_data_offset));
6742 __ j(kNotEqual, &start_loop); // Return if same class.
6743 // If `cls` was poisoned above, unpoison it.
6744 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006746 }
6747
6748 case TypeCheckKind::kBitstringCheck: {
6749 // /* HeapReference<Class> */ temp = obj->klass_
6750 GenerateReferenceLoadTwoRegisters(instruction,
6751 temp_loc,
6752 obj_loc,
6753 class_offset,
6754 kWithoutReadBarrier);
6755
6756 GenerateBitstringTypeCheckCompare(instruction, temp);
6757 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
6758 break;
6759 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006760 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006762 if (done.IsLinked()) {
6763 __ Bind(&done);
6764 }
6765
Roland Levillain0d5a2812015-11-13 10:07:31 +00006766 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006767}
6768
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006769void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006770 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6771 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006772 InvokeRuntimeCallingConvention calling_convention;
6773 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6774}
6775
6776void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006777 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006778 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006779 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006780 if (instruction->IsEnter()) {
6781 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6782 } else {
6783 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6784 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006785}
6786
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05306787void LocationsBuilderX86_64::VisitX86AndNot(HX86AndNot* instruction) {
6788 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
6789 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
6790 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
6791 locations->SetInAt(0, Location::RequiresRegister());
6792 // There is no immediate variant of negated bitwise and in X86.
6793 locations->SetInAt(1, Location::RequiresRegister());
6794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6795}
6796
6797void LocationsBuilderX86_64::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
6798 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
6799 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
6800 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
6801 locations->SetInAt(0, Location::RequiresRegister());
6802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6803}
6804
6805void InstructionCodeGeneratorX86_64::VisitX86AndNot(HX86AndNot* instruction) {
6806 LocationSummary* locations = instruction->GetLocations();
6807 Location first = locations->InAt(0);
6808 Location second = locations->InAt(1);
6809 Location dest = locations->Out();
6810 __ andn(dest.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
6811}
6812
6813void InstructionCodeGeneratorX86_64::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
6814 LocationSummary* locations = instruction->GetLocations();
6815 Location src = locations->InAt(0);
6816 Location dest = locations->Out();
6817 switch (instruction->GetOpKind()) {
6818 case HInstruction::kAnd:
6819 __ blsr(dest.AsRegister<CpuRegister>(), src.AsRegister<CpuRegister>());
6820 break;
6821 case HInstruction::kXor:
6822 __ blsmsk(dest.AsRegister<CpuRegister>(), src.AsRegister<CpuRegister>());
6823 break;
6824 default:
6825 LOG(FATAL) << "Unreachable";
6826 }
6827}
6828
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006829void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6830void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6831void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6832
6833void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6834 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006835 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006836 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6837 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006838 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006839 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006840 locations->SetOut(Location::SameAsFirstInput());
6841}
6842
6843void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6844 HandleBitwiseOperation(instruction);
6845}
6846
6847void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6848 HandleBitwiseOperation(instruction);
6849}
6850
6851void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6852 HandleBitwiseOperation(instruction);
6853}
6854
6855void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6856 LocationSummary* locations = instruction->GetLocations();
6857 Location first = locations->InAt(0);
6858 Location second = locations->InAt(1);
6859 DCHECK(first.Equals(locations->Out()));
6860
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006861 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006862 if (second.IsRegister()) {
6863 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006864 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006865 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006866 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006867 } else {
6868 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006869 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006870 }
6871 } else if (second.IsConstant()) {
6872 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6873 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006874 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006875 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006876 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006877 } else {
6878 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006879 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006880 }
6881 } else {
6882 Address address(CpuRegister(RSP), second.GetStackIndex());
6883 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006884 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006885 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006886 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006887 } else {
6888 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006889 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006890 }
6891 }
6892 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006893 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006894 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6895 bool second_is_constant = false;
6896 int64_t value = 0;
6897 if (second.IsConstant()) {
6898 second_is_constant = true;
6899 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006900 }
Mark Mendell40741f32015-04-20 22:10:34 -04006901 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006902
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006903 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006904 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006905 if (is_int32_value) {
6906 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6907 } else {
6908 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6909 }
6910 } else if (second.IsDoubleStackSlot()) {
6911 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006912 } else {
6913 __ andq(first_reg, second.AsRegister<CpuRegister>());
6914 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006915 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006916 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006917 if (is_int32_value) {
6918 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6919 } else {
6920 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6921 }
6922 } else if (second.IsDoubleStackSlot()) {
6923 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006924 } else {
6925 __ orq(first_reg, second.AsRegister<CpuRegister>());
6926 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006927 } else {
6928 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006929 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006930 if (is_int32_value) {
6931 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6932 } else {
6933 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6934 }
6935 } else if (second.IsDoubleStackSlot()) {
6936 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006937 } else {
6938 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6939 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006940 }
6941 }
6942}
6943
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006944void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6945 HInstruction* instruction,
6946 Location out,
6947 uint32_t offset,
6948 Location maybe_temp,
6949 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006950 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006951 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006952 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006953 if (kUseBakerReadBarrier) {
6954 // Load with fast path based Baker's read barrier.
6955 // /* HeapReference<Object> */ out = *(out + offset)
6956 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006957 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006958 } else {
6959 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006960 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006961 // in the following move operation, as we will need it for the
6962 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006963 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006964 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006965 // /* HeapReference<Object> */ out = *(out + offset)
6966 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006967 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006968 }
6969 } else {
6970 // Plain load with no read barrier.
6971 // /* HeapReference<Object> */ out = *(out + offset)
6972 __ movl(out_reg, Address(out_reg, offset));
6973 __ MaybeUnpoisonHeapReference(out_reg);
6974 }
6975}
6976
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006977void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6978 HInstruction* instruction,
6979 Location out,
6980 Location obj,
6981 uint32_t offset,
6982 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006983 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6984 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006985 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006986 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006987 if (kUseBakerReadBarrier) {
6988 // Load with fast path based Baker's read barrier.
6989 // /* HeapReference<Object> */ out = *(obj + offset)
6990 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006991 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006992 } else {
6993 // Load with slow path based read barrier.
6994 // /* HeapReference<Object> */ out = *(obj + offset)
6995 __ movl(out_reg, Address(obj_reg, offset));
6996 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6997 }
6998 } else {
6999 // Plain load with no read barrier.
7000 // /* HeapReference<Object> */ out = *(obj + offset)
7001 __ movl(out_reg, Address(obj_reg, offset));
7002 __ MaybeUnpoisonHeapReference(out_reg);
7003 }
7004}
7005
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007006void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
7007 HInstruction* instruction,
7008 Location root,
7009 const Address& address,
7010 Label* fixup_label,
7011 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007012 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007013 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007014 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007015 if (kUseBakerReadBarrier) {
7016 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7017 // Baker's read barrier are used:
7018 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007019 // root = obj.field;
7020 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7021 // if (temp != null) {
7022 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007023 // }
7024
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007025 // /* GcRoot<mirror::Object> */ root = *address
7026 __ movl(root_reg, address);
7027 if (fixup_label != nullptr) {
7028 __ Bind(fixup_label);
7029 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007030 static_assert(
7031 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7032 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7033 "have different sizes.");
7034 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7035 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7036 "have different sizes.");
7037
Vladimir Marko953437b2016-08-24 08:30:46 +00007038 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007039 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007040 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007041 codegen_->AddSlowPath(slow_path);
7042
Roland Levillaind966ce72017-02-09 16:20:14 +00007043 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
7044 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007045 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007046 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
7047 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007048 __ j(kNotEqual, slow_path->GetEntryLabel());
7049 __ Bind(slow_path->GetExitLabel());
7050 } else {
7051 // GC root loaded through a slow path for read barriers other
7052 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007053 // /* GcRoot<mirror::Object>* */ root = address
7054 __ leaq(root_reg, address);
7055 if (fixup_label != nullptr) {
7056 __ Bind(fixup_label);
7057 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007058 // /* mirror::Object* */ root = root->Read()
7059 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7060 }
7061 } else {
7062 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007063 // /* GcRoot<mirror::Object> */ root = *address
7064 __ movl(root_reg, address);
7065 if (fixup_label != nullptr) {
7066 __ Bind(fixup_label);
7067 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007068 // Note that GC roots are not affected by heap poisoning, thus we
7069 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007070 }
7071}
7072
7073void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7074 Location ref,
7075 CpuRegister obj,
7076 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007077 bool needs_null_check) {
7078 DCHECK(kEmitCompilerReadBarrier);
7079 DCHECK(kUseBakerReadBarrier);
7080
7081 // /* HeapReference<Object> */ ref = *(obj + offset)
7082 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007083 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007084}
7085
7086void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7087 Location ref,
7088 CpuRegister obj,
7089 uint32_t data_offset,
7090 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007091 bool needs_null_check) {
7092 DCHECK(kEmitCompilerReadBarrier);
7093 DCHECK(kUseBakerReadBarrier);
7094
Roland Levillain3d312422016-06-23 13:53:42 +01007095 static_assert(
7096 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7097 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007098 // /* HeapReference<Object> */ ref =
7099 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007100 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007101 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007102}
7103
7104void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7105 Location ref,
7106 CpuRegister obj,
7107 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007108 bool needs_null_check,
7109 bool always_update_field,
7110 CpuRegister* temp1,
7111 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007112 DCHECK(kEmitCompilerReadBarrier);
7113 DCHECK(kUseBakerReadBarrier);
7114
7115 // In slow path based read barriers, the read barrier call is
7116 // inserted after the original load. However, in fast path based
7117 // Baker's read barriers, we need to perform the load of
7118 // mirror::Object::monitor_ *before* the original reference load.
7119 // This load-load ordering is required by the read barrier.
7120 // The fast path/slow path (for Baker's algorithm) should look like:
7121 //
7122 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7123 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7124 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007125 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007126 // if (is_gray) {
7127 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7128 // }
7129 //
7130 // Note: the original implementation in ReadBarrier::Barrier is
7131 // slightly more complex as:
7132 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007133 // the high-bits of rb_state, which are expected to be all zeroes
7134 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
7135 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007136 // - it performs additional checks that we do not do here for
7137 // performance reasons.
7138
7139 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007140 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7141
Vladimir Marko953437b2016-08-24 08:30:46 +00007142 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007143 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007144 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007145 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7146 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7147 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7148
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007149 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007150 // ref = ReadBarrier::Mark(ref);
7151 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7152 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007153 if (needs_null_check) {
7154 MaybeRecordImplicitNullCheck(instruction);
7155 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007156
7157 // Load fence to prevent load-load reordering.
7158 // Note that this is a no-op, thanks to the x86-64 memory model.
7159 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7160
7161 // The actual reference load.
7162 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007163 __ movl(ref_reg, src); // Flags are unaffected.
7164
7165 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7166 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007167 SlowPathCode* slow_path;
7168 if (always_update_field) {
7169 DCHECK(temp1 != nullptr);
7170 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007171 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007172 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
7173 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007174 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007175 instruction, ref, /* unpoison_ref_before_marking */ true);
7176 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007177 AddSlowPath(slow_path);
7178
7179 // We have done the "if" of the gray bit check above, now branch based on the flags.
7180 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007181
7182 // Object* ref = ref_addr->AsMirrorPtr()
7183 __ MaybeUnpoisonHeapReference(ref_reg);
7184
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007185 __ Bind(slow_path->GetExitLabel());
7186}
7187
7188void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
7189 Location out,
7190 Location ref,
7191 Location obj,
7192 uint32_t offset,
7193 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007194 DCHECK(kEmitCompilerReadBarrier);
7195
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007196 // Insert a slow path based read barrier *after* the reference load.
7197 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007198 // If heap poisoning is enabled, the unpoisoning of the loaded
7199 // reference will be carried out by the runtime within the slow
7200 // path.
7201 //
7202 // Note that `ref` currently does not get unpoisoned (when heap
7203 // poisoning is enabled), which is alright as the `ref` argument is
7204 // not used by the artReadBarrierSlow entry point.
7205 //
7206 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007207 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007208 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
7209 AddSlowPath(slow_path);
7210
Roland Levillain0d5a2812015-11-13 10:07:31 +00007211 __ jmp(slow_path->GetEntryLabel());
7212 __ Bind(slow_path->GetExitLabel());
7213}
7214
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007215void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7216 Location out,
7217 Location ref,
7218 Location obj,
7219 uint32_t offset,
7220 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007221 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007222 // Baker's read barriers shall be handled by the fast path
7223 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
7224 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007225 // If heap poisoning is enabled, unpoisoning will be taken care of
7226 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007227 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007228 } else if (kPoisonHeapReferences) {
7229 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
7230 }
7231}
7232
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007233void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7234 Location out,
7235 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007236 DCHECK(kEmitCompilerReadBarrier);
7237
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007238 // Insert a slow path based read barrier *after* the GC root load.
7239 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007240 // Note that GC roots are not affected by heap poisoning, so we do
7241 // not need to do anything special for this here.
7242 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007243 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007244 AddSlowPath(slow_path);
7245
Roland Levillain0d5a2812015-11-13 10:07:31 +00007246 __ jmp(slow_path->GetEntryLabel());
7247 __ Bind(slow_path->GetExitLabel());
7248}
7249
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007250void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007251 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007252 LOG(FATAL) << "Unreachable";
7253}
7254
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007255void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007256 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007257 LOG(FATAL) << "Unreachable";
7258}
7259
Mark Mendellfe57faa2015-09-18 09:26:15 -04007260// Simple implementation of packed switch - generate cascaded compare/jumps.
7261void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7262 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007263 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007264 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04007265 locations->AddTemp(Location::RequiresRegister());
7266 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04007267}
7268
7269void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7270 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007271 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007272 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04007273 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
7274 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
7275 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007276 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7277
7278 // Should we generate smaller inline compare/jumps?
7279 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7280 // Figure out the correct compare values and jump conditions.
7281 // Handle the first compare/branch as a special case because it might
7282 // jump to the default case.
7283 DCHECK_GT(num_entries, 2u);
7284 Condition first_condition;
7285 uint32_t index;
7286 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7287 if (lower_bound != 0) {
7288 first_condition = kLess;
7289 __ cmpl(value_reg_in, Immediate(lower_bound));
7290 __ j(first_condition, codegen_->GetLabelOf(default_block));
7291 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
7292
7293 index = 1;
7294 } else {
7295 // Handle all the compare/jumps below.
7296 first_condition = kBelow;
7297 index = 0;
7298 }
7299
7300 // Handle the rest of the compare/jumps.
7301 for (; index + 1 < num_entries; index += 2) {
7302 int32_t compare_to_value = lower_bound + index + 1;
7303 __ cmpl(value_reg_in, Immediate(compare_to_value));
7304 // Jump to successors[index] if value < case_value[index].
7305 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7306 // Jump to successors[index + 1] if value == case_value[index + 1].
7307 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7308 }
7309
7310 if (index != num_entries) {
7311 // There are an odd number of entries. Handle the last one.
7312 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00007313 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007314 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
7315 }
7316
7317 // And the default for any other value.
7318 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7319 __ jmp(codegen_->GetLabelOf(default_block));
7320 }
7321 return;
7322 }
Mark Mendell9c86b482015-09-18 13:36:07 -04007323
7324 // Remove the bias, if needed.
7325 Register value_reg_out = value_reg_in.AsRegister();
7326 if (lower_bound != 0) {
7327 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
7328 value_reg_out = temp_reg.AsRegister();
7329 }
7330 CpuRegister value_reg(value_reg_out);
7331
7332 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04007333 __ cmpl(value_reg, Immediate(num_entries - 1));
7334 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007335
Mark Mendell9c86b482015-09-18 13:36:07 -04007336 // We are in the range of the table.
7337 // Load the address of the jump table in the constant area.
7338 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007339
Mark Mendell9c86b482015-09-18 13:36:07 -04007340 // Load the (signed) offset from the jump table.
7341 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
7342
7343 // Add the offset to the address of the table base.
7344 __ addq(temp_reg, base_reg);
7345
7346 // And jump.
7347 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007348}
7349
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007350void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7351 ATTRIBUTE_UNUSED) {
7352 LOG(FATAL) << "Unreachable";
7353}
7354
7355void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7356 ATTRIBUTE_UNUSED) {
7357 LOG(FATAL) << "Unreachable";
7358}
7359
Aart Bikc5d47542016-01-27 17:00:35 -08007360void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
7361 if (value == 0) {
7362 __ xorl(dest, dest);
7363 } else {
7364 __ movl(dest, Immediate(value));
7365 }
7366}
7367
Mark Mendell92e83bf2015-05-07 11:25:03 -04007368void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
7369 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08007370 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007371 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00007372 } else if (IsUint<32>(value)) {
7373 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007374 __ movl(dest, Immediate(static_cast<int32_t>(value)));
7375 } else {
7376 __ movq(dest, Immediate(value));
7377 }
7378}
7379
Mark Mendell7c0b44f2016-02-01 10:08:35 -05007380void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
7381 if (value == 0) {
7382 __ xorps(dest, dest);
7383 } else {
7384 __ movss(dest, LiteralInt32Address(value));
7385 }
7386}
7387
7388void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
7389 if (value == 0) {
7390 __ xorpd(dest, dest);
7391 } else {
7392 __ movsd(dest, LiteralInt64Address(value));
7393 }
7394}
7395
7396void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
7397 Load32BitValue(dest, bit_cast<int32_t, float>(value));
7398}
7399
7400void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
7401 Load64BitValue(dest, bit_cast<int64_t, double>(value));
7402}
7403
Aart Bika19616e2016-02-01 18:57:58 -08007404void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
7405 if (value == 0) {
7406 __ testl(dest, dest);
7407 } else {
7408 __ cmpl(dest, Immediate(value));
7409 }
7410}
7411
7412void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
7413 if (IsInt<32>(value)) {
7414 if (value == 0) {
7415 __ testq(dest, dest);
7416 } else {
7417 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7418 }
7419 } else {
7420 // Value won't fit in an int.
7421 __ cmpq(dest, LiteralInt64Address(value));
7422 }
7423}
7424
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007425void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7426 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007427 GenerateIntCompare(lhs_reg, rhs);
7428}
7429
7430void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007431 if (rhs.IsConstant()) {
7432 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007433 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007434 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007435 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007436 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007437 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007438 }
7439}
7440
7441void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7442 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7443 if (rhs.IsConstant()) {
7444 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7445 Compare64BitValue(lhs_reg, value);
7446 } else if (rhs.IsDoubleStackSlot()) {
7447 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7448 } else {
7449 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7450 }
7451}
7452
7453Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7454 Location index,
7455 ScaleFactor scale,
7456 uint32_t data_offset) {
7457 return index.IsConstant() ?
7458 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7459 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7460}
7461
Mark Mendellcfa410b2015-05-25 16:02:44 -04007462void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7463 DCHECK(dest.IsDoubleStackSlot());
7464 if (IsInt<32>(value)) {
7465 // Can move directly as an int32 constant.
7466 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7467 Immediate(static_cast<int32_t>(value)));
7468 } else {
7469 Load64BitValue(CpuRegister(TMP), value);
7470 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7471 }
7472}
7473
Mark Mendell9c86b482015-09-18 13:36:07 -04007474/**
7475 * Class to handle late fixup of offsets into constant area.
7476 */
7477class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7478 public:
7479 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7480 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7481
7482 protected:
7483 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7484
7485 CodeGeneratorX86_64* codegen_;
7486
7487 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007488 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell9c86b482015-09-18 13:36:07 -04007489 // Patch the correct offset for the instruction. We use the address of the
7490 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7491 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7492 int32_t relative_position = constant_offset - pos;
7493
7494 // Patch in the right value.
7495 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7496 }
7497
7498 // Location in constant area that the fixup refers to.
7499 size_t offset_into_constant_area_;
7500};
7501
7502/**
7503 t * Class to handle late fixup of offsets to a jump table that will be created in the
7504 * constant area.
7505 */
7506class JumpTableRIPFixup : public RIPFixup {
7507 public:
7508 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7509 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7510
7511 void CreateJumpTable() {
7512 X86_64Assembler* assembler = codegen_->GetAssembler();
7513
7514 // Ensure that the reference to the jump table has the correct offset.
7515 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7516 SetOffset(offset_in_constant_table);
7517
7518 // Compute the offset from the start of the function to this jump table.
7519 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7520
7521 // Populate the jump table with the correct values for the jump table.
7522 int32_t num_entries = switch_instr_->GetNumEntries();
7523 HBasicBlock* block = switch_instr_->GetBlock();
7524 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7525 // The value that we want is the target offset - the position of the table.
7526 for (int32_t i = 0; i < num_entries; i++) {
7527 HBasicBlock* b = successors[i];
7528 Label* l = codegen_->GetLabelOf(b);
7529 DCHECK(l->IsBound());
7530 int32_t offset_to_block = l->Position() - current_table_offset;
7531 assembler->AppendInt32(offset_to_block);
7532 }
7533 }
7534
7535 private:
7536 const HPackedSwitch* switch_instr_;
7537};
7538
Mark Mendellf55c3e02015-03-26 21:07:46 -04007539void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7540 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007541 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007542 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7543 // 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 -04007544 assembler->Align(4, 0);
7545 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007546
7547 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007548 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007549 jump_table->CreateJumpTable();
7550 }
7551
7552 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007553 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007554 }
7555
7556 // And finish up.
7557 CodeGenerator::Finalize(allocator);
7558}
7559
Mark Mendellf55c3e02015-03-26 21:07:46 -04007560Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007561 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007562 return Address::RIP(fixup);
7563}
7564
7565Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007566 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007567 return Address::RIP(fixup);
7568}
7569
7570Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007571 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007572 return Address::RIP(fixup);
7573}
7574
7575Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007576 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007577 return Address::RIP(fixup);
7578}
7579
Andreas Gampe85b62f22015-09-09 13:15:38 -07007580// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007581void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007582 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007583 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007584 return;
7585 }
7586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007587 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007588
7589 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7590 if (trg.Equals(return_loc)) {
7591 return;
7592 }
7593
7594 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007595 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007596 parallel_move.AddMove(return_loc, trg, type, nullptr);
7597 GetMoveResolver()->EmitNativeCode(&parallel_move);
7598}
7599
Mark Mendell9c86b482015-09-18 13:36:07 -04007600Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7601 // Create a fixup to be used to create and address the jump table.
7602 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007603 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007604
7605 // We have to populate the jump tables.
7606 fixups_to_jump_tables_.push_back(table_fixup);
7607 return Address::RIP(table_fixup);
7608}
7609
Mark Mendellea5af682015-10-22 17:35:49 -04007610void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7611 const Address& addr_high,
7612 int64_t v,
7613 HInstruction* instruction) {
7614 if (IsInt<32>(v)) {
7615 int32_t v_32 = v;
7616 __ movq(addr_low, Immediate(v_32));
7617 MaybeRecordImplicitNullCheck(instruction);
7618 } else {
7619 // Didn't fit in a register. Do it in pieces.
7620 int32_t low_v = Low32Bits(v);
7621 int32_t high_v = High32Bits(v);
7622 __ movl(addr_low, Immediate(low_v));
7623 MaybeRecordImplicitNullCheck(instruction);
7624 __ movl(addr_high, Immediate(high_v));
7625 }
7626}
7627
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007628void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7629 const uint8_t* roots_data,
7630 const PatchInfo<Label>& info,
7631 uint64_t index_in_table) const {
7632 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7633 uintptr_t address =
7634 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00007635 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007636 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7637 dchecked_integral_cast<uint32_t>(address);
7638}
7639
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007640void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7641 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007642 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007643 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007644 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007645 }
7646
7647 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007648 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007649 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007650 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007651 }
7652}
7653
Roland Levillain4d027112015-07-01 15:41:14 +01007654#undef __
7655
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007656} // namespace x86_64
7657} // namespace art