blob: d54484c065dffa037378f60a6cb8043d34ec7779 [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
Vladimir Marko86c87522020-05-11 16:55:55 +010019#include "arch/x86_64/jni_frame_x86_64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000020#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010028#include "interpreter/mterp/nterp.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080029#include "intrinsics.h"
30#include "intrinsics_x86_64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000031#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Andreas Gamped4901292017-05-30 18:41:34 -070033#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070034#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036#include "mirror/object_reference.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000037#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010038#include "thread.h"
39#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010041#include "utils/x86_64/assembler_x86_64.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000042#include "utils/x86_64/constants_x86_64.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010043#include "utils/x86_64/managed_register_x86_64.h"
44
Vladimir Marko0a516052019-10-14 13:00:44 +000045namespace art {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010046
Roland Levillain0d5a2812015-11-13 10:07:31 +000047template<class MirrorType>
48class GcRoot;
49
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010050namespace x86_64 {
51
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010053static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000054// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
55// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
56// generates less code/data with a small num_entries.
57static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010058
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000059static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000060static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010061
Mark Mendell24f2dfa2015-01-14 19:51:45 -050062static constexpr int kC2ConditionMask = 0x400;
63
Vladimir Marko3232dbb2018-07-25 15:42:46 +010064static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
65 // Custom calling convention: RAX serves as both input and output.
66 RegisterSet caller_saves = RegisterSet::Empty();
67 caller_saves.Add(Location::RegisterLocation(RAX));
68 return caller_saves;
69}
70
Roland Levillain7cbd27f2016-08-11 23:53:33 +010071// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
72#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070073#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074
Andreas Gampe85b62f22015-09-09 13:15:38 -070075class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000077 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010079 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +000080 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000082 if (instruction_->CanThrowIntoCatchBlock()) {
83 // Live registers will be restored in the catch block if caught.
84 SaveLiveRegisters(codegen, instruction_->GetLocations());
85 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010086 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000087 instruction_,
88 instruction_->GetDexPc(),
89 this);
Roland Levillain888d0672015-11-23 18:53:50 +000090 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 }
92
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010093 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010094
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010095 const char* GetDescription() const override { return "NullCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010096
Nicolas Geoffraye5038322014-07-04 09:41:32 +010097 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010098 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
99};
100
Andreas Gampe85b62f22015-09-09 13:15:38 -0700101class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000102 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000103 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000104
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100105 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000106 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000107 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100108 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000109 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000110 }
111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100112 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100114 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100115
Calin Juravled0d48522014-11-04 16:40:20 +0000116 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000117 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
118};
119
Andreas Gampe85b62f22015-09-09 13:15:38 -0700120class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000121 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, DataType::Type type, bool is_div)
David Srbecky9cd6d372016-02-09 15:24:47 +0000123 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100125 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000126 __ Bind(GetEntryLabel());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100127 if (type_ == DataType::Type::kInt32) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000128 if (is_div_) {
129 __ negl(cpu_reg_);
130 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400131 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000132 }
133
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000134 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100135 DCHECK_EQ(DataType::Type::kInt64, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000136 if (is_div_) {
137 __ negq(cpu_reg_);
138 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400139 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000141 }
Calin Juravled0d48522014-11-04 16:40:20 +0000142 __ jmp(GetExitLabel());
143 }
144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100145 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100146
Calin Juravled0d48522014-11-04 16:40:20 +0000147 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000148 const CpuRegister cpu_reg_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 const DataType::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000150 const bool is_div_;
151 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000152};
153
Andreas Gampe85b62f22015-09-09 13:15:38 -0700154class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100156 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000157 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100159 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700160 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000161 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700163 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100164 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000165 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700166 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 if (successor_ == nullptr) {
168 __ jmp(GetReturnLabel());
169 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000170 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 }
173
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 Label* GetReturnLabel() {
175 DCHECK(successor_ == nullptr);
176 return &return_label_;
177 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100179 HBasicBlock* GetSuccessor() const {
180 return successor_;
181 }
182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100183 const char* GetDescription() const override { return "SuspendCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100184
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 Label return_label_;
188
189 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
190};
191
Andreas Gampe85b62f22015-09-09 13:15:38 -0700192class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100193 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100194 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100197 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100198 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000199 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000201 if (instruction_->CanThrowIntoCatchBlock()) {
202 // Live registers will be restored in the catch block if caught.
203 SaveLiveRegisters(codegen, instruction_->GetLocations());
204 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400205 // Are we using an array length from memory?
206 HInstruction* array_length = instruction_->InputAt(1);
207 Location length_loc = locations->InAt(1);
208 InvokeRuntimeCallingConvention calling_convention;
209 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
210 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100211 HArrayLength* length = array_length->AsArrayLength();
Nicolas Geoffray003444a2017-10-17 10:58:42 +0100212 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400213 Location array_loc = array_length->GetLocations()->InAt(0);
214 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
215 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
216 // Check for conflicts with index.
217 if (length_loc.Equals(locations->InAt(0))) {
218 // We know we aren't using parameter 2.
219 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
220 }
221 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100222 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100223 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700224 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400225 }
226
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000227 // We're moving two locations to locations that could overlap, so we need a parallel
228 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000229 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100230 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000231 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100232 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400233 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100234 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100235 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100236 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
237 ? kQuickThrowStringBounds
238 : kQuickThrowArrayBounds;
239 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100240 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000241 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100242 }
243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100244 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100245
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100246 const char* GetDescription() const override { return "BoundsCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100247
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100248 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100249 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
250};
251
Andreas Gampe85b62f22015-09-09 13:15:38 -0700252class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100253 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100254 LoadClassSlowPathX86_64(HLoadClass* cls, HInstruction* at)
255 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100257 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100259
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100260 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000261 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100262 Location out = locations->Out();
263 const uint32_t dex_pc = instruction_->GetDexPc();
264 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
265 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
266
Roland Levillain0d5a2812015-11-13 10:07:31 +0000267 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100268 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270
Vladimir Markoea4c1262017-02-06 19:59:33 +0000271 // Custom calling convention: RAX serves as both input and output.
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100272 if (must_resolve_type) {
273 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_64_codegen->GetGraph()->GetDexFile()));
274 dex::TypeIndex type_index = cls_->GetTypeIndex();
275 __ movl(CpuRegister(RAX), Immediate(type_index.index_));
Vladimir Marko8f63f102020-09-28 12:10:28 +0100276 if (cls_->NeedsAccessCheck()) {
277 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
278 x86_64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
279 } else {
280 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
281 x86_64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
282 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100283 // If we also must_do_clinit, the resolved type is now in the correct register.
284 } else {
285 DCHECK(must_do_clinit);
286 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
287 x86_64_codegen->Move(Location::RegisterLocation(RAX), source);
288 }
289 if (must_do_clinit) {
290 x86_64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
291 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000292 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100293
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000294 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295 if (out.IsValid()) {
296 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000297 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000298 }
299
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000300 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100301 __ jmp(GetExitLabel());
302 }
303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100304 const char* GetDescription() const override { return "LoadClassSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100305
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100306 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000307 // The class this slow path will load.
308 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100309
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100311};
312
Vladimir Markoaad75c62016-10-03 08:46:48 +0000313class LoadStringSlowPathX86_64 : public SlowPathCode {
314 public:
315 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100317 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000318 LocationSummary* locations = instruction_->GetLocations();
319 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
320
321 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
322 __ Bind(GetEntryLabel());
323 SaveLiveRegisters(codegen, locations);
324
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000325 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100326 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000327 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000328 x86_64_codegen->InvokeRuntime(kQuickResolveString,
329 instruction_,
330 instruction_->GetDexPc(),
331 this);
332 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
333 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
334 RestoreLiveRegisters(codegen, locations);
335
Vladimir Markoaad75c62016-10-03 08:46:48 +0000336 __ jmp(GetExitLabel());
337 }
338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100339 const char* GetDescription() const override { return "LoadStringSlowPathX86_64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000340
341 private:
342 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
343};
344
Andreas Gampe85b62f22015-09-09 13:15:38 -0700345class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000347 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000348 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100350 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000351 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100352 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 DCHECK(instruction_->IsCheckCast()
354 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000355
Roland Levillain0d5a2812015-11-13 10:07:31 +0000356 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000357 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000358
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000359 if (kPoisonHeapReferences &&
360 instruction_->IsCheckCast() &&
361 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
362 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
363 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<CpuRegister>());
364 }
365
Vladimir Marko87584542017-12-12 17:47:52 +0000366 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367 SaveLiveRegisters(codegen, locations);
368 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000369
370 // We're moving two locations to locations that could overlap, so we need a parallel
371 // move resolver.
372 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800373 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800374 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100375 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800376 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800377 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100378 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000379 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100380 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800381 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000382 } else {
383 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800384 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
385 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000386 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000387
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000388 if (!is_fatal_) {
389 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000390 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000391 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000392
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000393 RestoreLiveRegisters(codegen, locations);
394 __ jmp(GetExitLabel());
395 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000396 }
397
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100398 const char* GetDescription() const override { return "TypeCheckSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100400 bool IsFatal() const override { return is_fatal_; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000401
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000402 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000403 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000404
405 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
406};
407
Andreas Gampe85b62f22015-09-09 13:15:38 -0700408class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700409 public:
Aart Bik42249c32016-01-07 15:33:50 -0800410 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000411 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100413 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000414 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700415 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100416 LocationSummary* locations = instruction_->GetLocations();
417 SaveLiveRegisters(codegen, locations);
418 InvokeRuntimeCallingConvention calling_convention;
419 x86_64_codegen->Load32BitValue(
420 CpuRegister(calling_convention.GetRegisterAt(0)),
421 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100422 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100423 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700424 }
425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100426 const char* GetDescription() const override { return "DeoptimizationSlowPathX86_64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100427
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700428 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700429 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
430};
431
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432class ArraySetSlowPathX86_64 : public SlowPathCode {
433 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000434 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100436 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 LocationSummary* locations = instruction_->GetLocations();
438 __ Bind(GetEntryLabel());
439 SaveLiveRegisters(codegen, locations);
440
441 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100442 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100443 parallel_move.AddMove(
444 locations->InAt(0),
445 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100446 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100447 nullptr);
448 parallel_move.AddMove(
449 locations->InAt(1),
450 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100451 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 nullptr);
453 parallel_move.AddMove(
454 locations->InAt(2),
455 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100456 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100457 nullptr);
458 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
459
Roland Levillain0d5a2812015-11-13 10:07:31 +0000460 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100461 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000462 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100463 RestoreLiveRegisters(codegen, locations);
464 __ jmp(GetExitLabel());
465 }
466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100467 const char* GetDescription() const override { return "ArraySetSlowPathX86_64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100468
469 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100470 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
471};
472
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100473// Slow path marking an object reference `ref` during a read
474// barrier. The field `obj.field` in the object `obj` holding this
475// reference does not get updated by this slow path after marking (see
476// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
477//
478// This means that after the execution of this slow path, `ref` will
479// always be up-to-date, but `obj.field` may not; i.e., after the
480// flip, `ref` will be a to-space reference, but `obj.field` will
481// probably still be a from-space reference (unless it gets updated by
482// another thread, or if another thread installed another object
483// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000484class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
485 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
487 Location ref,
488 bool unpoison_ref_before_marking)
489 : SlowPathCode(instruction),
490 ref_(ref),
491 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000492 DCHECK(kEmitCompilerReadBarrier);
493 }
494
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100495 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86_64"; }
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000496
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100497 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000498 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
500 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000501 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000503 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000504 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000505 instruction_->IsStaticFieldGet() ||
506 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100507 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000508 instruction_->IsLoadClass() ||
509 instruction_->IsLoadString() ||
510 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100511 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100512 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
513 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000514 << "Unexpected instruction in read barrier marking slow path: "
515 << instruction_->DebugName();
516
517 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000519 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000521 }
Roland Levillain4359e612016-07-20 11:32:19 +0100522 // No need to save live registers; it's taken care of by the
523 // entrypoint. Also, there is no need to update the stack mask,
524 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000525 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100526 DCHECK_NE(ref_reg, RSP);
527 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100528 // "Compact" slow path, saving two moves.
529 //
530 // Instead of using the standard runtime calling convention (input
531 // and output in R0):
532 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100533 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100534 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100535 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100536 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100537 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100538 // of a dedicated entrypoint:
539 //
540 // rX <- ReadBarrierMarkRegX(rX)
541 //
542 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100543 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100544 // This runtime call does not require a stack map.
545 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000546 __ jmp(GetExitLabel());
547 }
548
549 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100550 // The location (register) of the marked object reference.
551 const Location ref_;
552 // Should the reference in `ref_` be unpoisoned prior to marking it?
553 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000554
555 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
556};
557
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100558// Slow path marking an object reference `ref` during a read barrier,
559// and if needed, atomically updating the field `obj.field` in the
560// object `obj` holding this reference after marking (contrary to
561// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
562// `obj.field`).
563//
564// This means that after the execution of this slow path, both `ref`
565// and `obj.field` will be up-to-date; i.e., after the flip, both will
566// hold the same to-space reference (unless another thread installed
567// another object reference (different from `ref`) in `obj.field`).
568class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
569 public:
570 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
571 Location ref,
572 CpuRegister obj,
573 const Address& field_addr,
574 bool unpoison_ref_before_marking,
575 CpuRegister temp1,
576 CpuRegister temp2)
577 : SlowPathCode(instruction),
578 ref_(ref),
579 obj_(obj),
580 field_addr_(field_addr),
581 unpoison_ref_before_marking_(unpoison_ref_before_marking),
582 temp1_(temp1),
583 temp2_(temp2) {
584 DCHECK(kEmitCompilerReadBarrier);
585 }
586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100587 const char* GetDescription() const override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100588 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
589 }
590
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100591 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100592 LocationSummary* locations = instruction_->GetLocations();
593 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
594 Register ref_reg = ref_cpu_reg.AsRegister();
595 DCHECK(locations->CanCall());
596 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
597 // This slow path is only used by the UnsafeCASObject intrinsic.
598 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
599 << "Unexpected instruction in read barrier marking and field updating slow path: "
600 << instruction_->DebugName();
601 DCHECK(instruction_->GetLocations()->Intrinsified());
602 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
603
604 __ Bind(GetEntryLabel());
605 if (unpoison_ref_before_marking_) {
606 // Object* ref = ref_addr->AsMirrorPtr()
607 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
608 }
609
610 // Save the old (unpoisoned) reference.
611 __ movl(temp1_, ref_cpu_reg);
612
613 // No need to save live registers; it's taken care of by the
614 // entrypoint. Also, there is no need to update the stack mask,
615 // as this runtime call will not trigger a garbage collection.
616 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
617 DCHECK_NE(ref_reg, RSP);
618 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
619 // "Compact" slow path, saving two moves.
620 //
621 // Instead of using the standard runtime calling convention (input
622 // and output in R0):
623 //
624 // RDI <- ref
625 // RAX <- ReadBarrierMark(RDI)
626 // ref <- RAX
627 //
628 // we just use rX (the register containing `ref`) as input and output
629 // of a dedicated entrypoint:
630 //
631 // rX <- ReadBarrierMarkRegX(rX)
632 //
633 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100634 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100635 // This runtime call does not require a stack map.
636 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
637
638 // If the new reference is different from the old reference,
639 // update the field in the holder (`*field_addr`).
640 //
641 // Note that this field could also hold a different object, if
642 // another thread had concurrently changed it. In that case, the
643 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
644 // operation below would abort the CAS, leaving the field as-is.
645 NearLabel done;
646 __ cmpl(temp1_, ref_cpu_reg);
647 __ j(kEqual, &done);
648
649 // Update the the holder's field atomically. This may fail if
650 // mutator updates before us, but it's OK. This is achived
651 // using a strong compare-and-set (CAS) operation with relaxed
652 // memory synchronization ordering, where the expected value is
653 // the old reference and the desired value is the new reference.
654 // This operation is implemented with a 32-bit LOCK CMPXLCHG
655 // instruction, which requires the expected value (the old
656 // reference) to be in EAX. Save RAX beforehand, and move the
657 // expected value (stored in `temp1_`) into EAX.
658 __ movq(temp2_, CpuRegister(RAX));
659 __ movl(CpuRegister(RAX), temp1_);
660
661 // Convenience aliases.
662 CpuRegister base = obj_;
663 CpuRegister expected = CpuRegister(RAX);
664 CpuRegister value = ref_cpu_reg;
665
666 bool base_equals_value = (base.AsRegister() == value.AsRegister());
667 Register value_reg = ref_reg;
668 if (kPoisonHeapReferences) {
669 if (base_equals_value) {
670 // If `base` and `value` are the same register location, move
671 // `value_reg` to a temporary register. This way, poisoning
672 // `value_reg` won't invalidate `base`.
673 value_reg = temp1_.AsRegister();
674 __ movl(CpuRegister(value_reg), base);
675 }
676
677 // Check that the register allocator did not assign the location
678 // of `expected` (RAX) to `value` nor to `base`, so that heap
679 // poisoning (when enabled) works as intended below.
680 // - If `value` were equal to `expected`, both references would
681 // be poisoned twice, meaning they would not be poisoned at
682 // all, as heap poisoning uses address negation.
683 // - If `base` were equal to `expected`, poisoning `expected`
684 // would invalidate `base`.
685 DCHECK_NE(value_reg, expected.AsRegister());
686 DCHECK_NE(base.AsRegister(), expected.AsRegister());
687
688 __ PoisonHeapReference(expected);
689 __ PoisonHeapReference(CpuRegister(value_reg));
690 }
691
692 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
693
694 // If heap poisoning is enabled, we need to unpoison the values
695 // that were poisoned earlier.
696 if (kPoisonHeapReferences) {
697 if (base_equals_value) {
698 // `value_reg` has been moved to a temporary register, no need
699 // to unpoison it.
700 } else {
701 __ UnpoisonHeapReference(CpuRegister(value_reg));
702 }
703 // No need to unpoison `expected` (RAX), as it is be overwritten below.
704 }
705
706 // Restore RAX.
707 __ movq(CpuRegister(RAX), temp2_);
708
709 __ Bind(&done);
710 __ jmp(GetExitLabel());
711 }
712
713 private:
714 // The location (register) of the marked object reference.
715 const Location ref_;
716 // The register containing the object holding the marked object reference field.
717 const CpuRegister obj_;
718 // The address of the marked reference field. The base of this address must be `obj_`.
719 const Address field_addr_;
720
721 // Should the reference in `ref_` be unpoisoned prior to marking it?
722 const bool unpoison_ref_before_marking_;
723
724 const CpuRegister temp1_;
725 const CpuRegister temp2_;
726
727 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
728};
729
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730// Slow path generating a read barrier for a heap reference.
731class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
732 public:
733 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
734 Location out,
735 Location ref,
736 Location obj,
737 uint32_t offset,
738 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000739 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000740 out_(out),
741 ref_(ref),
742 obj_(obj),
743 offset_(offset),
744 index_(index) {
745 DCHECK(kEmitCompilerReadBarrier);
746 // If `obj` is equal to `out` or `ref`, it means the initial
747 // object has been overwritten by (or after) the heap object
748 // reference load to be instrumented, e.g.:
749 //
750 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000751 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000752 //
753 // In that case, we have lost the information about the original
754 // object, and the emitted read barrier cannot work properly.
755 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
756 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
757}
758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100759 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000760 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
761 LocationSummary* locations = instruction_->GetLocations();
762 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
763 DCHECK(locations->CanCall());
764 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100765 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000766 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100767 instruction_->IsStaticFieldGet() ||
768 instruction_->IsArrayGet() ||
769 instruction_->IsInstanceOf() ||
770 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000771 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000772 << "Unexpected instruction in read barrier for heap reference slow path: "
773 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000774
775 __ Bind(GetEntryLabel());
776 SaveLiveRegisters(codegen, locations);
777
778 // We may have to change the index's value, but as `index_` is a
779 // constant member (like other "inputs" of this slow path),
780 // introduce a copy of it, `index`.
781 Location index = index_;
782 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100783 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000784 if (instruction_->IsArrayGet()) {
785 // Compute real offset and store it in index_.
786 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
787 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
788 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
789 // We are about to change the value of `index_reg` (see the
790 // calls to art::x86_64::X86_64Assembler::shll and
791 // art::x86_64::X86_64Assembler::AddImmediate below), but it
792 // has not been saved by the previous call to
793 // art::SlowPathCode::SaveLiveRegisters, as it is a
794 // callee-save register --
795 // art::SlowPathCode::SaveLiveRegisters does not consider
796 // callee-save registers, as it has been designed with the
797 // assumption that callee-save registers are supposed to be
798 // handled by the called function. So, as a callee-save
799 // register, `index_reg` _would_ eventually be saved onto
800 // the stack, but it would be too late: we would have
801 // changed its value earlier. Therefore, we manually save
802 // it here into another freely available register,
803 // `free_reg`, chosen of course among the caller-save
804 // registers (as a callee-save `free_reg` register would
805 // exhibit the same problem).
806 //
807 // Note we could have requested a temporary register from
808 // the register allocator instead; but we prefer not to, as
809 // this is a slow path, and we know we can find a
810 // caller-save register that is available.
811 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
812 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
813 index_reg = free_reg;
814 index = Location::RegisterLocation(index_reg);
815 } else {
816 // The initial register stored in `index_` has already been
817 // saved in the call to art::SlowPathCode::SaveLiveRegisters
818 // (as it is not a callee-save register), so we can freely
819 // use it.
820 }
821 // Shifting the index value contained in `index_reg` by the
822 // scale factor (2) cannot overflow in practice, as the
823 // runtime is unable to allocate object arrays with a size
824 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
825 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
826 static_assert(
827 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
828 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
829 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
830 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100831 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
832 // intrinsics, `index_` is not shifted by a scale factor of 2
833 // (as in the case of ArrayGet), as it is actually an offset
834 // to an object field within an object.
835 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000836 DCHECK(instruction_->GetLocations()->Intrinsified());
837 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
838 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
839 << instruction_->AsInvoke()->GetIntrinsic();
840 DCHECK_EQ(offset_, 0U);
841 DCHECK(index_.IsRegister());
842 }
843 }
844
845 // We're moving two or three locations to locations that could
846 // overlap, so we need a parallel move resolver.
847 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100848 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849 parallel_move.AddMove(ref_,
850 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100851 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000852 nullptr);
853 parallel_move.AddMove(obj_,
854 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100855 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000856 nullptr);
857 if (index.IsValid()) {
858 parallel_move.AddMove(index,
859 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100860 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000861 nullptr);
862 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
863 } else {
864 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
865 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
866 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100867 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 instruction_,
869 instruction_->GetDexPc(),
870 this);
871 CheckEntrypointTypes<
872 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
873 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
874
875 RestoreLiveRegisters(codegen, locations);
876 __ jmp(GetExitLabel());
877 }
878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100879 const char* GetDescription() const override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000880 return "ReadBarrierForHeapReferenceSlowPathX86_64";
881 }
882
883 private:
884 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
885 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
886 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
887 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
888 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
889 return static_cast<CpuRegister>(i);
890 }
891 }
892 // We shall never fail to find a free caller-save register, as
893 // there are more than two core caller-save registers on x86-64
894 // (meaning it is possible to find one which is different from
895 // `ref` and `obj`).
896 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
897 LOG(FATAL) << "Could not find a free caller-save register";
898 UNREACHABLE();
899 }
900
Roland Levillain0d5a2812015-11-13 10:07:31 +0000901 const Location out_;
902 const Location ref_;
903 const Location obj_;
904 const uint32_t offset_;
905 // An additional location containing an index to an array.
906 // Only used for HArrayGet and the UnsafeGetObject &
907 // UnsafeGetObjectVolatile intrinsics.
908 const Location index_;
909
910 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
911};
912
913// Slow path generating a read barrier for a GC root.
914class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
915 public:
916 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000917 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000918 DCHECK(kEmitCompilerReadBarrier);
919 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100921 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000922 LocationSummary* locations = instruction_->GetLocations();
923 DCHECK(locations->CanCall());
924 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000925 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
926 << "Unexpected instruction in read barrier for GC root slow path: "
927 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000928
929 __ Bind(GetEntryLabel());
930 SaveLiveRegisters(codegen, locations);
931
932 InvokeRuntimeCallingConvention calling_convention;
933 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
934 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100935 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000936 instruction_,
937 instruction_->GetDexPc(),
938 this);
939 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
940 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
941
942 RestoreLiveRegisters(codegen, locations);
943 __ jmp(GetExitLabel());
944 }
945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100946 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86_64"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000947
948 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000949 const Location out_;
950 const Location root_;
951
952 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
953};
954
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100955#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100956// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
957#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100958
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700960 switch (cond) {
961 case kCondEQ: return kEqual;
962 case kCondNE: return kNotEqual;
963 case kCondLT: return kLess;
964 case kCondLE: return kLessEqual;
965 case kCondGT: return kGreater;
966 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700967 case kCondB: return kBelow;
968 case kCondBE: return kBelowEqual;
969 case kCondA: return kAbove;
970 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700971 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100972 LOG(FATAL) << "Unreachable";
973 UNREACHABLE();
974}
975
Aart Bike9f37602015-10-09 11:15:55 -0700976// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100977inline Condition X86_64FPCondition(IfCondition cond) {
978 switch (cond) {
979 case kCondEQ: return kEqual;
980 case kCondNE: return kNotEqual;
981 case kCondLT: return kBelow;
982 case kCondLE: return kBelowEqual;
983 case kCondGT: return kAbove;
984 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700985 default: break; // should not happen
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800986 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100987 LOG(FATAL) << "Unreachable";
988 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700989}
990
Vladimir Marko86c87522020-05-11 16:55:55 +0100991void CodeGeneratorX86_64::BlockNonVolatileXmmRegisters(LocationSummary* locations) {
992 // We have to ensure that the native code we call directly (such as @CriticalNative
993 // or some intrinsic helpers, say Math.sin()) doesn't clobber the XMM registers
994 // which are non-volatile for ART, but volatile for Native calls. This will ensure
995 // that they are saved in the prologue and properly restored.
996 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
997 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
998 }
999}
1000
Vladimir Markodc151b22015-10-15 18:02:30 +01001001HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
1002 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01001003 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00001004 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01001005}
1006
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001007void CodeGeneratorX86_64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
1008 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001009 case MethodLoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko44ca0752019-07-29 10:18:25 +01001010 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01001011 __ leal(temp.AsRegister<CpuRegister>(),
Vladimir Marko4ef451a2020-07-23 09:54:27 +00001012 Address::Absolute(kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001013 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01001014 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001015 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00001016 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1017 __ movl(temp.AsRegister<CpuRegister>(),
Vladimir Marko4ef451a2020-07-23 09:54:27 +00001018 Address::Absolute(kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001019 RecordBootImageRelRoPatch(GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00001020 break;
1021 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001022 case MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001023 __ movq(temp.AsRegister<CpuRegister>(),
Vladimir Marko4ef451a2020-07-23 09:54:27 +00001024 Address::Absolute(kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001025 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01001026 // No need for memory fence, thanks to the x86-64 memory model.
Vladimir Marko58155012015-08-19 12:49:41 +00001027 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001028 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001029 case MethodLoadKind::kJitDirectAddress: {
1030 Load64BitValue(temp.AsRegister<CpuRegister>(),
1031 reinterpret_cast<int64_t>(invoke->GetResolvedMethod()));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001032 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001033 }
1034 case MethodLoadKind::kRuntimeCall: {
1035 // Test situation, don't do anything.
1036 break;
1037 }
1038 default: {
1039 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
1040 UNREACHABLE();
1041 }
1042 }
1043}
1044
1045void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
1046 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
1047 // All registers are assumed to be correctly set up.
1048
1049 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
1050 switch (invoke->GetMethodLoadKind()) {
1051 case MethodLoadKind::kStringInit: {
1052 // temp = thread->string_init_entrypoint
1053 uint32_t offset =
1054 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
1055 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip= */ true));
1056 break;
1057 }
1058 case MethodLoadKind::kRecursive: {
1059 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
1060 break;
1061 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001062 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001063 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1064 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001065 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001066 case MethodLoadKind::kBootImageLinkTimePcRelative:
1067 // For kCallCriticalNative we skip loading the method and do the call directly.
1068 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
1069 break;
1070 }
1071 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001072 default: {
1073 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
1074 break;
1075 }
Vladimir Marko58155012015-08-19 12:49:41 +00001076 }
1077
1078 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001079 case CodePtrLocation::kCallSelf:
Vladimir Marko58155012015-08-19 12:49:41 +00001080 __ call(&frame_entry_label_);
Vladimir Marko86c87522020-05-11 16:55:55 +01001081 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00001082 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001083 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01001084 size_t out_frame_size =
1085 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86_64,
1086 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01001087 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001088 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
1089 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
1090 __ call(Address::Absolute(kPlaceholder32BitOffset, /* no_rip= */ false));
1091 RecordBootImageJniEntrypointPatch(invoke);
1092 } else {
1093 // (callee_method + offset_of_jni_entry_point)()
1094 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1095 ArtMethod::EntryPointFromJniOffset(kX86_64PointerSize).SizeValue()));
1096 }
Vladimir Marko86c87522020-05-11 16:55:55 +01001097 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
1098 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
1099 switch (invoke->GetType()) {
1100 case DataType::Type::kBool:
1101 __ movzxb(CpuRegister(RAX), CpuRegister(RAX));
1102 break;
1103 case DataType::Type::kInt8:
1104 __ movsxb(CpuRegister(RAX), CpuRegister(RAX));
1105 break;
1106 case DataType::Type::kUint16:
1107 __ movzxw(CpuRegister(RAX), CpuRegister(RAX));
1108 break;
1109 case DataType::Type::kInt16:
1110 __ movsxw(CpuRegister(RAX), CpuRegister(RAX));
1111 break;
1112 case DataType::Type::kInt32:
1113 case DataType::Type::kInt64:
1114 case DataType::Type::kFloat32:
1115 case DataType::Type::kFloat64:
1116 case DataType::Type::kVoid:
1117 break;
1118 default:
1119 DCHECK(false) << invoke->GetType();
1120 break;
1121 }
1122 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01001123 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01001124 }
1125 break;
1126 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001127 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00001128 // (callee_method + offset_of_quick_compiled_code)()
1129 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1130 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001131 kX86_64PointerSize).SizeValue()));
Vladimir Marko86c87522020-05-11 16:55:55 +01001132 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00001133 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001134 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001135
1136 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001137}
1138
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001139void CodeGeneratorX86_64::GenerateVirtualCall(
1140 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001141 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1142 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1143 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001144
1145 // Use the calling convention instead of the location of the receiver, as
1146 // intrinsics may have put the receiver in a different register. In the intrinsics
1147 // slow path, the arguments have been moved to the right place, so here we are
1148 // guaranteed that the receiver is the first register of the calling convention.
1149 InvokeDexCallingConvention calling_convention;
1150 Register receiver = calling_convention.GetRegisterAt(0);
1151
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001152 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001153 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001154 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001155 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001156 // Instead of simply (possibly) unpoisoning `temp` here, we should
1157 // emit a read barrier for the previous class reference load.
1158 // However this is not required in practice, as this is an
1159 // intermediate/temporary reference and because the current
1160 // concurrent copying collector keeps the from-space memory
1161 // intact/accessible until the end of the marking phase (the
1162 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001163 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00001164
Nicolas Geoffray20036d82019-11-28 16:15:00 +00001165 MaybeGenerateInlineCacheCheck(invoke, temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00001166
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001167 // temp = temp->GetMethodAt(method_offset);
1168 __ movq(temp, Address(temp, method_offset));
1169 // call temp->GetEntryPoint();
1170 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001171 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001172 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001173}
1174
Vladimir Marko6fd16062018-06-26 11:02:04 +01001175void CodeGeneratorX86_64::RecordBootImageIntrinsicPatch(uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001176 boot_image_other_patches_.emplace_back(/* target_dex_file= */ nullptr, intrinsic_data);
1177 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001178}
1179
Vladimir Markob066d432018-01-03 13:14:37 +00001180void CodeGeneratorX86_64::RecordBootImageRelRoPatch(uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001181 boot_image_other_patches_.emplace_back(/* target_dex_file= */ nullptr, boot_image_offset);
1182 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00001183}
1184
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001185void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvoke* invoke) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001186 boot_image_method_patches_.emplace_back(invoke->GetResolvedMethodReference().dex_file,
1187 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001188 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001189}
1190
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001191void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvoke* invoke) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001192 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
1193 method_bss_entry_patches_.emplace_back(invoke->GetMethodReference().dex_file,
1194 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001195 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001196}
1197
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001198void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1199 boot_image_type_patches_.emplace_back(
1200 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001201 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001202}
1203
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001204Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01001205 ArenaDeque<PatchInfo<Label>>* patches = nullptr;
1206 switch (load_class->GetLoadKind()) {
1207 case HLoadClass::LoadKind::kBssEntry:
1208 patches = &type_bss_entry_patches_;
1209 break;
1210 case HLoadClass::LoadKind::kBssEntryPublic:
1211 patches = &public_type_bss_entry_patches_;
1212 break;
1213 case HLoadClass::LoadKind::kBssEntryPackage:
1214 patches = &package_type_bss_entry_patches_;
1215 break;
1216 default:
1217 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
1218 UNREACHABLE();
1219 }
1220 patches->emplace_back(&load_class->GetDexFile(), load_class->GetTypeIndex().index_);
1221 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001222}
1223
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001224void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1225 boot_image_string_patches_.emplace_back(
1226 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1227 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001228}
1229
Vladimir Markoaad75c62016-10-03 08:46:48 +00001230Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001231 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001232 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001233 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001234}
1235
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001236void CodeGeneratorX86_64::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
1237 boot_image_jni_entrypoint_patches_.emplace_back(invoke->GetResolvedMethodReference().dex_file,
1238 invoke->GetResolvedMethodReference().index);
1239 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
1240}
1241
Vladimir Marko6fd16062018-06-26 11:02:04 +01001242void CodeGeneratorX86_64::LoadBootImageAddress(CpuRegister reg, uint32_t boot_image_reference) {
1243 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00001244 __ leal(reg,
1245 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Marko6fd16062018-06-26 11:02:04 +01001246 RecordBootImageIntrinsicPatch(boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01001247 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00001248 __ movl(reg,
1249 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Marko6fd16062018-06-26 11:02:04 +01001250 RecordBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001251 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01001252 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001253 gc::Heap* heap = Runtime::Current()->GetHeap();
1254 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001255 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001256 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
1257 }
1258}
1259
Vladimir Markode91ca92020-10-27 13:41:40 +00001260void CodeGeneratorX86_64::LoadIntrinsicDeclaringClass(CpuRegister reg, HInvoke* invoke) {
1261 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001262 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001263 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Markode91ca92020-10-27 13:41:40 +00001264 __ leal(reg,
Vladimir Marko4ef451a2020-07-23 09:54:27 +00001265 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001266 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01001267 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1268 boot_image_type_patches_.emplace_back(target_method.dex_file, type_idx.index_);
1269 __ Bind(&boot_image_type_patches_.back().label);
1270 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00001271 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
1272 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001273 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01001274}
1275
Vladimir Markoaad75c62016-10-03 08:46:48 +00001276// The label points to the end of the "movl" or another instruction but the literal offset
1277// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1278constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1279
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001280template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001281inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1282 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001283 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001284 for (const PatchInfo<Label>& info : infos) {
1285 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1286 linker_patches->push_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001287 Factory(literal_offset, info.target_dex_file, info.label.Position(), info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001288 }
1289}
1290
Vladimir Marko6fd16062018-06-26 11:02:04 +01001291template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1292linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1293 const DexFile* target_dex_file,
1294 uint32_t pc_insn_offset,
1295 uint32_t boot_image_offset) {
1296 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1297 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001298}
1299
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001300void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001301 DCHECK(linker_patches->empty());
1302 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001303 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001304 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001305 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001306 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01001307 public_type_bss_entry_patches_.size() +
1308 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001309 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001310 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001311 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01001312 boot_image_other_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001313 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01001314 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001315 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1316 boot_image_method_patches_, linker_patches);
1317 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1318 boot_image_type_patches_, linker_patches);
1319 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001320 boot_image_string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001321 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001322 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001323 DCHECK(boot_image_type_patches_.empty());
1324 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01001325 }
1326 if (GetCompilerOptions().IsBootImage()) {
1327 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1328 boot_image_other_patches_, linker_patches);
1329 } else {
1330 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
1331 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001332 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001333 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1334 method_bss_entry_patches_, linker_patches);
1335 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1336 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01001337 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
1338 public_type_bss_entry_patches_, linker_patches);
1339 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
1340 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001341 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1342 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001343 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
1344 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001345 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001346}
1347
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001348void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001349 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001350}
1351
1352void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001353 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001354}
1355
Vladimir Markoa0431112018-06-25 09:32:54 +01001356const X86_64InstructionSetFeatures& CodeGeneratorX86_64::GetInstructionSetFeatures() const {
1357 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86_64InstructionSetFeatures();
1358}
1359
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001360size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1361 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1362 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001363}
1364
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001365size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1366 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1367 return kX86_64WordSize;
1368}
1369
1370size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001371 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001372 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001373 } else {
1374 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1375 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001376 return GetSlowPathFPWidth();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001377}
1378
1379size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001380 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001381 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001382 } else {
1383 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1384 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001385 return GetSlowPathFPWidth();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001386}
1387
Calin Juravle175dc732015-08-25 15:42:32 +01001388void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1389 HInstruction* instruction,
1390 uint32_t dex_pc,
1391 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001392 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001393 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1394 if (EntrypointRequiresStackMap(entrypoint)) {
1395 RecordPcInfo(instruction, dex_pc, slow_path);
1396 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001397}
1398
Roland Levillaindec8f632016-07-22 17:10:06 +01001399void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1400 HInstruction* instruction,
1401 SlowPathCode* slow_path) {
1402 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001403 GenerateInvokeRuntime(entry_point_offset);
1404}
1405
1406void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001407 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip= */ true));
Roland Levillaindec8f632016-07-22 17:10:06 +01001408}
1409
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001410static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001411// Use a fake return address register to mimic Quick.
1412static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001413CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001414 const CompilerOptions& compiler_options,
1415 OptimizingCompilerStats* stats)
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001416 : CodeGenerator(graph,
1417 kNumberOfCpuRegisters,
1418 kNumberOfFloatRegisters,
1419 kNumberOfCpuRegisterPairs,
1420 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1421 arraysize(kCoreCalleeSaves))
1422 | (1 << kFakeReturnRegister),
1423 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1424 arraysize(kFpuCalleeSaves)),
1425 compiler_options,
1426 stats),
1427 block_labels_(nullptr),
1428 location_builder_(graph, this),
1429 instruction_visitor_(graph, this),
1430 move_resolver_(graph->GetAllocator(), this),
1431 assembler_(graph->GetAllocator()),
1432 constant_area_start_(0),
1433 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1434 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1435 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1436 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1437 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1438 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1439 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1440 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1441 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1442 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1443 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1444 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1445 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001446 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1447}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001448
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001449InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1450 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001451 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001452 assembler_(codegen->GetAssembler()),
1453 codegen_(codegen) {}
1454
David Brazdil58282f42016-01-14 12:45:10 +00001455void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001456 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001457 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001458
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001459 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001460 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001461}
1462
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001463static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001464 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001465}
David Srbecky9d8606d2015-04-12 09:35:32 +01001466
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001467static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001468 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001469}
1470
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001471void CodeGeneratorX86_64::MaybeIncrementHotness(bool is_frame_entry) {
1472 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1473 NearLabel overflow;
1474 Register method = kMethodRegisterArgument;
1475 if (!is_frame_entry) {
1476 CHECK(RequiresCurrentMethod());
1477 method = TMP;
1478 __ movq(CpuRegister(method), Address(CpuRegister(RSP), kCurrentMethodStackOffset));
1479 }
1480 __ cmpw(Address(CpuRegister(method), ArtMethod::HotnessCountOffset().Int32Value()),
1481 Immediate(ArtMethod::MaxCounter()));
1482 __ j(kEqual, &overflow);
1483 __ addw(Address(CpuRegister(method), ArtMethod::HotnessCountOffset().Int32Value()),
1484 Immediate(1));
1485 __ Bind(&overflow);
1486 }
1487
1488 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001489 ScopedProfilingInfoUse spiu(
1490 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1491 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001492 if (info != nullptr) {
1493 uint64_t address = reinterpret_cast64<uint64_t>(info);
1494 NearLabel done;
1495 __ movq(CpuRegister(TMP), Immediate(address));
1496 __ addw(Address(CpuRegister(TMP), ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1497 Immediate(1));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001498 __ andw(Address(CpuRegister(TMP), ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1499 Immediate(interpreter::kTieredHotnessMask));
1500 __ j(kNotZero, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001501 if (HasEmptyFrame()) {
1502 CHECK(is_frame_entry);
1503 // Frame alignment, and the stub expects the method on the stack.
1504 __ pushq(CpuRegister(RDI));
1505 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1506 __ cfi().RelOffset(DWARFReg(RDI), 0);
1507 } else if (!RequiresCurrentMethod()) {
1508 CHECK(is_frame_entry);
1509 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
1510 }
1511 GenerateInvokeRuntime(
1512 GetThreadOffset<kX86_64PointerSize>(kQuickCompileOptimized).Int32Value());
1513 if (HasEmptyFrame()) {
1514 __ popq(CpuRegister(RDI));
1515 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1516 __ cfi().Restore(DWARFReg(RDI));
1517 }
1518 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001519 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001520 }
1521}
1522
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001523void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001524 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001525 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001526 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001527 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001528 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001529
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001530
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001531 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001532 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1533 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001534 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001535 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001536
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001537 if (!HasEmptyFrame()) {
1538 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1539 Register reg = kCoreCalleeSaves[i];
1540 if (allocated_registers_.ContainsCoreRegister(reg)) {
1541 __ pushq(CpuRegister(reg));
1542 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1543 __ cfi().RelOffset(DWARFReg(reg), 0);
1544 }
1545 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001546
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001547 int adjust = GetFrameSize() - GetCoreSpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001548 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001549 uint32_t xmm_spill_location = GetFpuSpillStart();
1550 size_t xmm_spill_slot_size = GetCalleePreservedFPWidth();
1551
1552 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1553 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1554 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1555 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1556 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
1557 }
1558 }
1559
1560 // Save the current method if we need it. Note that we do not
1561 // do this in HCurrentMethod, as the instruction might have been removed
1562 // in the SSA graph.
1563 if (RequiresCurrentMethod()) {
1564 CHECK(!HasEmptyFrame());
1565 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1566 CpuRegister(kMethodRegisterArgument));
1567 }
1568
1569 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1570 CHECK(!HasEmptyFrame());
1571 // Initialize should_deoptimize flag to 0.
1572 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Nicolas Geoffray98893962015-01-21 12:32:32 +00001573 }
1574 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001575
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001576 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001577}
1578
1579void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001580 __ cfi().RememberState();
1581 if (!HasEmptyFrame()) {
1582 uint32_t xmm_spill_location = GetFpuSpillStart();
Artem Serov6a0b6572019-07-26 20:38:37 +01001583 size_t xmm_spill_slot_size = GetCalleePreservedFPWidth();
David Srbeckyc34dc932015-04-12 09:27:43 +01001584 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1585 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1586 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1587 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1588 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1589 }
1590 }
1591
1592 int adjust = GetFrameSize() - GetCoreSpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001593 DecreaseFrame(adjust);
David Srbeckyc34dc932015-04-12 09:27:43 +01001594
1595 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1596 Register reg = kCoreCalleeSaves[i];
1597 if (allocated_registers_.ContainsCoreRegister(reg)) {
1598 __ popq(CpuRegister(reg));
1599 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1600 __ cfi().Restore(DWARFReg(reg));
1601 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001602 }
1603 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001604 __ ret();
1605 __ cfi().RestoreState();
1606 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001607}
1608
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001609void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1610 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001611}
1612
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001613void CodeGeneratorX86_64::Move(Location destination, Location source) {
1614 if (source.Equals(destination)) {
1615 return;
1616 }
1617 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001618 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001619 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001620 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001621 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001622 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001623 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001624 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1625 } else if (source.IsConstant()) {
1626 HConstant* constant = source.GetConstant();
1627 if (constant->IsLongConstant()) {
1628 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1629 } else {
1630 Load32BitValue(dest, GetInt32ValueOf(constant));
1631 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632 } else {
1633 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001634 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001635 }
1636 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001637 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001638 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001639 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001640 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001641 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1642 } else if (source.IsConstant()) {
1643 HConstant* constant = source.GetConstant();
1644 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1645 if (constant->IsFloatConstant()) {
1646 Load32BitValue(dest, static_cast<int32_t>(value));
1647 } else {
1648 Load64BitValue(dest, value);
1649 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001651 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001652 } else {
1653 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001654 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001655 }
1656 } else if (destination.IsStackSlot()) {
1657 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001658 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001660 } else if (source.IsFpuRegister()) {
1661 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001663 } else if (source.IsConstant()) {
1664 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001665 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001666 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001668 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001669 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1670 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671 }
1672 } else {
1673 DCHECK(destination.IsDoubleStackSlot());
1674 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001675 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001676 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001677 } else if (source.IsFpuRegister()) {
1678 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001680 } else if (source.IsConstant()) {
1681 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001682 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1683 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001684 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001685 } else {
1686 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001687 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1688 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001689 }
1690 }
1691}
1692
Calin Juravle175dc732015-08-25 15:42:32 +01001693void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1694 DCHECK(location.IsRegister());
1695 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1696}
1697
Calin Juravlee460d1d2015-09-29 04:52:17 +01001698void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001700 Move(dst, src);
1701}
1702
1703void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1704 if (location.IsRegister()) {
1705 locations->AddTemp(location);
1706 } else {
1707 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1708 }
1709}
1710
David Brazdilfc6a86a2015-06-26 10:33:45 +00001711void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001712 if (successor->IsExitBlock()) {
1713 DCHECK(got->GetPrevious()->AlwaysThrows());
1714 return; // no code needed
1715 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001716
1717 HBasicBlock* block = got->GetBlock();
1718 HInstruction* previous = got->GetPrevious();
1719
1720 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001721 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001722 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001723 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1724 return;
1725 }
1726
1727 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1728 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1729 }
1730 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001731 __ jmp(codegen_->GetLabelOf(successor));
1732 }
1733}
1734
David Brazdilfc6a86a2015-06-26 10:33:45 +00001735void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1736 got->SetLocations(nullptr);
1737}
1738
1739void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1740 HandleGoto(got, got->GetSuccessor());
1741}
1742
1743void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1744 try_boundary->SetLocations(nullptr);
1745}
1746
1747void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1748 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1749 if (!successor->IsExitBlock()) {
1750 HandleGoto(try_boundary, successor);
1751 }
1752}
1753
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001754void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1755 exit->SetLocations(nullptr);
1756}
1757
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001758void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001759}
1760
Mark Mendell152408f2015-12-31 12:28:50 -05001761template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001762void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001763 LabelType* true_label,
1764 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001765 if (cond->IsFPConditionTrueIfNaN()) {
1766 __ j(kUnordered, true_label);
1767 } else if (cond->IsFPConditionFalseIfNaN()) {
1768 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001769 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001770 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001771}
1772
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001773void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001774 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001775
Mark Mendellc4701932015-04-10 13:18:51 -04001776 Location left = locations->InAt(0);
1777 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001779 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001780 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001781 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 case DataType::Type::kInt8:
1783 case DataType::Type::kUint16:
1784 case DataType::Type::kInt16:
1785 case DataType::Type::kInt32:
1786 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001787 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001788 break;
1789 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001790 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001791 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001792 break;
1793 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001794 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001795 if (right.IsFpuRegister()) {
1796 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1797 } else if (right.IsConstant()) {
1798 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1799 codegen_->LiteralFloatAddress(
1800 right.GetConstant()->AsFloatConstant()->GetValue()));
1801 } else {
1802 DCHECK(right.IsStackSlot());
1803 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1804 Address(CpuRegister(RSP), right.GetStackIndex()));
1805 }
Mark Mendellc4701932015-04-10 13:18:51 -04001806 break;
1807 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001809 if (right.IsFpuRegister()) {
1810 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1811 } else if (right.IsConstant()) {
1812 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1813 codegen_->LiteralDoubleAddress(
1814 right.GetConstant()->AsDoubleConstant()->GetValue()));
1815 } else {
1816 DCHECK(right.IsDoubleStackSlot());
1817 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1818 Address(CpuRegister(RSP), right.GetStackIndex()));
1819 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001820 break;
1821 }
1822 default:
1823 LOG(FATAL) << "Unexpected condition type " << type;
1824 }
1825}
1826
1827template<class LabelType>
1828void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1829 LabelType* true_target_in,
1830 LabelType* false_target_in) {
1831 // Generated branching requires both targets to be explicit. If either of the
1832 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1833 LabelType fallthrough_target;
1834 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1835 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1836
1837 // Generate the comparison to set the CC.
1838 GenerateCompareTest(condition);
1839
1840 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001842 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001843 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001844 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1845 break;
1846 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001848 GenerateFPJumps(condition, true_target, false_target);
1849 break;
1850 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001851 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001852 GenerateFPJumps(condition, true_target, false_target);
1853 break;
1854 }
1855 default:
1856 LOG(FATAL) << "Unexpected condition type " << type;
1857 }
1858
David Brazdil0debae72015-11-12 18:37:00 +00001859 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001860 __ jmp(false_target);
1861 }
David Brazdil0debae72015-11-12 18:37:00 +00001862
1863 if (fallthrough_target.IsLinked()) {
1864 __ Bind(&fallthrough_target);
1865 }
Mark Mendellc4701932015-04-10 13:18:51 -04001866}
1867
David Brazdil0debae72015-11-12 18:37:00 +00001868static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1869 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1870 // are set only strictly before `branch`. We can't use the eflags on long
1871 // conditions if they are materialized due to the complex branching.
1872 return cond->IsCondition() &&
1873 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001874 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001875}
1876
Mark Mendell152408f2015-12-31 12:28:50 -05001877template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001878void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001879 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001880 LabelType* true_target,
1881 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001882 HInstruction* cond = instruction->InputAt(condition_input_index);
1883
1884 if (true_target == nullptr && false_target == nullptr) {
1885 // Nothing to do. The code always falls through.
1886 return;
1887 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001888 // Constant condition, statically compared against "true" (integer value 1).
1889 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001890 if (true_target != nullptr) {
1891 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001892 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001893 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001894 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001895 if (false_target != nullptr) {
1896 __ jmp(false_target);
1897 }
1898 }
1899 return;
1900 }
1901
1902 // The following code generates these patterns:
1903 // (1) true_target == nullptr && false_target != nullptr
1904 // - opposite condition true => branch to false_target
1905 // (2) true_target != nullptr && false_target == nullptr
1906 // - condition true => branch to true_target
1907 // (3) true_target != nullptr && false_target != nullptr
1908 // - condition true => branch to true_target
1909 // - branch to false_target
1910 if (IsBooleanValueOrMaterializedCondition(cond)) {
1911 if (AreEflagsSetFrom(cond, instruction)) {
1912 if (true_target == nullptr) {
1913 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1914 } else {
1915 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1916 }
1917 } else {
1918 // Materialized condition, compare against 0.
1919 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1920 if (lhs.IsRegister()) {
1921 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1922 } else {
1923 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1924 }
1925 if (true_target == nullptr) {
1926 __ j(kEqual, false_target);
1927 } else {
1928 __ j(kNotEqual, true_target);
1929 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001930 }
1931 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001932 // Condition has not been materialized, use its inputs as the
1933 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001934 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001935
David Brazdil0debae72015-11-12 18:37:00 +00001936 // If this is a long or FP comparison that has been folded into
1937 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 DataType::Type type = condition->InputAt(0)->GetType();
1939 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001940 GenerateCompareTestAndBranch(condition, true_target, false_target);
1941 return;
1942 }
1943
1944 Location lhs = condition->GetLocations()->InAt(0);
1945 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001946 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001947 if (true_target == nullptr) {
1948 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1949 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001950 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001951 }
Dave Allison20dfc792014-06-16 20:44:29 -07001952 }
David Brazdil0debae72015-11-12 18:37:00 +00001953
1954 // If neither branch falls through (case 3), the conditional branch to `true_target`
1955 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1956 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001957 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001958 }
1959}
1960
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001961void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001962 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001963 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001964 locations->SetInAt(0, Location::Any());
1965 }
1966}
1967
1968void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001969 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1970 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1971 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1972 nullptr : codegen_->GetLabelOf(true_successor);
1973 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1974 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001975 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001976}
1977
1978void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001979 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001980 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001981 InvokeRuntimeCallingConvention calling_convention;
1982 RegisterSet caller_saves = RegisterSet::Empty();
1983 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1984 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001985 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001986 locations->SetInAt(0, Location::Any());
1987 }
1988}
1989
1990void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001991 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001992 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001993 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001994 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001995 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001996}
1997
Mingyao Yang063fc772016-08-02 11:02:54 -07001998void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001999 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002000 LocationSummary(flag, LocationSummary::kNoCall);
2001 locations->SetOut(Location::RequiresRegister());
2002}
2003
2004void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2005 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
2006 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2007}
2008
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002009static bool SelectCanUseCMOV(HSelect* select) {
2010 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002012 return false;
2013 }
2014
2015 // A FP condition doesn't generate the single CC that we need.
2016 HInstruction* condition = select->GetCondition();
2017 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002018 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002019 return false;
2020 }
2021
2022 // We can generate a CMOV for this Select.
2023 return true;
2024}
2025
David Brazdil74eb1b22015-12-14 11:44:01 +00002026void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002027 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002029 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002030 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002031 } else {
2032 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002033 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05002034 if (select->InputAt(1)->IsConstant()) {
2035 locations->SetInAt(1, Location::RequiresRegister());
2036 } else {
2037 locations->SetInAt(1, Location::Any());
2038 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002039 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002040 locations->SetInAt(1, Location::Any());
2041 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002042 }
2043 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2044 locations->SetInAt(2, Location::RequiresRegister());
2045 }
2046 locations->SetOut(Location::SameAsFirstInput());
2047}
2048
2049void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
2050 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002051 if (SelectCanUseCMOV(select)) {
2052 // If both the condition and the source types are integer, we can generate
2053 // a CMOV to implement Select.
2054 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05002055 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002056 DCHECK(locations->InAt(0).Equals(locations->Out()));
2057
2058 HInstruction* select_condition = select->GetCondition();
2059 Condition cond = kNotEqual;
2060
2061 // Figure out how to test the 'condition'.
2062 if (select_condition->IsCondition()) {
2063 HCondition* condition = select_condition->AsCondition();
2064 if (!condition->IsEmittedAtUseSite()) {
2065 // This was a previously materialized condition.
2066 // Can we use the existing condition code?
2067 if (AreEflagsSetFrom(condition, select)) {
2068 // Materialization was the previous instruction. Condition codes are right.
2069 cond = X86_64IntegerCondition(condition->GetCondition());
2070 } else {
2071 // No, we have to recreate the condition code.
2072 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
2073 __ testl(cond_reg, cond_reg);
2074 }
2075 } else {
2076 GenerateCompareTest(condition);
2077 cond = X86_64IntegerCondition(condition->GetCondition());
2078 }
2079 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002080 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002081 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
2082 __ testl(cond_reg, cond_reg);
2083 }
2084
2085 // If the condition is true, overwrite the output, which already contains false.
2086 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002087 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05002088 if (value_true_loc.IsRegister()) {
2089 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
2090 } else {
2091 __ cmov(cond,
2092 value_false,
2093 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
2094 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002095 } else {
2096 NearLabel false_target;
2097 GenerateTestAndBranch<NearLabel>(select,
Andreas Gampe3db70682018-12-26 15:12:03 -08002098 /* condition_input_index= */ 2,
2099 /* true_target= */ nullptr,
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002100 &false_target);
2101 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2102 __ Bind(&false_target);
2103 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002104}
2105
David Srbecky0cf44932015-12-09 14:09:59 +00002106void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002107 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002108}
2109
David Srbeckyd28f4a02016-03-14 17:14:24 +00002110void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2111 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002112}
2113
Vladimir Markodec78172020-06-19 15:31:23 +01002114void CodeGeneratorX86_64::IncreaseFrame(size_t adjustment) {
2115 __ subq(CpuRegister(RSP), Immediate(adjustment));
2116 __ cfi().AdjustCFAOffset(adjustment);
2117}
2118
2119void CodeGeneratorX86_64::DecreaseFrame(size_t adjustment) {
2120 __ addq(CpuRegister(RSP), Immediate(adjustment));
2121 __ cfi().AdjustCFAOffset(-adjustment);
2122}
2123
David Srbeckyc7098ff2016-02-09 14:30:11 +00002124void CodeGeneratorX86_64::GenerateNop() {
2125 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002126}
2127
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002130 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002131 // Handle the long/FP comparisons made in instruction simplification.
2132 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002134 locations->SetInAt(0, Location::RequiresRegister());
2135 locations->SetInAt(1, Location::Any());
2136 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 case DataType::Type::kFloat32:
2138 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04002139 locations->SetInAt(0, Location::RequiresFpuRegister());
2140 locations->SetInAt(1, Location::Any());
2141 break;
2142 default:
2143 locations->SetInAt(0, Location::RequiresRegister());
2144 locations->SetInAt(1, Location::Any());
2145 break;
2146 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002147 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002148 locations->SetOut(Location::RequiresRegister());
2149 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150}
2151
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002152void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002153 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002154 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002155 }
Mark Mendellc4701932015-04-10 13:18:51 -04002156
2157 LocationSummary* locations = cond->GetLocations();
2158 Location lhs = locations->InAt(0);
2159 Location rhs = locations->InAt(1);
2160 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05002161 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002162
2163 switch (cond->InputAt(0)->GetType()) {
2164 default:
2165 // Integer case.
2166
2167 // Clear output register: setcc only sets the low byte.
2168 __ xorl(reg, reg);
2169
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002170 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002171 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002172 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002174 // Clear output register: setcc only sets the low byte.
2175 __ xorl(reg, reg);
2176
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002177 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002178 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002179 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002180 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04002181 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2182 if (rhs.IsConstant()) {
2183 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
2184 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
2185 } else if (rhs.IsStackSlot()) {
2186 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2187 } else {
2188 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2189 }
2190 GenerateFPJumps(cond, &true_label, &false_label);
2191 break;
2192 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002194 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2195 if (rhs.IsConstant()) {
2196 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
2197 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
2198 } else if (rhs.IsDoubleStackSlot()) {
2199 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2200 } else {
2201 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2202 }
2203 GenerateFPJumps(cond, &true_label, &false_label);
2204 break;
2205 }
2206 }
2207
2208 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002209 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002210
Roland Levillain4fa13f62015-07-06 18:11:54 +01002211 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002212 __ Bind(&false_label);
2213 __ xorl(reg, reg);
2214 __ jmp(&done_label);
2215
Roland Levillain4fa13f62015-07-06 18:11:54 +01002216 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002217 __ Bind(&true_label);
2218 __ movl(reg, Immediate(1));
2219 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002220}
2221
2222void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002223 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002224}
2225
2226void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002227 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002228}
2229
2230void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002231 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002232}
2233
2234void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002235 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002236}
2237
2238void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002239 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002240}
2241
2242void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002243 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002244}
2245
2246void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002247 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002248}
2249
2250void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002251 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002252}
2253
2254void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002255 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002256}
2257
2258void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002259 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002260}
2261
2262void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002263 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002264}
2265
2266void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002267 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002268}
2269
Aart Bike9f37602015-10-09 11:15:55 -07002270void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002271 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002272}
2273
2274void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002275 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002276}
2277
2278void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002279 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002280}
2281
2282void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002283 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002284}
2285
2286void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002287 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002288}
2289
2290void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002291 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002292}
2293
2294void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002295 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002296}
2297
2298void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002299 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002300}
2301
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002302void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002303 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002304 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002305 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002307 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002310 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 case DataType::Type::kInt32:
2312 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002313 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002314 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2316 break;
2317 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 case DataType::Type::kFloat32:
2319 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002320 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002321 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002322 locations->SetOut(Location::RequiresRegister());
2323 break;
2324 }
2325 default:
2326 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2327 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002328}
2329
2330void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002331 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002333 Location left = locations->InAt(0);
2334 Location right = locations->InAt(1);
2335
Mark Mendell0c9497d2015-08-21 09:30:05 -04002336 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002338 Condition less_cond = kLess;
2339
Calin Juravleddb7df22014-11-25 20:56:51 +00002340 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002342 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002345 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002347 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002348 break;
2349 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002350 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002351 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002352 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002353 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002355 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2356 if (right.IsConstant()) {
2357 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2358 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2359 } else if (right.IsStackSlot()) {
2360 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2361 } else {
2362 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2363 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002364 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002365 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002366 break;
2367 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002369 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2370 if (right.IsConstant()) {
2371 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2372 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2373 } else if (right.IsDoubleStackSlot()) {
2374 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2375 } else {
2376 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2377 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002378 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002379 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002380 break;
2381 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002382 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002383 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002384 }
Aart Bika19616e2016-02-01 18:57:58 -08002385
Calin Juravleddb7df22014-11-25 20:56:51 +00002386 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002387 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002388 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002389
Calin Juravle91debbc2014-11-26 19:01:09 +00002390 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002391 __ movl(out, Immediate(1));
2392 __ jmp(&done);
2393
2394 __ Bind(&less);
2395 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002396
2397 __ Bind(&done);
2398}
2399
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002400void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002401 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002402 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002403 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002404}
2405
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002406void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002407 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002408}
2409
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002410void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2411 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002412 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002413 locations->SetOut(Location::ConstantLocation(constant));
2414}
2415
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002416void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002417 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002418}
2419
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002421 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002422 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002423 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002424}
2425
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002426void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002427 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002428}
2429
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002430void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2431 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002432 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002433 locations->SetOut(Location::ConstantLocation(constant));
2434}
2435
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002436void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002437 // Will be generated at use site.
2438}
2439
2440void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2441 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002442 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002443 locations->SetOut(Location::ConstantLocation(constant));
2444}
2445
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002446void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2447 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002448 // Will be generated at use site.
2449}
2450
Igor Murashkind01745e2017-04-05 16:40:31 -07002451void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2452 constructor_fence->SetLocations(nullptr);
2453}
2454
2455void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2456 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2457 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2458}
2459
Calin Juravle27df7582015-04-17 19:12:31 +01002460void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2461 memory_barrier->SetLocations(nullptr);
2462}
2463
2464void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002465 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002466}
2467
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002468void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2469 ret->SetLocations(nullptr);
2470}
2471
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002472void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002473 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002474}
2475
2476void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002477 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002478 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002479 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002480 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002482 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 case DataType::Type::kInt8:
2484 case DataType::Type::kUint16:
2485 case DataType::Type::kInt16:
2486 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002488 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002489 break;
2490
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 case DataType::Type::kFloat32:
2492 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002493 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002494 break;
2495
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002496 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002497 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002498 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002499}
2500
2501void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002502 switch (ret->InputAt(0)->GetType()) {
2503 case DataType::Type::kReference:
2504 case DataType::Type::kBool:
2505 case DataType::Type::kUint8:
2506 case DataType::Type::kInt8:
2507 case DataType::Type::kUint16:
2508 case DataType::Type::kInt16:
2509 case DataType::Type::kInt32:
2510 case DataType::Type::kInt64:
2511 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
2512 break;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002513
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002514 case DataType::Type::kFloat32: {
2515 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
2516 XMM0);
2517 // To simplify callers of an OSR method, we put the return value in both
2518 // floating point and core register.
2519 if (GetGraph()->IsCompilingOsr()) {
2520 __ movd(CpuRegister(RAX), XmmRegister(XMM0), /* is64bit= */ false);
2521 }
2522 break;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002523 }
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002524 case DataType::Type::kFloat64: {
2525 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
2526 XMM0);
2527 // To simplify callers of an OSR method, we put the return value in both
2528 // floating point and core register.
2529 if (GetGraph()->IsCompilingOsr()) {
2530 __ movd(CpuRegister(RAX), XmmRegister(XMM0), /* is64bit= */ true);
2531 }
2532 break;
2533 }
2534
2535 default:
2536 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002537 }
2538 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002539}
2540
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002542 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002543 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002545 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kInt8:
2547 case DataType::Type::kUint16:
2548 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002549 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002551 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002553 return Location::RegisterLocation(RAX);
2554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002556 return Location::NoLocation();
2557
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002558 case DataType::Type::kFloat64:
2559 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002560 return Location::FpuRegisterLocation(XMM0);
2561 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002562
2563 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002564}
2565
2566Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2567 return Location::RegisterLocation(kMethodRegisterArgument);
2568}
2569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002571 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002572 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002573 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002574 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 case DataType::Type::kInt8:
2576 case DataType::Type::kUint16:
2577 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002578 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002579 uint32_t index = gp_index_++;
2580 stack_index_++;
2581 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002582 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002583 } else {
2584 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2585 }
2586 }
2587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002589 uint32_t index = gp_index_;
2590 stack_index_ += 2;
2591 if (index < calling_convention.GetNumberOfRegisters()) {
2592 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002593 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002594 } else {
2595 gp_index_ += 2;
2596 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2597 }
2598 }
2599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002601 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002602 stack_index_++;
2603 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002604 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002605 } else {
2606 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2607 }
2608 }
2609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002611 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002612 stack_index_ += 2;
2613 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002614 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002615 } else {
2616 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2617 }
2618 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002619
Aart Bik66c158e2018-01-31 12:55:04 -08002620 case DataType::Type::kUint32:
2621 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002623 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002624 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002625 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002626 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002627}
2628
Vladimir Marko86c87522020-05-11 16:55:55 +01002629Location CriticalNativeCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
2630 DCHECK_NE(type, DataType::Type::kReference);
2631
2632 Location location = Location::NoLocation();
2633 if (DataType::IsFloatingPointType(type)) {
2634 if (fpr_index_ < kParameterFloatRegistersLength) {
2635 location = Location::FpuRegisterLocation(kParameterFloatRegisters[fpr_index_]);
2636 ++fpr_index_;
2637 }
2638 } else {
2639 // Native ABI uses the same registers as managed, except that the method register RDI
2640 // is a normal argument.
2641 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
2642 location = Location::RegisterLocation(
2643 gpr_index_ == 0u ? RDI : kParameterCoreRegisters[gpr_index_ - 1u]);
2644 ++gpr_index_;
2645 }
2646 }
2647 if (location.IsInvalid()) {
2648 if (DataType::Is64BitType(type)) {
2649 location = Location::DoubleStackSlot(stack_offset_);
2650 } else {
2651 location = Location::StackSlot(stack_offset_);
2652 }
2653 stack_offset_ += kFramePointerSize;
2654
2655 if (for_register_allocation_) {
2656 location = Location::Any();
2657 }
2658 }
2659 return location;
2660}
2661
2662Location CriticalNativeCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type)
2663 const {
2664 // We perform conversion to the managed ABI return register after the call if needed.
2665 InvokeDexCallingConventionVisitorX86_64 dex_calling_convention;
2666 return dex_calling_convention.GetReturnLocation(type);
2667}
2668
2669Location CriticalNativeCallingConventionVisitorX86_64::GetMethodLocation() const {
2670 // Pass the method in the hidden argument RAX.
2671 return Location::RegisterLocation(RAX);
2672}
2673
Calin Juravle175dc732015-08-25 15:42:32 +01002674void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2675 // The trampoline uses the same calling convention as dex calling conventions,
2676 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2677 // the method_idx.
2678 HandleInvoke(invoke);
2679}
2680
2681void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2682 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2683}
2684
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002685void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002686 // Explicit clinit checks triggered by static invokes must have been pruned by
2687 // art::PrepareForRegisterAllocation.
2688 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002689
Mark Mendellfb8d2792015-03-31 22:16:59 -04002690 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002691 if (intrinsic.TryDispatch(invoke)) {
2692 return;
2693 }
2694
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002695 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002696 CriticalNativeCallingConventionVisitorX86_64 calling_convention_visitor(
2697 /*for_register_allocation=*/ true);
2698 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2699 CodeGeneratorX86_64::BlockNonVolatileXmmRegisters(invoke->GetLocations());
2700 } else {
2701 HandleInvoke(invoke);
2702 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002703}
2704
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002705static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2706 if (invoke->GetLocations()->Intrinsified()) {
2707 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2708 intrinsic.Dispatch(invoke);
2709 return true;
2710 }
2711 return false;
2712}
2713
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002714void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002715 // Explicit clinit checks triggered by static invokes must have been pruned by
2716 // art::PrepareForRegisterAllocation.
2717 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002718
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002719 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2720 return;
2721 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002722
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002723 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002724 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002725 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002726}
2727
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002728void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002729 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002730 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002731}
2732
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002733void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002734 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002735 if (intrinsic.TryDispatch(invoke)) {
2736 return;
2737 }
2738
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002739 HandleInvoke(invoke);
2740}
2741
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002742void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002743 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2744 return;
2745 }
2746
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002747 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002748 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002749}
2750
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002751void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2752 HandleInvoke(invoke);
2753 // Add the hidden argument.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002754 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2755 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2756 Location::RegisterLocation(RAX));
2757 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002758 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2759}
2760
Nicolas Geoffray20036d82019-11-28 16:15:00 +00002761void CodeGeneratorX86_64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
2762 CpuRegister klass) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002763 DCHECK_EQ(RDI, klass.AsRegister());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00002764 // We know the destination of an intrinsic, so no need to record inline
2765 // caches.
2766 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002767 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00002768 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002769 ScopedProfilingInfoUse spiu(
2770 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2771 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002772 if (info != nullptr) {
2773 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2774 uint64_t address = reinterpret_cast64<uint64_t>(cache);
2775 NearLabel done;
2776 __ movq(CpuRegister(TMP), Immediate(address));
2777 // Fast path for a monomorphic cache.
2778 __ cmpl(Address(CpuRegister(TMP), InlineCache::ClassesOffset().Int32Value()), klass);
2779 __ j(kEqual, &done);
2780 GenerateInvokeRuntime(
2781 GetThreadOffset<kX86_64PointerSize>(kQuickUpdateInlineCache).Int32Value());
2782 __ Bind(&done);
2783 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002784 }
2785}
2786
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002787void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2788 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002789 LocationSummary* locations = invoke->GetLocations();
2790 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002791 Location receiver = locations->InAt(0);
2792 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2793
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002794 if (receiver.IsStackSlot()) {
2795 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002796 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002797 __ movl(temp, Address(temp, class_offset));
2798 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002799 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002801 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002802 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002803 // Instead of simply (possibly) unpoisoning `temp` here, we should
2804 // emit a read barrier for the previous class reference load.
2805 // However this is not required in practice, as this is an
2806 // intermediate/temporary reference and because the current
2807 // concurrent copying collector keeps the from-space memory
2808 // intact/accessible until the end of the marking phase (the
2809 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002810 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002811
Nicolas Geoffray20036d82019-11-28 16:15:00 +00002812 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002813
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002814 if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRecursive &&
2815 invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002816 Location hidden_reg = locations->GetTemp(1);
2817 // Set the hidden argument. This is safe to do this here, as RAX
2818 // won't be modified thereafter, before the `call` instruction.
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002819 // We also do it after MaybeGenerateInlineCache that may use RAX.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002820 DCHECK_EQ(RAX, hidden_reg.AsRegister<Register>());
2821 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), hidden_reg, invoke);
2822 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002823
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002824 // temp = temp->GetAddressOfIMT()
2825 __ movq(temp,
2826 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2827 // temp = temp->GetImtEntryAt(method_offset);
2828 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002829 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002830 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002831 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002832 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2833 // We pass the method from the IMT in case of a conflict. This will ensure
2834 // we go into the runtime to resolve the actual method.
2835 Location hidden_reg = locations->GetTemp(1);
2836 __ movq(hidden_reg.AsRegister<CpuRegister>(), temp);
2837 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002838 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002839 __ call(Address(
2840 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002841
2842 DCHECK(!codegen_->IsLeafMethod());
2843 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2844}
2845
Orion Hodsonac141392017-01-13 11:53:47 +00002846void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002847 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
2848 if (intrinsic.TryDispatch(invoke)) {
2849 return;
2850 }
Orion Hodsonac141392017-01-13 11:53:47 +00002851 HandleInvoke(invoke);
2852}
2853
2854void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002855 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2856 return;
2857 }
Orion Hodsonac141392017-01-13 11:53:47 +00002858 codegen_->GenerateInvokePolymorphicCall(invoke);
2859}
2860
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002861void LocationsBuilderX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2862 HandleInvoke(invoke);
2863}
2864
2865void InstructionCodeGeneratorX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2866 codegen_->GenerateInvokeCustomCall(invoke);
2867}
2868
Roland Levillain88cb1752014-10-20 16:36:47 +01002869void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2870 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002871 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002872 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 case DataType::Type::kInt32:
2874 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002875 locations->SetInAt(0, Location::RequiresRegister());
2876 locations->SetOut(Location::SameAsFirstInput());
2877 break;
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kFloat32:
2880 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002881 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002882 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002883 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002884 break;
2885
2886 default:
2887 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2888 }
2889}
2890
2891void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2892 LocationSummary* locations = neg->GetLocations();
2893 Location out = locations->Out();
2894 Location in = locations->InAt(0);
2895 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002897 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002898 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002900 break;
2901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002903 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002904 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002906 break;
2907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002909 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002910 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002911 // Implement float negation with an exclusive or with value
2912 // 0x80000000 (mask for bit 31, representing the sign of a
2913 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002914 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002915 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002916 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002917 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002920 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002921 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002922 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002923 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002924 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002925 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002927 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002928 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002929
2930 default:
2931 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2932 }
2933}
2934
Roland Levillaindff1f282014-11-05 14:15:05 +00002935void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2936 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002937 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 DataType::Type result_type = conversion->GetResultType();
2939 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002940 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2941 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002942
Roland Levillaindff1f282014-11-05 14:15:05 +00002943 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002944 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002946 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002948 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2949 locations->SetInAt(0, Location::Any());
2950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002951 break;
2952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002954 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002956 locations->SetInAt(0, Location::Any());
2957 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2958 break;
2959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002961 locations->SetInAt(0, Location::RequiresFpuRegister());
2962 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002963 break;
2964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002966 locations->SetInAt(0, Location::RequiresFpuRegister());
2967 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002968 break;
2969
2970 default:
2971 LOG(FATAL) << "Unexpected type conversion from " << input_type
2972 << " to " << result_type;
2973 }
2974 break;
2975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002977 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002979 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002980 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002981 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 case DataType::Type::kInt16:
2983 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002984 // TODO: We would benefit from a (to-be-implemented)
2985 // Location::RegisterOrStackSlot requirement for this input.
2986 locations->SetInAt(0, Location::RequiresRegister());
2987 locations->SetOut(Location::RequiresRegister());
2988 break;
2989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002991 locations->SetInAt(0, Location::RequiresFpuRegister());
2992 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002993 break;
2994
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002995 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002996 locations->SetInAt(0, Location::RequiresFpuRegister());
2997 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002998 break;
2999
3000 default:
3001 LOG(FATAL) << "Unexpected type conversion from " << input_type
3002 << " to " << result_type;
3003 }
3004 break;
3005
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003007 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003008 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003009 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003011 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt16:
3013 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003014 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00003015 locations->SetOut(Location::RequiresFpuRegister());
3016 break;
3017
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003019 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00003020 locations->SetOut(Location::RequiresFpuRegister());
3021 break;
3022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003023 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04003024 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00003025 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003026 break;
3027
3028 default:
3029 LOG(FATAL) << "Unexpected type conversion from " << input_type
3030 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003031 }
Roland Levillaincff13742014-11-17 14:32:17 +00003032 break;
3033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003035 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003037 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003039 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 case DataType::Type::kInt16:
3041 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003042 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00003043 locations->SetOut(Location::RequiresFpuRegister());
3044 break;
3045
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003047 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003048 locations->SetOut(Location::RequiresFpuRegister());
3049 break;
3050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003052 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00003053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003054 break;
3055
3056 default:
3057 LOG(FATAL) << "Unexpected type conversion from " << input_type
3058 << " to " << result_type;
3059 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003060 break;
3061
3062 default:
3063 LOG(FATAL) << "Unexpected type conversion from " << input_type
3064 << " to " << result_type;
3065 }
3066}
3067
3068void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
3069 LocationSummary* locations = conversion->GetLocations();
3070 Location out = locations->Out();
3071 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 DataType::Type result_type = conversion->GetResultType();
3073 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003074 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3075 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00003076 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003077 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003078 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003079 case DataType::Type::kInt8:
3080 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003081 case DataType::Type::kInt16:
3082 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003083 case DataType::Type::kInt64:
3084 if (in.IsRegister()) {
3085 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
3086 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
3087 __ movzxb(out.AsRegister<CpuRegister>(),
3088 Address(CpuRegister(RSP), in.GetStackIndex()));
3089 } else {
3090 __ movl(out.AsRegister<CpuRegister>(),
3091 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
3092 }
3093 break;
3094
3095 default:
3096 LOG(FATAL) << "Unexpected type conversion from " << input_type
3097 << " to " << result_type;
3098 }
3099 break;
3100
3101 case DataType::Type::kInt8:
3102 switch (input_type) {
3103 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003104 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003105 case DataType::Type::kInt16:
3106 case DataType::Type::kInt32:
3107 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003108 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003109 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00003110 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00003112 Address(CpuRegister(RSP), in.GetStackIndex()));
3113 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00003115 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00003116 }
3117 break;
3118
3119 default:
3120 LOG(FATAL) << "Unexpected type conversion from " << input_type
3121 << " to " << result_type;
3122 }
3123 break;
3124
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003125 case DataType::Type::kUint16:
3126 switch (input_type) {
3127 case DataType::Type::kInt8:
3128 case DataType::Type::kInt16:
3129 case DataType::Type::kInt32:
3130 case DataType::Type::kInt64:
3131 if (in.IsRegister()) {
3132 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
3133 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
3134 __ movzxw(out.AsRegister<CpuRegister>(),
3135 Address(CpuRegister(RSP), in.GetStackIndex()));
3136 } else {
3137 __ movl(out.AsRegister<CpuRegister>(),
3138 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
3139 }
3140 break;
3141
3142 default:
3143 LOG(FATAL) << "Unexpected type conversion from " << input_type
3144 << " to " << result_type;
3145 }
3146 break;
3147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003149 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003151 case DataType::Type::kInt32:
3152 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00003153 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003154 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00003155 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003156 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00003157 Address(CpuRegister(RSP), in.GetStackIndex()));
3158 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003159 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00003160 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00003161 }
3162 break;
3163
3164 default:
3165 LOG(FATAL) << "Unexpected type conversion from " << input_type
3166 << " to " << result_type;
3167 }
3168 break;
3169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003170 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003171 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003172 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003173 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003174 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00003175 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003176 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00003177 Address(CpuRegister(RSP), in.GetStackIndex()));
3178 } else {
3179 DCHECK(in.IsConstant());
3180 DCHECK(in.GetConstant()->IsLongConstant());
3181 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003183 }
3184 break;
3185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003186 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003187 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3188 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003189 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003190
3191 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04003192 // if input >= (float)INT_MAX goto done
Nick Desaulniers98e97c62019-10-18 14:25:19 -07003193 __ comiss(input, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Roland Levillain3f8f9362014-12-02 17:45:01 +00003194 __ j(kAboveEqual, &done);
3195 // if input == NaN goto nan
3196 __ j(kUnordered, &nan);
3197 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00003198 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00003199 __ jmp(&done);
3200 __ Bind(&nan);
3201 // output = 0
3202 __ xorl(output, output);
3203 __ Bind(&done);
3204 break;
3205 }
3206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003207 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003208 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3209 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003210 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003211
3212 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04003213 // if input >= (double)INT_MAX goto done
3214 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003215 __ j(kAboveEqual, &done);
3216 // if input == NaN goto nan
3217 __ j(kUnordered, &nan);
3218 // output = double-to-int-truncate(input)
3219 __ cvttsd2si(output, input);
3220 __ jmp(&done);
3221 __ Bind(&nan);
3222 // output = 0
3223 __ xorl(output, output);
3224 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003225 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003226 }
Roland Levillain946e1432014-11-11 17:35:19 +00003227
3228 default:
3229 LOG(FATAL) << "Unexpected type conversion from " << input_type
3230 << " to " << result_type;
3231 }
3232 break;
3233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003234 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003235 switch (input_type) {
3236 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003237 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003238 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003240 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt16:
3242 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003243 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003245 break;
3246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00003248 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3249 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003250 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00003251
Mark Mendell92e83bf2015-05-07 11:25:03 -04003252 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04003253 // if input >= (float)LONG_MAX goto done
Nick Desaulniers98e97c62019-10-18 14:25:19 -07003254 __ comiss(input, codegen_->LiteralFloatAddress(static_cast<float>(kPrimLongMax)));
Roland Levillain624279f2014-12-04 11:54:28 +00003255 __ j(kAboveEqual, &done);
3256 // if input == NaN goto nan
3257 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003258 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00003259 __ cvttss2si(output, input, true);
3260 __ jmp(&done);
3261 __ Bind(&nan);
3262 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04003263 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00003264 __ Bind(&done);
3265 break;
3266 }
3267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003268 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003269 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3270 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003271 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003272
Mark Mendell92e83bf2015-05-07 11:25:03 -04003273 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04003274 // if input >= (double)LONG_MAX goto done
Nick Desaulniers98e97c62019-10-18 14:25:19 -07003275 __ comisd(input, codegen_->LiteralDoubleAddress(
3276 static_cast<double>(kPrimLongMax)));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003277 __ j(kAboveEqual, &done);
3278 // if input == NaN goto nan
3279 __ j(kUnordered, &nan);
3280 // output = double-to-long-truncate(input)
3281 __ cvttsd2si(output, input, true);
3282 __ jmp(&done);
3283 __ Bind(&nan);
3284 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04003285 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003286 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00003287 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003288 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003289
3290 default:
3291 LOG(FATAL) << "Unexpected type conversion from " << input_type
3292 << " to " << result_type;
3293 }
3294 break;
3295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003296 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003297 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003298 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003299 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003300 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003301 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003302 case DataType::Type::kInt16:
3303 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003304 if (in.IsRegister()) {
3305 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3306 } else if (in.IsConstant()) {
3307 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3308 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003309 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003310 } else {
3311 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3312 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3313 }
Roland Levillaincff13742014-11-17 14:32:17 +00003314 break;
3315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003317 if (in.IsRegister()) {
3318 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3319 } else if (in.IsConstant()) {
3320 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3321 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06003322 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003323 } else {
3324 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3325 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3326 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003327 break;
3328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04003330 if (in.IsFpuRegister()) {
3331 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3332 } else if (in.IsConstant()) {
3333 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
3334 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003335 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003336 } else {
3337 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
3338 Address(CpuRegister(RSP), in.GetStackIndex()));
3339 }
Roland Levillaincff13742014-11-17 14:32:17 +00003340 break;
3341
3342 default:
3343 LOG(FATAL) << "Unexpected type conversion from " << input_type
3344 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003345 }
Roland Levillaincff13742014-11-17 14:32:17 +00003346 break;
3347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003348 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003349 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003350 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003351 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003352 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003353 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003354 case DataType::Type::kInt16:
3355 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003356 if (in.IsRegister()) {
3357 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3358 } else if (in.IsConstant()) {
3359 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3360 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003361 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003362 } else {
3363 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3364 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3365 }
Roland Levillaincff13742014-11-17 14:32:17 +00003366 break;
3367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003368 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003369 if (in.IsRegister()) {
3370 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3371 } else if (in.IsConstant()) {
3372 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3373 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003374 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003375 } else {
3376 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3377 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3378 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003379 break;
3380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003382 if (in.IsFpuRegister()) {
3383 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3384 } else if (in.IsConstant()) {
3385 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3386 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003387 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003388 } else {
3389 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3390 Address(CpuRegister(RSP), in.GetStackIndex()));
3391 }
Roland Levillaincff13742014-11-17 14:32:17 +00003392 break;
3393
3394 default:
3395 LOG(FATAL) << "Unexpected type conversion from " << input_type
3396 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003397 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003398 break;
3399
3400 default:
3401 LOG(FATAL) << "Unexpected type conversion from " << input_type
3402 << " to " << result_type;
3403 }
3404}
3405
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003406void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003407 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003408 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003409 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003410 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003411 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003412 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003414 break;
3415 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003418 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003419 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003420 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003422 break;
3423 }
3424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003425 case DataType::Type::kFloat64:
3426 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003427 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003428 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003429 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003430 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003431 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003432
3433 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003434 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003435 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003436}
3437
3438void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3439 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003440 Location first = locations->InAt(0);
3441 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003442 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003443
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003444 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003446 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003447 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3448 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003449 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3450 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003451 } else {
3452 __ leal(out.AsRegister<CpuRegister>(), Address(
3453 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3454 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003455 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003456 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3457 __ addl(out.AsRegister<CpuRegister>(),
3458 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3459 } else {
3460 __ leal(out.AsRegister<CpuRegister>(), Address(
3461 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3462 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003463 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003464 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003465 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003466 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003467 break;
3468 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003469
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003470 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003471 if (second.IsRegister()) {
3472 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3473 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003474 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3475 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003476 } else {
3477 __ leaq(out.AsRegister<CpuRegister>(), Address(
3478 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3479 }
3480 } else {
3481 DCHECK(second.IsConstant());
3482 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3483 int32_t int32_value = Low32Bits(value);
3484 DCHECK_EQ(int32_value, value);
3485 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3486 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3487 } else {
3488 __ leaq(out.AsRegister<CpuRegister>(), Address(
3489 first.AsRegister<CpuRegister>(), int32_value));
3490 }
3491 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003492 break;
3493 }
3494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003495 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003496 if (second.IsFpuRegister()) {
3497 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3498 } else if (second.IsConstant()) {
3499 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003500 codegen_->LiteralFloatAddress(
3501 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003502 } else {
3503 DCHECK(second.IsStackSlot());
3504 __ addss(first.AsFpuRegister<XmmRegister>(),
3505 Address(CpuRegister(RSP), second.GetStackIndex()));
3506 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003507 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003508 }
3509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003511 if (second.IsFpuRegister()) {
3512 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3513 } else if (second.IsConstant()) {
3514 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003515 codegen_->LiteralDoubleAddress(
3516 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003517 } else {
3518 DCHECK(second.IsDoubleStackSlot());
3519 __ addsd(first.AsFpuRegister<XmmRegister>(),
3520 Address(CpuRegister(RSP), second.GetStackIndex()));
3521 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003522 break;
3523 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003524
3525 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003526 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003527 }
3528}
3529
3530void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003531 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003532 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003533 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003534 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003535 locations->SetInAt(0, Location::RequiresRegister());
3536 locations->SetInAt(1, Location::Any());
3537 locations->SetOut(Location::SameAsFirstInput());
3538 break;
3539 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003541 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003542 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003543 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003544 break;
3545 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 case DataType::Type::kFloat32:
3547 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003548 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003549 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003550 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003551 break;
Calin Juravle11351682014-10-23 15:38:15 +01003552 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003553 default:
Calin Juravle11351682014-10-23 15:38:15 +01003554 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003555 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003556}
3557
3558void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3559 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003560 Location first = locations->InAt(0);
3561 Location second = locations->InAt(1);
3562 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003563 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003564 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003565 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003567 } else if (second.IsConstant()) {
3568 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003570 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003571 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003572 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003573 break;
3574 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003575 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003576 if (second.IsConstant()) {
3577 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3578 DCHECK(IsInt<32>(value));
3579 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3580 } else {
3581 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3582 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003583 break;
3584 }
3585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003586 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003587 if (second.IsFpuRegister()) {
3588 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3589 } else if (second.IsConstant()) {
3590 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003591 codegen_->LiteralFloatAddress(
3592 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003593 } else {
3594 DCHECK(second.IsStackSlot());
3595 __ subss(first.AsFpuRegister<XmmRegister>(),
3596 Address(CpuRegister(RSP), second.GetStackIndex()));
3597 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003598 break;
Calin Juravle11351682014-10-23 15:38:15 +01003599 }
3600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003601 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003602 if (second.IsFpuRegister()) {
3603 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3604 } else if (second.IsConstant()) {
3605 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003606 codegen_->LiteralDoubleAddress(
3607 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003608 } else {
3609 DCHECK(second.IsDoubleStackSlot());
3610 __ subsd(first.AsFpuRegister<XmmRegister>(),
3611 Address(CpuRegister(RSP), second.GetStackIndex()));
3612 }
Calin Juravle11351682014-10-23 15:38:15 +01003613 break;
3614 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003615
3616 default:
Calin Juravle11351682014-10-23 15:38:15 +01003617 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003618 }
3619}
3620
Calin Juravle34bacdf2014-10-07 20:23:36 +01003621void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3622 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003623 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003624 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003626 locations->SetInAt(0, Location::RequiresRegister());
3627 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003628 if (mul->InputAt(1)->IsIntConstant()) {
3629 // Can use 3 operand multiply.
3630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3631 } else {
3632 locations->SetOut(Location::SameAsFirstInput());
3633 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003634 break;
3635 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003637 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003638 locations->SetInAt(1, Location::Any());
3639 if (mul->InputAt(1)->IsLongConstant() &&
3640 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003641 // Can use 3 operand multiply.
3642 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3643 } else {
3644 locations->SetOut(Location::SameAsFirstInput());
3645 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003646 break;
3647 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003648 case DataType::Type::kFloat32:
3649 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003650 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003651 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003652 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003653 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003654 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003655
3656 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003657 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003658 }
3659}
3660
3661void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3662 LocationSummary* locations = mul->GetLocations();
3663 Location first = locations->InAt(0);
3664 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003665 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003666 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003668 // The constant may have ended up in a register, so test explicitly to avoid
3669 // problems where the output may not be the same as the first operand.
3670 if (mul->InputAt(1)->IsIntConstant()) {
3671 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3672 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3673 } else if (second.IsRegister()) {
3674 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003676 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003677 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003678 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003679 __ imull(first.AsRegister<CpuRegister>(),
3680 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003681 }
3682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003683 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003684 // The constant may have ended up in a register, so test explicitly to avoid
3685 // problems where the output may not be the same as the first operand.
3686 if (mul->InputAt(1)->IsLongConstant()) {
3687 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3688 if (IsInt<32>(value)) {
3689 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3690 Immediate(static_cast<int32_t>(value)));
3691 } else {
3692 // Have to use the constant area.
3693 DCHECK(first.Equals(out));
3694 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3695 }
3696 } else if (second.IsRegister()) {
3697 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003698 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003699 } else {
3700 DCHECK(second.IsDoubleStackSlot());
3701 DCHECK(first.Equals(out));
3702 __ imulq(first.AsRegister<CpuRegister>(),
3703 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003704 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003705 break;
3706 }
3707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003708 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003709 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003710 if (second.IsFpuRegister()) {
3711 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3712 } else if (second.IsConstant()) {
3713 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003714 codegen_->LiteralFloatAddress(
3715 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003716 } else {
3717 DCHECK(second.IsStackSlot());
3718 __ mulss(first.AsFpuRegister<XmmRegister>(),
3719 Address(CpuRegister(RSP), second.GetStackIndex()));
3720 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003721 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003722 }
3723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003724 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003725 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003726 if (second.IsFpuRegister()) {
3727 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3728 } else if (second.IsConstant()) {
3729 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003730 codegen_->LiteralDoubleAddress(
3731 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003732 } else {
3733 DCHECK(second.IsDoubleStackSlot());
3734 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3735 Address(CpuRegister(RSP), second.GetStackIndex()));
3736 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003737 break;
3738 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003739
3740 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003741 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003742 }
3743}
3744
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003745void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3746 uint32_t stack_adjustment, bool is_float) {
3747 if (source.IsStackSlot()) {
3748 DCHECK(is_float);
3749 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3750 } else if (source.IsDoubleStackSlot()) {
3751 DCHECK(!is_float);
3752 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3753 } else {
3754 // Write the value to the temporary location on the stack and load to FP stack.
3755 if (is_float) {
3756 Location stack_temp = Location::StackSlot(temp_offset);
3757 codegen_->Move(stack_temp, source);
3758 __ flds(Address(CpuRegister(RSP), temp_offset));
3759 } else {
3760 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3761 codegen_->Move(stack_temp, source);
3762 __ fldl(Address(CpuRegister(RSP), temp_offset));
3763 }
3764 }
3765}
3766
3767void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003768 DataType::Type type = rem->GetResultType();
3769 bool is_float = type == DataType::Type::kFloat32;
3770 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003771 LocationSummary* locations = rem->GetLocations();
3772 Location first = locations->InAt(0);
3773 Location second = locations->InAt(1);
3774 Location out = locations->Out();
3775
3776 // Create stack space for 2 elements.
3777 // TODO: enhance register allocator to ask for stack temporaries.
3778 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3779
3780 // Load the values to the FP stack in reverse order, using temporaries if needed.
3781 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3782 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3783
3784 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003785 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003786 __ Bind(&retry);
3787 __ fprem();
3788
3789 // Move FP status to AX.
3790 __ fstsw();
3791
3792 // And see if the argument reduction is complete. This is signaled by the
3793 // C2 FPU flag bit set to 0.
3794 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3795 __ j(kNotEqual, &retry);
3796
3797 // We have settled on the final value. Retrieve it into an XMM register.
3798 // Store FP top of stack to real stack.
3799 if (is_float) {
3800 __ fsts(Address(CpuRegister(RSP), 0));
3801 } else {
3802 __ fstl(Address(CpuRegister(RSP), 0));
3803 }
3804
3805 // Pop the 2 items from the FP stack.
3806 __ fucompp();
3807
3808 // Load the value from the stack into an XMM register.
3809 DCHECK(out.IsFpuRegister()) << out;
3810 if (is_float) {
3811 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3812 } else {
3813 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3814 }
3815
3816 // And remove the temporary stack space we allocated.
3817 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3818}
3819
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003820void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3821 DCHECK(instruction->IsDiv() || instruction->IsRem());
3822
3823 LocationSummary* locations = instruction->GetLocations();
3824 Location second = locations->InAt(1);
3825 DCHECK(second.IsConstant());
3826
3827 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3828 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003829 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003830
3831 DCHECK(imm == 1 || imm == -1);
3832
3833 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003835 if (instruction->IsRem()) {
3836 __ xorl(output_register, output_register);
3837 } else {
3838 __ movl(output_register, input_register);
3839 if (imm == -1) {
3840 __ negl(output_register);
3841 }
3842 }
3843 break;
3844 }
3845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003846 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003847 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003848 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003849 } else {
3850 __ movq(output_register, input_register);
3851 if (imm == -1) {
3852 __ negq(output_register);
3853 }
3854 }
3855 break;
3856 }
3857
3858 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003859 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003860 }
3861}
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303862void InstructionCodeGeneratorX86_64::RemByPowerOfTwo(HRem* instruction) {
3863 LocationSummary* locations = instruction->GetLocations();
3864 Location second = locations->InAt(1);
3865 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3866 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3867 int64_t imm = Int64FromConstant(second.GetConstant());
3868 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3869 uint64_t abs_imm = AbsOrMin(imm);
3870 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3871 if (instruction->GetResultType() == DataType::Type::kInt32) {
3872 NearLabel done;
3873 __ movl(out, numerator);
3874 __ andl(out, Immediate(abs_imm-1));
3875 __ j(Condition::kZero, &done);
3876 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3877 __ testl(numerator, numerator);
3878 __ cmov(Condition::kLess, out, tmp, false);
3879 __ Bind(&done);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003880
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303881 } else {
3882 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3883 codegen_->Load64BitValue(tmp, abs_imm - 1);
3884 NearLabel done;
3885
3886 __ movq(out, numerator);
3887 __ andq(out, tmp);
3888 __ j(Condition::kZero, &done);
3889 __ movq(tmp, numerator);
3890 __ sarq(tmp, Immediate(63));
3891 __ shlq(tmp, Immediate(WhichPowerOf2(abs_imm)));
3892 __ orq(out, tmp);
3893 __ Bind(&done);
3894 }
3895}
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003896void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003897 LocationSummary* locations = instruction->GetLocations();
3898 Location second = locations->InAt(1);
3899
3900 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3901 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3902
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003903 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003904 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3905 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003906
3907 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 if (instruction->GetResultType() == DataType::Type::kInt32) {
Atul Bajaj1cc73292018-11-15 11:36:53 +05303910 // When denominator is equal to 2, we can add signed bit and numerator to tmp.
3911 // Below we are using addl instruction instead of cmov which give us 1 cycle benefit.
3912 if (abs_imm == 2) {
3913 __ leal(tmp, Address(numerator, 0));
3914 __ shrl(tmp, Immediate(31));
3915 __ addl(tmp, numerator);
3916 } else {
3917 __ leal(tmp, Address(numerator, abs_imm - 1));
3918 __ testl(numerator, numerator);
3919 __ cmov(kGreaterEqual, tmp, numerator);
3920 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003921 int shift = CTZ(imm);
3922 __ sarl(tmp, Immediate(shift));
3923
3924 if (imm < 0) {
3925 __ negl(tmp);
3926 }
3927
3928 __ movl(output_register, tmp);
3929 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003930 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003931 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
Atul Bajaj1cc73292018-11-15 11:36:53 +05303932 if (abs_imm == 2) {
3933 __ movq(rdx, numerator);
3934 __ shrq(rdx, Immediate(63));
3935 __ addq(rdx, numerator);
3936 } else {
3937 codegen_->Load64BitValue(rdx, abs_imm - 1);
3938 __ addq(rdx, numerator);
3939 __ testq(numerator, numerator);
3940 __ cmov(kGreaterEqual, rdx, numerator);
3941 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003942 int shift = CTZ(imm);
3943 __ sarq(rdx, Immediate(shift));
3944
3945 if (imm < 0) {
3946 __ negq(rdx);
3947 }
3948
3949 __ movq(output_register, rdx);
3950 }
3951}
3952
3953void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3954 DCHECK(instruction->IsDiv() || instruction->IsRem());
3955
3956 LocationSummary* locations = instruction->GetLocations();
3957 Location second = locations->InAt(1);
3958
3959 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3960 : locations->GetTemp(0).AsRegister<CpuRegister>();
3961 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3962 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3963 : locations->Out().AsRegister<CpuRegister>();
3964 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3965
3966 DCHECK_EQ(RAX, eax.AsRegister());
3967 DCHECK_EQ(RDX, edx.AsRegister());
3968 if (instruction->IsDiv()) {
3969 DCHECK_EQ(RAX, out.AsRegister());
3970 } else {
3971 DCHECK_EQ(RDX, out.AsRegister());
3972 }
3973
3974 int64_t magic;
3975 int shift;
3976
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003977 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003979 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3980
Andreas Gampe3db70682018-12-26 15:12:03 -08003981 CalculateMagicAndShiftForDivRem(imm, false /* is_long= */, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003982
3983 __ movl(numerator, eax);
3984
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003985 __ movl(eax, Immediate(magic));
3986 __ imull(numerator);
3987
3988 if (imm > 0 && magic < 0) {
3989 __ addl(edx, numerator);
3990 } else if (imm < 0 && magic > 0) {
3991 __ subl(edx, numerator);
3992 }
3993
3994 if (shift != 0) {
3995 __ sarl(edx, Immediate(shift));
3996 }
3997
3998 __ movl(eax, edx);
3999 __ shrl(edx, Immediate(31));
4000 __ addl(edx, eax);
4001
4002 if (instruction->IsRem()) {
4003 __ movl(eax, numerator);
4004 __ imull(edx, Immediate(imm));
4005 __ subl(eax, edx);
4006 __ movl(edx, eax);
4007 } else {
4008 __ movl(eax, edx);
4009 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004010 } else {
4011 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
4012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004013 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004014
4015 CpuRegister rax = eax;
4016 CpuRegister rdx = edx;
4017
Andreas Gampe3db70682018-12-26 15:12:03 -08004018 CalculateMagicAndShiftForDivRem(imm, true /* is_long= */, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004019
4020 // Save the numerator.
4021 __ movq(numerator, rax);
4022
4023 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04004024 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004025
4026 // RDX:RAX = magic * numerator
4027 __ imulq(numerator);
4028
4029 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004030 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004031 __ addq(rdx, numerator);
4032 } else if (imm < 0 && magic > 0) {
4033 // RDX -= numerator
4034 __ subq(rdx, numerator);
4035 }
4036
4037 // Shift if needed.
4038 if (shift != 0) {
4039 __ sarq(rdx, Immediate(shift));
4040 }
4041
4042 // RDX += 1 if RDX < 0
4043 __ movq(rax, rdx);
4044 __ shrq(rdx, Immediate(63));
4045 __ addq(rdx, rax);
4046
4047 if (instruction->IsRem()) {
4048 __ movq(rax, numerator);
4049
4050 if (IsInt<32>(imm)) {
4051 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
4052 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004053 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004054 }
4055
4056 __ subq(rax, rdx);
4057 __ movq(rdx, rax);
4058 } else {
4059 __ movq(rax, rdx);
4060 }
4061 }
4062}
4063
Calin Juravlebacfec32014-11-14 15:54:36 +00004064void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4065 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 DataType::Type type = instruction->GetResultType();
4067 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00004068
4069 bool is_div = instruction->IsDiv();
4070 LocationSummary* locations = instruction->GetLocations();
4071
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004072 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4073 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00004074
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004076 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00004077
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004078 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004079 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00004080
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004081 if (imm == 0) {
4082 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4083 } else if (imm == 1 || imm == -1) {
4084 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05304085 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4086 if (is_div) {
4087 DivByPowerOfTwo(instruction->AsDiv());
4088 } else {
4089 RemByPowerOfTwo(instruction->AsRem());
4090 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004091 } else {
4092 DCHECK(imm <= -2 || imm >= 2);
4093 GenerateDivRemWithAnyConstant(instruction);
4094 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004095 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004096 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004097 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00004098 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004099 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004100
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004101 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4102 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
4103 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
4104 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004106 __ cmpl(second_reg, Immediate(-1));
4107 __ j(kEqual, slow_path->GetEntryLabel());
4108 // edx:eax <- sign-extended of eax
4109 __ cdq();
4110 // eax = quotient, edx = remainder
4111 __ idivl(second_reg);
4112 } else {
4113 __ cmpq(second_reg, Immediate(-1));
4114 __ j(kEqual, slow_path->GetEntryLabel());
4115 // rdx:rax <- sign-extended of rax
4116 __ cqo();
4117 // rax = quotient, rdx = remainder
4118 __ idivq(second_reg);
4119 }
4120 __ Bind(slow_path->GetExitLabel());
4121 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004122}
4123
Calin Juravle7c4954d2014-10-28 16:57:40 +00004124void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
4125 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004126 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004127 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 case DataType::Type::kInt32:
4129 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00004130 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004131 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004132 locations->SetOut(Location::SameAsFirstInput());
4133 // Intel uses edx:eax as the dividend.
4134 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004135 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
4136 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
4137 // output and request another temp.
4138 if (div->InputAt(1)->IsConstant()) {
4139 locations->AddTemp(Location::RequiresRegister());
4140 }
Calin Juravled0d48522014-11-04 16:40:20 +00004141 break;
4142 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 case DataType::Type::kFloat32:
4145 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004146 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04004147 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004148 locations->SetOut(Location::SameAsFirstInput());
4149 break;
4150 }
4151
4152 default:
4153 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4154 }
4155}
4156
4157void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
4158 LocationSummary* locations = div->GetLocations();
4159 Location first = locations->InAt(0);
4160 Location second = locations->InAt(1);
4161 DCHECK(first.Equals(locations->Out()));
4162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004164 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 case DataType::Type::kInt32:
4166 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004167 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00004168 break;
4169 }
4170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004171 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004172 if (second.IsFpuRegister()) {
4173 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4174 } else if (second.IsConstant()) {
4175 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004176 codegen_->LiteralFloatAddress(
4177 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04004178 } else {
4179 DCHECK(second.IsStackSlot());
4180 __ divss(first.AsFpuRegister<XmmRegister>(),
4181 Address(CpuRegister(RSP), second.GetStackIndex()));
4182 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004183 break;
4184 }
4185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004186 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004187 if (second.IsFpuRegister()) {
4188 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4189 } else if (second.IsConstant()) {
4190 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004191 codegen_->LiteralDoubleAddress(
4192 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04004193 } else {
4194 DCHECK(second.IsDoubleStackSlot());
4195 __ divsd(first.AsFpuRegister<XmmRegister>(),
4196 Address(CpuRegister(RSP), second.GetStackIndex()));
4197 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004198 break;
4199 }
4200
4201 default:
4202 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4203 }
4204}
4205
Calin Juravlebacfec32014-11-14 15:54:36 +00004206void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004207 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004208 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004209 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00004210
4211 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 case DataType::Type::kInt32:
4213 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004214 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004215 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004216 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
4217 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004218 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4219 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
4220 // output and request another temp.
4221 if (rem->InputAt(1)->IsConstant()) {
4222 locations->AddTemp(Location::RequiresRegister());
4223 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004224 break;
4225 }
4226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004227 case DataType::Type::kFloat32:
4228 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004229 locations->SetInAt(0, Location::Any());
4230 locations->SetInAt(1, Location::Any());
4231 locations->SetOut(Location::RequiresFpuRegister());
4232 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004233 break;
4234 }
4235
4236 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004237 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004238 }
4239}
4240
4241void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004242 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004243 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 case DataType::Type::kInt32:
4245 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004246 GenerateDivRemIntegral(rem);
4247 break;
4248 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004249 case DataType::Type::kFloat32:
4250 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004251 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00004252 break;
4253 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004254 default:
4255 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
4256 }
4257}
4258
Aart Bik1f8d51b2018-02-15 10:42:37 -08004259static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4260 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4261 switch (minmax->GetResultType()) {
4262 case DataType::Type::kInt32:
4263 case DataType::Type::kInt64:
4264 locations->SetInAt(0, Location::RequiresRegister());
4265 locations->SetInAt(1, Location::RequiresRegister());
4266 locations->SetOut(Location::SameAsFirstInput());
4267 break;
4268 case DataType::Type::kFloat32:
4269 case DataType::Type::kFloat64:
4270 locations->SetInAt(0, Location::RequiresFpuRegister());
4271 locations->SetInAt(1, Location::RequiresFpuRegister());
4272 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
4273 // the second input to be the output (we can simply swap inputs).
4274 locations->SetOut(Location::SameAsFirstInput());
4275 break;
4276 default:
4277 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4278 }
4279}
4280
Aart Bik351df3e2018-03-07 11:54:57 -08004281void InstructionCodeGeneratorX86_64::GenerateMinMaxInt(LocationSummary* locations,
4282 bool is_min,
4283 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004284 Location op1_loc = locations->InAt(0);
4285 Location op2_loc = locations->InAt(1);
4286
4287 // Shortcut for same input locations.
4288 if (op1_loc.Equals(op2_loc)) {
4289 // Can return immediately, as op1_loc == out_loc.
4290 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4291 // a copy here.
4292 DCHECK(locations->Out().Equals(op1_loc));
4293 return;
4294 }
4295
4296 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4297 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
4298
4299 // (out := op1)
4300 // out <=? op2
4301 // if out is min jmp done
4302 // out := op2
4303 // done:
4304
4305 if (type == DataType::Type::kInt64) {
4306 __ cmpq(out, op2);
4307 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ true);
4308 } else {
4309 DCHECK_EQ(type, DataType::Type::kInt32);
4310 __ cmpl(out, op2);
4311 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ false);
4312 }
4313}
4314
4315void InstructionCodeGeneratorX86_64::GenerateMinMaxFP(LocationSummary* locations,
4316 bool is_min,
4317 DataType::Type type) {
4318 Location op1_loc = locations->InAt(0);
4319 Location op2_loc = locations->InAt(1);
4320 Location out_loc = locations->Out();
4321 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4322
4323 // Shortcut for same input locations.
4324 if (op1_loc.Equals(op2_loc)) {
4325 DCHECK(out_loc.Equals(op1_loc));
4326 return;
4327 }
4328
4329 // (out := op1)
4330 // out <=? op2
4331 // if Nan jmp Nan_label
4332 // if out is min jmp done
4333 // if op2 is min jmp op2_label
4334 // handle -0/+0
4335 // jmp done
4336 // Nan_label:
4337 // out := NaN
4338 // op2_label:
4339 // out := op2
4340 // done:
4341 //
4342 // This removes one jmp, but needs to copy one input (op1) to out.
4343 //
4344 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
4345
4346 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4347
4348 NearLabel nan, done, op2_label;
4349 if (type == DataType::Type::kFloat64) {
4350 __ ucomisd(out, op2);
4351 } else {
4352 DCHECK_EQ(type, DataType::Type::kFloat32);
4353 __ ucomiss(out, op2);
4354 }
4355
4356 __ j(Condition::kParityEven, &nan);
4357
4358 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4359 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4360
4361 // Handle 0.0/-0.0.
4362 if (is_min) {
4363 if (type == DataType::Type::kFloat64) {
4364 __ orpd(out, op2);
4365 } else {
4366 __ orps(out, op2);
4367 }
4368 } else {
4369 if (type == DataType::Type::kFloat64) {
4370 __ andpd(out, op2);
4371 } else {
4372 __ andps(out, op2);
4373 }
4374 }
4375 __ jmp(&done);
4376
4377 // NaN handling.
4378 __ Bind(&nan);
4379 if (type == DataType::Type::kFloat64) {
4380 __ movsd(out, codegen_->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
4381 } else {
4382 __ movss(out, codegen_->LiteralInt32Address(INT32_C(0x7FC00000)));
4383 }
4384 __ jmp(&done);
4385
4386 // out := op2;
4387 __ Bind(&op2_label);
4388 if (type == DataType::Type::kFloat64) {
4389 __ movsd(out, op2);
4390 } else {
4391 __ movss(out, op2);
4392 }
4393
4394 // Done.
4395 __ Bind(&done);
4396}
4397
Aart Bik351df3e2018-03-07 11:54:57 -08004398void InstructionCodeGeneratorX86_64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4399 DataType::Type type = minmax->GetResultType();
4400 switch (type) {
4401 case DataType::Type::kInt32:
4402 case DataType::Type::kInt64:
4403 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4404 break;
4405 case DataType::Type::kFloat32:
4406 case DataType::Type::kFloat64:
4407 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4408 break;
4409 default:
4410 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4411 }
4412}
4413
Aart Bik1f8d51b2018-02-15 10:42:37 -08004414void LocationsBuilderX86_64::VisitMin(HMin* min) {
4415 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4416}
4417
4418void InstructionCodeGeneratorX86_64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004419 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004420}
4421
4422void LocationsBuilderX86_64::VisitMax(HMax* max) {
4423 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4424}
4425
4426void InstructionCodeGeneratorX86_64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004427 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004428}
4429
Aart Bik3dad3412018-02-28 12:01:46 -08004430void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
4431 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4432 switch (abs->GetResultType()) {
4433 case DataType::Type::kInt32:
4434 case DataType::Type::kInt64:
4435 locations->SetInAt(0, Location::RequiresRegister());
4436 locations->SetOut(Location::SameAsFirstInput());
4437 locations->AddTemp(Location::RequiresRegister());
4438 break;
4439 case DataType::Type::kFloat32:
4440 case DataType::Type::kFloat64:
4441 locations->SetInAt(0, Location::RequiresFpuRegister());
4442 locations->SetOut(Location::SameAsFirstInput());
4443 locations->AddTemp(Location::RequiresFpuRegister());
4444 break;
4445 default:
4446 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4447 }
4448}
4449
4450void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
4451 LocationSummary* locations = abs->GetLocations();
4452 switch (abs->GetResultType()) {
4453 case DataType::Type::kInt32: {
4454 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4455 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4456 // Create mask.
4457 __ movl(mask, out);
4458 __ sarl(mask, Immediate(31));
4459 // Add mask.
4460 __ addl(out, mask);
4461 __ xorl(out, mask);
4462 break;
4463 }
4464 case DataType::Type::kInt64: {
4465 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4466 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4467 // Create mask.
4468 __ movq(mask, out);
4469 __ sarq(mask, Immediate(63));
4470 // Add mask.
4471 __ addq(out, mask);
4472 __ xorq(out, mask);
4473 break;
4474 }
4475 case DataType::Type::kFloat32: {
4476 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4477 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4478 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
4479 __ andps(out, mask);
4480 break;
4481 }
4482 case DataType::Type::kFloat64: {
4483 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4484 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4485 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
4486 __ andpd(out, mask);
4487 break;
4488 }
4489 default:
4490 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4491 }
4492}
4493
Calin Juravled0d48522014-11-04 16:40:20 +00004494void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004495 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004496 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00004497}
4498
4499void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004500 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004501 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004502 codegen_->AddSlowPath(slow_path);
4503
4504 LocationSummary* locations = instruction->GetLocations();
4505 Location value = locations->InAt(0);
4506
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004507 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004508 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004509 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004510 case DataType::Type::kInt8:
4511 case DataType::Type::kUint16:
4512 case DataType::Type::kInt16:
4513 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004514 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004515 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004516 __ j(kEqual, slow_path->GetEntryLabel());
4517 } else if (value.IsStackSlot()) {
4518 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4519 __ j(kEqual, slow_path->GetEntryLabel());
4520 } else {
4521 DCHECK(value.IsConstant()) << value;
4522 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004523 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004524 }
4525 }
4526 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004527 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004529 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004530 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004531 __ j(kEqual, slow_path->GetEntryLabel());
4532 } else if (value.IsDoubleStackSlot()) {
4533 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4534 __ j(kEqual, slow_path->GetEntryLabel());
4535 } else {
4536 DCHECK(value.IsConstant()) << value;
4537 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004538 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004539 }
4540 }
4541 break;
4542 }
4543 default:
4544 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004545 }
Calin Juravled0d48522014-11-04 16:40:20 +00004546}
4547
Calin Juravle9aec02f2014-11-18 23:06:35 +00004548void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
4549 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4550
4551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004552 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004553
4554 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 case DataType::Type::kInt32:
4556 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004557 locations->SetInAt(0, Location::RequiresRegister());
4558 // The shift count needs to be in CL.
4559 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
4560 locations->SetOut(Location::SameAsFirstInput());
4561 break;
4562 }
4563 default:
4564 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4565 }
4566}
4567
4568void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
4569 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4570
4571 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004572 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004573 Location second = locations->InAt(1);
4574
4575 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004576 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004577 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004579 if (op->IsShl()) {
4580 __ shll(first_reg, second_reg);
4581 } else if (op->IsShr()) {
4582 __ sarl(first_reg, second_reg);
4583 } else {
4584 __ shrl(first_reg, second_reg);
4585 }
4586 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004588 if (op->IsShl()) {
4589 __ shll(first_reg, imm);
4590 } else if (op->IsShr()) {
4591 __ sarl(first_reg, imm);
4592 } else {
4593 __ shrl(first_reg, imm);
4594 }
4595 }
4596 break;
4597 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004598 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004599 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004600 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004601 if (op->IsShl()) {
4602 __ shlq(first_reg, second_reg);
4603 } else if (op->IsShr()) {
4604 __ sarq(first_reg, second_reg);
4605 } else {
4606 __ shrq(first_reg, second_reg);
4607 }
4608 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004610 if (op->IsShl()) {
4611 __ shlq(first_reg, imm);
4612 } else if (op->IsShr()) {
4613 __ sarq(first_reg, imm);
4614 } else {
4615 __ shrq(first_reg, imm);
4616 }
4617 }
4618 break;
4619 }
4620 default:
4621 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004622 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004623 }
4624}
4625
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004626void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4627 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004628 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004629
4630 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 case DataType::Type::kInt32:
4632 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004633 locations->SetInAt(0, Location::RequiresRegister());
4634 // The shift count needs to be in CL (unless it is a constant).
4635 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4636 locations->SetOut(Location::SameAsFirstInput());
4637 break;
4638 }
4639 default:
4640 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4641 UNREACHABLE();
4642 }
4643}
4644
4645void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4646 LocationSummary* locations = ror->GetLocations();
4647 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4648 Location second = locations->InAt(1);
4649
4650 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004652 if (second.IsRegister()) {
4653 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4654 __ rorl(first_reg, second_reg);
4655 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004657 __ rorl(first_reg, imm);
4658 }
4659 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004660 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004661 if (second.IsRegister()) {
4662 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4663 __ rorq(first_reg, second_reg);
4664 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004665 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004666 __ rorq(first_reg, imm);
4667 }
4668 break;
4669 default:
4670 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4671 UNREACHABLE();
4672 }
4673}
4674
Calin Juravle9aec02f2014-11-18 23:06:35 +00004675void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4676 HandleShift(shl);
4677}
4678
4679void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4680 HandleShift(shl);
4681}
4682
4683void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4684 HandleShift(shr);
4685}
4686
4687void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4688 HandleShift(shr);
4689}
4690
4691void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4692 HandleShift(ushr);
4693}
4694
4695void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4696 HandleShift(ushr);
4697}
4698
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004699void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4701 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004702 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07004703 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004704 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004705}
4706
4707void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004708 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4709 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4710 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004711}
4712
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004713void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004714 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4715 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004716 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004717 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004718 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4719 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004720}
4721
4722void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004723 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4724 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004725 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004726 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004727 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004728}
4729
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004730void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004731 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004732 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004733 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4734 if (location.IsStackSlot()) {
4735 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4736 } else if (location.IsDoubleStackSlot()) {
4737 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4738 }
4739 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004740}
4741
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004742void InstructionCodeGeneratorX86_64::VisitParameterValue(
4743 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004744 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004745}
4746
4747void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4748 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004749 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004750 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4751}
4752
4753void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4754 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4755 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004756}
4757
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004758void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4759 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004760 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004761 locations->SetInAt(0, Location::RequiresRegister());
4762 locations->SetOut(Location::RequiresRegister());
4763}
4764
4765void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4766 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004767 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004768 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004769 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004770 __ movq(locations->Out().AsRegister<CpuRegister>(),
4771 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004772 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004773 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004774 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004775 __ movq(locations->Out().AsRegister<CpuRegister>(),
4776 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4777 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004778 __ movq(locations->Out().AsRegister<CpuRegister>(),
4779 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004780 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004781}
4782
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004783void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004784 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004785 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004786 locations->SetInAt(0, Location::RequiresRegister());
4787 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004788}
4789
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004790void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4791 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004792 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4793 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004794 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004795 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004797 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004798 break;
4799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004800 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004801 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004802 break;
4803
4804 default:
4805 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4806 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004807}
4808
David Brazdil66d126e2015-04-03 16:02:44 +01004809void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4810 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004811 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004812 locations->SetInAt(0, Location::RequiresRegister());
4813 locations->SetOut(Location::SameAsFirstInput());
4814}
4815
4816void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004817 LocationSummary* locations = bool_not->GetLocations();
4818 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4819 locations->Out().AsRegister<CpuRegister>().AsRegister());
4820 Location out = locations->Out();
4821 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4822}
4823
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004824void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004825 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004826 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004827 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004828 locations->SetInAt(i, Location::Any());
4829 }
4830 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004831}
4832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004833void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004834 LOG(FATAL) << "Unimplemented";
4835}
4836
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004837void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004838 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004839 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004840 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004841 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4842 */
4843 switch (kind) {
4844 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004845 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004846 break;
4847 }
4848 case MemBarrierKind::kAnyStore:
4849 case MemBarrierKind::kLoadAny:
4850 case MemBarrierKind::kStoreStore: {
4851 // nop
4852 break;
4853 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004854 case MemBarrierKind::kNTStoreStore:
4855 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004856 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004857 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004858 }
4859}
4860
4861void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
Alex Light3a73ffb2021-01-25 14:11:05 +00004862 DCHECK(instruction->IsInstanceFieldGet() ||
4863 instruction->IsStaticFieldGet() ||
4864 instruction->IsPredicatedInstanceFieldGet());
Calin Juravle52c48962014-12-16 17:02:57 +00004865
Roland Levillain0d5a2812015-11-13 10:07:31 +00004866 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004867 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00004868 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Nicolas Geoffray39468442014-09-02 15:17:15 +01004869 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004870 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4871 object_field_get_with_read_barrier
4872 ? LocationSummary::kCallOnSlowPath
4873 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004874 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004875 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004876 }
Alex Light3a73ffb2021-01-25 14:11:05 +00004877 // receiver_input
4878 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
4879 if (is_predicated) {
4880 if (DataType::IsFloatingPointType(instruction->GetType())) {
4881 locations->SetInAt(0, Location::RequiresFpuRegister());
4882 } else {
4883 locations->SetInAt(0, Location::RequiresRegister());
4884 }
4885 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004886 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00004887 locations->SetOut(is_predicated ? Location::SameAsFirstInput()
4888 : Location::RequiresFpuRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004889 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00004890 // The output overlaps for an object field get when read barriers are
4891 // enabled: we do not want the move to overwrite the object's location, as
4892 // we need it to emit the read barrier. For predicated instructions we can
4893 // always overlap since the output is SameAsFirst and the default value.
4894 locations->SetOut(is_predicated ? Location::SameAsFirstInput() : Location::RequiresRegister(),
4895 object_field_get_with_read_barrier || is_predicated
4896 ? Location::kOutputOverlap
4897 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004898 }
Calin Juravle52c48962014-12-16 17:02:57 +00004899}
4900
4901void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4902 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00004903 DCHECK(instruction->IsInstanceFieldGet() ||
4904 instruction->IsStaticFieldGet() ||
4905 instruction->IsPredicatedInstanceFieldGet());
Calin Juravle52c48962014-12-16 17:02:57 +00004906
4907 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00004908 Location base_loc = locations->InAt(instruction->IsPredicatedInstanceFieldGet() ? 1 : 0);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004909 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004910 Location out = locations->Out();
4911 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004912 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4913 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004914 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4915
Vladimir Marko61b92282017-10-11 13:23:17 +01004916 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004917 case DataType::Type::kBool:
4918 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004919 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4920 break;
4921 }
4922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004923 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004924 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4925 break;
4926 }
4927
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004928 case DataType::Type::kUint16: {
4929 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004930 break;
4931 }
4932
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004933 case DataType::Type::kInt16: {
4934 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004935 break;
4936 }
4937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004938 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004939 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4940 break;
4941 }
4942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004943 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004944 // /* HeapReference<Object> */ out = *(base + offset)
4945 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004946 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004947 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004948 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08004949 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004950 if (is_volatile) {
4951 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4952 }
4953 } else {
4954 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4955 codegen_->MaybeRecordImplicitNullCheck(instruction);
4956 if (is_volatile) {
4957 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4958 }
4959 // If read barriers are enabled, emit read barriers other than
4960 // Baker's using a slow path (and also unpoison the loaded
4961 // reference, if heap poisoning is enabled).
4962 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4963 }
4964 break;
4965 }
4966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004967 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004968 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4969 break;
4970 }
4971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004972 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004973 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4974 break;
4975 }
4976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004977 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004978 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4979 break;
4980 }
4981
Aart Bik66c158e2018-01-31 12:55:04 -08004982 case DataType::Type::kUint32:
4983 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004984 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004985 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004986 UNREACHABLE();
4987 }
4988
Vladimir Marko61b92282017-10-11 13:23:17 +01004989 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004990 // Potential implicit null checks, in the case of reference
4991 // fields, are handled in the previous switch statement.
4992 } else {
4993 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004994 }
Roland Levillain4d027112015-07-01 15:41:14 +01004995
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004996 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004997 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004998 // Memory barriers, in the case of references, are also handled
4999 // in the previous switch statement.
5000 } else {
5001 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5002 }
Roland Levillain4d027112015-07-01 15:41:14 +01005003 }
Calin Juravle52c48962014-12-16 17:02:57 +00005004}
5005
5006void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
5007 const FieldInfo& field_info) {
5008 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5009
5010 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005011 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04005013 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005014 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01005015 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005016
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005017 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005018 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04005019 if (is_volatile) {
5020 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5021 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
5022 } else {
5023 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5024 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005025 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04005026 if (is_volatile) {
5027 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5028 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
5029 } else {
5030 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5031 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005032 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005033 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01005034 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01005035 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005036 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005037 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01005038 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005039 locations->AddTemp(Location::RequiresRegister());
5040 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005041}
5042
Calin Juravle52c48962014-12-16 17:02:57 +00005043void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005044 const FieldInfo& field_info,
5045 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005046 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5047
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005049 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
5050 Location value = locations->InAt(1);
5051 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005052 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005053 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alex Light3a73ffb2021-01-25 14:11:05 +00005054 bool is_predicated =
5055 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Calin Juravle52c48962014-12-16 17:02:57 +00005056
5057 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005058 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005059 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060
Mark Mendellea5af682015-10-22 17:35:49 -04005061 bool maybe_record_implicit_null_check_done = false;
5062
Alex Light3a73ffb2021-01-25 14:11:05 +00005063 NearLabel pred_is_null;
5064 if (is_predicated) {
5065 __ testl(base, base);
5066 __ j(kZero, &pred_is_null);
5067 }
5068
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005070 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005071 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04005073 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005074 __ movb(Address(base, offset),
5075 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04005076 } else {
5077 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
5078 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079 break;
5080 }
5081
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005082 case DataType::Type::kUint16:
5083 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04005084 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005085 __ movw(Address(base, offset),
5086 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04005087 } else {
5088 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
5089 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005090 break;
5091 }
5092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005093 case DataType::Type::kInt32:
5094 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04005095 if (value.IsConstant()) {
5096 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005097 // `field_type == DataType::Type::kReference` implies `v == 0`.
5098 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01005099 // Note: if heap poisoning is enabled, no need to poison
5100 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01005101 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04005102 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005103 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01005104 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
5105 __ movl(temp, value.AsRegister<CpuRegister>());
5106 __ PoisonHeapReference(temp);
5107 __ movl(Address(base, offset), temp);
5108 } else {
5109 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
5110 }
Mark Mendell40741f32015-04-20 22:10:34 -04005111 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112 break;
5113 }
5114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005115 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04005116 if (value.IsConstant()) {
5117 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04005118 codegen_->MoveInt64ToAddress(Address(base, offset),
5119 Address(base, offset + sizeof(int32_t)),
5120 v,
5121 instruction);
5122 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04005123 } else {
5124 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
5125 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126 break;
5127 }
5128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005129 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04005130 if (value.IsConstant()) {
5131 int32_t v =
5132 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5133 __ movl(Address(base, offset), Immediate(v));
5134 } else {
5135 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5136 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005137 break;
5138 }
5139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005140 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04005141 if (value.IsConstant()) {
5142 int64_t v =
5143 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5144 codegen_->MoveInt64ToAddress(Address(base, offset),
5145 Address(base, offset + sizeof(int32_t)),
5146 v,
5147 instruction);
5148 maybe_record_implicit_null_check_done = true;
5149 } else {
5150 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5151 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005152 break;
5153 }
5154
Aart Bik66c158e2018-01-31 12:55:04 -08005155 case DataType::Type::kUint32:
5156 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005157 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005159 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005160 }
Calin Juravle52c48962014-12-16 17:02:57 +00005161
Mark Mendellea5af682015-10-22 17:35:49 -04005162 if (!maybe_record_implicit_null_check_done) {
5163 codegen_->MaybeRecordImplicitNullCheck(instruction);
5164 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005165
5166 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5167 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
5168 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005169 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005170 }
5171
Calin Juravle52c48962014-12-16 17:02:57 +00005172 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005173 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005174 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005175
5176 if (is_predicated) {
5177 __ Bind(&pred_is_null);
5178 }
Calin Juravle52c48962014-12-16 17:02:57 +00005179}
5180
5181void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5182 HandleFieldSet(instruction, instruction->GetFieldInfo());
5183}
5184
5185void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005186 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187}
5188
Alex Light3a73ffb2021-01-25 14:11:05 +00005189void LocationsBuilderX86_64::VisitPredicatedInstanceFieldGet(
5190 HPredicatedInstanceFieldGet* instruction) {
5191 HandleFieldGet(instruction);
5192}
5193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00005195 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005196}
5197
Alex Light3a73ffb2021-01-25 14:11:05 +00005198void InstructionCodeGeneratorX86_64::VisitPredicatedInstanceFieldGet(
5199 HPredicatedInstanceFieldGet* instruction) {
5200 NearLabel finish;
5201 LocationSummary* locations = instruction->GetLocations();
5202 CpuRegister target = locations->InAt(1).AsRegister<CpuRegister>();
5203 __ testl(target, target);
5204 __ j(kZero, &finish);
5205 HandleFieldGet(instruction, instruction->GetFieldInfo());
5206 __ Bind(&finish);
5207}
5208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005209void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00005210 HandleFieldGet(instruction, instruction->GetFieldInfo());
5211}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005212
Calin Juravle52c48962014-12-16 17:02:57 +00005213void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5214 HandleFieldGet(instruction);
5215}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216
Calin Juravle52c48962014-12-16 17:02:57 +00005217void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5218 HandleFieldGet(instruction, instruction->GetFieldInfo());
5219}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005220
Calin Juravle52c48962014-12-16 17:02:57 +00005221void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5222 HandleFieldSet(instruction, instruction->GetFieldInfo());
5223}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005224
Calin Juravle52c48962014-12-16 17:02:57 +00005225void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005226 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005227}
5228
Vladimir Marko552a1342017-10-31 10:56:47 +00005229void LocationsBuilderX86_64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5230 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(RAX));
5231}
5232
5233void InstructionCodeGeneratorX86_64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5234 __ movl(CpuRegister(RDI), Immediate(instruction->GetFormat()->GetValue()));
5235 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5236}
5237
Calin Juravlee460d1d2015-09-29 04:52:17 +01005238void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
5239 HUnresolvedInstanceFieldGet* instruction) {
5240 FieldAccessCallingConventionX86_64 calling_convention;
5241 codegen_->CreateUnresolvedFieldLocationSummary(
5242 instruction, instruction->GetFieldType(), calling_convention);
5243}
5244
5245void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
5246 HUnresolvedInstanceFieldGet* instruction) {
5247 FieldAccessCallingConventionX86_64 calling_convention;
5248 codegen_->GenerateUnresolvedFieldAccess(instruction,
5249 instruction->GetFieldType(),
5250 instruction->GetFieldIndex(),
5251 instruction->GetDexPc(),
5252 calling_convention);
5253}
5254
5255void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
5256 HUnresolvedInstanceFieldSet* instruction) {
5257 FieldAccessCallingConventionX86_64 calling_convention;
5258 codegen_->CreateUnresolvedFieldLocationSummary(
5259 instruction, instruction->GetFieldType(), calling_convention);
5260}
5261
5262void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
5263 HUnresolvedInstanceFieldSet* instruction) {
5264 FieldAccessCallingConventionX86_64 calling_convention;
5265 codegen_->GenerateUnresolvedFieldAccess(instruction,
5266 instruction->GetFieldType(),
5267 instruction->GetFieldIndex(),
5268 instruction->GetDexPc(),
5269 calling_convention);
5270}
5271
5272void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
5273 HUnresolvedStaticFieldGet* instruction) {
5274 FieldAccessCallingConventionX86_64 calling_convention;
5275 codegen_->CreateUnresolvedFieldLocationSummary(
5276 instruction, instruction->GetFieldType(), calling_convention);
5277}
5278
5279void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
5280 HUnresolvedStaticFieldGet* instruction) {
5281 FieldAccessCallingConventionX86_64 calling_convention;
5282 codegen_->GenerateUnresolvedFieldAccess(instruction,
5283 instruction->GetFieldType(),
5284 instruction->GetFieldIndex(),
5285 instruction->GetDexPc(),
5286 calling_convention);
5287}
5288
5289void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
5290 HUnresolvedStaticFieldSet* instruction) {
5291 FieldAccessCallingConventionX86_64 calling_convention;
5292 codegen_->CreateUnresolvedFieldLocationSummary(
5293 instruction, instruction->GetFieldType(), calling_convention);
5294}
5295
5296void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
5297 HUnresolvedStaticFieldSet* instruction) {
5298 FieldAccessCallingConventionX86_64 calling_convention;
5299 codegen_->GenerateUnresolvedFieldAccess(instruction,
5300 instruction->GetFieldType(),
5301 instruction->GetFieldIndex(),
5302 instruction->GetDexPc(),
5303 calling_convention);
5304}
5305
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005306void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005307 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5308 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5309 ? Location::RequiresRegister()
5310 : Location::Any();
5311 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005312}
5313
Calin Juravle2ae48182016-03-16 14:05:09 +00005314void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5315 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005316 return;
5317 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005318 LocationSummary* locations = instruction->GetLocations();
5319 Location obj = locations->InAt(0);
5320
5321 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005322 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005323}
5324
Calin Juravle2ae48182016-03-16 14:05:09 +00005325void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005326 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005327 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005328
5329 LocationSummary* locations = instruction->GetLocations();
5330 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005331
5332 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005333 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005334 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005335 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005336 } else {
5337 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005338 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005339 __ jmp(slow_path->GetEntryLabel());
5340 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005341 }
5342 __ j(kEqual, slow_path->GetEntryLabel());
5343}
5344
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005345void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005346 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005347}
5348
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005350 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005352 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005353 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5354 object_array_get_with_read_barrier
5355 ? LocationSummary::kCallOnSlowPath
5356 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005357 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005358 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005359 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005360 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005361 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005362 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5364 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 // The output overlaps for an object array get when read barriers
5366 // are enabled: we do not want the move to overwrite the array's
5367 // location, as we need it to emit the read barrier.
5368 locations->SetOut(
5369 Location::RequiresRegister(),
5370 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005371 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372}
5373
5374void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
5375 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005376 Location obj_loc = locations->InAt(0);
5377 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005379 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005380 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01005383 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005384 case DataType::Type::kBool:
5385 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005386 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005387 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 break;
5389 }
5390
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005392 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005393 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 break;
5395 }
5396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005397 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005398 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07005399 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5400 // Branch cases into compressed and uncompressed for each index's type.
5401 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5402 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005403 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005405 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5406 "Expecting 0=compressed, 1=uncompressed");
5407 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005408 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
5409 __ jmp(&done);
5410 __ Bind(&not_compressed);
5411 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5412 __ Bind(&done);
5413 } else {
5414 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5415 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 break;
5417 }
5418
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005419 case DataType::Type::kInt16: {
5420 CpuRegister out = out_loc.AsRegister<CpuRegister>();
5421 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5422 break;
5423 }
5424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005425 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005426 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005427 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 break;
5429 }
5430
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005431 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005432 static_assert(
5433 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5434 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005435 // /* HeapReference<Object> */ out =
5436 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5437 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005438 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005439 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005440 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005441 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005442 } else {
5443 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005444 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
5445 codegen_->MaybeRecordImplicitNullCheck(instruction);
5446 // If read barriers are enabled, emit read barriers other than
5447 // Baker's using a slow path (and also unpoison the loaded
5448 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005449 if (index.IsConstant()) {
5450 uint32_t offset =
5451 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005452 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5453 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005454 codegen_->MaybeGenerateReadBarrierSlow(
5455 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5456 }
5457 }
5458 break;
5459 }
5460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005461 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005462 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005463 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464 break;
5465 }
5466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005467 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005468 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005469 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005470 break;
5471 }
5472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005474 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005475 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005476 break;
5477 }
5478
Aart Bik66c158e2018-01-31 12:55:04 -08005479 case DataType::Type::kUint32:
5480 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005481 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005482 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005483 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005484 }
Roland Levillain4d027112015-07-01 15:41:14 +01005485
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005486 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005487 // Potential implicit null checks, in the case of reference
5488 // arrays, are handled in the previous switch statement.
5489 } else {
5490 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005491 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492}
5493
5494void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005495 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005496
5497 bool needs_write_barrier =
5498 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005499 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005500
Vladimir Markoca6fff82017-10-03 14:49:14 +01005501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005502 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005503 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005504
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005505 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04005506 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005507 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04005508 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005509 } else {
5510 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5511 }
5512
5513 if (needs_write_barrier) {
5514 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005515 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005516 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005517 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005518}
5519
5520void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
5521 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005522 Location array_loc = locations->InAt(0);
5523 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005525 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005526 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005527 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005528 bool needs_write_barrier =
5529 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530
5531 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005533 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005535 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005536 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005537 if (value.IsRegister()) {
5538 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005540 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005542 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 break;
5544 }
5545
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005546 case DataType::Type::kUint16:
5547 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005548 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005549 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005550 if (value.IsRegister()) {
5551 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005552 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005553 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01005554 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005555 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005556 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 break;
5558 }
5559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005560 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005561 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005562 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005563
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005564 if (!value.IsRegister()) {
5565 // Just setting null.
5566 DCHECK(instruction->InputAt(2)->IsNullConstant());
5567 DCHECK(value.IsConstant()) << value;
5568 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005569 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005570 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005571 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005572 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005573 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005574
5575 DCHECK(needs_write_barrier);
5576 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005577 Location temp_loc = locations->GetTemp(0);
5578 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005579
5580 bool can_value_be_null = instruction->GetValueCanBeNull();
5581 NearLabel do_store;
5582 if (can_value_be_null) {
5583 __ testl(register_value, register_value);
5584 __ j(kEqual, &do_store);
5585 }
5586
5587 SlowPathCode* slow_path = nullptr;
5588 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005589 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005590 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005591
5592 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5593 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5594 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005595
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005596 // Note that when Baker read barriers are enabled, the type
5597 // checks are performed without read barriers. This is fine,
5598 // even in the case where a class object is in the from-space
5599 // after the flip, as a comparison involving such a type would
5600 // not produce a false positive; it may of course produce a
5601 // false negative, in which case we would take the ArraySet
5602 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005603
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005604 // /* HeapReference<Class> */ temp = array->klass_
5605 __ movl(temp, Address(array, class_offset));
5606 codegen_->MaybeRecordImplicitNullCheck(instruction);
5607 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005608
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005609 // /* HeapReference<Class> */ temp = temp->component_type_
5610 __ movl(temp, Address(temp, component_offset));
5611 // If heap poisoning is enabled, no need to unpoison `temp`
5612 // nor the object reference in `register_value->klass`, as
5613 // we are comparing two poisoned references.
5614 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005615
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005616 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005617 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005618 __ j(kEqual, &do_put);
5619 // If heap poisoning is enabled, the `temp` reference has
5620 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005621 __ MaybeUnpoisonHeapReference(temp);
5622
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005623 // If heap poisoning is enabled, no need to unpoison the
5624 // heap reference loaded below, as it is only used for a
5625 // comparison with null.
5626 __ cmpl(Address(temp, super_offset), Immediate(0));
5627 __ j(kNotEqual, slow_path->GetEntryLabel());
5628 __ Bind(&do_put);
5629 } else {
5630 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005631 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005632 }
5633
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005634 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5635 codegen_->MarkGCCard(
5636 temp, card, array, value.AsRegister<CpuRegister>(), /* value_can_be_null= */ false);
5637
5638 if (can_value_be_null) {
5639 DCHECK(do_store.IsLinked());
5640 __ Bind(&do_store);
5641 }
5642
5643 Location source = value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005644 if (kPoisonHeapReferences) {
5645 __ movl(temp, register_value);
5646 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005647 source = temp_loc;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005648 }
5649
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005650 __ movl(address, source.AsRegister<CpuRegister>());
5651
5652 if (can_value_be_null || !needs_type_check) {
5653 codegen_->MaybeRecordImplicitNullCheck(instruction);
5654 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005655
Vladimir Marko0dda8c82019-05-16 12:47:40 +00005656 if (slow_path != nullptr) {
5657 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005658 }
5659
5660 break;
5661 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005663 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005664 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005665 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005666 if (value.IsRegister()) {
5667 __ movl(address, value.AsRegister<CpuRegister>());
5668 } else {
5669 DCHECK(value.IsConstant()) << value;
5670 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5671 __ movl(address, Immediate(v));
5672 }
5673 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005674 break;
5675 }
5676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005678 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005679 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005680 if (value.IsRegister()) {
5681 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005682 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005683 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005684 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005685 Address address_high =
5686 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005687 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005688 }
5689 break;
5690 }
5691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005692 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005693 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005694 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005695 if (value.IsFpuRegister()) {
5696 __ movss(address, value.AsFpuRegister<XmmRegister>());
5697 } else {
5698 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005699 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005700 __ movl(address, Immediate(v));
5701 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005703 break;
5704 }
5705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005707 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005708 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005709 if (value.IsFpuRegister()) {
5710 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5711 codegen_->MaybeRecordImplicitNullCheck(instruction);
5712 } else {
5713 int64_t v =
5714 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005715 Address address_high =
5716 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005717 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5718 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005719 break;
5720 }
5721
Aart Bik66c158e2018-01-31 12:55:04 -08005722 case DataType::Type::kUint32:
5723 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005725 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005726 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005727 }
5728}
5729
5730void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005731 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005732 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005733 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005734 if (!instruction->IsEmittedAtUseSite()) {
5735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005737}
5738
5739void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005740 if (instruction->IsEmittedAtUseSite()) {
5741 return;
5742 }
5743
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005744 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005745 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005746 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5747 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005748 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005749 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005750 // Mask out most significant bit in case the array is String's array of char.
5751 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005752 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005753 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005754}
5755
5756void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005757 RegisterSet caller_saves = RegisterSet::Empty();
5758 InvokeRuntimeCallingConvention calling_convention;
5759 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5760 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5761 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005762 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005763 HInstruction* length = instruction->InputAt(1);
5764 if (!length->IsEmittedAtUseSite()) {
5765 locations->SetInAt(1, Location::RegisterOrConstant(length));
5766 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005767}
5768
5769void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5770 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005771 Location index_loc = locations->InAt(0);
5772 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005773 SlowPathCode* slow_path =
5774 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005775
Mark Mendell99dbd682015-04-22 16:18:52 -04005776 if (length_loc.IsConstant()) {
5777 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5778 if (index_loc.IsConstant()) {
5779 // BCE will remove the bounds check if we are guarenteed to pass.
5780 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5781 if (index < 0 || index >= length) {
5782 codegen_->AddSlowPath(slow_path);
5783 __ jmp(slow_path->GetEntryLabel());
5784 } else {
5785 // Some optimization after BCE may have generated this, and we should not
5786 // generate a bounds check if it is a valid range.
5787 }
5788 return;
5789 }
5790
5791 // We have to reverse the jump condition because the length is the constant.
5792 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5793 __ cmpl(index_reg, Immediate(length));
5794 codegen_->AddSlowPath(slow_path);
5795 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005796 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005797 HInstruction* array_length = instruction->InputAt(1);
5798 if (array_length->IsEmittedAtUseSite()) {
5799 // Address the length field in the array.
5800 DCHECK(array_length->IsArrayLength());
5801 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5802 Location array_loc = array_length->GetLocations()->InAt(0);
5803 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005804 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005805 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5806 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005807 CpuRegister length_reg = CpuRegister(TMP);
5808 __ movl(length_reg, array_len);
5809 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005810 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005811 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005812 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005813 // Checking the bound for general case:
5814 // Array of char or String's array when the compression feature off.
5815 if (index_loc.IsConstant()) {
5816 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5817 __ cmpl(array_len, Immediate(value));
5818 } else {
5819 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5820 }
5821 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005822 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005823 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005824 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005825 }
5826 codegen_->AddSlowPath(slow_path);
5827 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005828 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005829}
5830
5831void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5832 CpuRegister card,
5833 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005834 CpuRegister value,
5835 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005836 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005837 if (value_can_be_null) {
5838 __ testl(value, value);
5839 __ j(kEqual, &is_null);
5840 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005841 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005842 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005843 /* no_rip= */ true));
Roland Levillainc73f0522018-08-14 15:16:50 +01005844 // Calculate the offset (in the card table) of the card corresponding to
5845 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005846 __ movq(temp, object);
5847 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005848 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5849 // `object`'s card.
5850 //
5851 // Register `card` contains the address of the card table. Note that the card
5852 // table's base is biased during its creation so that it always starts at an
5853 // address whose least-significant byte is equal to `kCardDirty` (see
5854 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5855 // below writes the `kCardDirty` (byte) value into the `object`'s card
5856 // (located at `card + object >> kCardShift`).
5857 //
5858 // This dual use of the value in register `card` (1. to calculate the location
5859 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5860 // (no need to explicitly load `kCardDirty` as an immediate value).
Roland Levillain4d027112015-07-01 15:41:14 +01005861 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005862 if (value_can_be_null) {
5863 __ Bind(&is_null);
5864 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005865}
5866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005867void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005868 LOG(FATAL) << "Unimplemented";
5869}
5870
5871void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005872 if (instruction->GetNext()->IsSuspendCheck() &&
5873 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5874 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5875 // The back edge will generate the suspend check.
5876 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5877 }
5878
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005879 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5880}
5881
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005882void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005883 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5884 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005885 // In suspend check slow path, usually there are no caller-save registers at all.
5886 // If SIMD instructions are present, however, we force spilling all live SIMD
5887 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005888 locations->SetCustomSlowPathCallerSaves(
5889 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005890}
5891
5892void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005893 HBasicBlock* block = instruction->GetBlock();
5894 if (block->GetLoopInformation() != nullptr) {
5895 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5896 // The back edge will generate the suspend check.
5897 return;
5898 }
5899 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5900 // The goto will generate the suspend check.
5901 return;
5902 }
5903 GenerateSuspendCheck(instruction, nullptr);
5904}
5905
5906void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5907 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005908 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005909 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5910 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005911 slow_path =
5912 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005913 instruction->SetSlowPath(slow_path);
5914 codegen_->AddSlowPath(slow_path);
5915 if (successor != nullptr) {
5916 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005917 }
5918 } else {
5919 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5920 }
5921
Andreas Gampe542451c2016-07-26 09:02:02 -07005922 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005923 /* no_rip= */ true),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005924 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005925 if (successor == nullptr) {
5926 __ j(kNotEqual, slow_path->GetEntryLabel());
5927 __ Bind(slow_path->GetReturnLabel());
5928 } else {
5929 __ j(kEqual, codegen_->GetLabelOf(successor));
5930 __ jmp(slow_path->GetEntryLabel());
5931 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005932}
5933
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005934X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5935 return codegen_->GetAssembler();
5936}
5937
5938void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005939 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005940 Location source = move->GetSource();
5941 Location destination = move->GetDestination();
5942
5943 if (source.IsRegister()) {
5944 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005945 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005946 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005947 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005948 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005949 } else {
5950 DCHECK(destination.IsDoubleStackSlot());
5951 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005952 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005953 }
5954 } else if (source.IsStackSlot()) {
5955 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005956 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005957 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005958 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005959 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005960 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005961 } else {
5962 DCHECK(destination.IsStackSlot());
5963 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5964 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5965 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005966 } else if (source.IsDoubleStackSlot()) {
5967 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005968 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005969 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005970 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005971 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5972 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005973 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005974 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005975 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5976 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5977 }
Aart Bik5576f372017-03-23 16:17:37 -07005978 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005979 if (destination.IsFpuRegister()) {
5980 __ movups(destination.AsFpuRegister<XmmRegister>(),
5981 Address(CpuRegister(RSP), source.GetStackIndex()));
5982 } else {
5983 DCHECK(destination.IsSIMDStackSlot());
5984 size_t high = kX86_64WordSize;
5985 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5986 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5987 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5988 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5989 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005990 } else if (source.IsConstant()) {
5991 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005992 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5993 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005994 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005995 if (value == 0) {
5996 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5997 } else {
5998 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5999 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006000 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006001 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00006002 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006003 }
6004 } else if (constant->IsLongConstant()) {
6005 int64_t value = constant->AsLongConstant()->GetValue();
6006 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04006007 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006008 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006009 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04006010 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006011 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006012 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006013 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006014 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006015 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006016 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006017 } else {
6018 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006019 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006020 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
6021 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006022 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006023 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006024 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006025 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006026 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006027 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006028 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006029 } else {
6030 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04006031 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006032 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006033 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006034 } else if (source.IsFpuRegister()) {
6035 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006036 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006037 } else if (destination.IsStackSlot()) {
6038 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00006039 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006040 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006041 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00006042 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006043 } else {
6044 DCHECK(destination.IsSIMDStackSlot());
6045 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
6046 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006047 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006048 }
6049}
6050
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01006051void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006052 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01006053 __ movl(Address(CpuRegister(RSP), mem), reg);
6054 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006055}
6056
Mark Mendell8a1c7282015-06-29 15:41:28 -04006057void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
6058 __ movq(CpuRegister(TMP), reg1);
6059 __ movq(reg1, reg2);
6060 __ movq(reg2, CpuRegister(TMP));
6061}
6062
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01006063void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
6064 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
6065 __ movq(Address(CpuRegister(RSP), mem), reg);
6066 __ movq(reg, CpuRegister(TMP));
6067}
6068
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006069void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
6070 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
6071 __ movss(Address(CpuRegister(RSP), mem), reg);
6072 __ movd(reg, CpuRegister(TMP));
6073}
6074
6075void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
6076 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
6077 __ movsd(Address(CpuRegister(RSP), mem), reg);
6078 __ movd(reg, CpuRegister(TMP));
6079}
6080
Aart Bikcfe50bb2017-12-12 14:54:12 -08006081void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
6082 size_t extra_slot = 2 * kX86_64WordSize;
6083 __ subq(CpuRegister(RSP), Immediate(extra_slot));
6084 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
6085 ExchangeMemory64(0, mem + extra_slot, 2);
6086 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
6087 __ addq(CpuRegister(RSP), Immediate(extra_slot));
6088}
6089
6090void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
6091 ScratchRegisterScope ensure_scratch(
6092 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
6093
6094 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
6095 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
6096 __ movl(CpuRegister(ensure_scratch.GetRegister()),
6097 Address(CpuRegister(RSP), mem2 + stack_offset));
6098 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
6099 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
6100 CpuRegister(ensure_scratch.GetRegister()));
6101}
6102
6103void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
6104 ScratchRegisterScope ensure_scratch(
6105 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
6106
6107 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
6108
6109 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6110 for (int i = 0; i < num_of_qwords; i++) {
6111 __ movq(CpuRegister(TMP),
6112 Address(CpuRegister(RSP), mem1 + stack_offset));
6113 __ movq(CpuRegister(ensure_scratch.GetRegister()),
6114 Address(CpuRegister(RSP), mem2 + stack_offset));
6115 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
6116 CpuRegister(TMP));
6117 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
6118 CpuRegister(ensure_scratch.GetRegister()));
6119 stack_offset += kX86_64WordSize;
6120 }
6121}
6122
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006123void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006124 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006125 Location source = move->GetSource();
6126 Location destination = move->GetDestination();
6127
6128 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04006129 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006130 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006131 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006132 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006133 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006134 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006135 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01006136 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006137 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01006138 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006139 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01006140 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006141 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006142 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006143 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
6144 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6145 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006146 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006147 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006148 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006149 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006150 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006151 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006152 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006153 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006154 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6155 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
6156 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6157 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6158 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6159 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006160 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006161 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00006162 }
6163}
6164
6165
6166void ParallelMoveResolverX86_64::SpillScratch(int reg) {
6167 __ pushq(CpuRegister(reg));
6168}
6169
6170
6171void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
6172 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006173}
6174
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006175void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006176 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006177 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6178 const size_t status_byte_offset =
6179 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006180 constexpr uint32_t shifted_visibly_initialized_value =
6181 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006182
Vladimir Markobf121912019-06-04 13:49:05 +01006183 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006184 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006185 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006186}
6187
Vladimir Marko175e7862018-03-27 09:03:13 +00006188void InstructionCodeGeneratorX86_64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6189 CpuRegister temp) {
6190 uint32_t path_to_root = check->GetBitstringPathToRoot();
6191 uint32_t mask = check->GetBitstringMask();
6192 DCHECK(IsPowerOfTwo(mask + 1));
6193 size_t mask_bits = WhichPowerOf2(mask + 1);
6194
6195 if (mask_bits == 16u) {
6196 // Compare the bitstring in memory.
6197 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6198 } else {
6199 // /* uint32_t */ temp = temp->status_
6200 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6201 // Compare the bitstring bits using SUB.
6202 __ subl(temp, Immediate(path_to_root));
6203 // Shift out bits that do not contribute to the comparison.
6204 __ shll(temp, Immediate(32u - mask_bits));
6205 }
6206}
6207
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006208HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
6209 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006210 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006211 case HLoadClass::LoadKind::kInvalid:
6212 LOG(FATAL) << "UNREACHABLE";
6213 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006214 case HLoadClass::LoadKind::kReferrersClass:
6215 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006216 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006217 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01006219 case HLoadClass::LoadKind::kBssEntryPublic:
6220 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01006221 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006222 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006223 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006224 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006225 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006226 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006227 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006228 break;
6229 }
6230 return desired_class_load_kind;
6231}
6232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006233void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006234 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006235 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006236 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00006237 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006238 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00006239 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00006240 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006241 return;
6242 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01006243 DCHECK_EQ(cls->NeedsAccessCheck(),
6244 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
6245 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006246
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006247 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6248 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006249 ? LocationSummary::kCallOnSlowPath
6250 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006251 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006252 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006253 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006254 }
6255
Vladimir Marko41559982017-01-06 14:04:23 +00006256 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006257 locations->SetInAt(0, Location::RequiresRegister());
6258 }
6259 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006260 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6261 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6262 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006263 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006264 } else {
6265 // For non-Baker read barrier we have a temp-clobbering call.
6266 }
6267 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006268}
6269
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006270Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006271 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006272 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006273 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006274 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006275 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006276 PatchInfo<Label>* info = &jit_class_patches_.back();
6277 return &info->label;
6278}
6279
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006280// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6281// move.
6282void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006283 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006284 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006285 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006286 return;
6287 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01006288 DCHECK_EQ(cls->NeedsAccessCheck(),
6289 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
6290 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01006291
Vladimir Marko41559982017-01-06 14:04:23 +00006292 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006293 Location out_loc = locations->Out();
6294 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006296 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6297 ? kWithoutReadBarrier
6298 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006299 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006300 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006301 case HLoadClass::LoadKind::kReferrersClass: {
6302 DCHECK(!cls->CanCallRuntime());
6303 DCHECK(!cls->MustGenerateClinitCheck());
6304 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6305 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
6306 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006307 cls,
6308 out_loc,
6309 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006310 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006311 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006312 break;
6313 }
6314 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko44ca0752019-07-29 10:18:25 +01006315 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6316 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006317 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006318 __ leal(out,
6319 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006320 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006321 break;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006322 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006323 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006324 __ movl(out,
6325 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Markode91ca92020-10-27 13:41:40 +00006326 codegen_->RecordBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006327 break;
6328 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01006329 case HLoadClass::LoadKind::kBssEntry:
6330 case HLoadClass::LoadKind::kBssEntryPublic:
6331 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006332 Address address = Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006333 /* no_rip= */ false);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006334 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6335 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
6336 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006337 // No need for memory fence, thanks to the x86-64 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006338 generate_null_check = true;
6339 break;
6340 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006341 case HLoadClass::LoadKind::kJitBootImageAddress: {
6342 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6343 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6344 DCHECK_NE(address, 0u);
6345 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
6346 break;
6347 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006348 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006349 Address address = Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006350 /* no_rip= */ true);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006351 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006352 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006353 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006354 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006355 break;
6356 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006357 default:
6358 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
6359 UNREACHABLE();
6360 }
6361
6362 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6363 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006364 SlowPathCode* slow_path =
6365 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006366 codegen_->AddSlowPath(slow_path);
6367 if (generate_null_check) {
6368 __ testl(out, out);
6369 __ j(kEqual, slow_path->GetEntryLabel());
6370 }
6371 if (cls->MustGenerateClinitCheck()) {
6372 GenerateClassInitializationCheck(slow_path, out);
6373 } else {
6374 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006375 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006376 }
6377}
6378
6379void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
6380 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006381 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006382 locations->SetInAt(0, Location::RequiresRegister());
6383 if (check->HasUses()) {
6384 locations->SetOut(Location::SameAsFirstInput());
6385 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006386 // Rely on the type initialization to save everything we need.
6387 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006388}
6389
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006390void LocationsBuilderX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6391 // Custom calling convention: RAX serves as both input and output.
6392 Location location = Location::RegisterLocation(RAX);
6393 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6394}
6395
6396void InstructionCodeGeneratorX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6397 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6398}
6399
Orion Hodson18259d72018-04-12 11:18:23 +01006400void LocationsBuilderX86_64::VisitLoadMethodType(HLoadMethodType* load) {
6401 // Custom calling convention: RAX serves as both input and output.
6402 Location location = Location::RegisterLocation(RAX);
6403 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6404}
6405
6406void InstructionCodeGeneratorX86_64::VisitLoadMethodType(HLoadMethodType* load) {
6407 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6408}
6409
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006410void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006411 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006412 SlowPathCode* slow_path =
6413 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006414 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006415 GenerateClassInitializationCheck(slow_path,
6416 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006417}
6418
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006419HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
6420 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006421 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006422 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006423 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006424 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006425 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006426 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006427 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006428 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006429 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006430 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006431 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006432 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 }
6434 return desired_string_load_kind;
6435}
6436
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006437void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006438 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006439 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006440 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006441 locations->SetOut(Location::RegisterLocation(RAX));
6442 } else {
6443 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006444 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
6445 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006446 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006447 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006448 } else {
6449 // For non-Baker read barrier we have a temp-clobbering call.
6450 }
6451 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006452 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006453}
6454
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006455Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006456 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006457 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006458 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006459 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006460 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006461 PatchInfo<Label>* info = &jit_string_patches_.back();
6462 return &info->label;
6463}
6464
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006465// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6466// move.
6467void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006468 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 Location out_loc = locations->Out();
6470 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006472 switch (load->GetLoadKind()) {
6473 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006474 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6475 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006476 __ leal(out,
6477 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006478 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006479 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006480 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006481 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006482 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006483 __ movl(out,
6484 Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /* no_rip= */ false));
Vladimir Markode91ca92020-10-27 13:41:40 +00006485 codegen_->RecordBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006486 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006487 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006488 case HLoadString::LoadKind::kBssEntry: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006489 Address address = Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006490 /* no_rip= */ false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006491 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6492 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006493 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006494 // No need for memory fence, thanks to the x86-64 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006495 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006496 codegen_->AddSlowPath(slow_path);
6497 __ testl(out, out);
6498 __ j(kEqual, slow_path->GetEntryLabel());
6499 __ Bind(slow_path->GetExitLabel());
6500 return;
6501 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006502 case HLoadString::LoadKind::kJitBootImageAddress: {
6503 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6504 DCHECK_NE(address, 0u);
6505 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
6506 return;
6507 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006508 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006509 Address address = Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset,
Andreas Gampe3db70682018-12-26 15:12:03 -08006510 /* no_rip= */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006511 Label* fixup_label = codegen_->NewJitRootStringPatch(
6512 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006513 // /* GcRoot<mirror::String> */ out = *address
6514 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6515 return;
6516 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006517 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006518 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006521 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006522 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006523 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006524 codegen_->InvokeRuntime(kQuickResolveString,
6525 load,
6526 load->GetDexPc());
6527 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006528}
6529
David Brazdilcb1c0552015-08-04 16:22:25 +01006530static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006531 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08006532 /* no_rip= */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01006533}
6534
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006535void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
6536 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006537 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006538 locations->SetOut(Location::RequiresRegister());
6539}
6540
6541void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006542 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
6543}
6544
6545void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006546 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006547}
6548
6549void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6550 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006551}
6552
6553void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006554 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6555 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006556 InvokeRuntimeCallingConvention calling_convention;
6557 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6558}
6559
6560void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006561 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006562 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006563}
6564
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006565// Temp is used for read barrier.
6566static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6567 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006568 !kUseBakerReadBarrier &&
6569 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006570 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006571 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6572 return 1;
6573 }
6574 return 0;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006575}
6576
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006577// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6578// interface pointer, the current interface is compared in memory.
6579// The other checks have one temp for loading the object's class.
6580static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6581 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6582 return 2;
6583 }
6584 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006585}
6586
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006587void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006589 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006590 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 case TypeCheckKind::kExactCheck:
6593 case TypeCheckKind::kAbstractClassCheck:
6594 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006595 case TypeCheckKind::kArrayObjectCheck: {
6596 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6597 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6598 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006599 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006600 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006601 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602 case TypeCheckKind::kUnresolvedCheck:
6603 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006604 call_kind = LocationSummary::kCallOnSlowPath;
6605 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006606 case TypeCheckKind::kBitstringCheck:
6607 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006609
Vladimir Markoca6fff82017-10-03 14:49:14 +01006610 LocationSummary* locations =
6611 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006612 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006613 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006614 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006616 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6617 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6618 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6619 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6620 } else {
6621 locations->SetInAt(1, Location::Any());
6622 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
6624 locations->SetOut(Location::RequiresRegister());
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006625 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006626}
6627
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006628void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006629 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 Location obj_loc = locations->InAt(0);
6632 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006633 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006634 Location out_loc = locations->Out();
6635 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006636 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6637 DCHECK_LE(num_temps, 1u);
6638 Location maybe_temp_loc = (num_temps >= 1u) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006639 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006640 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6641 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6642 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006643 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006644 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006645
6646 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006647 // Avoid null check if we know obj is not null.
6648 if (instruction->MustDoNullCheck()) {
6649 __ testl(obj, obj);
6650 __ j(kEqual, &zero);
6651 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006652
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006653 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006654 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006655 ReadBarrierOption read_barrier_option =
6656 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006657 // /* HeapReference<Class> */ out = obj->klass_
6658 GenerateReferenceLoadTwoRegisters(instruction,
6659 out_loc,
6660 obj_loc,
6661 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006662 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006663 if (cls.IsRegister()) {
6664 __ cmpl(out, cls.AsRegister<CpuRegister>());
6665 } else {
6666 DCHECK(cls.IsStackSlot()) << cls;
6667 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6668 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006669 if (zero.IsLinked()) {
6670 // Classes must be equal for the instanceof to succeed.
6671 __ j(kNotEqual, &zero);
6672 __ movl(out, Immediate(1));
6673 __ jmp(&done);
6674 } else {
6675 __ setcc(kEqual, out);
6676 // setcc only sets the low byte.
6677 __ andl(out, Immediate(1));
6678 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006679 break;
6680 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006682 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006683 ReadBarrierOption read_barrier_option =
6684 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006685 // /* HeapReference<Class> */ out = obj->klass_
6686 GenerateReferenceLoadTwoRegisters(instruction,
6687 out_loc,
6688 obj_loc,
6689 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006690 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006691 // If the class is abstract, we eagerly fetch the super class of the
6692 // object to avoid doing a comparison we know will fail.
6693 NearLabel loop, success;
6694 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006696 GenerateReferenceLoadOneRegister(instruction,
6697 out_loc,
6698 super_offset,
6699 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006700 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006701 __ testl(out, out);
6702 // If `out` is null, we use it for the result, and jump to `done`.
6703 __ j(kEqual, &done);
6704 if (cls.IsRegister()) {
6705 __ cmpl(out, cls.AsRegister<CpuRegister>());
6706 } else {
6707 DCHECK(cls.IsStackSlot()) << cls;
6708 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6709 }
6710 __ j(kNotEqual, &loop);
6711 __ movl(out, Immediate(1));
6712 if (zero.IsLinked()) {
6713 __ jmp(&done);
6714 }
6715 break;
6716 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006717
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006718 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006719 ReadBarrierOption read_barrier_option =
6720 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006721 // /* HeapReference<Class> */ out = obj->klass_
6722 GenerateReferenceLoadTwoRegisters(instruction,
6723 out_loc,
6724 obj_loc,
6725 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006726 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727 // Walk over the class hierarchy to find a match.
6728 NearLabel loop, success;
6729 __ Bind(&loop);
6730 if (cls.IsRegister()) {
6731 __ cmpl(out, cls.AsRegister<CpuRegister>());
6732 } else {
6733 DCHECK(cls.IsStackSlot()) << cls;
6734 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6735 }
6736 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006738 GenerateReferenceLoadOneRegister(instruction,
6739 out_loc,
6740 super_offset,
6741 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006742 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006743 __ testl(out, out);
6744 __ j(kNotEqual, &loop);
6745 // If `out` is null, we use it for the result, and jump to `done`.
6746 __ jmp(&done);
6747 __ Bind(&success);
6748 __ movl(out, Immediate(1));
6749 if (zero.IsLinked()) {
6750 __ jmp(&done);
6751 }
6752 break;
6753 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006754
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006755 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006756 ReadBarrierOption read_barrier_option =
6757 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006758 // /* HeapReference<Class> */ out = obj->klass_
6759 GenerateReferenceLoadTwoRegisters(instruction,
6760 out_loc,
6761 obj_loc,
6762 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006763 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006764 // Do an exact check.
6765 NearLabel exact_check;
6766 if (cls.IsRegister()) {
6767 __ cmpl(out, cls.AsRegister<CpuRegister>());
6768 } else {
6769 DCHECK(cls.IsStackSlot()) << cls;
6770 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6771 }
6772 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006773 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006774 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006775 GenerateReferenceLoadOneRegister(instruction,
6776 out_loc,
6777 component_offset,
6778 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006779 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006780 __ testl(out, out);
6781 // If `out` is null, we use it for the result, and jump to `done`.
6782 __ j(kEqual, &done);
6783 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6784 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006785 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006786 __ movl(out, Immediate(1));
6787 __ jmp(&done);
6788 break;
6789 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006790
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006791 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006792 // No read barrier since the slow path will retry upon failure.
6793 // /* HeapReference<Class> */ out = obj->klass_
6794 GenerateReferenceLoadTwoRegisters(instruction,
6795 out_loc,
6796 obj_loc,
6797 class_offset,
6798 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006799 if (cls.IsRegister()) {
6800 __ cmpl(out, cls.AsRegister<CpuRegister>());
6801 } else {
6802 DCHECK(cls.IsStackSlot()) << cls;
6803 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6804 }
6805 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006806 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08006807 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006808 codegen_->AddSlowPath(slow_path);
6809 __ j(kNotEqual, slow_path->GetEntryLabel());
6810 __ movl(out, Immediate(1));
6811 if (zero.IsLinked()) {
6812 __ jmp(&done);
6813 }
6814 break;
6815 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006816
Calin Juravle98893e12015-10-02 21:05:03 +01006817 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006818 case TypeCheckKind::kInterfaceCheck: {
6819 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006820 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006821 // cases.
6822 //
6823 // We cannot directly call the InstanceofNonTrivial runtime
6824 // entry point without resorting to a type checking slow path
6825 // here (i.e. by calling InvokeRuntime directly), as it would
6826 // require to assign fixed registers for the inputs of this
6827 // HInstanceOf instruction (following the runtime calling
6828 // convention), which might be cluttered by the potential first
6829 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006830 //
6831 // TODO: Introduce a new runtime entry point taking the object
6832 // to test (instead of its class) as argument, and let it deal
6833 // with the read barrier issues. This will let us refactor this
6834 // case of the `switch` code as it was previously (with a direct
6835 // call to the runtime not using a type checking slow path).
6836 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006838 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08006839 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006840 codegen_->AddSlowPath(slow_path);
6841 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006842 if (zero.IsLinked()) {
6843 __ jmp(&done);
6844 }
6845 break;
6846 }
Vladimir Marko175e7862018-03-27 09:03:13 +00006847
6848 case TypeCheckKind::kBitstringCheck: {
6849 // /* HeapReference<Class> */ temp = obj->klass_
6850 GenerateReferenceLoadTwoRegisters(instruction,
6851 out_loc,
6852 obj_loc,
6853 class_offset,
6854 kWithoutReadBarrier);
6855
6856 GenerateBitstringTypeCheckCompare(instruction, out);
6857 if (zero.IsLinked()) {
6858 __ j(kNotEqual, &zero);
6859 __ movl(out, Immediate(1));
6860 __ jmp(&done);
6861 } else {
6862 __ setcc(kEqual, out);
6863 // setcc only sets the low byte.
6864 __ andl(out, Immediate(1));
6865 }
6866 break;
6867 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006868 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006869
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006870 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006871 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006872 __ xorl(out, out);
6873 }
6874
6875 if (done.IsLinked()) {
6876 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006877 }
6878
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006879 if (slow_path != nullptr) {
6880 __ Bind(slow_path->GetExitLabel());
6881 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006882}
6883
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006884void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006885 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006886 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006887 LocationSummary* locations =
6888 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006889 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006890 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6891 // Require a register for the interface check since there is a loop that compares the class to
6892 // a memory address.
6893 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006894 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6895 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6896 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6897 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006898 } else {
6899 locations->SetInAt(1, Location::Any());
6900 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006901 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
6902 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006903}
6904
6905void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006906 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006907 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006908 Location obj_loc = locations->InAt(0);
6909 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006910 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006911 Location temp_loc = locations->GetTemp(0);
6912 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006913 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6914 DCHECK_GE(num_temps, 1u);
6915 DCHECK_LE(num_temps, 2u);
6916 Location maybe_temp2_loc = (num_temps >= 2u) ? locations->GetTemp(1) : Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006917 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6918 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6919 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6920 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6921 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6922 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006923 const uint32_t object_array_data_offset =
6924 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006925
Vladimir Marko87584542017-12-12 17:47:52 +00006926 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006927 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006928 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6929 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006930 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006931
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006932
6933 NearLabel done;
6934 // Avoid null check if we know obj is not null.
6935 if (instruction->MustDoNullCheck()) {
6936 __ testl(obj, obj);
6937 __ j(kEqual, &done);
6938 }
6939
Roland Levillain0d5a2812015-11-13 10:07:31 +00006940 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006941 case TypeCheckKind::kExactCheck:
6942 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006943 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006944 GenerateReferenceLoadTwoRegisters(instruction,
6945 temp_loc,
6946 obj_loc,
6947 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006948 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006949 if (cls.IsRegister()) {
6950 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6951 } else {
6952 DCHECK(cls.IsStackSlot()) << cls;
6953 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6954 }
6955 // Jump to slow path for throwing the exception or doing a
6956 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006957 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006958 break;
6959 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006961 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006962 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006963 GenerateReferenceLoadTwoRegisters(instruction,
6964 temp_loc,
6965 obj_loc,
6966 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006967 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006968 // If the class is abstract, we eagerly fetch the super class of the
6969 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006970 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006971 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006972 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006973 GenerateReferenceLoadOneRegister(instruction,
6974 temp_loc,
6975 super_offset,
6976 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006977 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006978
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006979 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6980 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006981 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006982 // Otherwise, compare the classes.
6983 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006984 if (cls.IsRegister()) {
6985 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6986 } else {
6987 DCHECK(cls.IsStackSlot()) << cls;
6988 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6989 }
6990 __ j(kNotEqual, &loop);
6991 break;
6992 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006993
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006994 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006995 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006996 GenerateReferenceLoadTwoRegisters(instruction,
6997 temp_loc,
6998 obj_loc,
6999 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007000 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007001 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007002 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007003 __ Bind(&loop);
7004 if (cls.IsRegister()) {
7005 __ cmpl(temp, cls.AsRegister<CpuRegister>());
7006 } else {
7007 DCHECK(cls.IsStackSlot()) << cls;
7008 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
7009 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007010 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007011
Roland Levillain0d5a2812015-11-13 10:07:31 +00007012 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007013 GenerateReferenceLoadOneRegister(instruction,
7014 temp_loc,
7015 super_offset,
7016 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007017 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007018
7019 // If the class reference currently in `temp` is not null, jump
7020 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007021 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007022 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007023 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007024 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007025 break;
7026 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007027
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007028 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00007029 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07007030 GenerateReferenceLoadTwoRegisters(instruction,
7031 temp_loc,
7032 obj_loc,
7033 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007034 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007035 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007036 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007037 if (cls.IsRegister()) {
7038 __ cmpl(temp, cls.AsRegister<CpuRegister>());
7039 } else {
7040 DCHECK(cls.IsStackSlot()) << cls;
7041 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
7042 }
7043 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007044
7045 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007046 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007047 GenerateReferenceLoadOneRegister(instruction,
7048 temp_loc,
7049 component_offset,
7050 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007051 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007052
7053 // If the component type is not null (i.e. the object is indeed
7054 // an array), jump to label `check_non_primitive_component_type`
7055 // to further check that this component type is not a primitive
7056 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007057 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007058 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007059 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007060 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007061 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007062 break;
7063 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007064
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007065 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07007066 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007067 //
7068 // We cannot directly call the CheckCast runtime entry point
7069 // without resorting to a type checking slow path here (i.e. by
7070 // calling InvokeRuntime directly), as it would require to
7071 // assign fixed registers for the inputs of this HInstanceOf
7072 // instruction (following the runtime calling convention), which
7073 // might be cluttered by the potential first read barrier
7074 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007075 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007076 break;
7077 }
7078
Vladimir Marko175e7862018-03-27 09:03:13 +00007079 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007080 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7081 // We can not get false positives by doing this.
7082 // /* HeapReference<Class> */ temp = obj->klass_
7083 GenerateReferenceLoadTwoRegisters(instruction,
7084 temp_loc,
7085 obj_loc,
7086 class_offset,
7087 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07007088
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007089 // /* HeapReference<Class> */ temp = temp->iftable_
7090 GenerateReferenceLoadTwoRegisters(instruction,
7091 temp_loc,
7092 temp_loc,
7093 iftable_offset,
7094 kWithoutReadBarrier);
7095 // Iftable is never null.
7096 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
7097 // Maybe poison the `cls` for direct comparison with memory.
7098 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
7099 // Loop through the iftable and check if any class matches.
7100 NearLabel start_loop;
7101 __ Bind(&start_loop);
7102 // Need to subtract first to handle the empty array case.
7103 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
7104 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7105 // Go to next interface if the classes do not match.
7106 __ cmpl(cls.AsRegister<CpuRegister>(),
7107 CodeGeneratorX86_64::ArrayAddress(temp,
7108 maybe_temp2_loc,
7109 TIMES_4,
7110 object_array_data_offset));
7111 __ j(kNotEqual, &start_loop); // Return if same class.
7112 // If `cls` was poisoned above, unpoison it.
7113 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007114 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007115 }
7116
7117 case TypeCheckKind::kBitstringCheck: {
7118 // /* HeapReference<Class> */ temp = obj->klass_
7119 GenerateReferenceLoadTwoRegisters(instruction,
7120 temp_loc,
7121 obj_loc,
7122 class_offset,
7123 kWithoutReadBarrier);
7124
7125 GenerateBitstringTypeCheckCompare(instruction, temp);
7126 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7127 break;
7128 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007129 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007131 if (done.IsLinked()) {
7132 __ Bind(&done);
7133 }
7134
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007136}
7137
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007138void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007139 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7140 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007141 InvokeRuntimeCallingConvention calling_convention;
7142 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7143}
7144
7145void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007146 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007147 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007148 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007149 if (instruction->IsEnter()) {
7150 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7151 } else {
7152 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7153 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007154}
7155
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307156void LocationsBuilderX86_64::VisitX86AndNot(HX86AndNot* instruction) {
7157 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7158 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7159 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7160 locations->SetInAt(0, Location::RequiresRegister());
7161 // There is no immediate variant of negated bitwise and in X86.
7162 locations->SetInAt(1, Location::RequiresRegister());
7163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7164}
7165
7166void LocationsBuilderX86_64::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7167 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7168 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7169 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7170 locations->SetInAt(0, Location::RequiresRegister());
7171 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7172}
7173
7174void InstructionCodeGeneratorX86_64::VisitX86AndNot(HX86AndNot* instruction) {
7175 LocationSummary* locations = instruction->GetLocations();
7176 Location first = locations->InAt(0);
7177 Location second = locations->InAt(1);
7178 Location dest = locations->Out();
7179 __ andn(dest.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
7180}
7181
7182void InstructionCodeGeneratorX86_64::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7183 LocationSummary* locations = instruction->GetLocations();
7184 Location src = locations->InAt(0);
7185 Location dest = locations->Out();
7186 switch (instruction->GetOpKind()) {
7187 case HInstruction::kAnd:
7188 __ blsr(dest.AsRegister<CpuRegister>(), src.AsRegister<CpuRegister>());
7189 break;
7190 case HInstruction::kXor:
7191 __ blsmsk(dest.AsRegister<CpuRegister>(), src.AsRegister<CpuRegister>());
7192 break;
7193 default:
7194 LOG(FATAL) << "Unreachable";
7195 }
7196}
7197
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007198void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7199void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7200void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7201
7202void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
7203 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007204 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007205 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7206 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007207 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04007208 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007209 locations->SetOut(Location::SameAsFirstInput());
7210}
7211
7212void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
7213 HandleBitwiseOperation(instruction);
7214}
7215
7216void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
7217 HandleBitwiseOperation(instruction);
7218}
7219
7220void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
7221 HandleBitwiseOperation(instruction);
7222}
7223
7224void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
7225 LocationSummary* locations = instruction->GetLocations();
7226 Location first = locations->InAt(0);
7227 Location second = locations->InAt(1);
7228 DCHECK(first.Equals(locations->Out()));
7229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007230 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007231 if (second.IsRegister()) {
7232 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007233 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007234 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007235 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007236 } else {
7237 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007238 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007239 }
7240 } else if (second.IsConstant()) {
7241 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
7242 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007243 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007244 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007245 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007246 } else {
7247 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007248 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007249 }
7250 } else {
7251 Address address(CpuRegister(RSP), second.GetStackIndex());
7252 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007253 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007254 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007255 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007256 } else {
7257 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007258 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007259 }
7260 }
7261 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007262 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007263 CpuRegister first_reg = first.AsRegister<CpuRegister>();
7264 bool second_is_constant = false;
7265 int64_t value = 0;
7266 if (second.IsConstant()) {
7267 second_is_constant = true;
7268 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007269 }
Mark Mendell40741f32015-04-20 22:10:34 -04007270 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007271
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007272 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007273 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04007274 if (is_int32_value) {
7275 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
7276 } else {
7277 __ andq(first_reg, codegen_->LiteralInt64Address(value));
7278 }
7279 } else if (second.IsDoubleStackSlot()) {
7280 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007281 } else {
7282 __ andq(first_reg, second.AsRegister<CpuRegister>());
7283 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007284 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007285 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04007286 if (is_int32_value) {
7287 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
7288 } else {
7289 __ orq(first_reg, codegen_->LiteralInt64Address(value));
7290 }
7291 } else if (second.IsDoubleStackSlot()) {
7292 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007293 } else {
7294 __ orq(first_reg, second.AsRegister<CpuRegister>());
7295 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007296 } else {
7297 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007298 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04007299 if (is_int32_value) {
7300 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
7301 } else {
7302 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
7303 }
7304 } else if (second.IsDoubleStackSlot()) {
7305 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007306 } else {
7307 __ xorq(first_reg, second.AsRegister<CpuRegister>());
7308 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007309 }
7310 }
7311}
7312
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007313void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
7314 HInstruction* instruction,
7315 Location out,
7316 uint32_t offset,
7317 Location maybe_temp,
7318 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007319 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007320 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007321 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007322 if (kUseBakerReadBarrier) {
7323 // Load with fast path based Baker's read barrier.
7324 // /* HeapReference<Object> */ out = *(out + offset)
7325 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007326 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007327 } else {
7328 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007329 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007330 // in the following move operation, as we will need it for the
7331 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007332 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007333 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007334 // /* HeapReference<Object> */ out = *(out + offset)
7335 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007336 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007337 }
7338 } else {
7339 // Plain load with no read barrier.
7340 // /* HeapReference<Object> */ out = *(out + offset)
7341 __ movl(out_reg, Address(out_reg, offset));
7342 __ MaybeUnpoisonHeapReference(out_reg);
7343 }
7344}
7345
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007346void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
7347 HInstruction* instruction,
7348 Location out,
7349 Location obj,
7350 uint32_t offset,
7351 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007352 CpuRegister out_reg = out.AsRegister<CpuRegister>();
7353 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007354 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007355 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007356 if (kUseBakerReadBarrier) {
7357 // Load with fast path based Baker's read barrier.
7358 // /* HeapReference<Object> */ out = *(obj + offset)
7359 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007360 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007361 } else {
7362 // Load with slow path based read barrier.
7363 // /* HeapReference<Object> */ out = *(obj + offset)
7364 __ movl(out_reg, Address(obj_reg, offset));
7365 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7366 }
7367 } else {
7368 // Plain load with no read barrier.
7369 // /* HeapReference<Object> */ out = *(obj + offset)
7370 __ movl(out_reg, Address(obj_reg, offset));
7371 __ MaybeUnpoisonHeapReference(out_reg);
7372 }
7373}
7374
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007375void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
7376 HInstruction* instruction,
7377 Location root,
7378 const Address& address,
7379 Label* fixup_label,
7380 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007381 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007382 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007383 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007384 if (kUseBakerReadBarrier) {
7385 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7386 // Baker's read barrier are used:
7387 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007388 // root = obj.field;
7389 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7390 // if (temp != null) {
7391 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007392 // }
7393
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007394 // /* GcRoot<mirror::Object> */ root = *address
7395 __ movl(root_reg, address);
7396 if (fixup_label != nullptr) {
7397 __ Bind(fixup_label);
7398 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007399 static_assert(
7400 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7401 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7402 "have different sizes.");
7403 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7404 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7405 "have different sizes.");
7406
Vladimir Marko953437b2016-08-24 08:30:46 +00007407 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007408 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08007409 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007410 codegen_->AddSlowPath(slow_path);
7411
Roland Levillaind966ce72017-02-09 16:20:14 +00007412 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
7413 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007414 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Andreas Gampe3db70682018-12-26 15:12:03 -08007415 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip= */ true), Immediate(0));
Roland Levillaind966ce72017-02-09 16:20:14 +00007416 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007417 __ j(kNotEqual, slow_path->GetEntryLabel());
7418 __ Bind(slow_path->GetExitLabel());
7419 } else {
7420 // GC root loaded through a slow path for read barriers other
7421 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007422 // /* GcRoot<mirror::Object>* */ root = address
7423 __ leaq(root_reg, address);
7424 if (fixup_label != nullptr) {
7425 __ Bind(fixup_label);
7426 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007427 // /* mirror::Object* */ root = root->Read()
7428 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7429 }
7430 } else {
7431 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007432 // /* GcRoot<mirror::Object> */ root = *address
7433 __ movl(root_reg, address);
7434 if (fixup_label != nullptr) {
7435 __ Bind(fixup_label);
7436 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007437 // Note that GC roots are not affected by heap poisoning, thus we
7438 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007439 }
7440}
7441
7442void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7443 Location ref,
7444 CpuRegister obj,
7445 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007446 bool needs_null_check) {
7447 DCHECK(kEmitCompilerReadBarrier);
7448 DCHECK(kUseBakerReadBarrier);
7449
7450 // /* HeapReference<Object> */ ref = *(obj + offset)
7451 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007452 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007453}
7454
7455void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7456 Location ref,
7457 CpuRegister obj,
7458 uint32_t data_offset,
7459 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007460 bool needs_null_check) {
7461 DCHECK(kEmitCompilerReadBarrier);
7462 DCHECK(kUseBakerReadBarrier);
7463
Roland Levillain3d312422016-06-23 13:53:42 +01007464 static_assert(
7465 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7466 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007467 // /* HeapReference<Object> */ ref =
7468 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007469 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007470 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007471}
7472
7473void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7474 Location ref,
7475 CpuRegister obj,
7476 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007477 bool needs_null_check,
7478 bool always_update_field,
7479 CpuRegister* temp1,
7480 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007481 DCHECK(kEmitCompilerReadBarrier);
7482 DCHECK(kUseBakerReadBarrier);
7483
7484 // In slow path based read barriers, the read barrier call is
7485 // inserted after the original load. However, in fast path based
7486 // Baker's read barriers, we need to perform the load of
7487 // mirror::Object::monitor_ *before* the original reference load.
7488 // This load-load ordering is required by the read barrier.
7489 // The fast path/slow path (for Baker's algorithm) should look like:
7490 //
7491 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7492 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7493 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007494 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007495 // if (is_gray) {
7496 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7497 // }
7498 //
7499 // Note: the original implementation in ReadBarrier::Barrier is
7500 // slightly more complex as:
7501 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007502 // the high-bits of rb_state, which are expected to be all zeroes
7503 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
7504 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007505 // - it performs additional checks that we do not do here for
7506 // performance reasons.
7507
7508 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007509 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7510
Vladimir Marko953437b2016-08-24 08:30:46 +00007511 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007512 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007513 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007514 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7515 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7516 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7517
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007518 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007519 // ref = ReadBarrier::Mark(ref);
7520 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7521 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007522 if (needs_null_check) {
7523 MaybeRecordImplicitNullCheck(instruction);
7524 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007525
7526 // Load fence to prevent load-load reordering.
7527 // Note that this is a no-op, thanks to the x86-64 memory model.
7528 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7529
7530 // The actual reference load.
7531 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007532 __ movl(ref_reg, src); // Flags are unaffected.
7533
7534 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7535 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007536 SlowPathCode* slow_path;
7537 if (always_update_field) {
7538 DCHECK(temp1 != nullptr);
7539 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007540 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08007541 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp1, *temp2);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007542 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007543 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Andreas Gampe3db70682018-12-26 15:12:03 -08007544 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007545 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007546 AddSlowPath(slow_path);
7547
7548 // We have done the "if" of the gray bit check above, now branch based on the flags.
7549 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007550
7551 // Object* ref = ref_addr->AsMirrorPtr()
7552 __ MaybeUnpoisonHeapReference(ref_reg);
7553
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007554 __ Bind(slow_path->GetExitLabel());
7555}
7556
7557void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
7558 Location out,
7559 Location ref,
7560 Location obj,
7561 uint32_t offset,
7562 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007563 DCHECK(kEmitCompilerReadBarrier);
7564
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007565 // Insert a slow path based read barrier *after* the reference load.
7566 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007567 // If heap poisoning is enabled, the unpoisoning of the loaded
7568 // reference will be carried out by the runtime within the slow
7569 // path.
7570 //
7571 // Note that `ref` currently does not get unpoisoned (when heap
7572 // poisoning is enabled), which is alright as the `ref` argument is
7573 // not used by the artReadBarrierSlow entry point.
7574 //
7575 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007576 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007577 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
7578 AddSlowPath(slow_path);
7579
Roland Levillain0d5a2812015-11-13 10:07:31 +00007580 __ jmp(slow_path->GetEntryLabel());
7581 __ Bind(slow_path->GetExitLabel());
7582}
7583
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007584void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7585 Location out,
7586 Location ref,
7587 Location obj,
7588 uint32_t offset,
7589 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007590 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007591 // Baker's read barriers shall be handled by the fast path
7592 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
7593 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007594 // If heap poisoning is enabled, unpoisoning will be taken care of
7595 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007596 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007597 } else if (kPoisonHeapReferences) {
7598 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
7599 }
7600}
7601
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007602void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7603 Location out,
7604 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007605 DCHECK(kEmitCompilerReadBarrier);
7606
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007607 // Insert a slow path based read barrier *after* the GC root load.
7608 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007609 // Note that GC roots are not affected by heap poisoning, so we do
7610 // not need to do anything special for this here.
7611 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007612 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007613 AddSlowPath(slow_path);
7614
Roland Levillain0d5a2812015-11-13 10:07:31 +00007615 __ jmp(slow_path->GetEntryLabel());
7616 __ Bind(slow_path->GetExitLabel());
7617}
7618
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007619void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007620 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007621 LOG(FATAL) << "Unreachable";
7622}
7623
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007624void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007625 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007626 LOG(FATAL) << "Unreachable";
7627}
7628
Mark Mendellfe57faa2015-09-18 09:26:15 -04007629// Simple implementation of packed switch - generate cascaded compare/jumps.
7630void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7631 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007632 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007633 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04007634 locations->AddTemp(Location::RequiresRegister());
7635 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04007636}
7637
7638void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7639 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007640 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007641 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04007642 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
7643 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
7644 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007645 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7646
7647 // Should we generate smaller inline compare/jumps?
7648 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7649 // Figure out the correct compare values and jump conditions.
7650 // Handle the first compare/branch as a special case because it might
7651 // jump to the default case.
7652 DCHECK_GT(num_entries, 2u);
7653 Condition first_condition;
7654 uint32_t index;
7655 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7656 if (lower_bound != 0) {
7657 first_condition = kLess;
7658 __ cmpl(value_reg_in, Immediate(lower_bound));
7659 __ j(first_condition, codegen_->GetLabelOf(default_block));
7660 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
7661
7662 index = 1;
7663 } else {
7664 // Handle all the compare/jumps below.
7665 first_condition = kBelow;
7666 index = 0;
7667 }
7668
7669 // Handle the rest of the compare/jumps.
7670 for (; index + 1 < num_entries; index += 2) {
7671 int32_t compare_to_value = lower_bound + index + 1;
7672 __ cmpl(value_reg_in, Immediate(compare_to_value));
7673 // Jump to successors[index] if value < case_value[index].
7674 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7675 // Jump to successors[index + 1] if value == case_value[index + 1].
7676 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7677 }
7678
7679 if (index != num_entries) {
7680 // There are an odd number of entries. Handle the last one.
7681 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00007682 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007683 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
7684 }
7685
7686 // And the default for any other value.
7687 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7688 __ jmp(codegen_->GetLabelOf(default_block));
7689 }
7690 return;
7691 }
Mark Mendell9c86b482015-09-18 13:36:07 -04007692
7693 // Remove the bias, if needed.
7694 Register value_reg_out = value_reg_in.AsRegister();
7695 if (lower_bound != 0) {
7696 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
7697 value_reg_out = temp_reg.AsRegister();
7698 }
7699 CpuRegister value_reg(value_reg_out);
7700
7701 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04007702 __ cmpl(value_reg, Immediate(num_entries - 1));
7703 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007704
Mark Mendell9c86b482015-09-18 13:36:07 -04007705 // We are in the range of the table.
7706 // Load the address of the jump table in the constant area.
7707 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007708
Mark Mendell9c86b482015-09-18 13:36:07 -04007709 // Load the (signed) offset from the jump table.
7710 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
7711
7712 // Add the offset to the address of the table base.
7713 __ addq(temp_reg, base_reg);
7714
7715 // And jump.
7716 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007717}
7718
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007719void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7720 ATTRIBUTE_UNUSED) {
7721 LOG(FATAL) << "Unreachable";
7722}
7723
7724void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7725 ATTRIBUTE_UNUSED) {
7726 LOG(FATAL) << "Unreachable";
7727}
7728
Aart Bikc5d47542016-01-27 17:00:35 -08007729void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
7730 if (value == 0) {
7731 __ xorl(dest, dest);
7732 } else {
7733 __ movl(dest, Immediate(value));
7734 }
7735}
7736
Mark Mendell92e83bf2015-05-07 11:25:03 -04007737void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
7738 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08007739 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007740 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00007741 } else if (IsUint<32>(value)) {
7742 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007743 __ movl(dest, Immediate(static_cast<int32_t>(value)));
7744 } else {
7745 __ movq(dest, Immediate(value));
7746 }
7747}
7748
Mark Mendell7c0b44f2016-02-01 10:08:35 -05007749void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
7750 if (value == 0) {
7751 __ xorps(dest, dest);
7752 } else {
7753 __ movss(dest, LiteralInt32Address(value));
7754 }
7755}
7756
7757void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
7758 if (value == 0) {
7759 __ xorpd(dest, dest);
7760 } else {
7761 __ movsd(dest, LiteralInt64Address(value));
7762 }
7763}
7764
7765void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
7766 Load32BitValue(dest, bit_cast<int32_t, float>(value));
7767}
7768
7769void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
7770 Load64BitValue(dest, bit_cast<int64_t, double>(value));
7771}
7772
Aart Bika19616e2016-02-01 18:57:58 -08007773void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
7774 if (value == 0) {
7775 __ testl(dest, dest);
7776 } else {
7777 __ cmpl(dest, Immediate(value));
7778 }
7779}
7780
7781void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
7782 if (IsInt<32>(value)) {
7783 if (value == 0) {
7784 __ testq(dest, dest);
7785 } else {
7786 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7787 }
7788 } else {
7789 // Value won't fit in an int.
7790 __ cmpq(dest, LiteralInt64Address(value));
7791 }
7792}
7793
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007794void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7795 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007796 GenerateIntCompare(lhs_reg, rhs);
7797}
7798
7799void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007800 if (rhs.IsConstant()) {
7801 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007802 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007803 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007804 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007805 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007806 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007807 }
7808}
7809
7810void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7811 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7812 if (rhs.IsConstant()) {
7813 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7814 Compare64BitValue(lhs_reg, value);
7815 } else if (rhs.IsDoubleStackSlot()) {
7816 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7817 } else {
7818 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7819 }
7820}
7821
7822Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7823 Location index,
7824 ScaleFactor scale,
7825 uint32_t data_offset) {
7826 return index.IsConstant() ?
7827 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7828 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7829}
7830
Mark Mendellcfa410b2015-05-25 16:02:44 -04007831void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7832 DCHECK(dest.IsDoubleStackSlot());
7833 if (IsInt<32>(value)) {
7834 // Can move directly as an int32 constant.
7835 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7836 Immediate(static_cast<int32_t>(value)));
7837 } else {
7838 Load64BitValue(CpuRegister(TMP), value);
7839 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7840 }
7841}
7842
Mark Mendell9c86b482015-09-18 13:36:07 -04007843/**
7844 * Class to handle late fixup of offsets into constant area.
7845 */
7846class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7847 public:
7848 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7849 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7850
7851 protected:
7852 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7853
7854 CodeGeneratorX86_64* codegen_;
7855
7856 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007857 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell9c86b482015-09-18 13:36:07 -04007858 // Patch the correct offset for the instruction. We use the address of the
7859 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7860 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7861 int32_t relative_position = constant_offset - pos;
7862
7863 // Patch in the right value.
7864 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7865 }
7866
7867 // Location in constant area that the fixup refers to.
7868 size_t offset_into_constant_area_;
7869};
7870
7871/**
7872 t * Class to handle late fixup of offsets to a jump table that will be created in the
7873 * constant area.
7874 */
7875class JumpTableRIPFixup : public RIPFixup {
7876 public:
7877 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7878 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7879
7880 void CreateJumpTable() {
7881 X86_64Assembler* assembler = codegen_->GetAssembler();
7882
7883 // Ensure that the reference to the jump table has the correct offset.
7884 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7885 SetOffset(offset_in_constant_table);
7886
7887 // Compute the offset from the start of the function to this jump table.
7888 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7889
7890 // Populate the jump table with the correct values for the jump table.
7891 int32_t num_entries = switch_instr_->GetNumEntries();
7892 HBasicBlock* block = switch_instr_->GetBlock();
7893 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7894 // The value that we want is the target offset - the position of the table.
7895 for (int32_t i = 0; i < num_entries; i++) {
7896 HBasicBlock* b = successors[i];
7897 Label* l = codegen_->GetLabelOf(b);
7898 DCHECK(l->IsBound());
7899 int32_t offset_to_block = l->Position() - current_table_offset;
7900 assembler->AppendInt32(offset_to_block);
7901 }
7902 }
7903
7904 private:
7905 const HPackedSwitch* switch_instr_;
7906};
7907
Mark Mendellf55c3e02015-03-26 21:07:46 -04007908void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7909 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007910 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007911 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7912 // 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 -04007913 assembler->Align(4, 0);
7914 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007915
7916 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007917 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007918 jump_table->CreateJumpTable();
7919 }
7920
7921 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007922 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007923 }
7924
7925 // And finish up.
7926 CodeGenerator::Finalize(allocator);
7927}
7928
Mark Mendellf55c3e02015-03-26 21:07:46 -04007929Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007930 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007931 return Address::RIP(fixup);
7932}
7933
7934Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007935 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007936 return Address::RIP(fixup);
7937}
7938
7939Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007940 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007941 return Address::RIP(fixup);
7942}
7943
7944Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007945 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007946 return Address::RIP(fixup);
7947}
7948
Andreas Gampe85b62f22015-09-09 13:15:38 -07007949// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007950void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007951 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007952 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007953 return;
7954 }
7955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007956 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007957
7958 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7959 if (trg.Equals(return_loc)) {
7960 return;
7961 }
7962
7963 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007964 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007965 parallel_move.AddMove(return_loc, trg, type, nullptr);
7966 GetMoveResolver()->EmitNativeCode(&parallel_move);
7967}
7968
Mark Mendell9c86b482015-09-18 13:36:07 -04007969Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7970 // Create a fixup to be used to create and address the jump table.
7971 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007972 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007973
7974 // We have to populate the jump tables.
7975 fixups_to_jump_tables_.push_back(table_fixup);
7976 return Address::RIP(table_fixup);
7977}
7978
Mark Mendellea5af682015-10-22 17:35:49 -04007979void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7980 const Address& addr_high,
7981 int64_t v,
7982 HInstruction* instruction) {
7983 if (IsInt<32>(v)) {
7984 int32_t v_32 = v;
7985 __ movq(addr_low, Immediate(v_32));
7986 MaybeRecordImplicitNullCheck(instruction);
7987 } else {
7988 // Didn't fit in a register. Do it in pieces.
7989 int32_t low_v = Low32Bits(v);
7990 int32_t high_v = High32Bits(v);
7991 __ movl(addr_low, Immediate(low_v));
7992 MaybeRecordImplicitNullCheck(instruction);
7993 __ movl(addr_high, Immediate(high_v));
7994 }
7995}
7996
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007997void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7998 const uint8_t* roots_data,
7999 const PatchInfo<Label>& info,
8000 uint64_t index_in_table) const {
8001 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8002 uintptr_t address =
8003 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008004 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008005 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8006 dchecked_integral_cast<uint32_t>(address);
8007}
8008
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008009void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8010 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008011 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008012 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008013 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008014 }
8015
8016 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008017 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008018 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008019 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008020 }
8021}
8022
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308023bool LocationsBuilderX86_64::CpuHasAvxFeatureFlag() {
8024 return codegen_->GetInstructionSetFeatures().HasAVX();
8025}
8026
8027bool LocationsBuilderX86_64::CpuHasAvx2FeatureFlag() {
8028 return codegen_->GetInstructionSetFeatures().HasAVX2();
8029}
8030
8031bool InstructionCodeGeneratorX86_64::CpuHasAvxFeatureFlag() {
8032 return codegen_->GetInstructionSetFeatures().HasAVX();
8033}
8034
8035bool InstructionCodeGeneratorX86_64::CpuHasAvx2FeatureFlag() {
8036 return codegen_->GetInstructionSetFeatures().HasAVX2();
8037}
8038
Roland Levillain4d027112015-07-01 15:41:14 +01008039#undef __
8040
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01008041} // namespace x86_64
8042} // namespace art