blob: 8067b9c8a682a3ff3f51f9aa1912a5b14ea444a5 [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();
Andreas Gampe3db70682018-12-26 15:12:03 -0800995 __ 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>(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001004 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>(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001010 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>(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001016 Address::Absolute(kDummy32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001017 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01001018 // No need for memory fence, thanks to the x86-64 memory model.
Vladimir Marko58155012015-08-19 12:49:41 +00001019 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001020 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001021 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
1022 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
1023 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001024 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1025 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1026 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001027 }
Vladimir Marko58155012015-08-19 12:49:41 +00001028 }
1029
1030 switch (invoke->GetCodePtrLocation()) {
1031 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1032 __ call(&frame_entry_label_);
1033 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001034 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1035 // (callee_method + offset_of_quick_compiled_code)()
1036 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1037 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001038 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001039 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001040 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001041 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001042
1043 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001044}
1045
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001046void CodeGeneratorX86_64::GenerateVirtualCall(
1047 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001048 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1049 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1050 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001051
1052 // Use the calling convention instead of the location of the receiver, as
1053 // intrinsics may have put the receiver in a different register. In the intrinsics
1054 // slow path, the arguments have been moved to the right place, so here we are
1055 // guaranteed that the receiver is the first register of the calling convention.
1056 InvokeDexCallingConvention calling_convention;
1057 Register receiver = calling_convention.GetRegisterAt(0);
1058
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001059 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001060 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001061 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001062 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001063 // Instead of simply (possibly) unpoisoning `temp` here, we should
1064 // emit a read barrier for the previous class reference load.
1065 // However this is not required in practice, as this is an
1066 // intermediate/temporary reference and because the current
1067 // concurrent copying collector keeps the from-space memory
1068 // intact/accessible until the end of the marking phase (the
1069 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001070 __ MaybeUnpoisonHeapReference(temp);
1071 // temp = temp->GetMethodAt(method_offset);
1072 __ movq(temp, Address(temp, method_offset));
1073 // call temp->GetEntryPoint();
1074 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001075 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001076 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001077}
1078
Vladimir Marko6fd16062018-06-26 11:02:04 +01001079void CodeGeneratorX86_64::RecordBootImageIntrinsicPatch(uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001080 boot_image_other_patches_.emplace_back(/* target_dex_file= */ nullptr, intrinsic_data);
1081 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001082}
1083
Vladimir Markob066d432018-01-03 13:14:37 +00001084void CodeGeneratorX86_64::RecordBootImageRelRoPatch(uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001085 boot_image_other_patches_.emplace_back(/* target_dex_file= */ nullptr, boot_image_offset);
1086 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00001087}
1088
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001089void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
1090 boot_image_method_patches_.emplace_back(
1091 invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001092 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001093}
1094
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001095void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
1096 method_bss_entry_patches_.emplace_back(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
1097 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001098}
1099
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001100void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1101 boot_image_type_patches_.emplace_back(
1102 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001103 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001104}
1105
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001106Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001107 type_bss_entry_patches_.emplace_back(
1108 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001109 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001110}
1111
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001112void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1113 boot_image_string_patches_.emplace_back(
1114 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1115 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001116}
1117
Vladimir Markoaad75c62016-10-03 08:46:48 +00001118Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
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()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001126 __ leal(reg, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip= */ false));
Vladimir Marko6fd16062018-06-26 11:02:04 +01001127 RecordBootImageIntrinsicPatch(boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01001128 } else if (GetCompilerOptions().GetCompilePic()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001129 __ 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,
Andreas Gampe3db70682018-12-26 15:12:03 -08001149 Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip= */ false));
Vladimir Marko6fd16062018-06-26 11:02:04 +01001150 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() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01001194 boot_image_other_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 Marko764d4542017-05-16 10:31:41 +01001203 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001204 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001205 DCHECK(boot_image_type_patches_.empty());
1206 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01001207 }
1208 if (GetCompilerOptions().IsBootImage()) {
1209 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1210 boot_image_other_patches_, linker_patches);
1211 } else {
1212 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
1213 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001214 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001215 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1216 method_bss_entry_patches_, linker_patches);
1217 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1218 type_bss_entry_patches_, linker_patches);
1219 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1220 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001221 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001222}
1223
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001225 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001226}
1227
1228void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001229 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230}
1231
Vladimir Markoa0431112018-06-25 09:32:54 +01001232const X86_64InstructionSetFeatures& CodeGeneratorX86_64::GetInstructionSetFeatures() const {
1233 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86_64InstructionSetFeatures();
1234}
1235
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001236size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1237 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1238 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001239}
1240
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001241size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1242 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1243 return kX86_64WordSize;
1244}
1245
1246size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001247 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001248 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001249 } else {
1250 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1251 }
1252 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001253}
1254
1255size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001256 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001257 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001258 } else {
1259 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1260 }
1261 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001262}
1263
Calin Juravle175dc732015-08-25 15:42:32 +01001264void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1265 HInstruction* instruction,
1266 uint32_t dex_pc,
1267 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001268 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001269 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1270 if (EntrypointRequiresStackMap(entrypoint)) {
1271 RecordPcInfo(instruction, dex_pc, slow_path);
1272 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001273}
1274
Roland Levillaindec8f632016-07-22 17:10:06 +01001275void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1276 HInstruction* instruction,
1277 SlowPathCode* slow_path) {
1278 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001279 GenerateInvokeRuntime(entry_point_offset);
1280}
1281
1282void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001283 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip= */ true));
Roland Levillaindec8f632016-07-22 17:10:06 +01001284}
1285
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001286static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001287// Use a fake return address register to mimic Quick.
1288static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001289CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001290 const CompilerOptions& compiler_options,
1291 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001292 : CodeGenerator(graph,
1293 kNumberOfCpuRegisters,
1294 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001295 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001296 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1297 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001298 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001299 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1300 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001301 compiler_options,
1302 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001303 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001304 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001305 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001306 move_resolver_(graph->GetAllocator(), this),
1307 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001308 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001309 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1310 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1311 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1312 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001313 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001314 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001315 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001316 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1317 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1318 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001319 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1320}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001321
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001322InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1323 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001324 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001325 assembler_(codegen->GetAssembler()),
1326 codegen_(codegen) {}
1327
David Brazdil58282f42016-01-14 12:45:10 +00001328void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001329 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001330 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001331
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001332 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001333 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001334}
1335
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001336static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001337 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001338}
David Srbecky9d8606d2015-04-12 09:35:32 +01001339
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001340static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001341 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001342}
1343
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001344void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001345 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001346 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001347 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001348 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001349 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001350
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001351 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1352 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1353 ArtMethod::HotnessCountOffset().Int32Value()),
1354 Immediate(1));
1355 }
1356
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001357 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001358 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1359 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001360 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001361 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001362
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001363 if (HasEmptyFrame()) {
1364 return;
1365 }
1366
Nicolas Geoffray98893962015-01-21 12:32:32 +00001367 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001368 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001369 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001370 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001371 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1372 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001373 }
1374 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001375
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001376 int adjust = GetFrameSize() - GetCoreSpillSize();
1377 __ subq(CpuRegister(RSP), Immediate(adjust));
1378 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001379 uint32_t xmm_spill_location = GetFpuSpillStart();
1380 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001381
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001382 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1383 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001384 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1385 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1386 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001387 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001388 }
1389
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001390 // Save the current method if we need it. Note that we do not
1391 // do this in HCurrentMethod, as the instruction might have been removed
1392 // in the SSA graph.
1393 if (RequiresCurrentMethod()) {
1394 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1395 CpuRegister(kMethodRegisterArgument));
1396 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001397
1398 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1399 // Initialize should_deoptimize flag to 0.
1400 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1401 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001402}
1403
1404void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001405 __ cfi().RememberState();
1406 if (!HasEmptyFrame()) {
1407 uint32_t xmm_spill_location = GetFpuSpillStart();
1408 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1409 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1410 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1411 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1412 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1413 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1414 }
1415 }
1416
1417 int adjust = GetFrameSize() - GetCoreSpillSize();
1418 __ addq(CpuRegister(RSP), Immediate(adjust));
1419 __ cfi().AdjustCFAOffset(-adjust);
1420
1421 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1422 Register reg = kCoreCalleeSaves[i];
1423 if (allocated_registers_.ContainsCoreRegister(reg)) {
1424 __ popq(CpuRegister(reg));
1425 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1426 __ cfi().Restore(DWARFReg(reg));
1427 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001428 }
1429 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001430 __ ret();
1431 __ cfi().RestoreState();
1432 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001433}
1434
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001435void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1436 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001437}
1438
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001439void CodeGeneratorX86_64::Move(Location destination, Location source) {
1440 if (source.Equals(destination)) {
1441 return;
1442 }
1443 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001444 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001445 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001446 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001447 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001448 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001449 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001450 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1451 } else if (source.IsConstant()) {
1452 HConstant* constant = source.GetConstant();
1453 if (constant->IsLongConstant()) {
1454 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1455 } else {
1456 Load32BitValue(dest, GetInt32ValueOf(constant));
1457 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001458 } else {
1459 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001460 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 }
1462 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001463 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001464 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001465 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001467 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1468 } else if (source.IsConstant()) {
1469 HConstant* constant = source.GetConstant();
1470 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1471 if (constant->IsFloatConstant()) {
1472 Load32BitValue(dest, static_cast<int32_t>(value));
1473 } else {
1474 Load64BitValue(dest, value);
1475 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001476 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001477 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001478 } else {
1479 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001480 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001481 }
1482 } else if (destination.IsStackSlot()) {
1483 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001484 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001485 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001486 } else if (source.IsFpuRegister()) {
1487 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001489 } else if (source.IsConstant()) {
1490 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001491 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001492 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001494 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001495 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1496 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001497 }
1498 } else {
1499 DCHECK(destination.IsDoubleStackSlot());
1500 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001501 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 } else if (source.IsFpuRegister()) {
1504 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001505 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001506 } else if (source.IsConstant()) {
1507 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001508 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1509 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001510 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001511 } else {
1512 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001513 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1514 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001515 }
1516 }
1517}
1518
Calin Juravle175dc732015-08-25 15:42:32 +01001519void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1520 DCHECK(location.IsRegister());
1521 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1522}
1523
Calin Juravlee460d1d2015-09-29 04:52:17 +01001524void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 Move(dst, src);
1527}
1528
1529void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1530 if (location.IsRegister()) {
1531 locations->AddTemp(location);
1532 } else {
1533 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1534 }
1535}
1536
David Brazdilfc6a86a2015-06-26 10:33:45 +00001537void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001538 if (successor->IsExitBlock()) {
1539 DCHECK(got->GetPrevious()->AlwaysThrows());
1540 return; // no code needed
1541 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001542
1543 HBasicBlock* block = got->GetBlock();
1544 HInstruction* previous = got->GetPrevious();
1545
1546 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001547 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001548 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1549 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1550 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1551 Immediate(1));
1552 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001553 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1554 return;
1555 }
1556
1557 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1558 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1559 }
1560 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001561 __ jmp(codegen_->GetLabelOf(successor));
1562 }
1563}
1564
David Brazdilfc6a86a2015-06-26 10:33:45 +00001565void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1566 got->SetLocations(nullptr);
1567}
1568
1569void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1570 HandleGoto(got, got->GetSuccessor());
1571}
1572
1573void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1574 try_boundary->SetLocations(nullptr);
1575}
1576
1577void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1578 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1579 if (!successor->IsExitBlock()) {
1580 HandleGoto(try_boundary, successor);
1581 }
1582}
1583
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1585 exit->SetLocations(nullptr);
1586}
1587
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001588void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001589}
1590
Mark Mendell152408f2015-12-31 12:28:50 -05001591template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001592void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001593 LabelType* true_label,
1594 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001595 if (cond->IsFPConditionTrueIfNaN()) {
1596 __ j(kUnordered, true_label);
1597 } else if (cond->IsFPConditionFalseIfNaN()) {
1598 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001599 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001600 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001601}
1602
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001603void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001604 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001605
Mark Mendellc4701932015-04-10 13:18:51 -04001606 Location left = locations->InAt(0);
1607 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001608 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001609 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001611 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 case DataType::Type::kInt8:
1613 case DataType::Type::kUint16:
1614 case DataType::Type::kInt16:
1615 case DataType::Type::kInt32:
1616 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001617 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001618 break;
1619 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001621 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001622 break;
1623 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001624 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001625 if (right.IsFpuRegister()) {
1626 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1627 } else if (right.IsConstant()) {
1628 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1629 codegen_->LiteralFloatAddress(
1630 right.GetConstant()->AsFloatConstant()->GetValue()));
1631 } else {
1632 DCHECK(right.IsStackSlot());
1633 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1634 Address(CpuRegister(RSP), right.GetStackIndex()));
1635 }
Mark Mendellc4701932015-04-10 13:18:51 -04001636 break;
1637 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001639 if (right.IsFpuRegister()) {
1640 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1641 } else if (right.IsConstant()) {
1642 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1643 codegen_->LiteralDoubleAddress(
1644 right.GetConstant()->AsDoubleConstant()->GetValue()));
1645 } else {
1646 DCHECK(right.IsDoubleStackSlot());
1647 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1648 Address(CpuRegister(RSP), right.GetStackIndex()));
1649 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001650 break;
1651 }
1652 default:
1653 LOG(FATAL) << "Unexpected condition type " << type;
1654 }
1655}
1656
1657template<class LabelType>
1658void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1659 LabelType* true_target_in,
1660 LabelType* false_target_in) {
1661 // Generated branching requires both targets to be explicit. If either of the
1662 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1663 LabelType fallthrough_target;
1664 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1665 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1666
1667 // Generate the comparison to set the CC.
1668 GenerateCompareTest(condition);
1669
1670 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001671 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001672 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001674 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1675 break;
1676 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001678 GenerateFPJumps(condition, true_target, false_target);
1679 break;
1680 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001682 GenerateFPJumps(condition, true_target, false_target);
1683 break;
1684 }
1685 default:
1686 LOG(FATAL) << "Unexpected condition type " << type;
1687 }
1688
David Brazdil0debae72015-11-12 18:37:00 +00001689 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001690 __ jmp(false_target);
1691 }
David Brazdil0debae72015-11-12 18:37:00 +00001692
1693 if (fallthrough_target.IsLinked()) {
1694 __ Bind(&fallthrough_target);
1695 }
Mark Mendellc4701932015-04-10 13:18:51 -04001696}
1697
David Brazdil0debae72015-11-12 18:37:00 +00001698static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1699 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1700 // are set only strictly before `branch`. We can't use the eflags on long
1701 // conditions if they are materialized due to the complex branching.
1702 return cond->IsCondition() &&
1703 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001704 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001705}
1706
Mark Mendell152408f2015-12-31 12:28:50 -05001707template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001708void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001709 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001710 LabelType* true_target,
1711 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001712 HInstruction* cond = instruction->InputAt(condition_input_index);
1713
1714 if (true_target == nullptr && false_target == nullptr) {
1715 // Nothing to do. The code always falls through.
1716 return;
1717 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001718 // Constant condition, statically compared against "true" (integer value 1).
1719 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001720 if (true_target != nullptr) {
1721 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001722 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001723 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001724 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001725 if (false_target != nullptr) {
1726 __ jmp(false_target);
1727 }
1728 }
1729 return;
1730 }
1731
1732 // The following code generates these patterns:
1733 // (1) true_target == nullptr && false_target != nullptr
1734 // - opposite condition true => branch to false_target
1735 // (2) true_target != nullptr && false_target == nullptr
1736 // - condition true => branch to true_target
1737 // (3) true_target != nullptr && false_target != nullptr
1738 // - condition true => branch to true_target
1739 // - branch to false_target
1740 if (IsBooleanValueOrMaterializedCondition(cond)) {
1741 if (AreEflagsSetFrom(cond, instruction)) {
1742 if (true_target == nullptr) {
1743 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1744 } else {
1745 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1746 }
1747 } else {
1748 // Materialized condition, compare against 0.
1749 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1750 if (lhs.IsRegister()) {
1751 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1752 } else {
1753 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1754 }
1755 if (true_target == nullptr) {
1756 __ j(kEqual, false_target);
1757 } else {
1758 __ j(kNotEqual, true_target);
1759 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001760 }
1761 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001762 // Condition has not been materialized, use its inputs as the
1763 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001764 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001765
David Brazdil0debae72015-11-12 18:37:00 +00001766 // If this is a long or FP comparison that has been folded into
1767 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001768 DataType::Type type = condition->InputAt(0)->GetType();
1769 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001770 GenerateCompareTestAndBranch(condition, true_target, false_target);
1771 return;
1772 }
1773
1774 Location lhs = condition->GetLocations()->InAt(0);
1775 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001776 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001777 if (true_target == nullptr) {
1778 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1779 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001780 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001781 }
Dave Allison20dfc792014-06-16 20:44:29 -07001782 }
David Brazdil0debae72015-11-12 18:37:00 +00001783
1784 // If neither branch falls through (case 3), the conditional branch to `true_target`
1785 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1786 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001787 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001788 }
1789}
1790
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001791void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001792 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001793 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001794 locations->SetInAt(0, Location::Any());
1795 }
1796}
1797
1798void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001799 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1800 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1801 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1802 nullptr : codegen_->GetLabelOf(true_successor);
1803 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1804 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001805 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001806}
1807
1808void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001809 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001810 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001811 InvokeRuntimeCallingConvention calling_convention;
1812 RegisterSet caller_saves = RegisterSet::Empty();
1813 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1814 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001815 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001816 locations->SetInAt(0, Location::Any());
1817 }
1818}
1819
1820void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001821 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001822 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001823 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001824 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001825 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001826}
1827
Mingyao Yang063fc772016-08-02 11:02:54 -07001828void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001829 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001830 LocationSummary(flag, LocationSummary::kNoCall);
1831 locations->SetOut(Location::RequiresRegister());
1832}
1833
1834void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1835 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1836 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1837}
1838
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001839static bool SelectCanUseCMOV(HSelect* select) {
1840 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001842 return false;
1843 }
1844
1845 // A FP condition doesn't generate the single CC that we need.
1846 HInstruction* condition = select->GetCondition();
1847 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001849 return false;
1850 }
1851
1852 // We can generate a CMOV for this Select.
1853 return true;
1854}
1855
David Brazdil74eb1b22015-12-14 11:44:01 +00001856void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001857 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001859 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001860 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001861 } else {
1862 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001863 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001864 if (select->InputAt(1)->IsConstant()) {
1865 locations->SetInAt(1, Location::RequiresRegister());
1866 } else {
1867 locations->SetInAt(1, Location::Any());
1868 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001869 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001870 locations->SetInAt(1, Location::Any());
1871 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001872 }
1873 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1874 locations->SetInAt(2, Location::RequiresRegister());
1875 }
1876 locations->SetOut(Location::SameAsFirstInput());
1877}
1878
1879void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1880 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001881 if (SelectCanUseCMOV(select)) {
1882 // If both the condition and the source types are integer, we can generate
1883 // a CMOV to implement Select.
1884 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001885 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001886 DCHECK(locations->InAt(0).Equals(locations->Out()));
1887
1888 HInstruction* select_condition = select->GetCondition();
1889 Condition cond = kNotEqual;
1890
1891 // Figure out how to test the 'condition'.
1892 if (select_condition->IsCondition()) {
1893 HCondition* condition = select_condition->AsCondition();
1894 if (!condition->IsEmittedAtUseSite()) {
1895 // This was a previously materialized condition.
1896 // Can we use the existing condition code?
1897 if (AreEflagsSetFrom(condition, select)) {
1898 // Materialization was the previous instruction. Condition codes are right.
1899 cond = X86_64IntegerCondition(condition->GetCondition());
1900 } else {
1901 // No, we have to recreate the condition code.
1902 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1903 __ testl(cond_reg, cond_reg);
1904 }
1905 } else {
1906 GenerateCompareTest(condition);
1907 cond = X86_64IntegerCondition(condition->GetCondition());
1908 }
1909 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001910 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001911 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1912 __ testl(cond_reg, cond_reg);
1913 }
1914
1915 // If the condition is true, overwrite the output, which already contains false.
1916 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001917 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001918 if (value_true_loc.IsRegister()) {
1919 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1920 } else {
1921 __ cmov(cond,
1922 value_false,
1923 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1924 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001925 } else {
1926 NearLabel false_target;
1927 GenerateTestAndBranch<NearLabel>(select,
Andreas Gampe3db70682018-12-26 15:12:03 -08001928 /* condition_input_index= */ 2,
1929 /* true_target= */ nullptr,
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001930 &false_target);
1931 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1932 __ Bind(&false_target);
1933 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001934}
1935
David Srbecky0cf44932015-12-09 14:09:59 +00001936void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001937 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001938}
1939
David Srbeckyd28f4a02016-03-14 17:14:24 +00001940void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1941 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001942}
1943
1944void CodeGeneratorX86_64::GenerateNop() {
1945 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001946}
1947
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001949 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001950 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001951 // Handle the long/FP comparisons made in instruction simplification.
1952 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001953 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001954 locations->SetInAt(0, Location::RequiresRegister());
1955 locations->SetInAt(1, Location::Any());
1956 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 case DataType::Type::kFloat32:
1958 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001959 locations->SetInAt(0, Location::RequiresFpuRegister());
1960 locations->SetInAt(1, Location::Any());
1961 break;
1962 default:
1963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetInAt(1, Location::Any());
1965 break;
1966 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001967 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001968 locations->SetOut(Location::RequiresRegister());
1969 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001970}
1971
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001973 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001974 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001975 }
Mark Mendellc4701932015-04-10 13:18:51 -04001976
1977 LocationSummary* locations = cond->GetLocations();
1978 Location lhs = locations->InAt(0);
1979 Location rhs = locations->InAt(1);
1980 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001981 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001982
1983 switch (cond->InputAt(0)->GetType()) {
1984 default:
1985 // Integer case.
1986
1987 // Clear output register: setcc only sets the low byte.
1988 __ xorl(reg, reg);
1989
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001990 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001991 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001992 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001993 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001994 // Clear output register: setcc only sets the low byte.
1995 __ xorl(reg, reg);
1996
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001997 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001998 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001999 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04002001 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2002 if (rhs.IsConstant()) {
2003 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
2004 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
2005 } else if (rhs.IsStackSlot()) {
2006 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2007 } else {
2008 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2009 }
2010 GenerateFPJumps(cond, &true_label, &false_label);
2011 break;
2012 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002014 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2015 if (rhs.IsConstant()) {
2016 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
2017 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
2018 } else if (rhs.IsDoubleStackSlot()) {
2019 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2020 } else {
2021 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2022 }
2023 GenerateFPJumps(cond, &true_label, &false_label);
2024 break;
2025 }
2026 }
2027
2028 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002029 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002030
Roland Levillain4fa13f62015-07-06 18:11:54 +01002031 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002032 __ Bind(&false_label);
2033 __ xorl(reg, reg);
2034 __ jmp(&done_label);
2035
Roland Levillain4fa13f62015-07-06 18:11:54 +01002036 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002037 __ Bind(&true_label);
2038 __ movl(reg, Immediate(1));
2039 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002040}
2041
2042void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002043 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002044}
2045
2046void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002047 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002048}
2049
2050void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002051 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002052}
2053
2054void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002055 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002056}
2057
2058void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002060}
2061
2062void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002063 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002064}
2065
2066void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002067 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002068}
2069
2070void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002071 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002072}
2073
2074void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002075 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002076}
2077
2078void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002079 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002080}
2081
2082void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002083 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002084}
2085
2086void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002087 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002088}
2089
Aart Bike9f37602015-10-09 11:15:55 -07002090void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002091 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002092}
2093
2094void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002095 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002096}
2097
2098void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002099 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002100}
2101
2102void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002103 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002104}
2105
2106void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002107 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002108}
2109
2110void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002112}
2113
2114void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002116}
2117
2118void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002120}
2121
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002122void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002123 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002124 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002125 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002127 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002130 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 case DataType::Type::kInt32:
2132 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002133 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002134 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2136 break;
2137 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 case DataType::Type::kFloat32:
2139 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002140 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002141 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002142 locations->SetOut(Location::RequiresRegister());
2143 break;
2144 }
2145 default:
2146 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2147 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002148}
2149
2150void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002151 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002152 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002153 Location left = locations->InAt(0);
2154 Location right = locations->InAt(1);
2155
Mark Mendell0c9497d2015-08-21 09:30:05 -04002156 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002158 Condition less_cond = kLess;
2159
Calin Juravleddb7df22014-11-25 20:56:51 +00002160 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002162 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002165 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002166 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002167 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002168 break;
2169 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002170 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002171 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002172 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002173 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002175 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2176 if (right.IsConstant()) {
2177 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2178 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2179 } else if (right.IsStackSlot()) {
2180 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2181 } else {
2182 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2183 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002184 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002185 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002186 break;
2187 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002189 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2190 if (right.IsConstant()) {
2191 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2192 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2193 } else if (right.IsDoubleStackSlot()) {
2194 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2195 } else {
2196 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2197 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002198 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002199 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002200 break;
2201 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002202 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002203 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002204 }
Aart Bika19616e2016-02-01 18:57:58 -08002205
Calin Juravleddb7df22014-11-25 20:56:51 +00002206 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002207 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002208 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002209
Calin Juravle91debbc2014-11-26 19:01:09 +00002210 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002211 __ movl(out, Immediate(1));
2212 __ jmp(&done);
2213
2214 __ Bind(&less);
2215 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002216
2217 __ Bind(&done);
2218}
2219
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002220void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002221 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002222 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002223 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002224}
2225
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002226void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002227 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228}
2229
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002230void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2231 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002232 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002233 locations->SetOut(Location::ConstantLocation(constant));
2234}
2235
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002236void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002237 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002238}
2239
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002240void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002241 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002242 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002243 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002244}
2245
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002246void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002247 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002248}
2249
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002250void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002252 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002253 locations->SetOut(Location::ConstantLocation(constant));
2254}
2255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002256void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002257 // Will be generated at use site.
2258}
2259
2260void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2261 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002262 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002263 locations->SetOut(Location::ConstantLocation(constant));
2264}
2265
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002266void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2267 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002268 // Will be generated at use site.
2269}
2270
Igor Murashkind01745e2017-04-05 16:40:31 -07002271void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2272 constructor_fence->SetLocations(nullptr);
2273}
2274
2275void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2276 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2277 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2278}
2279
Calin Juravle27df7582015-04-17 19:12:31 +01002280void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2281 memory_barrier->SetLocations(nullptr);
2282}
2283
2284void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002285 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002286}
2287
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002288void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2289 ret->SetLocations(nullptr);
2290}
2291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002292void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002293 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002294}
2295
2296void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002297 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002298 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002299 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002300 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002302 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 case DataType::Type::kInt8:
2304 case DataType::Type::kUint16:
2305 case DataType::Type::kInt16:
2306 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002308 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002309 break;
2310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 case DataType::Type::kFloat32:
2312 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002313 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002314 break;
2315
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002316 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002317 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002318 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002319}
2320
2321void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2322 if (kIsDebugBuild) {
2323 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002324 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002326 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 case DataType::Type::kInt8:
2328 case DataType::Type::kUint16:
2329 case DataType::Type::kInt16:
2330 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002333 break;
2334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 case DataType::Type::kFloat32:
2336 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002338 XMM0);
2339 break;
2340
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002341 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002342 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002343 }
2344 }
2345 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002346}
2347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002349 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002350 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002352 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 case DataType::Type::kInt8:
2354 case DataType::Type::kUint16:
2355 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002356 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002358 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002360 return Location::RegisterLocation(RAX);
2361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002363 return Location::NoLocation();
2364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002365 case DataType::Type::kFloat64:
2366 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002367 return Location::FpuRegisterLocation(XMM0);
2368 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002369
2370 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002371}
2372
2373Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2374 return Location::RegisterLocation(kMethodRegisterArgument);
2375}
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002378 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002379 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002380 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002381 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002382 case DataType::Type::kInt8:
2383 case DataType::Type::kUint16:
2384 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002385 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002386 uint32_t index = gp_index_++;
2387 stack_index_++;
2388 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002389 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002390 } else {
2391 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2392 }
2393 }
2394
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002396 uint32_t index = gp_index_;
2397 stack_index_ += 2;
2398 if (index < calling_convention.GetNumberOfRegisters()) {
2399 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002400 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002401 } else {
2402 gp_index_ += 2;
2403 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2404 }
2405 }
2406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002408 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002409 stack_index_++;
2410 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002411 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412 } else {
2413 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2414 }
2415 }
2416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002418 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002419 stack_index_ += 2;
2420 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002421 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002422 } else {
2423 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2424 }
2425 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002426
Aart Bik66c158e2018-01-31 12:55:04 -08002427 case DataType::Type::kUint32:
2428 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002429 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002430 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002431 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002432 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002433 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002434}
2435
Calin Juravle175dc732015-08-25 15:42:32 +01002436void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2437 // The trampoline uses the same calling convention as dex calling conventions,
2438 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2439 // the method_idx.
2440 HandleInvoke(invoke);
2441}
2442
2443void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2444 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2445}
2446
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002447void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002448 // Explicit clinit checks triggered by static invokes must have been pruned by
2449 // art::PrepareForRegisterAllocation.
2450 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002451
Mark Mendellfb8d2792015-03-31 22:16:59 -04002452 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002453 if (intrinsic.TryDispatch(invoke)) {
2454 return;
2455 }
2456
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002457 HandleInvoke(invoke);
2458}
2459
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002460static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2461 if (invoke->GetLocations()->Intrinsified()) {
2462 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2463 intrinsic.Dispatch(invoke);
2464 return true;
2465 }
2466 return false;
2467}
2468
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002469void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002470 // Explicit clinit checks triggered by static invokes must have been pruned by
2471 // art::PrepareForRegisterAllocation.
2472 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002473
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002474 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2475 return;
2476 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002477
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002478 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002479 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002480 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002481}
2482
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002483void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002484 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002485 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002486}
2487
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002488void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002489 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002490 if (intrinsic.TryDispatch(invoke)) {
2491 return;
2492 }
2493
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002494 HandleInvoke(invoke);
2495}
2496
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002497void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002498 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2499 return;
2500 }
2501
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002502 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002503 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002504}
2505
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002506void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2507 HandleInvoke(invoke);
2508 // Add the hidden argument.
2509 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2510}
2511
2512void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2513 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002514 LocationSummary* locations = invoke->GetLocations();
2515 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2516 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002517 Location receiver = locations->InAt(0);
2518 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2519
Roland Levillain0d5a2812015-11-13 10:07:31 +00002520 // Set the hidden argument. This is safe to do this here, as RAX
2521 // won't be modified thereafter, before the `call` instruction.
2522 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002523 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002524
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002525 if (receiver.IsStackSlot()) {
2526 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002527 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002528 __ movl(temp, Address(temp, class_offset));
2529 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002530 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002531 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002532 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002533 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002534 // Instead of simply (possibly) unpoisoning `temp` here, we should
2535 // emit a read barrier for the previous class reference load.
2536 // However this is not required in practice, as this is an
2537 // intermediate/temporary reference and because the current
2538 // concurrent copying collector keeps the from-space memory
2539 // intact/accessible until the end of the marking phase (the
2540 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002541 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002542 // temp = temp->GetAddressOfIMT()
2543 __ movq(temp,
2544 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2545 // temp = temp->GetImtEntryAt(method_offset);
2546 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002547 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002548 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002549 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002550 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002551 __ call(Address(
2552 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002553
2554 DCHECK(!codegen_->IsLeafMethod());
2555 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2556}
2557
Orion Hodsonac141392017-01-13 11:53:47 +00002558void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2559 HandleInvoke(invoke);
2560}
2561
2562void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2563 codegen_->GenerateInvokePolymorphicCall(invoke);
2564}
2565
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002566void LocationsBuilderX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2567 HandleInvoke(invoke);
2568}
2569
2570void InstructionCodeGeneratorX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2571 codegen_->GenerateInvokeCustomCall(invoke);
2572}
2573
Roland Levillain88cb1752014-10-20 16:36:47 +01002574void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002576 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002577 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kInt32:
2579 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002580 locations->SetInAt(0, Location::RequiresRegister());
2581 locations->SetOut(Location::SameAsFirstInput());
2582 break;
2583
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002584 case DataType::Type::kFloat32:
2585 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002586 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002587 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002588 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002589 break;
2590
2591 default:
2592 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2593 }
2594}
2595
2596void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2597 LocationSummary* locations = neg->GetLocations();
2598 Location out = locations->Out();
2599 Location in = locations->InAt(0);
2600 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002602 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002603 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002604 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002605 break;
2606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002608 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002609 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002611 break;
2612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002614 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002615 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002616 // Implement float negation with an exclusive or with value
2617 // 0x80000000 (mask for bit 31, representing the sign of a
2618 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002619 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002621 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002622 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002625 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002626 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002627 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002628 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002629 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002630 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002631 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002632 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002633 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002634
2635 default:
2636 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2637 }
2638}
2639
Roland Levillaindff1f282014-11-05 14:15:05 +00002640void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2641 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002642 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002643 DataType::Type result_type = conversion->GetResultType();
2644 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002645 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2646 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002647
Roland Levillaindff1f282014-11-05 14:15:05 +00002648 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002649 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002651 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2654 locations->SetInAt(0, Location::Any());
2655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002656 break;
2657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002659 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002661 locations->SetInAt(0, Location::Any());
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663 break;
2664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002666 locations->SetInAt(0, Location::RequiresFpuRegister());
2667 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002668 break;
2669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002671 locations->SetInAt(0, Location::RequiresFpuRegister());
2672 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002673 break;
2674
2675 default:
2676 LOG(FATAL) << "Unexpected type conversion from " << input_type
2677 << " to " << result_type;
2678 }
2679 break;
2680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002682 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002684 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002686 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kInt16:
2688 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002689 // TODO: We would benefit from a (to-be-implemented)
2690 // Location::RegisterOrStackSlot requirement for this input.
2691 locations->SetInAt(0, Location::RequiresRegister());
2692 locations->SetOut(Location::RequiresRegister());
2693 break;
2694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002696 locations->SetInAt(0, Location::RequiresFpuRegister());
2697 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002698 break;
2699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002701 locations->SetInAt(0, Location::RequiresFpuRegister());
2702 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 }
2709 break;
2710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002712 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002713 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002714 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002715 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002716 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 case DataType::Type::kInt16:
2718 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002719 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002720 locations->SetOut(Location::RequiresFpuRegister());
2721 break;
2722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002724 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002725 locations->SetOut(Location::RequiresFpuRegister());
2726 break;
2727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002729 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected type conversion from " << input_type
2735 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002736 }
Roland Levillaincff13742014-11-17 14:32:17 +00002737 break;
2738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002740 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002742 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002744 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kInt16:
2746 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002747 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002748 locations->SetOut(Location::RequiresFpuRegister());
2749 break;
2750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002752 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002753 locations->SetOut(Location::RequiresFpuRegister());
2754 break;
2755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002757 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002758 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002759 break;
2760
2761 default:
2762 LOG(FATAL) << "Unexpected type conversion from " << input_type
2763 << " to " << result_type;
2764 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002765 break;
2766
2767 default:
2768 LOG(FATAL) << "Unexpected type conversion from " << input_type
2769 << " to " << result_type;
2770 }
2771}
2772
2773void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2774 LocationSummary* locations = conversion->GetLocations();
2775 Location out = locations->Out();
2776 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 DataType::Type result_type = conversion->GetResultType();
2778 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002779 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2780 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002781 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002782 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002783 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002784 case DataType::Type::kInt8:
2785 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 case DataType::Type::kInt16:
2787 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002788 case DataType::Type::kInt64:
2789 if (in.IsRegister()) {
2790 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2791 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2792 __ movzxb(out.AsRegister<CpuRegister>(),
2793 Address(CpuRegister(RSP), in.GetStackIndex()));
2794 } else {
2795 __ movl(out.AsRegister<CpuRegister>(),
2796 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2797 }
2798 break;
2799
2800 default:
2801 LOG(FATAL) << "Unexpected type conversion from " << input_type
2802 << " to " << result_type;
2803 }
2804 break;
2805
2806 case DataType::Type::kInt8:
2807 switch (input_type) {
2808 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002810 case DataType::Type::kInt16:
2811 case DataType::Type::kInt32:
2812 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002813 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002815 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002817 Address(CpuRegister(RSP), in.GetStackIndex()));
2818 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002819 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002820 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002821 }
2822 break;
2823
2824 default:
2825 LOG(FATAL) << "Unexpected type conversion from " << input_type
2826 << " to " << result_type;
2827 }
2828 break;
2829
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002830 case DataType::Type::kUint16:
2831 switch (input_type) {
2832 case DataType::Type::kInt8:
2833 case DataType::Type::kInt16:
2834 case DataType::Type::kInt32:
2835 case DataType::Type::kInt64:
2836 if (in.IsRegister()) {
2837 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2838 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2839 __ movzxw(out.AsRegister<CpuRegister>(),
2840 Address(CpuRegister(RSP), in.GetStackIndex()));
2841 } else {
2842 __ movl(out.AsRegister<CpuRegister>(),
2843 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2844 }
2845 break;
2846
2847 default:
2848 LOG(FATAL) << "Unexpected type conversion from " << input_type
2849 << " to " << result_type;
2850 }
2851 break;
2852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002854 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002856 case DataType::Type::kInt32:
2857 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002858 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002859 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002860 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002861 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002862 Address(CpuRegister(RSP), in.GetStackIndex()));
2863 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002865 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002866 }
2867 break;
2868
2869 default:
2870 LOG(FATAL) << "Unexpected type conversion from " << input_type
2871 << " to " << result_type;
2872 }
2873 break;
2874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002876 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002878 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002880 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002882 Address(CpuRegister(RSP), in.GetStackIndex()));
2883 } else {
2884 DCHECK(in.IsConstant());
2885 DCHECK(in.GetConstant()->IsLongConstant());
2886 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002888 }
2889 break;
2890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002892 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2893 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002894 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002895
2896 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002897 // if input >= (float)INT_MAX goto done
2898 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002899 __ j(kAboveEqual, &done);
2900 // if input == NaN goto nan
2901 __ j(kUnordered, &nan);
2902 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002903 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002904 __ jmp(&done);
2905 __ Bind(&nan);
2906 // output = 0
2907 __ xorl(output, output);
2908 __ Bind(&done);
2909 break;
2910 }
2911
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002913 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2914 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002915 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002916
2917 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002918 // if input >= (double)INT_MAX goto done
2919 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002920 __ j(kAboveEqual, &done);
2921 // if input == NaN goto nan
2922 __ j(kUnordered, &nan);
2923 // output = double-to-int-truncate(input)
2924 __ cvttsd2si(output, input);
2925 __ jmp(&done);
2926 __ Bind(&nan);
2927 // output = 0
2928 __ xorl(output, output);
2929 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002930 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002931 }
Roland Levillain946e1432014-11-11 17:35:19 +00002932
2933 default:
2934 LOG(FATAL) << "Unexpected type conversion from " << input_type
2935 << " to " << result_type;
2936 }
2937 break;
2938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002940 switch (input_type) {
2941 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002943 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002945 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt16:
2947 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002948 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002949 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002950 break;
2951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002953 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2954 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002955 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002956
Mark Mendell92e83bf2015-05-07 11:25:03 -04002957 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002958 // if input >= (float)LONG_MAX goto done
2959 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002960 __ j(kAboveEqual, &done);
2961 // if input == NaN goto nan
2962 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002963 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002964 __ cvttss2si(output, input, true);
2965 __ jmp(&done);
2966 __ Bind(&nan);
2967 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002968 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002969 __ Bind(&done);
2970 break;
2971 }
2972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002974 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2975 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002976 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002977
Mark Mendell92e83bf2015-05-07 11:25:03 -04002978 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002979 // if input >= (double)LONG_MAX goto done
2980 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002981 __ j(kAboveEqual, &done);
2982 // if input == NaN goto nan
2983 __ j(kUnordered, &nan);
2984 // output = double-to-long-truncate(input)
2985 __ cvttsd2si(output, input, true);
2986 __ jmp(&done);
2987 __ Bind(&nan);
2988 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002989 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002990 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002991 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002992 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002993
2994 default:
2995 LOG(FATAL) << "Unexpected type conversion from " << input_type
2996 << " to " << result_type;
2997 }
2998 break;
2999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003001 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003003 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003005 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 case DataType::Type::kInt16:
3007 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003008 if (in.IsRegister()) {
3009 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3010 } else if (in.IsConstant()) {
3011 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3012 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003013 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003014 } else {
3015 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3016 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3017 }
Roland Levillaincff13742014-11-17 14:32:17 +00003018 break;
3019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003021 if (in.IsRegister()) {
3022 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3023 } else if (in.IsConstant()) {
3024 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3025 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06003026 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003027 } else {
3028 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3029 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3030 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003031 break;
3032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003033 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04003034 if (in.IsFpuRegister()) {
3035 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3036 } else if (in.IsConstant()) {
3037 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
3038 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003039 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003040 } else {
3041 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
3042 Address(CpuRegister(RSP), in.GetStackIndex()));
3043 }
Roland Levillaincff13742014-11-17 14:32:17 +00003044 break;
3045
3046 default:
3047 LOG(FATAL) << "Unexpected type conversion from " << input_type
3048 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003049 }
Roland Levillaincff13742014-11-17 14:32:17 +00003050 break;
3051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003052 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003053 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003054 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003055 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003057 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003058 case DataType::Type::kInt16:
3059 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003060 if (in.IsRegister()) {
3061 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3062 } else if (in.IsConstant()) {
3063 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3064 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003065 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003066 } else {
3067 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3068 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3069 }
Roland Levillaincff13742014-11-17 14:32:17 +00003070 break;
3071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003073 if (in.IsRegister()) {
3074 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3075 } else if (in.IsConstant()) {
3076 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3077 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003078 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003079 } else {
3080 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3081 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3082 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003083 break;
3084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003086 if (in.IsFpuRegister()) {
3087 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3088 } else if (in.IsConstant()) {
3089 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3090 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003091 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003092 } else {
3093 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3094 Address(CpuRegister(RSP), in.GetStackIndex()));
3095 }
Roland Levillaincff13742014-11-17 14:32:17 +00003096 break;
3097
3098 default:
3099 LOG(FATAL) << "Unexpected type conversion from " << input_type
3100 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003101 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003102 break;
3103
3104 default:
3105 LOG(FATAL) << "Unexpected type conversion from " << input_type
3106 << " to " << result_type;
3107 }
3108}
3109
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003110void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003111 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003112 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003113 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003114 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003115 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003116 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003118 break;
3119 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003122 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003123 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003124 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003126 break;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kFloat64:
3130 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003131 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003132 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003133 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003134 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003135 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003136
3137 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003138 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003139 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003140}
3141
3142void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3143 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003144 Location first = locations->InAt(0);
3145 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003146 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003147
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003148 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003150 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003151 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3152 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003153 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3154 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003155 } else {
3156 __ leal(out.AsRegister<CpuRegister>(), Address(
3157 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3158 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003159 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003160 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3161 __ addl(out.AsRegister<CpuRegister>(),
3162 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3163 } else {
3164 __ leal(out.AsRegister<CpuRegister>(), Address(
3165 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3166 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003167 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003168 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003169 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003170 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003171 break;
3172 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003173
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003175 if (second.IsRegister()) {
3176 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3177 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003178 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3179 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003180 } else {
3181 __ leaq(out.AsRegister<CpuRegister>(), Address(
3182 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3183 }
3184 } else {
3185 DCHECK(second.IsConstant());
3186 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3187 int32_t int32_value = Low32Bits(value);
3188 DCHECK_EQ(int32_value, value);
3189 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3190 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3191 } else {
3192 __ leaq(out.AsRegister<CpuRegister>(), Address(
3193 first.AsRegister<CpuRegister>(), int32_value));
3194 }
3195 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003196 break;
3197 }
3198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003200 if (second.IsFpuRegister()) {
3201 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3202 } else if (second.IsConstant()) {
3203 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003204 codegen_->LiteralFloatAddress(
3205 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003206 } else {
3207 DCHECK(second.IsStackSlot());
3208 __ addss(first.AsFpuRegister<XmmRegister>(),
3209 Address(CpuRegister(RSP), second.GetStackIndex()));
3210 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003211 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003212 }
3213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003215 if (second.IsFpuRegister()) {
3216 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3217 } else if (second.IsConstant()) {
3218 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003219 codegen_->LiteralDoubleAddress(
3220 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003221 } else {
3222 DCHECK(second.IsDoubleStackSlot());
3223 __ addsd(first.AsFpuRegister<XmmRegister>(),
3224 Address(CpuRegister(RSP), second.GetStackIndex()));
3225 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003226 break;
3227 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003228
3229 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003230 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003231 }
3232}
3233
3234void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003235 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003236 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003237 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003238 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003239 locations->SetInAt(0, Location::RequiresRegister());
3240 locations->SetInAt(1, Location::Any());
3241 locations->SetOut(Location::SameAsFirstInput());
3242 break;
3243 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003244 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003245 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003246 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003247 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003248 break;
3249 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003250 case DataType::Type::kFloat32:
3251 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003252 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003253 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003254 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003255 break;
Calin Juravle11351682014-10-23 15:38:15 +01003256 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003257 default:
Calin Juravle11351682014-10-23 15:38:15 +01003258 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003259 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003260}
3261
3262void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3263 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003264 Location first = locations->InAt(0);
3265 Location second = locations->InAt(1);
3266 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003267 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003268 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003269 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003271 } else if (second.IsConstant()) {
3272 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003273 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003274 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003275 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003276 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003277 break;
3278 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003280 if (second.IsConstant()) {
3281 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3282 DCHECK(IsInt<32>(value));
3283 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3284 } else {
3285 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3286 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003287 break;
3288 }
3289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003291 if (second.IsFpuRegister()) {
3292 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3293 } else if (second.IsConstant()) {
3294 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003295 codegen_->LiteralFloatAddress(
3296 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003297 } else {
3298 DCHECK(second.IsStackSlot());
3299 __ subss(first.AsFpuRegister<XmmRegister>(),
3300 Address(CpuRegister(RSP), second.GetStackIndex()));
3301 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003302 break;
Calin Juravle11351682014-10-23 15:38:15 +01003303 }
3304
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003305 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003306 if (second.IsFpuRegister()) {
3307 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3308 } else if (second.IsConstant()) {
3309 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003310 codegen_->LiteralDoubleAddress(
3311 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003312 } else {
3313 DCHECK(second.IsDoubleStackSlot());
3314 __ subsd(first.AsFpuRegister<XmmRegister>(),
3315 Address(CpuRegister(RSP), second.GetStackIndex()));
3316 }
Calin Juravle11351682014-10-23 15:38:15 +01003317 break;
3318 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003319
3320 default:
Calin Juravle11351682014-10-23 15:38:15 +01003321 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003322 }
3323}
3324
Calin Juravle34bacdf2014-10-07 20:23:36 +01003325void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003327 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003328 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003330 locations->SetInAt(0, Location::RequiresRegister());
3331 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003332 if (mul->InputAt(1)->IsIntConstant()) {
3333 // Can use 3 operand multiply.
3334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3335 } else {
3336 locations->SetOut(Location::SameAsFirstInput());
3337 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003338 break;
3339 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003340 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003341 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003342 locations->SetInAt(1, Location::Any());
3343 if (mul->InputAt(1)->IsLongConstant() &&
3344 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003345 // Can use 3 operand multiply.
3346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3347 } else {
3348 locations->SetOut(Location::SameAsFirstInput());
3349 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003350 break;
3351 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003352 case DataType::Type::kFloat32:
3353 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003354 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003355 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003356 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003357 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003358 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003359
3360 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003361 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003362 }
3363}
3364
3365void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3366 LocationSummary* locations = mul->GetLocations();
3367 Location first = locations->InAt(0);
3368 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003369 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003370 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003372 // The constant may have ended up in a register, so test explicitly to avoid
3373 // problems where the output may not be the same as the first operand.
3374 if (mul->InputAt(1)->IsIntConstant()) {
3375 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3376 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3377 } else if (second.IsRegister()) {
3378 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003380 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003381 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003382 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003383 __ imull(first.AsRegister<CpuRegister>(),
3384 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003385 }
3386 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003387 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003388 // The constant may have ended up in a register, so test explicitly to avoid
3389 // problems where the output may not be the same as the first operand.
3390 if (mul->InputAt(1)->IsLongConstant()) {
3391 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3392 if (IsInt<32>(value)) {
3393 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3394 Immediate(static_cast<int32_t>(value)));
3395 } else {
3396 // Have to use the constant area.
3397 DCHECK(first.Equals(out));
3398 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3399 }
3400 } else if (second.IsRegister()) {
3401 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003402 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003403 } else {
3404 DCHECK(second.IsDoubleStackSlot());
3405 DCHECK(first.Equals(out));
3406 __ imulq(first.AsRegister<CpuRegister>(),
3407 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003408 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003409 break;
3410 }
3411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003413 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003414 if (second.IsFpuRegister()) {
3415 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3416 } else if (second.IsConstant()) {
3417 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003418 codegen_->LiteralFloatAddress(
3419 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003420 } else {
3421 DCHECK(second.IsStackSlot());
3422 __ mulss(first.AsFpuRegister<XmmRegister>(),
3423 Address(CpuRegister(RSP), second.GetStackIndex()));
3424 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003425 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003426 }
3427
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003428 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003429 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003430 if (second.IsFpuRegister()) {
3431 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3432 } else if (second.IsConstant()) {
3433 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003434 codegen_->LiteralDoubleAddress(
3435 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003436 } else {
3437 DCHECK(second.IsDoubleStackSlot());
3438 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3439 Address(CpuRegister(RSP), second.GetStackIndex()));
3440 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003441 break;
3442 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003443
3444 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003445 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003446 }
3447}
3448
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003449void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3450 uint32_t stack_adjustment, bool is_float) {
3451 if (source.IsStackSlot()) {
3452 DCHECK(is_float);
3453 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3454 } else if (source.IsDoubleStackSlot()) {
3455 DCHECK(!is_float);
3456 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3457 } else {
3458 // Write the value to the temporary location on the stack and load to FP stack.
3459 if (is_float) {
3460 Location stack_temp = Location::StackSlot(temp_offset);
3461 codegen_->Move(stack_temp, source);
3462 __ flds(Address(CpuRegister(RSP), temp_offset));
3463 } else {
3464 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3465 codegen_->Move(stack_temp, source);
3466 __ fldl(Address(CpuRegister(RSP), temp_offset));
3467 }
3468 }
3469}
3470
3471void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003472 DataType::Type type = rem->GetResultType();
3473 bool is_float = type == DataType::Type::kFloat32;
3474 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003475 LocationSummary* locations = rem->GetLocations();
3476 Location first = locations->InAt(0);
3477 Location second = locations->InAt(1);
3478 Location out = locations->Out();
3479
3480 // Create stack space for 2 elements.
3481 // TODO: enhance register allocator to ask for stack temporaries.
3482 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3483
3484 // Load the values to the FP stack in reverse order, using temporaries if needed.
3485 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3486 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3487
3488 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003489 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003490 __ Bind(&retry);
3491 __ fprem();
3492
3493 // Move FP status to AX.
3494 __ fstsw();
3495
3496 // And see if the argument reduction is complete. This is signaled by the
3497 // C2 FPU flag bit set to 0.
3498 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3499 __ j(kNotEqual, &retry);
3500
3501 // We have settled on the final value. Retrieve it into an XMM register.
3502 // Store FP top of stack to real stack.
3503 if (is_float) {
3504 __ fsts(Address(CpuRegister(RSP), 0));
3505 } else {
3506 __ fstl(Address(CpuRegister(RSP), 0));
3507 }
3508
3509 // Pop the 2 items from the FP stack.
3510 __ fucompp();
3511
3512 // Load the value from the stack into an XMM register.
3513 DCHECK(out.IsFpuRegister()) << out;
3514 if (is_float) {
3515 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3516 } else {
3517 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3518 }
3519
3520 // And remove the temporary stack space we allocated.
3521 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3522}
3523
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003524void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3525 DCHECK(instruction->IsDiv() || instruction->IsRem());
3526
3527 LocationSummary* locations = instruction->GetLocations();
3528 Location second = locations->InAt(1);
3529 DCHECK(second.IsConstant());
3530
3531 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3532 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003533 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003534
3535 DCHECK(imm == 1 || imm == -1);
3536
3537 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003538 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539 if (instruction->IsRem()) {
3540 __ xorl(output_register, output_register);
3541 } else {
3542 __ movl(output_register, input_register);
3543 if (imm == -1) {
3544 __ negl(output_register);
3545 }
3546 }
3547 break;
3548 }
3549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003550 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003551 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003552 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003553 } else {
3554 __ movq(output_register, input_register);
3555 if (imm == -1) {
3556 __ negq(output_register);
3557 }
3558 }
3559 break;
3560 }
3561
3562 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003563 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 }
3565}
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303566void InstructionCodeGeneratorX86_64::RemByPowerOfTwo(HRem* instruction) {
3567 LocationSummary* locations = instruction->GetLocations();
3568 Location second = locations->InAt(1);
3569 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3570 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3571 int64_t imm = Int64FromConstant(second.GetConstant());
3572 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3573 uint64_t abs_imm = AbsOrMin(imm);
3574 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3575 if (instruction->GetResultType() == DataType::Type::kInt32) {
3576 NearLabel done;
3577 __ movl(out, numerator);
3578 __ andl(out, Immediate(abs_imm-1));
3579 __ j(Condition::kZero, &done);
3580 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3581 __ testl(numerator, numerator);
3582 __ cmov(Condition::kLess, out, tmp, false);
3583 __ Bind(&done);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303585 } else {
3586 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3587 codegen_->Load64BitValue(tmp, abs_imm - 1);
3588 NearLabel done;
3589
3590 __ movq(out, numerator);
3591 __ andq(out, tmp);
3592 __ j(Condition::kZero, &done);
3593 __ movq(tmp, numerator);
3594 __ sarq(tmp, Immediate(63));
3595 __ shlq(tmp, Immediate(WhichPowerOf2(abs_imm)));
3596 __ orq(out, tmp);
3597 __ Bind(&done);
3598 }
3599}
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003600void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601 LocationSummary* locations = instruction->GetLocations();
3602 Location second = locations->InAt(1);
3603
3604 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3605 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3606
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003607 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003608 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3609 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003610
3611 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003613 if (instruction->GetResultType() == DataType::Type::kInt32) {
Atul Bajaj1cc73292018-11-15 11:36:53 +05303614 // When denominator is equal to 2, we can add signed bit and numerator to tmp.
3615 // Below we are using addl instruction instead of cmov which give us 1 cycle benefit.
3616 if (abs_imm == 2) {
3617 __ leal(tmp, Address(numerator, 0));
3618 __ shrl(tmp, Immediate(31));
3619 __ addl(tmp, numerator);
3620 } else {
3621 __ leal(tmp, Address(numerator, abs_imm - 1));
3622 __ testl(numerator, numerator);
3623 __ cmov(kGreaterEqual, tmp, numerator);
3624 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003625 int shift = CTZ(imm);
3626 __ sarl(tmp, Immediate(shift));
3627
3628 if (imm < 0) {
3629 __ negl(tmp);
3630 }
3631
3632 __ movl(output_register, tmp);
3633 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003634 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003635 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
Atul Bajaj1cc73292018-11-15 11:36:53 +05303636 if (abs_imm == 2) {
3637 __ movq(rdx, numerator);
3638 __ shrq(rdx, Immediate(63));
3639 __ addq(rdx, numerator);
3640 } else {
3641 codegen_->Load64BitValue(rdx, abs_imm - 1);
3642 __ addq(rdx, numerator);
3643 __ testq(numerator, numerator);
3644 __ cmov(kGreaterEqual, rdx, numerator);
3645 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003646 int shift = CTZ(imm);
3647 __ sarq(rdx, Immediate(shift));
3648
3649 if (imm < 0) {
3650 __ negq(rdx);
3651 }
3652
3653 __ movq(output_register, rdx);
3654 }
3655}
3656
3657void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3658 DCHECK(instruction->IsDiv() || instruction->IsRem());
3659
3660 LocationSummary* locations = instruction->GetLocations();
3661 Location second = locations->InAt(1);
3662
3663 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3664 : locations->GetTemp(0).AsRegister<CpuRegister>();
3665 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3666 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3667 : locations->Out().AsRegister<CpuRegister>();
3668 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3669
3670 DCHECK_EQ(RAX, eax.AsRegister());
3671 DCHECK_EQ(RDX, edx.AsRegister());
3672 if (instruction->IsDiv()) {
3673 DCHECK_EQ(RAX, out.AsRegister());
3674 } else {
3675 DCHECK_EQ(RDX, out.AsRegister());
3676 }
3677
3678 int64_t magic;
3679 int shift;
3680
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003681 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003682 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003683 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3684
Andreas Gampe3db70682018-12-26 15:12:03 -08003685 CalculateMagicAndShiftForDivRem(imm, false /* is_long= */, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003686
3687 __ movl(numerator, eax);
3688
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003689 __ movl(eax, Immediate(magic));
3690 __ imull(numerator);
3691
3692 if (imm > 0 && magic < 0) {
3693 __ addl(edx, numerator);
3694 } else if (imm < 0 && magic > 0) {
3695 __ subl(edx, numerator);
3696 }
3697
3698 if (shift != 0) {
3699 __ sarl(edx, Immediate(shift));
3700 }
3701
3702 __ movl(eax, edx);
3703 __ shrl(edx, Immediate(31));
3704 __ addl(edx, eax);
3705
3706 if (instruction->IsRem()) {
3707 __ movl(eax, numerator);
3708 __ imull(edx, Immediate(imm));
3709 __ subl(eax, edx);
3710 __ movl(edx, eax);
3711 } else {
3712 __ movl(eax, edx);
3713 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003714 } else {
3715 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003717 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003718
3719 CpuRegister rax = eax;
3720 CpuRegister rdx = edx;
3721
Andreas Gampe3db70682018-12-26 15:12:03 -08003722 CalculateMagicAndShiftForDivRem(imm, true /* is_long= */, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003723
3724 // Save the numerator.
3725 __ movq(numerator, rax);
3726
3727 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003728 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003729
3730 // RDX:RAX = magic * numerator
3731 __ imulq(numerator);
3732
3733 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003734 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003735 __ addq(rdx, numerator);
3736 } else if (imm < 0 && magic > 0) {
3737 // RDX -= numerator
3738 __ subq(rdx, numerator);
3739 }
3740
3741 // Shift if needed.
3742 if (shift != 0) {
3743 __ sarq(rdx, Immediate(shift));
3744 }
3745
3746 // RDX += 1 if RDX < 0
3747 __ movq(rax, rdx);
3748 __ shrq(rdx, Immediate(63));
3749 __ addq(rdx, rax);
3750
3751 if (instruction->IsRem()) {
3752 __ movq(rax, numerator);
3753
3754 if (IsInt<32>(imm)) {
3755 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3756 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003757 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003758 }
3759
3760 __ subq(rax, rdx);
3761 __ movq(rdx, rax);
3762 } else {
3763 __ movq(rax, rdx);
3764 }
3765 }
3766}
3767
Calin Juravlebacfec32014-11-14 15:54:36 +00003768void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3769 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 DataType::Type type = instruction->GetResultType();
3771 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003772
3773 bool is_div = instruction->IsDiv();
3774 LocationSummary* locations = instruction->GetLocations();
3775
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003776 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3777 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003778
Roland Levillain271ab9c2014-11-27 15:23:57 +00003779 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003780 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003781
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003782 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003783 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003784
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003785 if (imm == 0) {
3786 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3787 } else if (imm == 1 || imm == -1) {
3788 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303789 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3790 if (is_div) {
3791 DivByPowerOfTwo(instruction->AsDiv());
3792 } else {
3793 RemByPowerOfTwo(instruction->AsRem());
3794 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003795 } else {
3796 DCHECK(imm <= -2 || imm >= 2);
3797 GenerateDivRemWithAnyConstant(instruction);
3798 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003799 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003800 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003801 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003802 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003803 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003804
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003805 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3806 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3807 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3808 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003810 __ cmpl(second_reg, Immediate(-1));
3811 __ j(kEqual, slow_path->GetEntryLabel());
3812 // edx:eax <- sign-extended of eax
3813 __ cdq();
3814 // eax = quotient, edx = remainder
3815 __ idivl(second_reg);
3816 } else {
3817 __ cmpq(second_reg, Immediate(-1));
3818 __ j(kEqual, slow_path->GetEntryLabel());
3819 // rdx:rax <- sign-extended of rax
3820 __ cqo();
3821 // rax = quotient, rdx = remainder
3822 __ idivq(second_reg);
3823 }
3824 __ Bind(slow_path->GetExitLabel());
3825 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003826}
3827
Calin Juravle7c4954d2014-10-28 16:57:40 +00003828void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3829 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003830 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003831 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003832 case DataType::Type::kInt32:
3833 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003834 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003835 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003836 locations->SetOut(Location::SameAsFirstInput());
3837 // Intel uses edx:eax as the dividend.
3838 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003839 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3840 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3841 // output and request another temp.
3842 if (div->InputAt(1)->IsConstant()) {
3843 locations->AddTemp(Location::RequiresRegister());
3844 }
Calin Juravled0d48522014-11-04 16:40:20 +00003845 break;
3846 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kFloat32:
3849 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003850 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003851 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003852 locations->SetOut(Location::SameAsFirstInput());
3853 break;
3854 }
3855
3856 default:
3857 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3858 }
3859}
3860
3861void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3862 LocationSummary* locations = div->GetLocations();
3863 Location first = locations->InAt(0);
3864 Location second = locations->InAt(1);
3865 DCHECK(first.Equals(locations->Out()));
3866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003867 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003868 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kInt32:
3870 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003871 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003872 break;
3873 }
3874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003875 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003876 if (second.IsFpuRegister()) {
3877 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3878 } else if (second.IsConstant()) {
3879 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003880 codegen_->LiteralFloatAddress(
3881 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003882 } else {
3883 DCHECK(second.IsStackSlot());
3884 __ divss(first.AsFpuRegister<XmmRegister>(),
3885 Address(CpuRegister(RSP), second.GetStackIndex()));
3886 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003887 break;
3888 }
3889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003890 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003891 if (second.IsFpuRegister()) {
3892 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3893 } else if (second.IsConstant()) {
3894 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003895 codegen_->LiteralDoubleAddress(
3896 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003897 } else {
3898 DCHECK(second.IsDoubleStackSlot());
3899 __ divsd(first.AsFpuRegister<XmmRegister>(),
3900 Address(CpuRegister(RSP), second.GetStackIndex()));
3901 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003902 break;
3903 }
3904
3905 default:
3906 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3907 }
3908}
3909
Calin Juravlebacfec32014-11-14 15:54:36 +00003910void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003912 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003913 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003914
3915 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 case DataType::Type::kInt32:
3917 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003918 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003919 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003920 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3921 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003922 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3923 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3924 // output and request another temp.
3925 if (rem->InputAt(1)->IsConstant()) {
3926 locations->AddTemp(Location::RequiresRegister());
3927 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003928 break;
3929 }
3930
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003931 case DataType::Type::kFloat32:
3932 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003933 locations->SetInAt(0, Location::Any());
3934 locations->SetInAt(1, Location::Any());
3935 locations->SetOut(Location::RequiresFpuRegister());
3936 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003937 break;
3938 }
3939
3940 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003941 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003942 }
3943}
3944
3945void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003946 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003947 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 case DataType::Type::kInt32:
3949 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003950 GenerateDivRemIntegral(rem);
3951 break;
3952 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 case DataType::Type::kFloat32:
3954 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003955 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003956 break;
3957 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003958 default:
3959 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3960 }
3961}
3962
Aart Bik1f8d51b2018-02-15 10:42:37 -08003963static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3964 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3965 switch (minmax->GetResultType()) {
3966 case DataType::Type::kInt32:
3967 case DataType::Type::kInt64:
3968 locations->SetInAt(0, Location::RequiresRegister());
3969 locations->SetInAt(1, Location::RequiresRegister());
3970 locations->SetOut(Location::SameAsFirstInput());
3971 break;
3972 case DataType::Type::kFloat32:
3973 case DataType::Type::kFloat64:
3974 locations->SetInAt(0, Location::RequiresFpuRegister());
3975 locations->SetInAt(1, Location::RequiresFpuRegister());
3976 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
3977 // the second input to be the output (we can simply swap inputs).
3978 locations->SetOut(Location::SameAsFirstInput());
3979 break;
3980 default:
3981 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3982 }
3983}
3984
Aart Bik351df3e2018-03-07 11:54:57 -08003985void InstructionCodeGeneratorX86_64::GenerateMinMaxInt(LocationSummary* locations,
3986 bool is_min,
3987 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003988 Location op1_loc = locations->InAt(0);
3989 Location op2_loc = locations->InAt(1);
3990
3991 // Shortcut for same input locations.
3992 if (op1_loc.Equals(op2_loc)) {
3993 // Can return immediately, as op1_loc == out_loc.
3994 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3995 // a copy here.
3996 DCHECK(locations->Out().Equals(op1_loc));
3997 return;
3998 }
3999
4000 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4001 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
4002
4003 // (out := op1)
4004 // out <=? op2
4005 // if out is min jmp done
4006 // out := op2
4007 // done:
4008
4009 if (type == DataType::Type::kInt64) {
4010 __ cmpq(out, op2);
4011 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ true);
4012 } else {
4013 DCHECK_EQ(type, DataType::Type::kInt32);
4014 __ cmpl(out, op2);
4015 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ false);
4016 }
4017}
4018
4019void InstructionCodeGeneratorX86_64::GenerateMinMaxFP(LocationSummary* locations,
4020 bool is_min,
4021 DataType::Type type) {
4022 Location op1_loc = locations->InAt(0);
4023 Location op2_loc = locations->InAt(1);
4024 Location out_loc = locations->Out();
4025 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4026
4027 // Shortcut for same input locations.
4028 if (op1_loc.Equals(op2_loc)) {
4029 DCHECK(out_loc.Equals(op1_loc));
4030 return;
4031 }
4032
4033 // (out := op1)
4034 // out <=? op2
4035 // if Nan jmp Nan_label
4036 // if out is min jmp done
4037 // if op2 is min jmp op2_label
4038 // handle -0/+0
4039 // jmp done
4040 // Nan_label:
4041 // out := NaN
4042 // op2_label:
4043 // out := op2
4044 // done:
4045 //
4046 // This removes one jmp, but needs to copy one input (op1) to out.
4047 //
4048 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
4049
4050 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4051
4052 NearLabel nan, done, op2_label;
4053 if (type == DataType::Type::kFloat64) {
4054 __ ucomisd(out, op2);
4055 } else {
4056 DCHECK_EQ(type, DataType::Type::kFloat32);
4057 __ ucomiss(out, op2);
4058 }
4059
4060 __ j(Condition::kParityEven, &nan);
4061
4062 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4063 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4064
4065 // Handle 0.0/-0.0.
4066 if (is_min) {
4067 if (type == DataType::Type::kFloat64) {
4068 __ orpd(out, op2);
4069 } else {
4070 __ orps(out, op2);
4071 }
4072 } else {
4073 if (type == DataType::Type::kFloat64) {
4074 __ andpd(out, op2);
4075 } else {
4076 __ andps(out, op2);
4077 }
4078 }
4079 __ jmp(&done);
4080
4081 // NaN handling.
4082 __ Bind(&nan);
4083 if (type == DataType::Type::kFloat64) {
4084 __ movsd(out, codegen_->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
4085 } else {
4086 __ movss(out, codegen_->LiteralInt32Address(INT32_C(0x7FC00000)));
4087 }
4088 __ jmp(&done);
4089
4090 // out := op2;
4091 __ Bind(&op2_label);
4092 if (type == DataType::Type::kFloat64) {
4093 __ movsd(out, op2);
4094 } else {
4095 __ movss(out, op2);
4096 }
4097
4098 // Done.
4099 __ Bind(&done);
4100}
4101
Aart Bik351df3e2018-03-07 11:54:57 -08004102void InstructionCodeGeneratorX86_64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4103 DataType::Type type = minmax->GetResultType();
4104 switch (type) {
4105 case DataType::Type::kInt32:
4106 case DataType::Type::kInt64:
4107 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4108 break;
4109 case DataType::Type::kFloat32:
4110 case DataType::Type::kFloat64:
4111 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4112 break;
4113 default:
4114 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4115 }
4116}
4117
Aart Bik1f8d51b2018-02-15 10:42:37 -08004118void LocationsBuilderX86_64::VisitMin(HMin* min) {
4119 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4120}
4121
4122void InstructionCodeGeneratorX86_64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004123 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004124}
4125
4126void LocationsBuilderX86_64::VisitMax(HMax* max) {
4127 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4128}
4129
4130void InstructionCodeGeneratorX86_64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004131 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004132}
4133
Aart Bik3dad3412018-02-28 12:01:46 -08004134void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
4135 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4136 switch (abs->GetResultType()) {
4137 case DataType::Type::kInt32:
4138 case DataType::Type::kInt64:
4139 locations->SetInAt(0, Location::RequiresRegister());
4140 locations->SetOut(Location::SameAsFirstInput());
4141 locations->AddTemp(Location::RequiresRegister());
4142 break;
4143 case DataType::Type::kFloat32:
4144 case DataType::Type::kFloat64:
4145 locations->SetInAt(0, Location::RequiresFpuRegister());
4146 locations->SetOut(Location::SameAsFirstInput());
4147 locations->AddTemp(Location::RequiresFpuRegister());
4148 break;
4149 default:
4150 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4151 }
4152}
4153
4154void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
4155 LocationSummary* locations = abs->GetLocations();
4156 switch (abs->GetResultType()) {
4157 case DataType::Type::kInt32: {
4158 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4159 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4160 // Create mask.
4161 __ movl(mask, out);
4162 __ sarl(mask, Immediate(31));
4163 // Add mask.
4164 __ addl(out, mask);
4165 __ xorl(out, mask);
4166 break;
4167 }
4168 case DataType::Type::kInt64: {
4169 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4170 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4171 // Create mask.
4172 __ movq(mask, out);
4173 __ sarq(mask, Immediate(63));
4174 // Add mask.
4175 __ addq(out, mask);
4176 __ xorq(out, mask);
4177 break;
4178 }
4179 case DataType::Type::kFloat32: {
4180 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4181 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4182 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
4183 __ andps(out, mask);
4184 break;
4185 }
4186 case DataType::Type::kFloat64: {
4187 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4188 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4189 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
4190 __ andpd(out, mask);
4191 break;
4192 }
4193 default:
4194 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4195 }
4196}
4197
Calin Juravled0d48522014-11-04 16:40:20 +00004198void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004199 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004200 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00004201}
4202
4203void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004204 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004205 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004206 codegen_->AddSlowPath(slow_path);
4207
4208 LocationSummary* locations = instruction->GetLocations();
4209 Location value = locations->InAt(0);
4210
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004211 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004213 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004214 case DataType::Type::kInt8:
4215 case DataType::Type::kUint16:
4216 case DataType::Type::kInt16:
4217 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004218 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004219 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004220 __ j(kEqual, slow_path->GetEntryLabel());
4221 } else if (value.IsStackSlot()) {
4222 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4223 __ j(kEqual, slow_path->GetEntryLabel());
4224 } else {
4225 DCHECK(value.IsConstant()) << value;
4226 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004227 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004228 }
4229 }
4230 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004231 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004232 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004233 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004234 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004235 __ j(kEqual, slow_path->GetEntryLabel());
4236 } else if (value.IsDoubleStackSlot()) {
4237 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4238 __ j(kEqual, slow_path->GetEntryLabel());
4239 } else {
4240 DCHECK(value.IsConstant()) << value;
4241 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004242 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004243 }
4244 }
4245 break;
4246 }
4247 default:
4248 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004249 }
Calin Juravled0d48522014-11-04 16:40:20 +00004250}
4251
Calin Juravle9aec02f2014-11-18 23:06:35 +00004252void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
4253 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4254
4255 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004256 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004257
4258 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004259 case DataType::Type::kInt32:
4260 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004261 locations->SetInAt(0, Location::RequiresRegister());
4262 // The shift count needs to be in CL.
4263 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
4264 locations->SetOut(Location::SameAsFirstInput());
4265 break;
4266 }
4267 default:
4268 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4269 }
4270}
4271
4272void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
4273 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4274
4275 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004276 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004277 Location second = locations->InAt(1);
4278
4279 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004280 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004281 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004282 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004283 if (op->IsShl()) {
4284 __ shll(first_reg, second_reg);
4285 } else if (op->IsShr()) {
4286 __ sarl(first_reg, second_reg);
4287 } else {
4288 __ shrl(first_reg, second_reg);
4289 }
4290 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004291 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004292 if (op->IsShl()) {
4293 __ shll(first_reg, imm);
4294 } else if (op->IsShr()) {
4295 __ sarl(first_reg, imm);
4296 } else {
4297 __ shrl(first_reg, imm);
4298 }
4299 }
4300 break;
4301 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004302 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004303 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004304 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004305 if (op->IsShl()) {
4306 __ shlq(first_reg, second_reg);
4307 } else if (op->IsShr()) {
4308 __ sarq(first_reg, second_reg);
4309 } else {
4310 __ shrq(first_reg, second_reg);
4311 }
4312 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004313 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004314 if (op->IsShl()) {
4315 __ shlq(first_reg, imm);
4316 } else if (op->IsShr()) {
4317 __ sarq(first_reg, imm);
4318 } else {
4319 __ shrq(first_reg, imm);
4320 }
4321 }
4322 break;
4323 }
4324 default:
4325 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004326 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004327 }
4328}
4329
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004330void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4331 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004332 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004333
4334 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004335 case DataType::Type::kInt32:
4336 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004337 locations->SetInAt(0, Location::RequiresRegister());
4338 // The shift count needs to be in CL (unless it is a constant).
4339 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4340 locations->SetOut(Location::SameAsFirstInput());
4341 break;
4342 }
4343 default:
4344 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4345 UNREACHABLE();
4346 }
4347}
4348
4349void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4350 LocationSummary* locations = ror->GetLocations();
4351 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4352 Location second = locations->InAt(1);
4353
4354 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004356 if (second.IsRegister()) {
4357 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4358 __ rorl(first_reg, second_reg);
4359 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004360 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004361 __ rorl(first_reg, imm);
4362 }
4363 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004364 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004365 if (second.IsRegister()) {
4366 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4367 __ rorq(first_reg, second_reg);
4368 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004369 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004370 __ rorq(first_reg, imm);
4371 }
4372 break;
4373 default:
4374 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4375 UNREACHABLE();
4376 }
4377}
4378
Calin Juravle9aec02f2014-11-18 23:06:35 +00004379void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4380 HandleShift(shl);
4381}
4382
4383void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4384 HandleShift(shl);
4385}
4386
4387void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4388 HandleShift(shr);
4389}
4390
4391void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4392 HandleShift(shr);
4393}
4394
4395void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4396 HandleShift(ushr);
4397}
4398
4399void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4400 HandleShift(ushr);
4401}
4402
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004403void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004404 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4405 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004406 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07004407 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004408 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004409}
4410
4411void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004412 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4413 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4414 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004415}
4416
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004417void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004418 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4419 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004420 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004421 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004422 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4423 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004424}
4425
4426void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004427 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4428 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004429 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004430 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004431 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004432}
4433
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004434void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004435 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004436 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004437 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4438 if (location.IsStackSlot()) {
4439 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4440 } else if (location.IsDoubleStackSlot()) {
4441 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4442 }
4443 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004444}
4445
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004446void InstructionCodeGeneratorX86_64::VisitParameterValue(
4447 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004448 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004449}
4450
4451void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4452 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004453 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004454 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4455}
4456
4457void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4458 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4459 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004460}
4461
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004462void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4463 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004464 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004465 locations->SetInAt(0, Location::RequiresRegister());
4466 locations->SetOut(Location::RequiresRegister());
4467}
4468
4469void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4470 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004471 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004472 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004473 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004474 __ movq(locations->Out().AsRegister<CpuRegister>(),
4475 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004476 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004477 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004478 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004479 __ movq(locations->Out().AsRegister<CpuRegister>(),
4480 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4481 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004482 __ movq(locations->Out().AsRegister<CpuRegister>(),
4483 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004484 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004485}
4486
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004487void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004488 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004489 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004490 locations->SetInAt(0, Location::RequiresRegister());
4491 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004492}
4493
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004494void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4495 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004496 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4497 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004498 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004499 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004500 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004502 break;
4503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004504 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004505 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004506 break;
4507
4508 default:
4509 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4510 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004511}
4512
David Brazdil66d126e2015-04-03 16:02:44 +01004513void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4514 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004515 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004516 locations->SetInAt(0, Location::RequiresRegister());
4517 locations->SetOut(Location::SameAsFirstInput());
4518}
4519
4520void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004521 LocationSummary* locations = bool_not->GetLocations();
4522 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4523 locations->Out().AsRegister<CpuRegister>().AsRegister());
4524 Location out = locations->Out();
4525 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4526}
4527
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004528void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004529 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004530 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004531 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004532 locations->SetInAt(i, Location::Any());
4533 }
4534 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004535}
4536
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004537void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004538 LOG(FATAL) << "Unimplemented";
4539}
4540
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004541void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004542 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004543 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004544 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004545 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4546 */
4547 switch (kind) {
4548 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004549 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004550 break;
4551 }
4552 case MemBarrierKind::kAnyStore:
4553 case MemBarrierKind::kLoadAny:
4554 case MemBarrierKind::kStoreStore: {
4555 // nop
4556 break;
4557 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004558 case MemBarrierKind::kNTStoreStore:
4559 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004560 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004561 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004562 }
4563}
4564
4565void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4566 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4567
Roland Levillain0d5a2812015-11-13 10:07:31 +00004568 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004570 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004571 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4572 object_field_get_with_read_barrier
4573 ? LocationSummary::kCallOnSlowPath
4574 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004575 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004576 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004577 }
Calin Juravle52c48962014-12-16 17:02:57 +00004578 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004579 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004580 locations->SetOut(Location::RequiresFpuRegister());
4581 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004582 // The output overlaps for an object field get when read barriers
4583 // are enabled: we do not want the move to overwrite the object's
4584 // location, as we need it to emit the read barrier.
4585 locations->SetOut(
4586 Location::RequiresRegister(),
4587 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004588 }
Calin Juravle52c48962014-12-16 17:02:57 +00004589}
4590
4591void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4592 const FieldInfo& field_info) {
4593 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4594
4595 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004596 Location base_loc = locations->InAt(0);
4597 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004598 Location out = locations->Out();
4599 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004600 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4601 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004602 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4603
Vladimir Marko61b92282017-10-11 13:23:17 +01004604 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004605 case DataType::Type::kBool:
4606 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004607 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4608 break;
4609 }
4610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004611 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004612 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4613 break;
4614 }
4615
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004616 case DataType::Type::kUint16: {
4617 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004618 break;
4619 }
4620
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004621 case DataType::Type::kInt16: {
4622 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004623 break;
4624 }
4625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004626 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004627 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4628 break;
4629 }
4630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004632 // /* HeapReference<Object> */ out = *(base + offset)
4633 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004634 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004635 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004636 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08004637 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004638 if (is_volatile) {
4639 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4640 }
4641 } else {
4642 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4643 codegen_->MaybeRecordImplicitNullCheck(instruction);
4644 if (is_volatile) {
4645 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4646 }
4647 // If read barriers are enabled, emit read barriers other than
4648 // Baker's using a slow path (and also unpoison the loaded
4649 // reference, if heap poisoning is enabled).
4650 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4651 }
4652 break;
4653 }
4654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004655 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4657 break;
4658 }
4659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004660 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004661 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4662 break;
4663 }
4664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004665 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004666 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4667 break;
4668 }
4669
Aart Bik66c158e2018-01-31 12:55:04 -08004670 case DataType::Type::kUint32:
4671 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004673 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004674 UNREACHABLE();
4675 }
4676
Vladimir Marko61b92282017-10-11 13:23:17 +01004677 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004678 // Potential implicit null checks, in the case of reference
4679 // fields, are handled in the previous switch statement.
4680 } else {
4681 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004682 }
Roland Levillain4d027112015-07-01 15:41:14 +01004683
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004684 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004685 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004686 // Memory barriers, in the case of references, are also handled
4687 // in the previous switch statement.
4688 } else {
4689 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4690 }
Roland Levillain4d027112015-07-01 15:41:14 +01004691 }
Calin Juravle52c48962014-12-16 17:02:57 +00004692}
4693
4694void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4695 const FieldInfo& field_info) {
4696 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4697
4698 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004699 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004700 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004701 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004702 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004703 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004704
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004705 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004706 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004707 if (is_volatile) {
4708 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4709 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4710 } else {
4711 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4712 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004713 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004714 if (is_volatile) {
4715 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4716 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4717 } else {
4718 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4719 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004720 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004721 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004722 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004723 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004724 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004726 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004727 locations->AddTemp(Location::RequiresRegister());
4728 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004729}
4730
Calin Juravle52c48962014-12-16 17:02:57 +00004731void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004732 const FieldInfo& field_info,
4733 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004734 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4735
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004736 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004737 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4738 Location value = locations->InAt(1);
4739 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004741 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4742
4743 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004745 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004746
Mark Mendellea5af682015-10-22 17:35:49 -04004747 bool maybe_record_implicit_null_check_done = false;
4748
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004750 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004751 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004752 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004753 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004754 __ movb(Address(base, offset),
4755 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004756 } else {
4757 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4758 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004759 break;
4760 }
4761
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004762 case DataType::Type::kUint16:
4763 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004764 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004765 __ movw(Address(base, offset),
4766 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004767 } else {
4768 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4769 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004770 break;
4771 }
4772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 case DataType::Type::kInt32:
4774 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004775 if (value.IsConstant()) {
4776 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 // `field_type == DataType::Type::kReference` implies `v == 0`.
4778 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004779 // Note: if heap poisoning is enabled, no need to poison
4780 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004781 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004782 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004784 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4785 __ movl(temp, value.AsRegister<CpuRegister>());
4786 __ PoisonHeapReference(temp);
4787 __ movl(Address(base, offset), temp);
4788 } else {
4789 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4790 }
Mark Mendell40741f32015-04-20 22:10:34 -04004791 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004792 break;
4793 }
4794
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004796 if (value.IsConstant()) {
4797 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004798 codegen_->MoveInt64ToAddress(Address(base, offset),
4799 Address(base, offset + sizeof(int32_t)),
4800 v,
4801 instruction);
4802 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004803 } else {
4804 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4805 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004806 break;
4807 }
4808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004809 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004810 if (value.IsConstant()) {
4811 int32_t v =
4812 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4813 __ movl(Address(base, offset), Immediate(v));
4814 } else {
4815 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4816 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004817 break;
4818 }
4819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004821 if (value.IsConstant()) {
4822 int64_t v =
4823 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4824 codegen_->MoveInt64ToAddress(Address(base, offset),
4825 Address(base, offset + sizeof(int32_t)),
4826 v,
4827 instruction);
4828 maybe_record_implicit_null_check_done = true;
4829 } else {
4830 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4831 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004832 break;
4833 }
4834
Aart Bik66c158e2018-01-31 12:55:04 -08004835 case DataType::Type::kUint32:
4836 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004837 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004838 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004839 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840 }
Calin Juravle52c48962014-12-16 17:02:57 +00004841
Mark Mendellea5af682015-10-22 17:35:49 -04004842 if (!maybe_record_implicit_null_check_done) {
4843 codegen_->MaybeRecordImplicitNullCheck(instruction);
4844 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004845
4846 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4847 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4848 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004849 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 }
4851
Calin Juravle52c48962014-12-16 17:02:57 +00004852 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004853 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004854 }
4855}
4856
4857void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4858 HandleFieldSet(instruction, instruction->GetFieldInfo());
4859}
4860
4861void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004862 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004863}
4864
4865void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004866 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004867}
4868
4869void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004870 HandleFieldGet(instruction, instruction->GetFieldInfo());
4871}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004872
Calin Juravle52c48962014-12-16 17:02:57 +00004873void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4874 HandleFieldGet(instruction);
4875}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004876
Calin Juravle52c48962014-12-16 17:02:57 +00004877void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4878 HandleFieldGet(instruction, instruction->GetFieldInfo());
4879}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004880
Calin Juravle52c48962014-12-16 17:02:57 +00004881void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4882 HandleFieldSet(instruction, instruction->GetFieldInfo());
4883}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004884
Calin Juravle52c48962014-12-16 17:02:57 +00004885void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004886 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004887}
4888
Vladimir Marko552a1342017-10-31 10:56:47 +00004889void LocationsBuilderX86_64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
4890 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(RAX));
4891}
4892
4893void InstructionCodeGeneratorX86_64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
4894 __ movl(CpuRegister(RDI), Immediate(instruction->GetFormat()->GetValue()));
4895 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
4896}
4897
Calin Juravlee460d1d2015-09-29 04:52:17 +01004898void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4899 HUnresolvedInstanceFieldGet* instruction) {
4900 FieldAccessCallingConventionX86_64 calling_convention;
4901 codegen_->CreateUnresolvedFieldLocationSummary(
4902 instruction, instruction->GetFieldType(), calling_convention);
4903}
4904
4905void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4906 HUnresolvedInstanceFieldGet* instruction) {
4907 FieldAccessCallingConventionX86_64 calling_convention;
4908 codegen_->GenerateUnresolvedFieldAccess(instruction,
4909 instruction->GetFieldType(),
4910 instruction->GetFieldIndex(),
4911 instruction->GetDexPc(),
4912 calling_convention);
4913}
4914
4915void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4916 HUnresolvedInstanceFieldSet* instruction) {
4917 FieldAccessCallingConventionX86_64 calling_convention;
4918 codegen_->CreateUnresolvedFieldLocationSummary(
4919 instruction, instruction->GetFieldType(), calling_convention);
4920}
4921
4922void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4923 HUnresolvedInstanceFieldSet* instruction) {
4924 FieldAccessCallingConventionX86_64 calling_convention;
4925 codegen_->GenerateUnresolvedFieldAccess(instruction,
4926 instruction->GetFieldType(),
4927 instruction->GetFieldIndex(),
4928 instruction->GetDexPc(),
4929 calling_convention);
4930}
4931
4932void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4933 HUnresolvedStaticFieldGet* instruction) {
4934 FieldAccessCallingConventionX86_64 calling_convention;
4935 codegen_->CreateUnresolvedFieldLocationSummary(
4936 instruction, instruction->GetFieldType(), calling_convention);
4937}
4938
4939void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4940 HUnresolvedStaticFieldGet* instruction) {
4941 FieldAccessCallingConventionX86_64 calling_convention;
4942 codegen_->GenerateUnresolvedFieldAccess(instruction,
4943 instruction->GetFieldType(),
4944 instruction->GetFieldIndex(),
4945 instruction->GetDexPc(),
4946 calling_convention);
4947}
4948
4949void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4950 HUnresolvedStaticFieldSet* instruction) {
4951 FieldAccessCallingConventionX86_64 calling_convention;
4952 codegen_->CreateUnresolvedFieldLocationSummary(
4953 instruction, instruction->GetFieldType(), calling_convention);
4954}
4955
4956void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4957 HUnresolvedStaticFieldSet* instruction) {
4958 FieldAccessCallingConventionX86_64 calling_convention;
4959 codegen_->GenerateUnresolvedFieldAccess(instruction,
4960 instruction->GetFieldType(),
4961 instruction->GetFieldIndex(),
4962 instruction->GetDexPc(),
4963 calling_convention);
4964}
4965
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004966void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004967 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4968 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4969 ? Location::RequiresRegister()
4970 : Location::Any();
4971 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004972}
4973
Calin Juravle2ae48182016-03-16 14:05:09 +00004974void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4975 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004976 return;
4977 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004978 LocationSummary* locations = instruction->GetLocations();
4979 Location obj = locations->InAt(0);
4980
4981 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004982 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004983}
4984
Calin Juravle2ae48182016-03-16 14:05:09 +00004985void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004986 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004987 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
4989 LocationSummary* locations = instruction->GetLocations();
4990 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004991
4992 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004993 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004994 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004996 } else {
4997 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004998 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004999 __ jmp(slow_path->GetEntryLabel());
5000 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 }
5002 __ j(kEqual, slow_path->GetEntryLabel());
5003}
5004
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005005void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005006 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005007}
5008
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005010 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005011 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005012 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005013 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5014 object_array_get_with_read_barrier
5015 ? LocationSummary::kCallOnSlowPath
5016 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005017 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005018 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005019 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005020 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005021 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005022 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005023 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5024 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005025 // The output overlaps for an object array get when read barriers
5026 // are enabled: we do not want the move to overwrite the array's
5027 // location, as we need it to emit the read barrier.
5028 locations->SetOut(
5029 Location::RequiresRegister(),
5030 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005031 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032}
5033
5034void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
5035 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005036 Location obj_loc = locations->InAt(0);
5037 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005039 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005040 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005042 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01005043 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005044 case DataType::Type::kBool:
5045 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005046 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005047 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048 break;
5049 }
5050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005051 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005052 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005053 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 break;
5055 }
5056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005057 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005058 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07005059 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5060 // Branch cases into compressed and uncompressed for each index's type.
5061 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5062 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005063 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005064 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005065 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5066 "Expecting 0=compressed, 1=uncompressed");
5067 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005068 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
5069 __ jmp(&done);
5070 __ Bind(&not_compressed);
5071 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5072 __ Bind(&done);
5073 } else {
5074 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5075 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 break;
5077 }
5078
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005079 case DataType::Type::kInt16: {
5080 CpuRegister out = out_loc.AsRegister<CpuRegister>();
5081 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5082 break;
5083 }
5084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005085 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005086 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005087 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 break;
5089 }
5090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005091 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005092 static_assert(
5093 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5094 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005095 // /* HeapReference<Object> */ out =
5096 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5097 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005098 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005099 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005100 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005101 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005102 } else {
5103 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005104 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
5105 codegen_->MaybeRecordImplicitNullCheck(instruction);
5106 // If read barriers are enabled, emit read barriers other than
5107 // Baker's using a slow path (and also unpoison the loaded
5108 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005109 if (index.IsConstant()) {
5110 uint32_t offset =
5111 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005112 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5113 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005114 codegen_->MaybeGenerateReadBarrierSlow(
5115 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5116 }
5117 }
5118 break;
5119 }
5120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005122 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005123 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 break;
5125 }
5126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005128 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005129 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005130 break;
5131 }
5132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005134 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005135 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005136 break;
5137 }
5138
Aart Bik66c158e2018-01-31 12:55:04 -08005139 case DataType::Type::kUint32:
5140 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005141 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005142 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005143 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 }
Roland Levillain4d027112015-07-01 15:41:14 +01005145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005146 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005147 // Potential implicit null checks, in the case of reference
5148 // arrays, are handled in the previous switch statement.
5149 } else {
5150 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005151 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152}
5153
5154void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005155 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005156
5157 bool needs_write_barrier =
5158 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005159 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160
Vladimir Markoca6fff82017-10-03 14:49:14 +01005161 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005162 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005163 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005164
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04005166 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005167 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04005168 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005169 } else {
5170 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5171 }
5172
5173 if (needs_write_barrier) {
5174 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005175 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005176 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005177 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178}
5179
5180void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
5181 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005182 Location array_loc = locations->InAt(0);
5183 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005185 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005187 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005188 bool needs_write_barrier =
5189 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190
5191 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005192 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005193 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005194 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005195 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005196 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005197 if (value.IsRegister()) {
5198 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005200 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005201 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005202 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 break;
5204 }
5205
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005206 case DataType::Type::kUint16:
5207 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005208 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005209 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005210 if (value.IsRegister()) {
5211 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005213 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01005214 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005216 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 break;
5218 }
5219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005220 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005221 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005222 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005223
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005224 if (!value.IsRegister()) {
5225 // Just setting null.
5226 DCHECK(instruction->InputAt(2)->IsNullConstant());
5227 DCHECK(value.IsConstant()) << value;
5228 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005229 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005230 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005231 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005232 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005233 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005234
5235 DCHECK(needs_write_barrier);
5236 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005237 Location temp_loc = locations->GetTemp(0);
5238 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005239
5240 bool can_value_be_null = instruction->GetValueCanBeNull();
5241 NearLabel do_store;
5242 if (can_value_be_null) {
5243 __ testl(register_value, register_value);
5244 __ j(kEqual, &do_store);
5245 }
5246
5247 SlowPathCode* slow_path = nullptr;
5248 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005249 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005250 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005251
5252 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5253 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5254 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005256 // Note that when Baker read barriers are enabled, the type
5257 // checks are performed without read barriers. This is fine,
5258 // even in the case where a class object is in the from-space
5259 // after the flip, as a comparison involving such a type would
5260 // not produce a false positive; it may of course produce a
5261 // false negative, in which case we would take the ArraySet
5262 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005263
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005264 // /* HeapReference<Class> */ temp = array->klass_
5265 __ movl(temp, Address(array, class_offset));
5266 codegen_->MaybeRecordImplicitNullCheck(instruction);
5267 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005268
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005269 // /* HeapReference<Class> */ temp = temp->component_type_
5270 __ movl(temp, Address(temp, component_offset));
5271 // If heap poisoning is enabled, no need to unpoison `temp`
5272 // nor the object reference in `register_value->klass`, as
5273 // we are comparing two poisoned references.
5274 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005275
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005276 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005277 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005278 __ j(kEqual, &do_put);
5279 // If heap poisoning is enabled, the `temp` reference has
5280 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005281 __ MaybeUnpoisonHeapReference(temp);
5282
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005283 // If heap poisoning is enabled, no need to unpoison the
5284 // heap reference loaded below, as it is only used for a
5285 // comparison with null.
5286 __ cmpl(Address(temp, super_offset), Immediate(0));
5287 __ j(kNotEqual, slow_path->GetEntryLabel());
5288 __ Bind(&do_put);
5289 } else {
5290 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005292 }
5293
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005294 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5295 codegen_->MarkGCCard(
5296 temp, card, array, value.AsRegister<CpuRegister>(), /* value_can_be_null= */ false);
5297
5298 if (can_value_be_null) {
5299 DCHECK(do_store.IsLinked());
5300 __ Bind(&do_store);
5301 }
5302
5303 Location source = value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005304 if (kPoisonHeapReferences) {
5305 __ movl(temp, register_value);
5306 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005307 source = temp_loc;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005308 }
5309
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005310 __ movl(address, source.AsRegister<CpuRegister>());
5311
5312 if (can_value_be_null || !needs_type_check) {
5313 codegen_->MaybeRecordImplicitNullCheck(instruction);
5314 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005316 if (slow_path != nullptr) {
5317 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 }
5319
5320 break;
5321 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005325 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 if (value.IsRegister()) {
5327 __ movl(address, value.AsRegister<CpuRegister>());
5328 } else {
5329 DCHECK(value.IsConstant()) << value;
5330 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5331 __ movl(address, Immediate(v));
5332 }
5333 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 break;
5335 }
5336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005339 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 if (value.IsRegister()) {
5341 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005342 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005344 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005345 Address address_high =
5346 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005347 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 }
5349 break;
5350 }
5351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005352 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005354 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005355 if (value.IsFpuRegister()) {
5356 __ movss(address, value.AsFpuRegister<XmmRegister>());
5357 } else {
5358 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005359 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005360 __ movl(address, Immediate(v));
5361 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005362 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005363 break;
5364 }
5365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005367 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005368 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005369 if (value.IsFpuRegister()) {
5370 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5371 codegen_->MaybeRecordImplicitNullCheck(instruction);
5372 } else {
5373 int64_t v =
5374 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005375 Address address_high =
5376 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005377 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5378 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005379 break;
5380 }
5381
Aart Bik66c158e2018-01-31 12:55:04 -08005382 case DataType::Type::kUint32:
5383 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005386 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 }
5388}
5389
5390void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005391 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005392 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005393 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005394 if (!instruction->IsEmittedAtUseSite()) {
5395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5396 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005397}
5398
5399void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005400 if (instruction->IsEmittedAtUseSite()) {
5401 return;
5402 }
5403
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005405 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005406 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5407 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005409 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005410 // Mask out most significant bit in case the array is String's array of char.
5411 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005412 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005413 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414}
5415
5416void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005417 RegisterSet caller_saves = RegisterSet::Empty();
5418 InvokeRuntimeCallingConvention calling_convention;
5419 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5420 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5421 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005422 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005423 HInstruction* length = instruction->InputAt(1);
5424 if (!length->IsEmittedAtUseSite()) {
5425 locations->SetInAt(1, Location::RegisterOrConstant(length));
5426 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005427}
5428
5429void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5430 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005431 Location index_loc = locations->InAt(0);
5432 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005433 SlowPathCode* slow_path =
5434 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435
Mark Mendell99dbd682015-04-22 16:18:52 -04005436 if (length_loc.IsConstant()) {
5437 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5438 if (index_loc.IsConstant()) {
5439 // BCE will remove the bounds check if we are guarenteed to pass.
5440 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5441 if (index < 0 || index >= length) {
5442 codegen_->AddSlowPath(slow_path);
5443 __ jmp(slow_path->GetEntryLabel());
5444 } else {
5445 // Some optimization after BCE may have generated this, and we should not
5446 // generate a bounds check if it is a valid range.
5447 }
5448 return;
5449 }
5450
5451 // We have to reverse the jump condition because the length is the constant.
5452 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5453 __ cmpl(index_reg, Immediate(length));
5454 codegen_->AddSlowPath(slow_path);
5455 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005456 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005457 HInstruction* array_length = instruction->InputAt(1);
5458 if (array_length->IsEmittedAtUseSite()) {
5459 // Address the length field in the array.
5460 DCHECK(array_length->IsArrayLength());
5461 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5462 Location array_loc = array_length->GetLocations()->InAt(0);
5463 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005464 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005465 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5466 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005467 CpuRegister length_reg = CpuRegister(TMP);
5468 __ movl(length_reg, array_len);
5469 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005470 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005471 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005472 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005473 // Checking the bound for general case:
5474 // Array of char or String's array when the compression feature off.
5475 if (index_loc.IsConstant()) {
5476 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5477 __ cmpl(array_len, Immediate(value));
5478 } else {
5479 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5480 }
5481 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005482 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005483 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005484 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005485 }
5486 codegen_->AddSlowPath(slow_path);
5487 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005488 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005489}
5490
5491void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5492 CpuRegister card,
5493 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005494 CpuRegister value,
5495 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005496 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005497 if (value_can_be_null) {
5498 __ testl(value, value);
5499 __ j(kEqual, &is_null);
5500 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005501 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005502 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005503 /* no_rip= */ true));
Roland Levillainc73f0522018-08-14 15:16:50 +01005504 // Calculate the offset (in the card table) of the card corresponding to
5505 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005506 __ movq(temp, object);
5507 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005508 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5509 // `object`'s card.
5510 //
5511 // Register `card` contains the address of the card table. Note that the card
5512 // table's base is biased during its creation so that it always starts at an
5513 // address whose least-significant byte is equal to `kCardDirty` (see
5514 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5515 // below writes the `kCardDirty` (byte) value into the `object`'s card
5516 // (located at `card + object >> kCardShift`).
5517 //
5518 // This dual use of the value in register `card` (1. to calculate the location
5519 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5520 // (no need to explicitly load `kCardDirty` as an immediate value).
Roland Levillain4d027112015-07-01 15:41:14 +01005521 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005522 if (value_can_be_null) {
5523 __ Bind(&is_null);
5524 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005525}
5526
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005527void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005528 LOG(FATAL) << "Unimplemented";
5529}
5530
5531void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005532 if (instruction->GetNext()->IsSuspendCheck() &&
5533 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5534 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5535 // The back edge will generate the suspend check.
5536 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5537 }
5538
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005539 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5540}
5541
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005542void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005543 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5544 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005545 // In suspend check slow path, usually there are no caller-save registers at all.
5546 // If SIMD instructions are present, however, we force spilling all live SIMD
5547 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005548 locations->SetCustomSlowPathCallerSaves(
5549 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005550}
5551
5552void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005553 HBasicBlock* block = instruction->GetBlock();
5554 if (block->GetLoopInformation() != nullptr) {
5555 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5556 // The back edge will generate the suspend check.
5557 return;
5558 }
5559 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5560 // The goto will generate the suspend check.
5561 return;
5562 }
5563 GenerateSuspendCheck(instruction, nullptr);
5564}
5565
5566void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5567 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005568 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005569 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5570 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005571 slow_path =
5572 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005573 instruction->SetSlowPath(slow_path);
5574 codegen_->AddSlowPath(slow_path);
5575 if (successor != nullptr) {
5576 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005577 }
5578 } else {
5579 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5580 }
5581
Andreas Gampe542451c2016-07-26 09:02:02 -07005582 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005583 /* no_rip= */ true),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005584 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005585 if (successor == nullptr) {
5586 __ j(kNotEqual, slow_path->GetEntryLabel());
5587 __ Bind(slow_path->GetReturnLabel());
5588 } else {
5589 __ j(kEqual, codegen_->GetLabelOf(successor));
5590 __ jmp(slow_path->GetEntryLabel());
5591 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005592}
5593
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005594X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5595 return codegen_->GetAssembler();
5596}
5597
5598void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005599 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005600 Location source = move->GetSource();
5601 Location destination = move->GetDestination();
5602
5603 if (source.IsRegister()) {
5604 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005605 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005606 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005607 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005608 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005609 } else {
5610 DCHECK(destination.IsDoubleStackSlot());
5611 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005613 }
5614 } else if (source.IsStackSlot()) {
5615 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005616 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005617 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005618 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005619 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005620 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005621 } else {
5622 DCHECK(destination.IsStackSlot());
5623 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5624 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5625 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005626 } else if (source.IsDoubleStackSlot()) {
5627 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005628 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005629 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005630 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005631 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5632 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005633 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005634 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005635 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5636 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5637 }
Aart Bik5576f372017-03-23 16:17:37 -07005638 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005639 if (destination.IsFpuRegister()) {
5640 __ movups(destination.AsFpuRegister<XmmRegister>(),
5641 Address(CpuRegister(RSP), source.GetStackIndex()));
5642 } else {
5643 DCHECK(destination.IsSIMDStackSlot());
5644 size_t high = kX86_64WordSize;
5645 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5646 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5647 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5648 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5649 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005650 } else if (source.IsConstant()) {
5651 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005652 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5653 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005654 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005655 if (value == 0) {
5656 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5657 } else {
5658 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5659 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005660 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005661 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005662 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005663 }
5664 } else if (constant->IsLongConstant()) {
5665 int64_t value = constant->AsLongConstant()->GetValue();
5666 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005667 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005668 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005669 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005670 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005671 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005672 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005673 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005674 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005675 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005676 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005677 } else {
5678 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005679 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005680 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5681 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005682 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005683 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005684 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005685 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005686 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005687 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005688 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005689 } else {
5690 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005691 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005692 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005693 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005694 } else if (source.IsFpuRegister()) {
5695 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005696 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005697 } else if (destination.IsStackSlot()) {
5698 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005699 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005700 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005701 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005702 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005703 } else {
5704 DCHECK(destination.IsSIMDStackSlot());
5705 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5706 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005707 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005708 }
5709}
5710
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005711void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005712 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005713 __ movl(Address(CpuRegister(RSP), mem), reg);
5714 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005715}
5716
Mark Mendell8a1c7282015-06-29 15:41:28 -04005717void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5718 __ movq(CpuRegister(TMP), reg1);
5719 __ movq(reg1, reg2);
5720 __ movq(reg2, CpuRegister(TMP));
5721}
5722
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005723void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5724 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5725 __ movq(Address(CpuRegister(RSP), mem), reg);
5726 __ movq(reg, CpuRegister(TMP));
5727}
5728
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005729void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5730 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5731 __ movss(Address(CpuRegister(RSP), mem), reg);
5732 __ movd(reg, CpuRegister(TMP));
5733}
5734
5735void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5736 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5737 __ movsd(Address(CpuRegister(RSP), mem), reg);
5738 __ movd(reg, CpuRegister(TMP));
5739}
5740
Aart Bikcfe50bb2017-12-12 14:54:12 -08005741void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5742 size_t extra_slot = 2 * kX86_64WordSize;
5743 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5744 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5745 ExchangeMemory64(0, mem + extra_slot, 2);
5746 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5747 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5748}
5749
5750void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5751 ScratchRegisterScope ensure_scratch(
5752 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5753
5754 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5755 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5756 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5757 Address(CpuRegister(RSP), mem2 + stack_offset));
5758 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5759 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5760 CpuRegister(ensure_scratch.GetRegister()));
5761}
5762
5763void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5764 ScratchRegisterScope ensure_scratch(
5765 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5766
5767 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5768
5769 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5770 for (int i = 0; i < num_of_qwords; i++) {
5771 __ movq(CpuRegister(TMP),
5772 Address(CpuRegister(RSP), mem1 + stack_offset));
5773 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5774 Address(CpuRegister(RSP), mem2 + stack_offset));
5775 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5776 CpuRegister(TMP));
5777 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5778 CpuRegister(ensure_scratch.GetRegister()));
5779 stack_offset += kX86_64WordSize;
5780 }
5781}
5782
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005783void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005784 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005785 Location source = move->GetSource();
5786 Location destination = move->GetDestination();
5787
5788 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005789 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005790 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005791 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005792 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005793 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005794 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005795 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005796 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005797 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005798 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005799 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005800 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005801 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005802 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005803 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5804 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5805 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005806 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005807 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005808 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005809 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005810 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005811 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005812 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005813 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005814 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5815 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5816 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5817 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5818 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5819 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005820 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005821 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005822 }
5823}
5824
5825
5826void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5827 __ pushq(CpuRegister(reg));
5828}
5829
5830
5831void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5832 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005833}
5834
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005835void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005836 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005837 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5838 const size_t status_byte_offset =
5839 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5840 constexpr uint32_t shifted_initialized_value =
5841 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5842
5843 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005844 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005845 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005846 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005847}
5848
Vladimir Marko175e7862018-03-27 09:03:13 +00005849void InstructionCodeGeneratorX86_64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
5850 CpuRegister temp) {
5851 uint32_t path_to_root = check->GetBitstringPathToRoot();
5852 uint32_t mask = check->GetBitstringMask();
5853 DCHECK(IsPowerOfTwo(mask + 1));
5854 size_t mask_bits = WhichPowerOf2(mask + 1);
5855
5856 if (mask_bits == 16u) {
5857 // Compare the bitstring in memory.
5858 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
5859 } else {
5860 // /* uint32_t */ temp = temp->status_
5861 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
5862 // Compare the bitstring bits using SUB.
5863 __ subl(temp, Immediate(path_to_root));
5864 // Shift out bits that do not contribute to the comparison.
5865 __ shll(temp, Immediate(32u - mask_bits));
5866 }
5867}
5868
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005869HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5870 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005871 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005872 case HLoadClass::LoadKind::kInvalid:
5873 LOG(FATAL) << "UNREACHABLE";
5874 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005875 case HLoadClass::LoadKind::kReferrersClass:
5876 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005877 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005878 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 case HLoadClass::LoadKind::kBssEntry:
5880 DCHECK(!Runtime::Current()->UseJitCompilation());
5881 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005882 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005883 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005884 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005885 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005886 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005887 break;
5888 }
5889 return desired_class_load_kind;
5890}
5891
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005892void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005893 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005894 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005895 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005896 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005897 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005898 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005899 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005900 return;
5901 }
Vladimir Marko41559982017-01-06 14:04:23 +00005902 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005903
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005904 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5905 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 ? LocationSummary::kCallOnSlowPath
5907 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005908 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005909 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005910 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005911 }
5912
Vladimir Marko41559982017-01-06 14:04:23 +00005913 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005914 locations->SetInAt(0, Location::RequiresRegister());
5915 }
5916 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005917 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5918 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5919 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005920 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005921 } else {
5922 // For non-Baker read barrier we have a temp-clobbering call.
5923 }
5924 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005925}
5926
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005927Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005928 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005929 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005930 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005931 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005932 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005933 PatchInfo<Label>* info = &jit_class_patches_.back();
5934 return &info->label;
5935}
5936
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005937// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5938// move.
5939void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005940 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005941 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005942 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005943 return;
5944 }
Vladimir Marko41559982017-01-06 14:04:23 +00005945 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005946
Vladimir Marko41559982017-01-06 14:04:23 +00005947 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005948 Location out_loc = locations->Out();
5949 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005951 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5952 ? kWithoutReadBarrier
5953 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005954 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005955 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005956 case HLoadClass::LoadKind::kReferrersClass: {
5957 DCHECK(!cls->CanCallRuntime());
5958 DCHECK(!cls->MustGenerateClinitCheck());
5959 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5960 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5961 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005962 cls,
5963 out_loc,
5964 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08005965 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005966 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005967 break;
5968 }
5969 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005970 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005971 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Andreas Gampe3db70682018-12-26 15:12:03 -08005972 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005973 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005974 break;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005975 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005976 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Andreas Gampe3db70682018-12-26 15:12:03 -08005977 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip= */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005978 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005979 break;
5980 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005981 case HLoadClass::LoadKind::kBssEntry: {
5982 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08005983 /* no_rip= */ false);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005984 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5985 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5986 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005987 // No need for memory fence, thanks to the x86-64 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005988 generate_null_check = true;
5989 break;
5990 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005991 case HLoadClass::LoadKind::kJitBootImageAddress: {
5992 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5993 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5994 DCHECK_NE(address, 0u);
5995 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
5996 break;
5997 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005998 case HLoadClass::LoadKind::kJitTableAddress: {
5999 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006000 /* no_rip= */ true);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006001 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006002 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006003 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006004 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006005 break;
6006 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006007 default:
6008 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
6009 UNREACHABLE();
6010 }
6011
6012 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6013 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006014 SlowPathCode* slow_path =
6015 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006016 codegen_->AddSlowPath(slow_path);
6017 if (generate_null_check) {
6018 __ testl(out, out);
6019 __ j(kEqual, slow_path->GetEntryLabel());
6020 }
6021 if (cls->MustGenerateClinitCheck()) {
6022 GenerateClassInitializationCheck(slow_path, out);
6023 } else {
6024 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006025 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006026 }
6027}
6028
6029void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
6030 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006031 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006032 locations->SetInAt(0, Location::RequiresRegister());
6033 if (check->HasUses()) {
6034 locations->SetOut(Location::SameAsFirstInput());
6035 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006036 // Rely on the type initialization to save everything we need.
6037 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038}
6039
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006040void LocationsBuilderX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6041 // Custom calling convention: RAX serves as both input and output.
6042 Location location = Location::RegisterLocation(RAX);
6043 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6044}
6045
6046void InstructionCodeGeneratorX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6047 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6048}
6049
Orion Hodson18259d72018-04-12 11:18:23 +01006050void LocationsBuilderX86_64::VisitLoadMethodType(HLoadMethodType* load) {
6051 // Custom calling convention: RAX serves as both input and output.
6052 Location location = Location::RegisterLocation(RAX);
6053 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6054}
6055
6056void InstructionCodeGeneratorX86_64::VisitLoadMethodType(HLoadMethodType* load) {
6057 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6058}
6059
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006060void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006061 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006062 SlowPathCode* slow_path =
6063 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006065 GenerateClassInitializationCheck(slow_path,
6066 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067}
6068
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006069HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
6070 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006071 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006072 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006073 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006074 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006075 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006076 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006077 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006078 case HLoadString::LoadKind::kJitTableAddress:
6079 DCHECK(Runtime::Current()->UseJitCompilation());
6080 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006081 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006082 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006083 }
6084 return desired_string_load_kind;
6085}
6086
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006087void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006088 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006089 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006090 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006091 locations->SetOut(Location::RegisterLocation(RAX));
6092 } else {
6093 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006094 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
6095 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006096 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006097 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006098 } else {
6099 // For non-Baker read barrier we have a temp-clobbering call.
6100 }
6101 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006102 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006103}
6104
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006105Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006106 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006107 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006108 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006109 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006110 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006111 PatchInfo<Label>* info = &jit_string_patches_.back();
6112 return &info->label;
6113}
6114
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006115// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6116// move.
6117void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006118 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119 Location out_loc = locations->Out();
6120 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006122 switch (load->GetLoadKind()) {
6123 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006124 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Andreas Gampe3db70682018-12-26 15:12:03 -08006125 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006126 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006127 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006128 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006129 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006130 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Andreas Gampe3db70682018-12-26 15:12:03 -08006131 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip= */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006132 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006133 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006134 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006135 case HLoadString::LoadKind::kBssEntry: {
6136 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006137 /* no_rip= */ false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006138 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6139 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006140 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006141 // No need for memory fence, thanks to the x86-64 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006142 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006143 codegen_->AddSlowPath(slow_path);
6144 __ testl(out, out);
6145 __ j(kEqual, slow_path->GetEntryLabel());
6146 __ Bind(slow_path->GetExitLabel());
6147 return;
6148 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006149 case HLoadString::LoadKind::kJitBootImageAddress: {
6150 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6151 DCHECK_NE(address, 0u);
6152 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
6153 return;
6154 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006155 case HLoadString::LoadKind::kJitTableAddress: {
6156 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006157 /* no_rip= */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006158 Label* fixup_label = codegen_->NewJitRootStringPatch(
6159 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006160 // /* GcRoot<mirror::String> */ out = *address
6161 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6162 return;
6163 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006164 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006165 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006166 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006168 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006169 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006170 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006171 codegen_->InvokeRuntime(kQuickResolveString,
6172 load,
6173 load->GetDexPc());
6174 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006175}
6176
David Brazdilcb1c0552015-08-04 16:22:25 +01006177static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006178 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08006179 /* no_rip= */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01006180}
6181
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006182void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
6183 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006184 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006185 locations->SetOut(Location::RequiresRegister());
6186}
6187
6188void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006189 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
6190}
6191
6192void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006193 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006194}
6195
6196void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6197 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006198}
6199
6200void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006201 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6202 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006203 InvokeRuntimeCallingConvention calling_convention;
6204 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6205}
6206
6207void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006208 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006209 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006210}
6211
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006212// Temp is used for read barrier.
6213static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6214 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006215 !kUseBakerReadBarrier &&
6216 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006217 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006218 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6219 return 1;
6220 }
6221 return 0;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006222}
6223
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006224// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6225// interface pointer, the current interface is compared in memory.
6226// The other checks have one temp for loading the object's class.
6227static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6228 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6229 return 2;
6230 }
6231 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006232}
6233
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006234void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006235 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006237 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006238 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 case TypeCheckKind::kExactCheck:
6240 case TypeCheckKind::kAbstractClassCheck:
6241 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006242 case TypeCheckKind::kArrayObjectCheck: {
6243 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6244 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6245 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006247 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006248 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006249 case TypeCheckKind::kUnresolvedCheck:
6250 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006251 call_kind = LocationSummary::kCallOnSlowPath;
6252 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006253 case TypeCheckKind::kBitstringCheck:
6254 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006256
Vladimir Markoca6fff82017-10-03 14:49:14 +01006257 LocationSummary* locations =
6258 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006259 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006260 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006261 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006263 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6264 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6265 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6266 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6267 } else {
6268 locations->SetInAt(1, Location::Any());
6269 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006270 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
6271 locations->SetOut(Location::RequiresRegister());
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006272 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006273}
6274
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006275void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006276 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006277 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 Location obj_loc = locations->InAt(0);
6279 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006280 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 Location out_loc = locations->Out();
6282 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006283 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6284 DCHECK_LE(num_temps, 1u);
6285 Location maybe_temp_loc = (num_temps >= 1u) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006286 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6288 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6289 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006290 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006292
6293 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006294 // Avoid null check if we know obj is not null.
6295 if (instruction->MustDoNullCheck()) {
6296 __ testl(obj, obj);
6297 __ j(kEqual, &zero);
6298 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006300 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006302 ReadBarrierOption read_barrier_option =
6303 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006304 // /* HeapReference<Class> */ out = obj->klass_
6305 GenerateReferenceLoadTwoRegisters(instruction,
6306 out_loc,
6307 obj_loc,
6308 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006309 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 if (cls.IsRegister()) {
6311 __ cmpl(out, cls.AsRegister<CpuRegister>());
6312 } else {
6313 DCHECK(cls.IsStackSlot()) << cls;
6314 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6315 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006316 if (zero.IsLinked()) {
6317 // Classes must be equal for the instanceof to succeed.
6318 __ j(kNotEqual, &zero);
6319 __ movl(out, Immediate(1));
6320 __ jmp(&done);
6321 } else {
6322 __ setcc(kEqual, out);
6323 // setcc only sets the low byte.
6324 __ andl(out, Immediate(1));
6325 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 break;
6327 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006329 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006330 ReadBarrierOption read_barrier_option =
6331 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006332 // /* HeapReference<Class> */ out = obj->klass_
6333 GenerateReferenceLoadTwoRegisters(instruction,
6334 out_loc,
6335 obj_loc,
6336 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006337 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 // If the class is abstract, we eagerly fetch the super class of the
6339 // object to avoid doing a comparison we know will fail.
6340 NearLabel loop, success;
6341 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006342 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006343 GenerateReferenceLoadOneRegister(instruction,
6344 out_loc,
6345 super_offset,
6346 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006347 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006348 __ testl(out, out);
6349 // If `out` is null, we use it for the result, and jump to `done`.
6350 __ j(kEqual, &done);
6351 if (cls.IsRegister()) {
6352 __ cmpl(out, cls.AsRegister<CpuRegister>());
6353 } else {
6354 DCHECK(cls.IsStackSlot()) << cls;
6355 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6356 }
6357 __ j(kNotEqual, &loop);
6358 __ movl(out, Immediate(1));
6359 if (zero.IsLinked()) {
6360 __ jmp(&done);
6361 }
6362 break;
6363 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006365 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006366 ReadBarrierOption read_barrier_option =
6367 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006368 // /* HeapReference<Class> */ out = obj->klass_
6369 GenerateReferenceLoadTwoRegisters(instruction,
6370 out_loc,
6371 obj_loc,
6372 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006373 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006374 // Walk over the class hierarchy to find a match.
6375 NearLabel loop, success;
6376 __ Bind(&loop);
6377 if (cls.IsRegister()) {
6378 __ cmpl(out, cls.AsRegister<CpuRegister>());
6379 } else {
6380 DCHECK(cls.IsStackSlot()) << cls;
6381 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6382 }
6383 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006385 GenerateReferenceLoadOneRegister(instruction,
6386 out_loc,
6387 super_offset,
6388 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006389 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390 __ testl(out, out);
6391 __ j(kNotEqual, &loop);
6392 // If `out` is null, we use it for the result, and jump to `done`.
6393 __ jmp(&done);
6394 __ Bind(&success);
6395 __ movl(out, Immediate(1));
6396 if (zero.IsLinked()) {
6397 __ jmp(&done);
6398 }
6399 break;
6400 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006403 ReadBarrierOption read_barrier_option =
6404 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006405 // /* HeapReference<Class> */ out = obj->klass_
6406 GenerateReferenceLoadTwoRegisters(instruction,
6407 out_loc,
6408 obj_loc,
6409 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006410 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006411 // Do an exact check.
6412 NearLabel exact_check;
6413 if (cls.IsRegister()) {
6414 __ cmpl(out, cls.AsRegister<CpuRegister>());
6415 } else {
6416 DCHECK(cls.IsStackSlot()) << cls;
6417 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6418 }
6419 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006422 GenerateReferenceLoadOneRegister(instruction,
6423 out_loc,
6424 component_offset,
6425 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006426 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 __ testl(out, out);
6428 // If `out` is null, we use it for the result, and jump to `done`.
6429 __ j(kEqual, &done);
6430 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6431 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006432 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 __ movl(out, Immediate(1));
6434 __ jmp(&done);
6435 break;
6436 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006437
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006439 // No read barrier since the slow path will retry upon failure.
6440 // /* HeapReference<Class> */ out = obj->klass_
6441 GenerateReferenceLoadTwoRegisters(instruction,
6442 out_loc,
6443 obj_loc,
6444 class_offset,
6445 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006446 if (cls.IsRegister()) {
6447 __ cmpl(out, cls.AsRegister<CpuRegister>());
6448 } else {
6449 DCHECK(cls.IsStackSlot()) << cls;
6450 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6451 }
6452 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006453 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08006454 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 codegen_->AddSlowPath(slow_path);
6456 __ j(kNotEqual, slow_path->GetEntryLabel());
6457 __ movl(out, Immediate(1));
6458 if (zero.IsLinked()) {
6459 __ jmp(&done);
6460 }
6461 break;
6462 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463
Calin Juravle98893e12015-10-02 21:05:03 +01006464 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006465 case TypeCheckKind::kInterfaceCheck: {
6466 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006467 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468 // cases.
6469 //
6470 // We cannot directly call the InstanceofNonTrivial runtime
6471 // entry point without resorting to a type checking slow path
6472 // here (i.e. by calling InvokeRuntime directly), as it would
6473 // require to assign fixed registers for the inputs of this
6474 // HInstanceOf instruction (following the runtime calling
6475 // convention), which might be cluttered by the potential first
6476 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006477 //
6478 // TODO: Introduce a new runtime entry point taking the object
6479 // to test (instead of its class) as argument, and let it deal
6480 // with the read barrier issues. This will let us refactor this
6481 // case of the `switch` code as it was previously (with a direct
6482 // call to the runtime not using a type checking slow path).
6483 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006485 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08006486 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487 codegen_->AddSlowPath(slow_path);
6488 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489 if (zero.IsLinked()) {
6490 __ jmp(&done);
6491 }
6492 break;
6493 }
Vladimir Marko175e7862018-03-27 09:03:13 +00006494
6495 case TypeCheckKind::kBitstringCheck: {
6496 // /* HeapReference<Class> */ temp = obj->klass_
6497 GenerateReferenceLoadTwoRegisters(instruction,
6498 out_loc,
6499 obj_loc,
6500 class_offset,
6501 kWithoutReadBarrier);
6502
6503 GenerateBitstringTypeCheckCompare(instruction, out);
6504 if (zero.IsLinked()) {
6505 __ j(kNotEqual, &zero);
6506 __ movl(out, Immediate(1));
6507 __ jmp(&done);
6508 } else {
6509 __ setcc(kEqual, out);
6510 // setcc only sets the low byte.
6511 __ andl(out, Immediate(1));
6512 }
6513 break;
6514 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006515 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006516
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006517 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006518 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006519 __ xorl(out, out);
6520 }
6521
6522 if (done.IsLinked()) {
6523 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006524 }
6525
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006526 if (slow_path != nullptr) {
6527 __ Bind(slow_path->GetExitLabel());
6528 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006529}
6530
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006531void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006532 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006533 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006534 LocationSummary* locations =
6535 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006537 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6538 // Require a register for the interface check since there is a loop that compares the class to
6539 // a memory address.
6540 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006541 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6542 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6543 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6544 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006545 } else {
6546 locations->SetInAt(1, Location::Any());
6547 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006548 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
6549 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006550}
6551
6552void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006553 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006554 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 Location obj_loc = locations->InAt(0);
6556 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006557 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 Location temp_loc = locations->GetTemp(0);
6559 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006560 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6561 DCHECK_GE(num_temps, 1u);
6562 DCHECK_LE(num_temps, 2u);
6563 Location maybe_temp2_loc = (num_temps >= 2u) ? locations->GetTemp(1) : Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006564 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6565 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6566 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6567 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6568 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6569 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006570 const uint32_t object_array_data_offset =
6571 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572
Vladimir Marko87584542017-12-12 17:47:52 +00006573 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006574 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006575 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6576 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006579
6580 NearLabel done;
6581 // Avoid null check if we know obj is not null.
6582 if (instruction->MustDoNullCheck()) {
6583 __ testl(obj, obj);
6584 __ j(kEqual, &done);
6585 }
6586
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 case TypeCheckKind::kExactCheck:
6589 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006590 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006591 GenerateReferenceLoadTwoRegisters(instruction,
6592 temp_loc,
6593 obj_loc,
6594 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006595 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 if (cls.IsRegister()) {
6597 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6598 } else {
6599 DCHECK(cls.IsStackSlot()) << cls;
6600 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6601 }
6602 // Jump to slow path for throwing the exception or doing a
6603 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 break;
6606 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006609 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006610 GenerateReferenceLoadTwoRegisters(instruction,
6611 temp_loc,
6612 obj_loc,
6613 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006614 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 // If the class is abstract, we eagerly fetch the super class of the
6616 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006617 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006618 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006619 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006620 GenerateReferenceLoadOneRegister(instruction,
6621 temp_loc,
6622 super_offset,
6623 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006624 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006626 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6627 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006628 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006629 // Otherwise, compare the classes.
6630 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006631 if (cls.IsRegister()) {
6632 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6633 } else {
6634 DCHECK(cls.IsStackSlot()) << cls;
6635 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6636 }
6637 __ j(kNotEqual, &loop);
6638 break;
6639 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006641 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006642 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006643 GenerateReferenceLoadTwoRegisters(instruction,
6644 temp_loc,
6645 obj_loc,
6646 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006647 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006648 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006649 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006650 __ Bind(&loop);
6651 if (cls.IsRegister()) {
6652 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6653 } else {
6654 DCHECK(cls.IsStackSlot()) << cls;
6655 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6656 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006657 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006658
Roland Levillain0d5a2812015-11-13 10:07:31 +00006659 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006660 GenerateReferenceLoadOneRegister(instruction,
6661 temp_loc,
6662 super_offset,
6663 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006664 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006665
6666 // If the class reference currently in `temp` is not null, jump
6667 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006669 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006671 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006672 break;
6673 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006675 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006676 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006677 GenerateReferenceLoadTwoRegisters(instruction,
6678 temp_loc,
6679 obj_loc,
6680 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006681 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006682 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006684 if (cls.IsRegister()) {
6685 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6686 } else {
6687 DCHECK(cls.IsStackSlot()) << cls;
6688 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6689 }
6690 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006691
6692 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006693 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006694 GenerateReferenceLoadOneRegister(instruction,
6695 temp_loc,
6696 component_offset,
6697 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006698 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699
6700 // If the component type is not null (i.e. the object is indeed
6701 // an array), jump to label `check_non_primitive_component_type`
6702 // to further check that this component type is not a primitive
6703 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006704 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006706 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006707 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006708 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709 break;
6710 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006712 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006713 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714 //
6715 // We cannot directly call the CheckCast runtime entry point
6716 // without resorting to a type checking slow path here (i.e. by
6717 // calling InvokeRuntime directly), as it would require to
6718 // assign fixed registers for the inputs of this HInstanceOf
6719 // instruction (following the runtime calling convention), which
6720 // might be cluttered by the potential first read barrier
6721 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006722 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006723 break;
6724 }
6725
Vladimir Marko175e7862018-03-27 09:03:13 +00006726 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006727 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6728 // We can not get false positives by doing this.
6729 // /* HeapReference<Class> */ temp = obj->klass_
6730 GenerateReferenceLoadTwoRegisters(instruction,
6731 temp_loc,
6732 obj_loc,
6733 class_offset,
6734 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006735
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006736 // /* HeapReference<Class> */ temp = temp->iftable_
6737 GenerateReferenceLoadTwoRegisters(instruction,
6738 temp_loc,
6739 temp_loc,
6740 iftable_offset,
6741 kWithoutReadBarrier);
6742 // Iftable is never null.
6743 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6744 // Maybe poison the `cls` for direct comparison with memory.
6745 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6746 // Loop through the iftable and check if any class matches.
6747 NearLabel start_loop;
6748 __ Bind(&start_loop);
6749 // Need to subtract first to handle the empty array case.
6750 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6751 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6752 // Go to next interface if the classes do not match.
6753 __ cmpl(cls.AsRegister<CpuRegister>(),
6754 CodeGeneratorX86_64::ArrayAddress(temp,
6755 maybe_temp2_loc,
6756 TIMES_4,
6757 object_array_data_offset));
6758 __ j(kNotEqual, &start_loop); // Return if same class.
6759 // If `cls` was poisoned above, unpoison it.
6760 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006762 }
6763
6764 case TypeCheckKind::kBitstringCheck: {
6765 // /* HeapReference<Class> */ temp = obj->klass_
6766 GenerateReferenceLoadTwoRegisters(instruction,
6767 temp_loc,
6768 obj_loc,
6769 class_offset,
6770 kWithoutReadBarrier);
6771
6772 GenerateBitstringTypeCheckCompare(instruction, temp);
6773 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
6774 break;
6775 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006776 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006777
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006778 if (done.IsLinked()) {
6779 __ Bind(&done);
6780 }
6781
Roland Levillain0d5a2812015-11-13 10:07:31 +00006782 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006783}
6784
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006785void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006786 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6787 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006788 InvokeRuntimeCallingConvention calling_convention;
6789 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6790}
6791
6792void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006793 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006794 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006795 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006796 if (instruction->IsEnter()) {
6797 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6798 } else {
6799 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6800 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006801}
6802
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05306803void LocationsBuilderX86_64::VisitX86AndNot(HX86AndNot* instruction) {
6804 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
6805 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
6806 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
6807 locations->SetInAt(0, Location::RequiresRegister());
6808 // There is no immediate variant of negated bitwise and in X86.
6809 locations->SetInAt(1, Location::RequiresRegister());
6810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6811}
6812
6813void LocationsBuilderX86_64::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
6814 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
6815 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
6816 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
6817 locations->SetInAt(0, Location::RequiresRegister());
6818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6819}
6820
6821void InstructionCodeGeneratorX86_64::VisitX86AndNot(HX86AndNot* instruction) {
6822 LocationSummary* locations = instruction->GetLocations();
6823 Location first = locations->InAt(0);
6824 Location second = locations->InAt(1);
6825 Location dest = locations->Out();
6826 __ andn(dest.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
6827}
6828
6829void InstructionCodeGeneratorX86_64::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
6830 LocationSummary* locations = instruction->GetLocations();
6831 Location src = locations->InAt(0);
6832 Location dest = locations->Out();
6833 switch (instruction->GetOpKind()) {
6834 case HInstruction::kAnd:
6835 __ blsr(dest.AsRegister<CpuRegister>(), src.AsRegister<CpuRegister>());
6836 break;
6837 case HInstruction::kXor:
6838 __ blsmsk(dest.AsRegister<CpuRegister>(), src.AsRegister<CpuRegister>());
6839 break;
6840 default:
6841 LOG(FATAL) << "Unreachable";
6842 }
6843}
6844
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006845void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6846void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6847void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6848
6849void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6850 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006851 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006852 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6853 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006854 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006855 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006856 locations->SetOut(Location::SameAsFirstInput());
6857}
6858
6859void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6860 HandleBitwiseOperation(instruction);
6861}
6862
6863void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6864 HandleBitwiseOperation(instruction);
6865}
6866
6867void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6868 HandleBitwiseOperation(instruction);
6869}
6870
6871void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6872 LocationSummary* locations = instruction->GetLocations();
6873 Location first = locations->InAt(0);
6874 Location second = locations->InAt(1);
6875 DCHECK(first.Equals(locations->Out()));
6876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006877 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006878 if (second.IsRegister()) {
6879 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006880 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006881 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006882 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006883 } else {
6884 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006885 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006886 }
6887 } else if (second.IsConstant()) {
6888 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6889 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006890 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006891 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006892 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006893 } else {
6894 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006895 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006896 }
6897 } else {
6898 Address address(CpuRegister(RSP), second.GetStackIndex());
6899 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006900 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006901 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006902 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006903 } else {
6904 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006905 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006906 }
6907 }
6908 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006909 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006910 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6911 bool second_is_constant = false;
6912 int64_t value = 0;
6913 if (second.IsConstant()) {
6914 second_is_constant = true;
6915 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006916 }
Mark Mendell40741f32015-04-20 22:10:34 -04006917 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006918
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006919 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006920 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006921 if (is_int32_value) {
6922 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6923 } else {
6924 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6925 }
6926 } else if (second.IsDoubleStackSlot()) {
6927 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006928 } else {
6929 __ andq(first_reg, second.AsRegister<CpuRegister>());
6930 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006931 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006932 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006933 if (is_int32_value) {
6934 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6935 } else {
6936 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6937 }
6938 } else if (second.IsDoubleStackSlot()) {
6939 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006940 } else {
6941 __ orq(first_reg, second.AsRegister<CpuRegister>());
6942 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006943 } else {
6944 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006945 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006946 if (is_int32_value) {
6947 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6948 } else {
6949 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6950 }
6951 } else if (second.IsDoubleStackSlot()) {
6952 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006953 } else {
6954 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6955 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006956 }
6957 }
6958}
6959
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006960void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6961 HInstruction* instruction,
6962 Location out,
6963 uint32_t offset,
6964 Location maybe_temp,
6965 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006966 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006967 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006968 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006969 if (kUseBakerReadBarrier) {
6970 // Load with fast path based Baker's read barrier.
6971 // /* HeapReference<Object> */ out = *(out + offset)
6972 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006973 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006974 } else {
6975 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006976 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006977 // in the following move operation, as we will need it for the
6978 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006979 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006980 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006981 // /* HeapReference<Object> */ out = *(out + offset)
6982 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006983 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006984 }
6985 } else {
6986 // Plain load with no read barrier.
6987 // /* HeapReference<Object> */ out = *(out + offset)
6988 __ movl(out_reg, Address(out_reg, offset));
6989 __ MaybeUnpoisonHeapReference(out_reg);
6990 }
6991}
6992
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006993void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6994 HInstruction* instruction,
6995 Location out,
6996 Location obj,
6997 uint32_t offset,
6998 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006999 CpuRegister out_reg = out.AsRegister<CpuRegister>();
7000 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007001 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007002 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007003 if (kUseBakerReadBarrier) {
7004 // Load with fast path based Baker's read barrier.
7005 // /* HeapReference<Object> */ out = *(obj + offset)
7006 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007007 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007008 } else {
7009 // Load with slow path based read barrier.
7010 // /* HeapReference<Object> */ out = *(obj + offset)
7011 __ movl(out_reg, Address(obj_reg, offset));
7012 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7013 }
7014 } else {
7015 // Plain load with no read barrier.
7016 // /* HeapReference<Object> */ out = *(obj + offset)
7017 __ movl(out_reg, Address(obj_reg, offset));
7018 __ MaybeUnpoisonHeapReference(out_reg);
7019 }
7020}
7021
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007022void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
7023 HInstruction* instruction,
7024 Location root,
7025 const Address& address,
7026 Label* fixup_label,
7027 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007028 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007029 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007030 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007031 if (kUseBakerReadBarrier) {
7032 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7033 // Baker's read barrier are used:
7034 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007035 // root = obj.field;
7036 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7037 // if (temp != null) {
7038 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007039 // }
7040
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007041 // /* GcRoot<mirror::Object> */ root = *address
7042 __ movl(root_reg, address);
7043 if (fixup_label != nullptr) {
7044 __ Bind(fixup_label);
7045 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007046 static_assert(
7047 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7048 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7049 "have different sizes.");
7050 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7051 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7052 "have different sizes.");
7053
Vladimir Marko953437b2016-08-24 08:30:46 +00007054 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007055 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08007056 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007057 codegen_->AddSlowPath(slow_path);
7058
Roland Levillaind966ce72017-02-09 16:20:14 +00007059 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
7060 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007061 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Andreas Gampe3db70682018-12-26 15:12:03 -08007062 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip= */ true), Immediate(0));
Roland Levillaind966ce72017-02-09 16:20:14 +00007063 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007064 __ j(kNotEqual, slow_path->GetEntryLabel());
7065 __ Bind(slow_path->GetExitLabel());
7066 } else {
7067 // GC root loaded through a slow path for read barriers other
7068 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007069 // /* GcRoot<mirror::Object>* */ root = address
7070 __ leaq(root_reg, address);
7071 if (fixup_label != nullptr) {
7072 __ Bind(fixup_label);
7073 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007074 // /* mirror::Object* */ root = root->Read()
7075 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7076 }
7077 } else {
7078 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007079 // /* GcRoot<mirror::Object> */ root = *address
7080 __ movl(root_reg, address);
7081 if (fixup_label != nullptr) {
7082 __ Bind(fixup_label);
7083 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007084 // Note that GC roots are not affected by heap poisoning, thus we
7085 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007086 }
7087}
7088
7089void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7090 Location ref,
7091 CpuRegister obj,
7092 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007093 bool needs_null_check) {
7094 DCHECK(kEmitCompilerReadBarrier);
7095 DCHECK(kUseBakerReadBarrier);
7096
7097 // /* HeapReference<Object> */ ref = *(obj + offset)
7098 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007099 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007100}
7101
7102void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7103 Location ref,
7104 CpuRegister obj,
7105 uint32_t data_offset,
7106 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007107 bool needs_null_check) {
7108 DCHECK(kEmitCompilerReadBarrier);
7109 DCHECK(kUseBakerReadBarrier);
7110
Roland Levillain3d312422016-06-23 13:53:42 +01007111 static_assert(
7112 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7113 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007114 // /* HeapReference<Object> */ ref =
7115 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007116 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007117 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007118}
7119
7120void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7121 Location ref,
7122 CpuRegister obj,
7123 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007124 bool needs_null_check,
7125 bool always_update_field,
7126 CpuRegister* temp1,
7127 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007128 DCHECK(kEmitCompilerReadBarrier);
7129 DCHECK(kUseBakerReadBarrier);
7130
7131 // In slow path based read barriers, the read barrier call is
7132 // inserted after the original load. However, in fast path based
7133 // Baker's read barriers, we need to perform the load of
7134 // mirror::Object::monitor_ *before* the original reference load.
7135 // This load-load ordering is required by the read barrier.
7136 // The fast path/slow path (for Baker's algorithm) should look like:
7137 //
7138 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7139 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7140 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007141 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007142 // if (is_gray) {
7143 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7144 // }
7145 //
7146 // Note: the original implementation in ReadBarrier::Barrier is
7147 // slightly more complex as:
7148 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007149 // the high-bits of rb_state, which are expected to be all zeroes
7150 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
7151 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007152 // - it performs additional checks that we do not do here for
7153 // performance reasons.
7154
7155 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007156 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7157
Vladimir Marko953437b2016-08-24 08:30:46 +00007158 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007159 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007160 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007161 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7162 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7163 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7164
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007165 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007166 // ref = ReadBarrier::Mark(ref);
7167 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7168 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007169 if (needs_null_check) {
7170 MaybeRecordImplicitNullCheck(instruction);
7171 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007172
7173 // Load fence to prevent load-load reordering.
7174 // Note that this is a no-op, thanks to the x86-64 memory model.
7175 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7176
7177 // The actual reference load.
7178 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007179 __ movl(ref_reg, src); // Flags are unaffected.
7180
7181 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7182 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007183 SlowPathCode* slow_path;
7184 if (always_update_field) {
7185 DCHECK(temp1 != nullptr);
7186 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007187 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08007188 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp1, *temp2);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007189 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007190 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08007191 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007192 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007193 AddSlowPath(slow_path);
7194
7195 // We have done the "if" of the gray bit check above, now branch based on the flags.
7196 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007197
7198 // Object* ref = ref_addr->AsMirrorPtr()
7199 __ MaybeUnpoisonHeapReference(ref_reg);
7200
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007201 __ Bind(slow_path->GetExitLabel());
7202}
7203
7204void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
7205 Location out,
7206 Location ref,
7207 Location obj,
7208 uint32_t offset,
7209 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007210 DCHECK(kEmitCompilerReadBarrier);
7211
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007212 // Insert a slow path based read barrier *after* the reference load.
7213 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007214 // If heap poisoning is enabled, the unpoisoning of the loaded
7215 // reference will be carried out by the runtime within the slow
7216 // path.
7217 //
7218 // Note that `ref` currently does not get unpoisoned (when heap
7219 // poisoning is enabled), which is alright as the `ref` argument is
7220 // not used by the artReadBarrierSlow entry point.
7221 //
7222 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007223 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007224 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
7225 AddSlowPath(slow_path);
7226
Roland Levillain0d5a2812015-11-13 10:07:31 +00007227 __ jmp(slow_path->GetEntryLabel());
7228 __ Bind(slow_path->GetExitLabel());
7229}
7230
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007231void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7232 Location out,
7233 Location ref,
7234 Location obj,
7235 uint32_t offset,
7236 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007237 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007238 // Baker's read barriers shall be handled by the fast path
7239 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
7240 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007241 // If heap poisoning is enabled, unpoisoning will be taken care of
7242 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007243 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007244 } else if (kPoisonHeapReferences) {
7245 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
7246 }
7247}
7248
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007249void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7250 Location out,
7251 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007252 DCHECK(kEmitCompilerReadBarrier);
7253
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007254 // Insert a slow path based read barrier *after* the GC root load.
7255 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007256 // Note that GC roots are not affected by heap poisoning, so we do
7257 // not need to do anything special for this here.
7258 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007259 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007260 AddSlowPath(slow_path);
7261
Roland Levillain0d5a2812015-11-13 10:07:31 +00007262 __ jmp(slow_path->GetEntryLabel());
7263 __ Bind(slow_path->GetExitLabel());
7264}
7265
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007266void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007267 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007268 LOG(FATAL) << "Unreachable";
7269}
7270
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007271void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007272 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007273 LOG(FATAL) << "Unreachable";
7274}
7275
Mark Mendellfe57faa2015-09-18 09:26:15 -04007276// Simple implementation of packed switch - generate cascaded compare/jumps.
7277void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7278 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007279 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007280 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04007281 locations->AddTemp(Location::RequiresRegister());
7282 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04007283}
7284
7285void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7286 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007287 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007288 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04007289 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
7290 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
7291 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007292 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7293
7294 // Should we generate smaller inline compare/jumps?
7295 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7296 // Figure out the correct compare values and jump conditions.
7297 // Handle the first compare/branch as a special case because it might
7298 // jump to the default case.
7299 DCHECK_GT(num_entries, 2u);
7300 Condition first_condition;
7301 uint32_t index;
7302 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7303 if (lower_bound != 0) {
7304 first_condition = kLess;
7305 __ cmpl(value_reg_in, Immediate(lower_bound));
7306 __ j(first_condition, codegen_->GetLabelOf(default_block));
7307 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
7308
7309 index = 1;
7310 } else {
7311 // Handle all the compare/jumps below.
7312 first_condition = kBelow;
7313 index = 0;
7314 }
7315
7316 // Handle the rest of the compare/jumps.
7317 for (; index + 1 < num_entries; index += 2) {
7318 int32_t compare_to_value = lower_bound + index + 1;
7319 __ cmpl(value_reg_in, Immediate(compare_to_value));
7320 // Jump to successors[index] if value < case_value[index].
7321 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7322 // Jump to successors[index + 1] if value == case_value[index + 1].
7323 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7324 }
7325
7326 if (index != num_entries) {
7327 // There are an odd number of entries. Handle the last one.
7328 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00007329 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007330 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
7331 }
7332
7333 // And the default for any other value.
7334 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7335 __ jmp(codegen_->GetLabelOf(default_block));
7336 }
7337 return;
7338 }
Mark Mendell9c86b482015-09-18 13:36:07 -04007339
7340 // Remove the bias, if needed.
7341 Register value_reg_out = value_reg_in.AsRegister();
7342 if (lower_bound != 0) {
7343 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
7344 value_reg_out = temp_reg.AsRegister();
7345 }
7346 CpuRegister value_reg(value_reg_out);
7347
7348 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04007349 __ cmpl(value_reg, Immediate(num_entries - 1));
7350 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007351
Mark Mendell9c86b482015-09-18 13:36:07 -04007352 // We are in the range of the table.
7353 // Load the address of the jump table in the constant area.
7354 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007355
Mark Mendell9c86b482015-09-18 13:36:07 -04007356 // Load the (signed) offset from the jump table.
7357 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
7358
7359 // Add the offset to the address of the table base.
7360 __ addq(temp_reg, base_reg);
7361
7362 // And jump.
7363 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007364}
7365
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007366void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7367 ATTRIBUTE_UNUSED) {
7368 LOG(FATAL) << "Unreachable";
7369}
7370
7371void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7372 ATTRIBUTE_UNUSED) {
7373 LOG(FATAL) << "Unreachable";
7374}
7375
Aart Bikc5d47542016-01-27 17:00:35 -08007376void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
7377 if (value == 0) {
7378 __ xorl(dest, dest);
7379 } else {
7380 __ movl(dest, Immediate(value));
7381 }
7382}
7383
Mark Mendell92e83bf2015-05-07 11:25:03 -04007384void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
7385 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08007386 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007387 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00007388 } else if (IsUint<32>(value)) {
7389 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007390 __ movl(dest, Immediate(static_cast<int32_t>(value)));
7391 } else {
7392 __ movq(dest, Immediate(value));
7393 }
7394}
7395
Mark Mendell7c0b44f2016-02-01 10:08:35 -05007396void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
7397 if (value == 0) {
7398 __ xorps(dest, dest);
7399 } else {
7400 __ movss(dest, LiteralInt32Address(value));
7401 }
7402}
7403
7404void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
7405 if (value == 0) {
7406 __ xorpd(dest, dest);
7407 } else {
7408 __ movsd(dest, LiteralInt64Address(value));
7409 }
7410}
7411
7412void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
7413 Load32BitValue(dest, bit_cast<int32_t, float>(value));
7414}
7415
7416void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
7417 Load64BitValue(dest, bit_cast<int64_t, double>(value));
7418}
7419
Aart Bika19616e2016-02-01 18:57:58 -08007420void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
7421 if (value == 0) {
7422 __ testl(dest, dest);
7423 } else {
7424 __ cmpl(dest, Immediate(value));
7425 }
7426}
7427
7428void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
7429 if (IsInt<32>(value)) {
7430 if (value == 0) {
7431 __ testq(dest, dest);
7432 } else {
7433 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7434 }
7435 } else {
7436 // Value won't fit in an int.
7437 __ cmpq(dest, LiteralInt64Address(value));
7438 }
7439}
7440
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007441void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7442 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007443 GenerateIntCompare(lhs_reg, rhs);
7444}
7445
7446void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007447 if (rhs.IsConstant()) {
7448 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007449 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007450 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007451 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007452 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007453 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007454 }
7455}
7456
7457void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7458 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7459 if (rhs.IsConstant()) {
7460 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7461 Compare64BitValue(lhs_reg, value);
7462 } else if (rhs.IsDoubleStackSlot()) {
7463 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7464 } else {
7465 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7466 }
7467}
7468
7469Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7470 Location index,
7471 ScaleFactor scale,
7472 uint32_t data_offset) {
7473 return index.IsConstant() ?
7474 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7475 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7476}
7477
Mark Mendellcfa410b2015-05-25 16:02:44 -04007478void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7479 DCHECK(dest.IsDoubleStackSlot());
7480 if (IsInt<32>(value)) {
7481 // Can move directly as an int32 constant.
7482 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7483 Immediate(static_cast<int32_t>(value)));
7484 } else {
7485 Load64BitValue(CpuRegister(TMP), value);
7486 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7487 }
7488}
7489
Mark Mendell9c86b482015-09-18 13:36:07 -04007490/**
7491 * Class to handle late fixup of offsets into constant area.
7492 */
7493class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7494 public:
7495 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7496 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7497
7498 protected:
7499 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7500
7501 CodeGeneratorX86_64* codegen_;
7502
7503 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007504 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell9c86b482015-09-18 13:36:07 -04007505 // Patch the correct offset for the instruction. We use the address of the
7506 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7507 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7508 int32_t relative_position = constant_offset - pos;
7509
7510 // Patch in the right value.
7511 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7512 }
7513
7514 // Location in constant area that the fixup refers to.
7515 size_t offset_into_constant_area_;
7516};
7517
7518/**
7519 t * Class to handle late fixup of offsets to a jump table that will be created in the
7520 * constant area.
7521 */
7522class JumpTableRIPFixup : public RIPFixup {
7523 public:
7524 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7525 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7526
7527 void CreateJumpTable() {
7528 X86_64Assembler* assembler = codegen_->GetAssembler();
7529
7530 // Ensure that the reference to the jump table has the correct offset.
7531 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7532 SetOffset(offset_in_constant_table);
7533
7534 // Compute the offset from the start of the function to this jump table.
7535 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7536
7537 // Populate the jump table with the correct values for the jump table.
7538 int32_t num_entries = switch_instr_->GetNumEntries();
7539 HBasicBlock* block = switch_instr_->GetBlock();
7540 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7541 // The value that we want is the target offset - the position of the table.
7542 for (int32_t i = 0; i < num_entries; i++) {
7543 HBasicBlock* b = successors[i];
7544 Label* l = codegen_->GetLabelOf(b);
7545 DCHECK(l->IsBound());
7546 int32_t offset_to_block = l->Position() - current_table_offset;
7547 assembler->AppendInt32(offset_to_block);
7548 }
7549 }
7550
7551 private:
7552 const HPackedSwitch* switch_instr_;
7553};
7554
Mark Mendellf55c3e02015-03-26 21:07:46 -04007555void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7556 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007557 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007558 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7559 // 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 -04007560 assembler->Align(4, 0);
7561 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007562
7563 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007564 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007565 jump_table->CreateJumpTable();
7566 }
7567
7568 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007569 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007570 }
7571
7572 // And finish up.
7573 CodeGenerator::Finalize(allocator);
7574}
7575
Mark Mendellf55c3e02015-03-26 21:07:46 -04007576Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007577 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007578 return Address::RIP(fixup);
7579}
7580
7581Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007582 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007583 return Address::RIP(fixup);
7584}
7585
7586Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007587 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007588 return Address::RIP(fixup);
7589}
7590
7591Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007592 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007593 return Address::RIP(fixup);
7594}
7595
Andreas Gampe85b62f22015-09-09 13:15:38 -07007596// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007597void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007598 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007599 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007600 return;
7601 }
7602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007603 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007604
7605 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7606 if (trg.Equals(return_loc)) {
7607 return;
7608 }
7609
7610 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007611 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007612 parallel_move.AddMove(return_loc, trg, type, nullptr);
7613 GetMoveResolver()->EmitNativeCode(&parallel_move);
7614}
7615
Mark Mendell9c86b482015-09-18 13:36:07 -04007616Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7617 // Create a fixup to be used to create and address the jump table.
7618 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007619 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007620
7621 // We have to populate the jump tables.
7622 fixups_to_jump_tables_.push_back(table_fixup);
7623 return Address::RIP(table_fixup);
7624}
7625
Mark Mendellea5af682015-10-22 17:35:49 -04007626void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7627 const Address& addr_high,
7628 int64_t v,
7629 HInstruction* instruction) {
7630 if (IsInt<32>(v)) {
7631 int32_t v_32 = v;
7632 __ movq(addr_low, Immediate(v_32));
7633 MaybeRecordImplicitNullCheck(instruction);
7634 } else {
7635 // Didn't fit in a register. Do it in pieces.
7636 int32_t low_v = Low32Bits(v);
7637 int32_t high_v = High32Bits(v);
7638 __ movl(addr_low, Immediate(low_v));
7639 MaybeRecordImplicitNullCheck(instruction);
7640 __ movl(addr_high, Immediate(high_v));
7641 }
7642}
7643
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007644void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7645 const uint8_t* roots_data,
7646 const PatchInfo<Label>& info,
7647 uint64_t index_in_table) const {
7648 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7649 uintptr_t address =
7650 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00007651 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007652 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7653 dchecked_integral_cast<uint32_t>(address);
7654}
7655
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007656void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7657 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007658 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007659 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007660 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007661 }
7662
7663 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007664 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007665 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007666 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007667 }
7668}
7669
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05307670bool LocationsBuilderX86_64::CpuHasAvxFeatureFlag() {
7671 return codegen_->GetInstructionSetFeatures().HasAVX();
7672}
7673
7674bool LocationsBuilderX86_64::CpuHasAvx2FeatureFlag() {
7675 return codegen_->GetInstructionSetFeatures().HasAVX2();
7676}
7677
7678bool InstructionCodeGeneratorX86_64::CpuHasAvxFeatureFlag() {
7679 return codegen_->GetInstructionSetFeatures().HasAVX();
7680}
7681
7682bool InstructionCodeGeneratorX86_64::CpuHasAvx2FeatureFlag() {
7683 return codegen_->GetInstructionSetFeatures().HasAVX2();
7684}
7685
Roland Levillain4d027112015-07-01 15:41:14 +01007686#undef __
7687
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007688} // namespace x86_64
7689} // namespace art