blob: 0bb56a2b4a5a7605840ecf56684b5572d684dc48 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070025#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080026#include "intrinsics.h"
27#include "intrinsics_x86_64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010028#include "linker/linker_patch.h"
Andreas Gamped4901292017-05-30 18:41:34 -070029#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "mirror/object_reference.h"
33#include "thread.h"
34#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036#include "utils/x86_64/assembler_x86_64.h"
37#include "utils/x86_64/managed_register_x86_64.h"
38
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010039namespace art {
40
Roland Levillain0d5a2812015-11-13 10:07:31 +000041template<class MirrorType>
42class GcRoot;
43
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010044namespace x86_64 {
45
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000048// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
49// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
50// generates less code/data with a small num_entries.
51static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000053static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000054static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Mark Mendell24f2dfa2015-01-14 19:51:45 -050056static constexpr int kC2ConditionMask = 0x400;
57
Roland Levillain7cbd27f2016-08-11 23:53:33 +010058// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
59#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000064 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames2ed20af2015-03-06 13:55:35 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000067 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010073 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000074 instruction_,
75 instruction_->GetDexPc(),
76 this);
Roland Levillain888d0672015-11-23 18:53:50 +000077 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 }
79
Alexandre Rames8158f282015-08-07 10:26:17 +010080 bool IsFatal() const OVERRIDE { return true; }
81
Alexandre Rames9931f312015-06-19 14:47:01 +010082 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
83
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
86};
87
Andreas Gampe85b62f22015-09-09 13:15:38 -070088class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000089 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000090 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000091
Alexandre Rames2ed20af2015-03-06 13:55:35 +000092 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000093 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000094 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010095 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100109 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, DataType::Type type, bool is_div)
David Srbecky9cd6d372016-02-09 15:24:47 +0000110 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100114 if (type_ == DataType::Type::kInt32) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negl(cpu_reg_);
117 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400118 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 }
120
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000121 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 DCHECK_EQ(DataType::Type::kInt64, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 if (is_div_) {
124 __ negq(cpu_reg_);
125 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400126 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000128 }
Calin Juravled0d48522014-11-04 16:40:20 +0000129 __ jmp(GetExitLabel());
130 }
131
Alexandre Rames9931f312015-06-19 14:47:01 +0100132 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
133
Calin Juravled0d48522014-11-04 16:40:20 +0000134 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const CpuRegister cpu_reg_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 const DataType::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 const bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Andreas Gampe85b62f22015-09-09 13:15:38 -0700141class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100143 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700147 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000148 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700150 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100151 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000152 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700153 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 if (successor_ == nullptr) {
155 __ jmp(GetReturnLabel());
156 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000157 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 }
160
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 Label* GetReturnLabel() {
162 DCHECK(successor_ == nullptr);
163 return &return_label_;
164 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100166 HBasicBlock* GetSuccessor() const {
167 return successor_;
168 }
169
Alexandre Rames9931f312015-06-19 14:47:01 +0100170 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
171
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Andreas Gampe85b62f22015-09-09 13:15:38 -0700179class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000182 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000186 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000188 if (instruction_->CanThrowIntoCatchBlock()) {
189 // Live registers will be restored in the catch block if caught.
190 SaveLiveRegisters(codegen, instruction_->GetLocations());
191 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400192 // Are we using an array length from memory?
193 HInstruction* array_length = instruction_->InputAt(1);
194 Location length_loc = locations->InAt(1);
195 InvokeRuntimeCallingConvention calling_convention;
196 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
197 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100198 HArrayLength* length = array_length->AsArrayLength();
Nicolas Geoffray003444a2017-10-17 10:58:42 +0100199 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400200 Location array_loc = array_length->GetLocations()->InAt(0);
201 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
202 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
203 // Check for conflicts with index.
204 if (length_loc.Equals(locations->InAt(0))) {
205 // We know we aren't using parameter 2.
206 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
207 }
208 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100209 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100210 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700211 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400212 }
213
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000214 // We're moving two locations to locations that could overlap, so we need a parallel
215 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000216 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100217 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000218 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100219 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400220 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100221 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100222 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100223 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
224 ? kQuickThrowStringBounds
225 : kQuickThrowArrayBounds;
226 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100227 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000228 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100229 }
230
Alexandre Rames8158f282015-08-07 10:26:17 +0100231 bool IsFatal() const OVERRIDE { return true; }
232
Alexandre Rames9931f312015-06-19 14:47:01 +0100233 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
234
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100235 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100236 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
237};
238
Andreas Gampe85b62f22015-09-09 13:15:38 -0700239class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 LoadClassSlowPathX86_64(HLoadClass* cls,
242 HInstruction* at,
243 uint32_t dex_pc,
244 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000245 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000246 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
247 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000250 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000251 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100253
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000254 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255
Vladimir Markoea4c1262017-02-06 19:59:33 +0000256 // Custom calling convention: RAX serves as both input and output.
257 __ movl(CpuRegister(RAX), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100258 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000259 instruction_,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000260 dex_pc_,
261 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000262 if (do_clinit_) {
263 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
264 } else {
265 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
266 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100267
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270 if (out.IsValid()) {
271 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000272 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 }
274
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000275 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100276 __ jmp(GetExitLabel());
277 }
278
Alexandre Rames9931f312015-06-19 14:47:01 +0100279 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
280
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100281 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 // The class this slow path will load.
283 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100284
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285 // The dex PC of `at_`.
286 const uint32_t dex_pc_;
287
288 // Whether to initialize the class.
289 const bool do_clinit_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100292};
293
Vladimir Markoaad75c62016-10-03 08:46:48 +0000294class LoadStringSlowPathX86_64 : public SlowPathCode {
295 public:
296 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
297
298 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
299 LocationSummary* locations = instruction_->GetLocations();
300 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
301
302 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
303 __ Bind(GetEntryLabel());
304 SaveLiveRegisters(codegen, locations);
305
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100307 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000308 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000309 x86_64_codegen->InvokeRuntime(kQuickResolveString,
310 instruction_,
311 instruction_->GetDexPc(),
312 this);
313 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
314 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
315 RestoreLiveRegisters(codegen, locations);
316
Vladimir Markoaad75c62016-10-03 08:46:48 +0000317 __ jmp(GetExitLabel());
318 }
319
320 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
321
322 private:
323 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
324};
325
Andreas Gampe85b62f22015-09-09 13:15:38 -0700326class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000329 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000331 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100333 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 DCHECK(instruction_->IsCheckCast()
335 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
Roland Levillain0d5a2812015-11-13 10:07:31 +0000337 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000339
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000340 if (kPoisonHeapReferences &&
341 instruction_->IsCheckCast() &&
342 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
343 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
344 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<CpuRegister>());
345 }
346
Vladimir Marko87584542017-12-12 17:47:52 +0000347 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348 SaveLiveRegisters(codegen, locations);
349 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000350
351 // We're moving two locations to locations that could overlap, so we need a parallel
352 // move resolver.
353 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800354 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800355 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100356 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800358 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100359 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100361 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800362 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 } else {
364 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800365 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
366 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000367 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 if (!is_fatal_) {
370 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000371 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000373
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 RestoreLiveRegisters(codegen, locations);
375 __ jmp(GetExitLabel());
376 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377 }
378
Alexandre Rames9931f312015-06-19 14:47:01 +0100379 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
380
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000381 bool IsFatal() const OVERRIDE { return is_fatal_; }
382
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000383 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000385
386 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
387};
388
Andreas Gampe85b62f22015-09-09 13:15:38 -0700389class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 public:
Aart Bik42249c32016-01-07 15:33:50 -0800391 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000392 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393
394 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000395 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100397 LocationSummary* locations = instruction_->GetLocations();
398 SaveLiveRegisters(codegen, locations);
399 InvokeRuntimeCallingConvention calling_convention;
400 x86_64_codegen->Load32BitValue(
401 CpuRegister(calling_convention.GetRegisterAt(0)),
402 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100403 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100404 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700405 }
406
Alexandre Rames9931f312015-06-19 14:47:01 +0100407 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
408
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700409 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
411};
412
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413class ArraySetSlowPathX86_64 : public SlowPathCode {
414 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000415 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416
417 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
418 LocationSummary* locations = instruction_->GetLocations();
419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, locations);
421
422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100423 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 parallel_move.AddMove(
425 locations->InAt(0),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100427 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100428 nullptr);
429 parallel_move.AddMove(
430 locations->InAt(1),
431 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100432 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 nullptr);
434 parallel_move.AddMove(
435 locations->InAt(2),
436 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100437 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 nullptr);
439 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
440
Roland Levillain0d5a2812015-11-13 10:07:31 +0000441 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100442 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000443 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 RestoreLiveRegisters(codegen, locations);
445 __ jmp(GetExitLabel());
446 }
447
448 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
449
450 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100451 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
452};
453
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100454// Slow path marking an object reference `ref` during a read
455// barrier. The field `obj.field` in the object `obj` holding this
456// reference does not get updated by this slow path after marking (see
457// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
458//
459// This means that after the execution of this slow path, `ref` will
460// always be up-to-date, but `obj.field` may not; i.e., after the
461// flip, `ref` will be a to-space reference, but `obj.field` will
462// probably still be a from-space reference (unless it gets updated by
463// another thread, or if another thread installed another object
464// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000465class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
466 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100467 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
468 Location ref,
469 bool unpoison_ref_before_marking)
470 : SlowPathCode(instruction),
471 ref_(ref),
472 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000473 DCHECK(kEmitCompilerReadBarrier);
474 }
475
476 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
479 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
481 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000482 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000484 DCHECK(instruction_->IsInstanceFieldGet() ||
485 instruction_->IsStaticFieldGet() ||
486 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100487 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000488 instruction_->IsLoadClass() ||
489 instruction_->IsLoadString() ||
490 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100491 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100492 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
493 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000494 << "Unexpected instruction in read barrier marking slow path: "
495 << instruction_->DebugName();
496
497 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100498 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000499 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000501 }
Roland Levillain4359e612016-07-20 11:32:19 +0100502 // No need to save live registers; it's taken care of by the
503 // entrypoint. Also, there is no need to update the stack mask,
504 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000505 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 DCHECK_NE(ref_reg, RSP);
507 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // "Compact" slow path, saving two moves.
509 //
510 // Instead of using the standard runtime calling convention (input
511 // and output in R0):
512 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100514 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100516 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100518 // of a dedicated entrypoint:
519 //
520 // rX <- ReadBarrierMarkRegX(rX)
521 //
522 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100523 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100524 // This runtime call does not require a stack map.
525 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000526 __ jmp(GetExitLabel());
527 }
528
529 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100530 // The location (register) of the marked object reference.
531 const Location ref_;
532 // Should the reference in `ref_` be unpoisoned prior to marking it?
533 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000534
535 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
536};
537
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100538// Slow path marking an object reference `ref` during a read barrier,
539// and if needed, atomically updating the field `obj.field` in the
540// object `obj` holding this reference after marking (contrary to
541// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
542// `obj.field`).
543//
544// This means that after the execution of this slow path, both `ref`
545// and `obj.field` will be up-to-date; i.e., after the flip, both will
546// hold the same to-space reference (unless another thread installed
547// another object reference (different from `ref`) in `obj.field`).
548class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
549 public:
550 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
551 Location ref,
552 CpuRegister obj,
553 const Address& field_addr,
554 bool unpoison_ref_before_marking,
555 CpuRegister temp1,
556 CpuRegister temp2)
557 : SlowPathCode(instruction),
558 ref_(ref),
559 obj_(obj),
560 field_addr_(field_addr),
561 unpoison_ref_before_marking_(unpoison_ref_before_marking),
562 temp1_(temp1),
563 temp2_(temp2) {
564 DCHECK(kEmitCompilerReadBarrier);
565 }
566
567 const char* GetDescription() const OVERRIDE {
568 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
569 }
570
571 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
572 LocationSummary* locations = instruction_->GetLocations();
573 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
574 Register ref_reg = ref_cpu_reg.AsRegister();
575 DCHECK(locations->CanCall());
576 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
577 // This slow path is only used by the UnsafeCASObject intrinsic.
578 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
579 << "Unexpected instruction in read barrier marking and field updating slow path: "
580 << instruction_->DebugName();
581 DCHECK(instruction_->GetLocations()->Intrinsified());
582 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
583
584 __ Bind(GetEntryLabel());
585 if (unpoison_ref_before_marking_) {
586 // Object* ref = ref_addr->AsMirrorPtr()
587 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
588 }
589
590 // Save the old (unpoisoned) reference.
591 __ movl(temp1_, ref_cpu_reg);
592
593 // No need to save live registers; it's taken care of by the
594 // entrypoint. Also, there is no need to update the stack mask,
595 // as this runtime call will not trigger a garbage collection.
596 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
597 DCHECK_NE(ref_reg, RSP);
598 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
599 // "Compact" slow path, saving two moves.
600 //
601 // Instead of using the standard runtime calling convention (input
602 // and output in R0):
603 //
604 // RDI <- ref
605 // RAX <- ReadBarrierMark(RDI)
606 // ref <- RAX
607 //
608 // we just use rX (the register containing `ref`) as input and output
609 // of a dedicated entrypoint:
610 //
611 // rX <- ReadBarrierMarkRegX(rX)
612 //
613 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100614 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100615 // This runtime call does not require a stack map.
616 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
617
618 // If the new reference is different from the old reference,
619 // update the field in the holder (`*field_addr`).
620 //
621 // Note that this field could also hold a different object, if
622 // another thread had concurrently changed it. In that case, the
623 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
624 // operation below would abort the CAS, leaving the field as-is.
625 NearLabel done;
626 __ cmpl(temp1_, ref_cpu_reg);
627 __ j(kEqual, &done);
628
629 // Update the the holder's field atomically. This may fail if
630 // mutator updates before us, but it's OK. This is achived
631 // using a strong compare-and-set (CAS) operation with relaxed
632 // memory synchronization ordering, where the expected value is
633 // the old reference and the desired value is the new reference.
634 // This operation is implemented with a 32-bit LOCK CMPXLCHG
635 // instruction, which requires the expected value (the old
636 // reference) to be in EAX. Save RAX beforehand, and move the
637 // expected value (stored in `temp1_`) into EAX.
638 __ movq(temp2_, CpuRegister(RAX));
639 __ movl(CpuRegister(RAX), temp1_);
640
641 // Convenience aliases.
642 CpuRegister base = obj_;
643 CpuRegister expected = CpuRegister(RAX);
644 CpuRegister value = ref_cpu_reg;
645
646 bool base_equals_value = (base.AsRegister() == value.AsRegister());
647 Register value_reg = ref_reg;
648 if (kPoisonHeapReferences) {
649 if (base_equals_value) {
650 // If `base` and `value` are the same register location, move
651 // `value_reg` to a temporary register. This way, poisoning
652 // `value_reg` won't invalidate `base`.
653 value_reg = temp1_.AsRegister();
654 __ movl(CpuRegister(value_reg), base);
655 }
656
657 // Check that the register allocator did not assign the location
658 // of `expected` (RAX) to `value` nor to `base`, so that heap
659 // poisoning (when enabled) works as intended below.
660 // - If `value` were equal to `expected`, both references would
661 // be poisoned twice, meaning they would not be poisoned at
662 // all, as heap poisoning uses address negation.
663 // - If `base` were equal to `expected`, poisoning `expected`
664 // would invalidate `base`.
665 DCHECK_NE(value_reg, expected.AsRegister());
666 DCHECK_NE(base.AsRegister(), expected.AsRegister());
667
668 __ PoisonHeapReference(expected);
669 __ PoisonHeapReference(CpuRegister(value_reg));
670 }
671
672 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
673
674 // If heap poisoning is enabled, we need to unpoison the values
675 // that were poisoned earlier.
676 if (kPoisonHeapReferences) {
677 if (base_equals_value) {
678 // `value_reg` has been moved to a temporary register, no need
679 // to unpoison it.
680 } else {
681 __ UnpoisonHeapReference(CpuRegister(value_reg));
682 }
683 // No need to unpoison `expected` (RAX), as it is be overwritten below.
684 }
685
686 // Restore RAX.
687 __ movq(CpuRegister(RAX), temp2_);
688
689 __ Bind(&done);
690 __ jmp(GetExitLabel());
691 }
692
693 private:
694 // The location (register) of the marked object reference.
695 const Location ref_;
696 // The register containing the object holding the marked object reference field.
697 const CpuRegister obj_;
698 // The address of the marked reference field. The base of this address must be `obj_`.
699 const Address field_addr_;
700
701 // Should the reference in `ref_` be unpoisoned prior to marking it?
702 const bool unpoison_ref_before_marking_;
703
704 const CpuRegister temp1_;
705 const CpuRegister temp2_;
706
707 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
708};
709
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710// Slow path generating a read barrier for a heap reference.
711class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
712 public:
713 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
714 Location out,
715 Location ref,
716 Location obj,
717 uint32_t offset,
718 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000719 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000720 out_(out),
721 ref_(ref),
722 obj_(obj),
723 offset_(offset),
724 index_(index) {
725 DCHECK(kEmitCompilerReadBarrier);
726 // If `obj` is equal to `out` or `ref`, it means the initial
727 // object has been overwritten by (or after) the heap object
728 // reference load to be instrumented, e.g.:
729 //
730 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000731 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000732 //
733 // In that case, we have lost the information about the original
734 // object, and the emitted read barrier cannot work properly.
735 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
736 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
737}
738
739 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
740 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
741 LocationSummary* locations = instruction_->GetLocations();
742 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
743 DCHECK(locations->CanCall());
744 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100745 DCHECK(instruction_->IsInstanceFieldGet() ||
746 instruction_->IsStaticFieldGet() ||
747 instruction_->IsArrayGet() ||
748 instruction_->IsInstanceOf() ||
749 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700750 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000751 << "Unexpected instruction in read barrier for heap reference slow path: "
752 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000753
754 __ Bind(GetEntryLabel());
755 SaveLiveRegisters(codegen, locations);
756
757 // We may have to change the index's value, but as `index_` is a
758 // constant member (like other "inputs" of this slow path),
759 // introduce a copy of it, `index`.
760 Location index = index_;
761 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100762 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000763 if (instruction_->IsArrayGet()) {
764 // Compute real offset and store it in index_.
765 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
766 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
767 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
768 // We are about to change the value of `index_reg` (see the
769 // calls to art::x86_64::X86_64Assembler::shll and
770 // art::x86_64::X86_64Assembler::AddImmediate below), but it
771 // has not been saved by the previous call to
772 // art::SlowPathCode::SaveLiveRegisters, as it is a
773 // callee-save register --
774 // art::SlowPathCode::SaveLiveRegisters does not consider
775 // callee-save registers, as it has been designed with the
776 // assumption that callee-save registers are supposed to be
777 // handled by the called function. So, as a callee-save
778 // register, `index_reg` _would_ eventually be saved onto
779 // the stack, but it would be too late: we would have
780 // changed its value earlier. Therefore, we manually save
781 // it here into another freely available register,
782 // `free_reg`, chosen of course among the caller-save
783 // registers (as a callee-save `free_reg` register would
784 // exhibit the same problem).
785 //
786 // Note we could have requested a temporary register from
787 // the register allocator instead; but we prefer not to, as
788 // this is a slow path, and we know we can find a
789 // caller-save register that is available.
790 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
791 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
792 index_reg = free_reg;
793 index = Location::RegisterLocation(index_reg);
794 } else {
795 // The initial register stored in `index_` has already been
796 // saved in the call to art::SlowPathCode::SaveLiveRegisters
797 // (as it is not a callee-save register), so we can freely
798 // use it.
799 }
800 // Shifting the index value contained in `index_reg` by the
801 // scale factor (2) cannot overflow in practice, as the
802 // runtime is unable to allocate object arrays with a size
803 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
804 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
805 static_assert(
806 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
807 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
808 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
809 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100810 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
811 // intrinsics, `index_` is not shifted by a scale factor of 2
812 // (as in the case of ArrayGet), as it is actually an offset
813 // to an object field within an object.
814 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000815 DCHECK(instruction_->GetLocations()->Intrinsified());
816 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
817 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
818 << instruction_->AsInvoke()->GetIntrinsic();
819 DCHECK_EQ(offset_, 0U);
820 DCHECK(index_.IsRegister());
821 }
822 }
823
824 // We're moving two or three locations to locations that could
825 // overlap, so we need a parallel move resolver.
826 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100827 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 parallel_move.AddMove(ref_,
829 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100830 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000831 nullptr);
832 parallel_move.AddMove(obj_,
833 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100834 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000835 nullptr);
836 if (index.IsValid()) {
837 parallel_move.AddMove(index,
838 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 nullptr);
841 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
842 } else {
843 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
844 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
845 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100846 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000847 instruction_,
848 instruction_->GetDexPc(),
849 this);
850 CheckEntrypointTypes<
851 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
852 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
853
854 RestoreLiveRegisters(codegen, locations);
855 __ jmp(GetExitLabel());
856 }
857
858 const char* GetDescription() const OVERRIDE {
859 return "ReadBarrierForHeapReferenceSlowPathX86_64";
860 }
861
862 private:
863 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
864 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
865 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
866 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
867 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
868 return static_cast<CpuRegister>(i);
869 }
870 }
871 // We shall never fail to find a free caller-save register, as
872 // there are more than two core caller-save registers on x86-64
873 // (meaning it is possible to find one which is different from
874 // `ref` and `obj`).
875 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
876 LOG(FATAL) << "Could not find a free caller-save register";
877 UNREACHABLE();
878 }
879
Roland Levillain0d5a2812015-11-13 10:07:31 +0000880 const Location out_;
881 const Location ref_;
882 const Location obj_;
883 const uint32_t offset_;
884 // An additional location containing an index to an array.
885 // Only used for HArrayGet and the UnsafeGetObject &
886 // UnsafeGetObjectVolatile intrinsics.
887 const Location index_;
888
889 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
890};
891
892// Slow path generating a read barrier for a GC root.
893class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
894 public:
895 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000896 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000897 DCHECK(kEmitCompilerReadBarrier);
898 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000899
900 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
901 LocationSummary* locations = instruction_->GetLocations();
902 DCHECK(locations->CanCall());
903 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000904 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
905 << "Unexpected instruction in read barrier for GC root slow path: "
906 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000907
908 __ Bind(GetEntryLabel());
909 SaveLiveRegisters(codegen, locations);
910
911 InvokeRuntimeCallingConvention calling_convention;
912 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
913 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100914 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000915 instruction_,
916 instruction_->GetDexPc(),
917 this);
918 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
919 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
920
921 RestoreLiveRegisters(codegen, locations);
922 __ jmp(GetExitLabel());
923 }
924
925 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
926
927 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000928 const Location out_;
929 const Location root_;
930
931 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
932};
933
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100934#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100935// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
936#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100937
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700939 switch (cond) {
940 case kCondEQ: return kEqual;
941 case kCondNE: return kNotEqual;
942 case kCondLT: return kLess;
943 case kCondLE: return kLessEqual;
944 case kCondGT: return kGreater;
945 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700946 case kCondB: return kBelow;
947 case kCondBE: return kBelowEqual;
948 case kCondA: return kAbove;
949 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700950 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 LOG(FATAL) << "Unreachable";
952 UNREACHABLE();
953}
954
Aart Bike9f37602015-10-09 11:15:55 -0700955// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100956inline Condition X86_64FPCondition(IfCondition cond) {
957 switch (cond) {
958 case kCondEQ: return kEqual;
959 case kCondNE: return kNotEqual;
960 case kCondLT: return kBelow;
961 case kCondLE: return kBelowEqual;
962 case kCondGT: return kAbove;
963 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700964 default: break; // should not happen
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800965 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100966 LOG(FATAL) << "Unreachable";
967 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700968}
969
Vladimir Markodc151b22015-10-15 18:02:30 +0100970HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
971 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100972 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000973 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100974}
975
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100976void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
977 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800978 // All registers are assumed to be correctly set up.
Vladimir Marko4ee8e292017-06-02 15:39:30 +0000979
Vladimir Marko58155012015-08-19 12:49:41 +0000980 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
981 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100982 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000983 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100984 uint32_t offset =
985 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
986 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000987 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100988 }
Vladimir Marko58155012015-08-19 12:49:41 +0000989 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000990 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000991 break;
Vladimir Marko65979462017-05-19 17:25:12 +0100992 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
993 DCHECK(GetCompilerOptions().IsBootImage());
994 __ leal(temp.AsRegister<CpuRegister>(),
995 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000996 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +0100997 break;
Vladimir Marko58155012015-08-19 12:49:41 +0000998 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +0000999 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +00001000 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001001 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001002 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001003 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001004 RecordMethodBssEntryPatch(invoke);
Vladimir Marko58155012015-08-19 12:49:41 +00001005 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001006 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001007 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1008 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1009 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001010 }
Vladimir Marko58155012015-08-19 12:49:41 +00001011 }
1012
1013 switch (invoke->GetCodePtrLocation()) {
1014 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1015 __ call(&frame_entry_label_);
1016 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001017 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1018 // (callee_method + offset_of_quick_compiled_code)()
1019 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1020 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001021 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001022 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001023 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001024 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001025
1026 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001027}
1028
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001029void CodeGeneratorX86_64::GenerateVirtualCall(
1030 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001031 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1032 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1033 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001034
1035 // Use the calling convention instead of the location of the receiver, as
1036 // intrinsics may have put the receiver in a different register. In the intrinsics
1037 // slow path, the arguments have been moved to the right place, so here we are
1038 // guaranteed that the receiver is the first register of the calling convention.
1039 InvokeDexCallingConvention calling_convention;
1040 Register receiver = calling_convention.GetRegisterAt(0);
1041
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001042 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001043 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001044 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001045 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001046 // Instead of simply (possibly) unpoisoning `temp` here, we should
1047 // emit a read barrier for the previous class reference load.
1048 // However this is not required in practice, as this is an
1049 // intermediate/temporary reference and because the current
1050 // concurrent copying collector keeps the from-space memory
1051 // intact/accessible until the end of the marking phase (the
1052 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001053 __ MaybeUnpoisonHeapReference(temp);
1054 // temp = temp->GetMethodAt(method_offset);
1055 __ movq(temp, Address(temp, method_offset));
1056 // call temp->GetEntryPoint();
1057 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001058 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001059 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001060}
1061
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001062void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
1063 boot_image_method_patches_.emplace_back(
1064 invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001065 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001066}
1067
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001068void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
1069 method_bss_entry_patches_.emplace_back(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
1070 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001071}
1072
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001073void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1074 boot_image_type_patches_.emplace_back(
1075 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001076 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001077}
1078
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001079Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001080 type_bss_entry_patches_.emplace_back(
1081 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001082 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001083}
1084
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001085void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1086 boot_image_string_patches_.emplace_back(
1087 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1088 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001089}
1090
Vladimir Markoaad75c62016-10-03 08:46:48 +00001091Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1092 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001093 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001094 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001095 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001096}
1097
Vladimir Markoaad75c62016-10-03 08:46:48 +00001098// The label points to the end of the "movl" or another instruction but the literal offset
1099// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1100constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1101
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001102template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001103inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1104 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001105 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001106 for (const PatchInfo<Label>& info : infos) {
1107 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1108 linker_patches->push_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001109 Factory(literal_offset, info.target_dex_file, info.label.Position(), info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001110 }
1111}
1112
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001113void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001114 DCHECK(linker_patches->empty());
1115 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001116 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001117 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001118 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001119 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001120 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001121 string_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001122 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001123 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001124 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1125 boot_image_method_patches_, linker_patches);
1126 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1127 boot_image_type_patches_, linker_patches);
1128 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001129 boot_image_string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001130 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01001131 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001132 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1133 boot_image_type_patches_, linker_patches);
1134 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001135 boot_image_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001136 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001137 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1138 method_bss_entry_patches_, linker_patches);
1139 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1140 type_bss_entry_patches_, linker_patches);
1141 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1142 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001143 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001144}
1145
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001146void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001147 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148}
1149
1150void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001151 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001154size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1155 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1156 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001157}
1158
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001159size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1160 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1161 return kX86_64WordSize;
1162}
1163
1164size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001165 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001166 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001167 } else {
1168 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1169 }
1170 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001171}
1172
1173size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001174 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001175 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001176 } else {
1177 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1178 }
1179 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001180}
1181
Calin Juravle175dc732015-08-25 15:42:32 +01001182void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1183 HInstruction* instruction,
1184 uint32_t dex_pc,
1185 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001186 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001187 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1188 if (EntrypointRequiresStackMap(entrypoint)) {
1189 RecordPcInfo(instruction, dex_pc, slow_path);
1190 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001191}
1192
Roland Levillaindec8f632016-07-22 17:10:06 +01001193void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1194 HInstruction* instruction,
1195 SlowPathCode* slow_path) {
1196 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001197 GenerateInvokeRuntime(entry_point_offset);
1198}
1199
1200void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001201 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1202}
1203
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001204static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001205// Use a fake return address register to mimic Quick.
1206static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001207CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001208 const X86_64InstructionSetFeatures& isa_features,
1209 const CompilerOptions& compiler_options,
1210 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001211 : CodeGenerator(graph,
1212 kNumberOfCpuRegisters,
1213 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001214 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001215 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1216 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001217 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001218 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1219 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001220 compiler_options,
1221 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001222 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001223 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001224 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001225 move_resolver_(graph->GetAllocator(), this),
1226 assembler_(graph->GetAllocator()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001227 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001228 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001229 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1230 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1231 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1232 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001233 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001234 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1235 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1236 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1237 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001238 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1239}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001241InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1242 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001243 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001244 assembler_(codegen->GetAssembler()),
1245 codegen_(codegen) {}
1246
David Brazdil58282f42016-01-14 12:45:10 +00001247void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001249 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001251 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001252 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253}
1254
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001255static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001256 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001257}
David Srbecky9d8606d2015-04-12 09:35:32 +01001258
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001259static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001260 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001261}
1262
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001263void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001264 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001265 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001266 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001267 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001268 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001269
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001270 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1271 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1272 ArtMethod::HotnessCountOffset().Int32Value()),
1273 Immediate(1));
1274 }
1275
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001276 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001277 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1278 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001279 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001280 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001281
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001282 if (HasEmptyFrame()) {
1283 return;
1284 }
1285
Nicolas Geoffray98893962015-01-21 12:32:32 +00001286 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001287 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001288 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001289 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001290 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1291 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001292 }
1293 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001294
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001295 int adjust = GetFrameSize() - GetCoreSpillSize();
1296 __ subq(CpuRegister(RSP), Immediate(adjust));
1297 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001298 uint32_t xmm_spill_location = GetFpuSpillStart();
1299 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001300
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001301 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1302 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001303 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1304 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1305 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001306 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001307 }
1308
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001309 // Save the current method if we need it. Note that we do not
1310 // do this in HCurrentMethod, as the instruction might have been removed
1311 // in the SSA graph.
1312 if (RequiresCurrentMethod()) {
1313 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1314 CpuRegister(kMethodRegisterArgument));
1315 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001316
1317 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1318 // Initialize should_deoptimize flag to 0.
1319 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1320 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001321}
1322
1323void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001324 __ cfi().RememberState();
1325 if (!HasEmptyFrame()) {
1326 uint32_t xmm_spill_location = GetFpuSpillStart();
1327 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1328 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1329 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1330 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1331 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1332 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1333 }
1334 }
1335
1336 int adjust = GetFrameSize() - GetCoreSpillSize();
1337 __ addq(CpuRegister(RSP), Immediate(adjust));
1338 __ cfi().AdjustCFAOffset(-adjust);
1339
1340 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1341 Register reg = kCoreCalleeSaves[i];
1342 if (allocated_registers_.ContainsCoreRegister(reg)) {
1343 __ popq(CpuRegister(reg));
1344 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1345 __ cfi().Restore(DWARFReg(reg));
1346 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001347 }
1348 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001349 __ ret();
1350 __ cfi().RestoreState();
1351 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001352}
1353
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001354void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1355 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356}
1357
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001358void CodeGeneratorX86_64::Move(Location destination, Location source) {
1359 if (source.Equals(destination)) {
1360 return;
1361 }
1362 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001363 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001364 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001365 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001366 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001367 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001368 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001369 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1370 } else if (source.IsConstant()) {
1371 HConstant* constant = source.GetConstant();
1372 if (constant->IsLongConstant()) {
1373 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1374 } else {
1375 Load32BitValue(dest, GetInt32ValueOf(constant));
1376 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001377 } else {
1378 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001379 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001380 }
1381 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001382 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001383 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001384 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001385 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001386 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1387 } else if (source.IsConstant()) {
1388 HConstant* constant = source.GetConstant();
1389 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1390 if (constant->IsFloatConstant()) {
1391 Load32BitValue(dest, static_cast<int32_t>(value));
1392 } else {
1393 Load64BitValue(dest, value);
1394 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001395 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001396 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001397 } else {
1398 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001399 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001400 }
1401 } else if (destination.IsStackSlot()) {
1402 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001403 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001404 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001405 } else if (source.IsFpuRegister()) {
1406 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001407 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001408 } else if (source.IsConstant()) {
1409 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001410 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001411 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001412 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001413 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001414 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1415 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001416 }
1417 } else {
1418 DCHECK(destination.IsDoubleStackSlot());
1419 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001420 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001422 } else if (source.IsFpuRegister()) {
1423 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001424 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001425 } else if (source.IsConstant()) {
1426 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001427 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1428 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001429 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001430 } else {
1431 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001432 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1433 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001434 }
1435 }
1436}
1437
Calin Juravle175dc732015-08-25 15:42:32 +01001438void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1439 DCHECK(location.IsRegister());
1440 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1441}
1442
Calin Juravlee460d1d2015-09-29 04:52:17 +01001443void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001444 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001445 Move(dst, src);
1446}
1447
1448void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1449 if (location.IsRegister()) {
1450 locations->AddTemp(location);
1451 } else {
1452 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1453 }
1454}
1455
David Brazdilfc6a86a2015-06-26 10:33:45 +00001456void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001457 if (successor->IsExitBlock()) {
1458 DCHECK(got->GetPrevious()->AlwaysThrows());
1459 return; // no code needed
1460 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001461
1462 HBasicBlock* block = got->GetBlock();
1463 HInstruction* previous = got->GetPrevious();
1464
1465 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001466 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001467 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1468 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1469 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1470 Immediate(1));
1471 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001472 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1473 return;
1474 }
1475
1476 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1477 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1478 }
1479 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001480 __ jmp(codegen_->GetLabelOf(successor));
1481 }
1482}
1483
David Brazdilfc6a86a2015-06-26 10:33:45 +00001484void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1485 got->SetLocations(nullptr);
1486}
1487
1488void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1489 HandleGoto(got, got->GetSuccessor());
1490}
1491
1492void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1493 try_boundary->SetLocations(nullptr);
1494}
1495
1496void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1497 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1498 if (!successor->IsExitBlock()) {
1499 HandleGoto(try_boundary, successor);
1500 }
1501}
1502
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001503void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1504 exit->SetLocations(nullptr);
1505}
1506
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001507void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508}
1509
Mark Mendell152408f2015-12-31 12:28:50 -05001510template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001511void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001512 LabelType* true_label,
1513 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001514 if (cond->IsFPConditionTrueIfNaN()) {
1515 __ j(kUnordered, true_label);
1516 } else if (cond->IsFPConditionFalseIfNaN()) {
1517 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001518 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001519 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001520}
1521
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001522void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001523 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001524
Mark Mendellc4701932015-04-10 13:18:51 -04001525 Location left = locations->InAt(0);
1526 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001527 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001528 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001530 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001531 case DataType::Type::kInt8:
1532 case DataType::Type::kUint16:
1533 case DataType::Type::kInt16:
1534 case DataType::Type::kInt32:
1535 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001536 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001537 break;
1538 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001540 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001541 break;
1542 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001543 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001544 if (right.IsFpuRegister()) {
1545 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1546 } else if (right.IsConstant()) {
1547 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1548 codegen_->LiteralFloatAddress(
1549 right.GetConstant()->AsFloatConstant()->GetValue()));
1550 } else {
1551 DCHECK(right.IsStackSlot());
1552 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1553 Address(CpuRegister(RSP), right.GetStackIndex()));
1554 }
Mark Mendellc4701932015-04-10 13:18:51 -04001555 break;
1556 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001557 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001558 if (right.IsFpuRegister()) {
1559 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1560 } else if (right.IsConstant()) {
1561 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1562 codegen_->LiteralDoubleAddress(
1563 right.GetConstant()->AsDoubleConstant()->GetValue()));
1564 } else {
1565 DCHECK(right.IsDoubleStackSlot());
1566 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1567 Address(CpuRegister(RSP), right.GetStackIndex()));
1568 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001569 break;
1570 }
1571 default:
1572 LOG(FATAL) << "Unexpected condition type " << type;
1573 }
1574}
1575
1576template<class LabelType>
1577void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1578 LabelType* true_target_in,
1579 LabelType* false_target_in) {
1580 // Generated branching requires both targets to be explicit. If either of the
1581 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1582 LabelType fallthrough_target;
1583 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1584 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1585
1586 // Generate the comparison to set the CC.
1587 GenerateCompareTest(condition);
1588
1589 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001590 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001591 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001592 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001593 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1594 break;
1595 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001596 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001597 GenerateFPJumps(condition, true_target, false_target);
1598 break;
1599 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001600 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001601 GenerateFPJumps(condition, true_target, false_target);
1602 break;
1603 }
1604 default:
1605 LOG(FATAL) << "Unexpected condition type " << type;
1606 }
1607
David Brazdil0debae72015-11-12 18:37:00 +00001608 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001609 __ jmp(false_target);
1610 }
David Brazdil0debae72015-11-12 18:37:00 +00001611
1612 if (fallthrough_target.IsLinked()) {
1613 __ Bind(&fallthrough_target);
1614 }
Mark Mendellc4701932015-04-10 13:18:51 -04001615}
1616
David Brazdil0debae72015-11-12 18:37:00 +00001617static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1618 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1619 // are set only strictly before `branch`. We can't use the eflags on long
1620 // conditions if they are materialized due to the complex branching.
1621 return cond->IsCondition() &&
1622 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001623 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001624}
1625
Mark Mendell152408f2015-12-31 12:28:50 -05001626template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001627void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001628 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001629 LabelType* true_target,
1630 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001631 HInstruction* cond = instruction->InputAt(condition_input_index);
1632
1633 if (true_target == nullptr && false_target == nullptr) {
1634 // Nothing to do. The code always falls through.
1635 return;
1636 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001637 // Constant condition, statically compared against "true" (integer value 1).
1638 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001639 if (true_target != nullptr) {
1640 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001641 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001642 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001643 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001644 if (false_target != nullptr) {
1645 __ jmp(false_target);
1646 }
1647 }
1648 return;
1649 }
1650
1651 // The following code generates these patterns:
1652 // (1) true_target == nullptr && false_target != nullptr
1653 // - opposite condition true => branch to false_target
1654 // (2) true_target != nullptr && false_target == nullptr
1655 // - condition true => branch to true_target
1656 // (3) true_target != nullptr && false_target != nullptr
1657 // - condition true => branch to true_target
1658 // - branch to false_target
1659 if (IsBooleanValueOrMaterializedCondition(cond)) {
1660 if (AreEflagsSetFrom(cond, instruction)) {
1661 if (true_target == nullptr) {
1662 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1663 } else {
1664 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1665 }
1666 } else {
1667 // Materialized condition, compare against 0.
1668 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1669 if (lhs.IsRegister()) {
1670 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1671 } else {
1672 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1673 }
1674 if (true_target == nullptr) {
1675 __ j(kEqual, false_target);
1676 } else {
1677 __ j(kNotEqual, true_target);
1678 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001679 }
1680 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001681 // Condition has not been materialized, use its inputs as the
1682 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001683 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001684
David Brazdil0debae72015-11-12 18:37:00 +00001685 // If this is a long or FP comparison that has been folded into
1686 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 DataType::Type type = condition->InputAt(0)->GetType();
1688 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001689 GenerateCompareTestAndBranch(condition, true_target, false_target);
1690 return;
1691 }
1692
1693 Location lhs = condition->GetLocations()->InAt(0);
1694 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001695 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001696 if (true_target == nullptr) {
1697 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1698 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001699 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001700 }
Dave Allison20dfc792014-06-16 20:44:29 -07001701 }
David Brazdil0debae72015-11-12 18:37:00 +00001702
1703 // If neither branch falls through (case 3), the conditional branch to `true_target`
1704 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1705 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001706 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001707 }
1708}
1709
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001710void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001711 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001712 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001713 locations->SetInAt(0, Location::Any());
1714 }
1715}
1716
1717void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001718 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1719 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1720 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1721 nullptr : codegen_->GetLabelOf(true_successor);
1722 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1723 nullptr : codegen_->GetLabelOf(false_successor);
1724 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001725}
1726
1727void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001728 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001729 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001730 InvokeRuntimeCallingConvention calling_convention;
1731 RegisterSet caller_saves = RegisterSet::Empty();
1732 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1733 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001734 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001735 locations->SetInAt(0, Location::Any());
1736 }
1737}
1738
1739void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001740 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001741 GenerateTestAndBranch<Label>(deoptimize,
1742 /* condition_input_index */ 0,
1743 slow_path->GetEntryLabel(),
1744 /* false_target */ nullptr);
1745}
1746
Mingyao Yang063fc772016-08-02 11:02:54 -07001747void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001748 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001749 LocationSummary(flag, LocationSummary::kNoCall);
1750 locations->SetOut(Location::RequiresRegister());
1751}
1752
1753void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1754 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1755 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1756}
1757
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001758static bool SelectCanUseCMOV(HSelect* select) {
1759 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001761 return false;
1762 }
1763
1764 // A FP condition doesn't generate the single CC that we need.
1765 HInstruction* condition = select->GetCondition();
1766 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001768 return false;
1769 }
1770
1771 // We can generate a CMOV for this Select.
1772 return true;
1773}
1774
David Brazdil74eb1b22015-12-14 11:44:01 +00001775void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001776 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001777 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001778 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001779 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001780 } else {
1781 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001782 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001783 if (select->InputAt(1)->IsConstant()) {
1784 locations->SetInAt(1, Location::RequiresRegister());
1785 } else {
1786 locations->SetInAt(1, Location::Any());
1787 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001788 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001789 locations->SetInAt(1, Location::Any());
1790 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001791 }
1792 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1793 locations->SetInAt(2, Location::RequiresRegister());
1794 }
1795 locations->SetOut(Location::SameAsFirstInput());
1796}
1797
1798void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1799 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001800 if (SelectCanUseCMOV(select)) {
1801 // If both the condition and the source types are integer, we can generate
1802 // a CMOV to implement Select.
1803 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001804 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001805 DCHECK(locations->InAt(0).Equals(locations->Out()));
1806
1807 HInstruction* select_condition = select->GetCondition();
1808 Condition cond = kNotEqual;
1809
1810 // Figure out how to test the 'condition'.
1811 if (select_condition->IsCondition()) {
1812 HCondition* condition = select_condition->AsCondition();
1813 if (!condition->IsEmittedAtUseSite()) {
1814 // This was a previously materialized condition.
1815 // Can we use the existing condition code?
1816 if (AreEflagsSetFrom(condition, select)) {
1817 // Materialization was the previous instruction. Condition codes are right.
1818 cond = X86_64IntegerCondition(condition->GetCondition());
1819 } else {
1820 // No, we have to recreate the condition code.
1821 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1822 __ testl(cond_reg, cond_reg);
1823 }
1824 } else {
1825 GenerateCompareTest(condition);
1826 cond = X86_64IntegerCondition(condition->GetCondition());
1827 }
1828 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001829 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001830 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1831 __ testl(cond_reg, cond_reg);
1832 }
1833
1834 // If the condition is true, overwrite the output, which already contains false.
1835 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001837 if (value_true_loc.IsRegister()) {
1838 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1839 } else {
1840 __ cmov(cond,
1841 value_false,
1842 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1843 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001844 } else {
1845 NearLabel false_target;
1846 GenerateTestAndBranch<NearLabel>(select,
1847 /* condition_input_index */ 2,
1848 /* true_target */ nullptr,
1849 &false_target);
1850 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1851 __ Bind(&false_target);
1852 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001853}
1854
David Srbecky0cf44932015-12-09 14:09:59 +00001855void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001856 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001857}
1858
David Srbeckyd28f4a02016-03-14 17:14:24 +00001859void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1860 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001861}
1862
1863void CodeGeneratorX86_64::GenerateNop() {
1864 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001865}
1866
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001867void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001870 // Handle the long/FP comparisons made in instruction simplification.
1871 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001873 locations->SetInAt(0, Location::RequiresRegister());
1874 locations->SetInAt(1, Location::Any());
1875 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 case DataType::Type::kFloat32:
1877 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001878 locations->SetInAt(0, Location::RequiresFpuRegister());
1879 locations->SetInAt(1, Location::Any());
1880 break;
1881 default:
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::Any());
1884 break;
1885 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001886 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001887 locations->SetOut(Location::RequiresRegister());
1888 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001889}
1890
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001891void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001892 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001893 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001894 }
Mark Mendellc4701932015-04-10 13:18:51 -04001895
1896 LocationSummary* locations = cond->GetLocations();
1897 Location lhs = locations->InAt(0);
1898 Location rhs = locations->InAt(1);
1899 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001900 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001901
1902 switch (cond->InputAt(0)->GetType()) {
1903 default:
1904 // Integer case.
1905
1906 // Clear output register: setcc only sets the low byte.
1907 __ xorl(reg, reg);
1908
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001909 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001910 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001911 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001912 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001913 // Clear output register: setcc only sets the low byte.
1914 __ xorl(reg, reg);
1915
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001916 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001917 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001918 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001920 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1921 if (rhs.IsConstant()) {
1922 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1923 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1924 } else if (rhs.IsStackSlot()) {
1925 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1926 } else {
1927 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1928 }
1929 GenerateFPJumps(cond, &true_label, &false_label);
1930 break;
1931 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001933 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1934 if (rhs.IsConstant()) {
1935 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1936 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1937 } else if (rhs.IsDoubleStackSlot()) {
1938 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1939 } else {
1940 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1941 }
1942 GenerateFPJumps(cond, &true_label, &false_label);
1943 break;
1944 }
1945 }
1946
1947 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001948 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001949
Roland Levillain4fa13f62015-07-06 18:11:54 +01001950 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001951 __ Bind(&false_label);
1952 __ xorl(reg, reg);
1953 __ jmp(&done_label);
1954
Roland Levillain4fa13f62015-07-06 18:11:54 +01001955 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001956 __ Bind(&true_label);
1957 __ movl(reg, Immediate(1));
1958 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001987}
1988
1989void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001991}
1992
1993void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001995}
1996
1997void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001999}
2000
2001void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002003}
2004
2005void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002007}
2008
Aart Bike9f37602015-10-09 11:15:55 -07002009void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
2021void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002022 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002023}
2024
2025void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002026 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002027}
2028
2029void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002030 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002031}
2032
2033void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002035}
2036
2037void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002039}
2040
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002041void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002042 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002044 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002046 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002049 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 case DataType::Type::kInt32:
2051 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002052 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002053 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002054 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2055 break;
2056 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 case DataType::Type::kFloat32:
2058 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002059 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002060 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002061 locations->SetOut(Location::RequiresRegister());
2062 break;
2063 }
2064 default:
2065 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2066 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002067}
2068
2069void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002070 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002071 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002072 Location left = locations->InAt(0);
2073 Location right = locations->InAt(1);
2074
Mark Mendell0c9497d2015-08-21 09:30:05 -04002075 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002077 Condition less_cond = kLess;
2078
Calin Juravleddb7df22014-11-25 20:56:51 +00002079 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002081 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002084 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002086 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002087 break;
2088 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002090 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002091 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002092 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002094 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2095 if (right.IsConstant()) {
2096 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2097 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2098 } else if (right.IsStackSlot()) {
2099 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2100 } else {
2101 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2102 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002103 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002104 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002105 break;
2106 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002108 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2109 if (right.IsConstant()) {
2110 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2111 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2112 } else if (right.IsDoubleStackSlot()) {
2113 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2114 } else {
2115 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2116 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002117 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002118 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002119 break;
2120 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002121 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002122 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002123 }
Aart Bika19616e2016-02-01 18:57:58 -08002124
Calin Juravleddb7df22014-11-25 20:56:51 +00002125 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002126 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002127 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002128
Calin Juravle91debbc2014-11-26 19:01:09 +00002129 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002130 __ movl(out, Immediate(1));
2131 __ jmp(&done);
2132
2133 __ Bind(&less);
2134 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002135
2136 __ Bind(&done);
2137}
2138
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002139void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002140 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002141 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002142 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002143}
2144
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002145void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002146 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002147}
2148
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002149void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2150 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002151 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002152 locations->SetOut(Location::ConstantLocation(constant));
2153}
2154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002155void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002156 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002157}
2158
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002159void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002160 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002161 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002162 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002163}
2164
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002165void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002166 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002167}
2168
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002169void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2170 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002171 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002172 locations->SetOut(Location::ConstantLocation(constant));
2173}
2174
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002175void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002176 // Will be generated at use site.
2177}
2178
2179void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2180 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002181 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002182 locations->SetOut(Location::ConstantLocation(constant));
2183}
2184
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002185void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2186 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002187 // Will be generated at use site.
2188}
2189
Igor Murashkind01745e2017-04-05 16:40:31 -07002190void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2191 constructor_fence->SetLocations(nullptr);
2192}
2193
2194void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2195 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2196 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2197}
2198
Calin Juravle27df7582015-04-17 19:12:31 +01002199void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2200 memory_barrier->SetLocations(nullptr);
2201}
2202
2203void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002204 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002205}
2206
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2208 ret->SetLocations(nullptr);
2209}
2210
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002211void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213}
2214
2215void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002216 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002217 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002218 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002219 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002221 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 case DataType::Type::kInt8:
2223 case DataType::Type::kUint16:
2224 case DataType::Type::kInt16:
2225 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002226 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002227 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228 break;
2229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002230 case DataType::Type::kFloat32:
2231 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002232 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 break;
2234
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002235 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002236 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002238}
2239
2240void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2241 if (kIsDebugBuild) {
2242 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002243 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002245 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 case DataType::Type::kInt8:
2247 case DataType::Type::kUint16:
2248 case DataType::Type::kInt16:
2249 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002251 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002252 break;
2253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002254 case DataType::Type::kFloat32:
2255 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002256 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002257 XMM0);
2258 break;
2259
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002260 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002261 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002262 }
2263 }
2264 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002265}
2266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002268 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002269 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002271 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 case DataType::Type::kInt8:
2273 case DataType::Type::kUint16:
2274 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002275 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002277 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002279 return Location::RegisterLocation(RAX);
2280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002282 return Location::NoLocation();
2283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002284 case DataType::Type::kFloat64:
2285 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002286 return Location::FpuRegisterLocation(XMM0);
2287 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002288
2289 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002290}
2291
2292Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2293 return Location::RegisterLocation(kMethodRegisterArgument);
2294}
2295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002297 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002298 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002300 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 case DataType::Type::kInt8:
2302 case DataType::Type::kUint16:
2303 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002304 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002305 uint32_t index = gp_index_++;
2306 stack_index_++;
2307 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002308 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002309 } else {
2310 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2311 }
2312 }
2313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002315 uint32_t index = gp_index_;
2316 stack_index_ += 2;
2317 if (index < calling_convention.GetNumberOfRegisters()) {
2318 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002319 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002320 } else {
2321 gp_index_ += 2;
2322 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2323 }
2324 }
2325
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002327 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002328 stack_index_++;
2329 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002330 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002331 } else {
2332 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2333 }
2334 }
2335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002337 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002338 stack_index_ += 2;
2339 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002340 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002341 } else {
2342 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2343 }
2344 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002345
Aart Bik66c158e2018-01-31 12:55:04 -08002346 case DataType::Type::kUint32:
2347 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002349 LOG(FATAL) << "Unexpected parameter type " << type;
2350 break;
2351 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002352 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002353}
2354
Calin Juravle175dc732015-08-25 15:42:32 +01002355void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2356 // The trampoline uses the same calling convention as dex calling conventions,
2357 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2358 // the method_idx.
2359 HandleInvoke(invoke);
2360}
2361
2362void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2363 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2364}
2365
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002366void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002367 // Explicit clinit checks triggered by static invokes must have been pruned by
2368 // art::PrepareForRegisterAllocation.
2369 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002370
Mark Mendellfb8d2792015-03-31 22:16:59 -04002371 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002372 if (intrinsic.TryDispatch(invoke)) {
2373 return;
2374 }
2375
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376 HandleInvoke(invoke);
2377}
2378
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002379static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2380 if (invoke->GetLocations()->Intrinsified()) {
2381 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2382 intrinsic.Dispatch(invoke);
2383 return true;
2384 }
2385 return false;
2386}
2387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002388void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002389 // Explicit clinit checks triggered by static invokes must have been pruned by
2390 // art::PrepareForRegisterAllocation.
2391 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002392
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002393 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2394 return;
2395 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002396
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002397 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002398 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002399 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002400}
2401
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002402void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002403 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002404 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002405}
2406
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002407void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002408 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002409 if (intrinsic.TryDispatch(invoke)) {
2410 return;
2411 }
2412
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002413 HandleInvoke(invoke);
2414}
2415
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002416void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002417 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2418 return;
2419 }
2420
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002421 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002422 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002423}
2424
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2426 HandleInvoke(invoke);
2427 // Add the hidden argument.
2428 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2429}
2430
2431void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2432 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002433 LocationSummary* locations = invoke->GetLocations();
2434 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2435 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002436 Location receiver = locations->InAt(0);
2437 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2438
Roland Levillain0d5a2812015-11-13 10:07:31 +00002439 // Set the hidden argument. This is safe to do this here, as RAX
2440 // won't be modified thereafter, before the `call` instruction.
2441 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002442 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002443
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444 if (receiver.IsStackSlot()) {
2445 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002446 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002447 __ movl(temp, Address(temp, class_offset));
2448 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002449 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002451 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002452 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002453 // Instead of simply (possibly) unpoisoning `temp` here, we should
2454 // emit a read barrier for the previous class reference load.
2455 // However this is not required in practice, as this is an
2456 // intermediate/temporary reference and because the current
2457 // concurrent copying collector keeps the from-space memory
2458 // intact/accessible until the end of the marking phase (the
2459 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002460 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002461 // temp = temp->GetAddressOfIMT()
2462 __ movq(temp,
2463 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2464 // temp = temp->GetImtEntryAt(method_offset);
2465 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002466 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002467 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002468 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002469 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002470 __ call(Address(
2471 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002472
2473 DCHECK(!codegen_->IsLeafMethod());
2474 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2475}
2476
Orion Hodsonac141392017-01-13 11:53:47 +00002477void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2478 HandleInvoke(invoke);
2479}
2480
2481void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2482 codegen_->GenerateInvokePolymorphicCall(invoke);
2483}
2484
Roland Levillain88cb1752014-10-20 16:36:47 +01002485void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2486 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002487 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002488 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002489 case DataType::Type::kInt32:
2490 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002491 locations->SetInAt(0, Location::RequiresRegister());
2492 locations->SetOut(Location::SameAsFirstInput());
2493 break;
2494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kFloat32:
2496 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002497 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002498 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002499 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002500 break;
2501
2502 default:
2503 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2504 }
2505}
2506
2507void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2508 LocationSummary* locations = neg->GetLocations();
2509 Location out = locations->Out();
2510 Location in = locations->InAt(0);
2511 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002512 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002513 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002514 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002516 break;
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002519 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002520 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002522 break;
2523
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002525 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002526 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002527 // Implement float negation with an exclusive or with value
2528 // 0x80000000 (mask for bit 31, representing the sign of a
2529 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002530 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002531 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002532 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002533 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002536 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002537 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002538 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002539 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002540 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002541 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002542 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002543 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002544 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002545
2546 default:
2547 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2548 }
2549}
2550
Roland Levillaindff1f282014-11-05 14:15:05 +00002551void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2552 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002553 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 DataType::Type result_type = conversion->GetResultType();
2555 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002556 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2557 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002558
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002560 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002562 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002564 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2565 locations->SetInAt(0, Location::Any());
2566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002567 break;
2568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002569 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002570 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002572 locations->SetInAt(0, Location::Any());
2573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2574 break;
2575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002577 locations->SetInAt(0, Location::RequiresFpuRegister());
2578 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002579 break;
2580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002581 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002582 locations->SetInAt(0, Location::RequiresFpuRegister());
2583 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002584 break;
2585
2586 default:
2587 LOG(FATAL) << "Unexpected type conversion from " << input_type
2588 << " to " << result_type;
2589 }
2590 break;
2591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002594 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002595 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002597 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598 case DataType::Type::kInt16:
2599 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002600 // TODO: We would benefit from a (to-be-implemented)
2601 // Location::RegisterOrStackSlot requirement for this input.
2602 locations->SetInAt(0, Location::RequiresRegister());
2603 locations->SetOut(Location::RequiresRegister());
2604 break;
2605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002606 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002607 locations->SetInAt(0, Location::RequiresFpuRegister());
2608 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002609 break;
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002612 locations->SetInAt(0, Location::RequiresFpuRegister());
2613 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 }
2620 break;
2621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002623 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002625 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002626 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002627 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 case DataType::Type::kInt16:
2629 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002630 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002631 locations->SetOut(Location::RequiresFpuRegister());
2632 break;
2633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002635 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002636 locations->SetOut(Location::RequiresFpuRegister());
2637 break;
2638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002640 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002642 break;
2643
2644 default:
2645 LOG(FATAL) << "Unexpected type conversion from " << input_type
2646 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002647 }
Roland Levillaincff13742014-11-17 14:32:17 +00002648 break;
2649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002651 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002655 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 case DataType::Type::kInt16:
2657 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002658 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002659 locations->SetOut(Location::RequiresFpuRegister());
2660 break;
2661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002663 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002664 locations->SetOut(Location::RequiresFpuRegister());
2665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002668 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002669 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
2681 }
2682}
2683
2684void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2685 LocationSummary* locations = conversion->GetLocations();
2686 Location out = locations->Out();
2687 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 DataType::Type result_type = conversion->GetResultType();
2689 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002690 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2691 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002692 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002693 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002694 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002695 case DataType::Type::kInt8:
2696 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 case DataType::Type::kInt16:
2698 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002699 case DataType::Type::kInt64:
2700 if (in.IsRegister()) {
2701 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2702 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2703 __ movzxb(out.AsRegister<CpuRegister>(),
2704 Address(CpuRegister(RSP), in.GetStackIndex()));
2705 } else {
2706 __ movl(out.AsRegister<CpuRegister>(),
2707 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2708 }
2709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 }
2715 break;
2716
2717 case DataType::Type::kInt8:
2718 switch (input_type) {
2719 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002721 case DataType::Type::kInt16:
2722 case DataType::Type::kInt32:
2723 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002724 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002726 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002728 Address(CpuRegister(RSP), in.GetStackIndex()));
2729 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002731 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002732 }
2733 break;
2734
2735 default:
2736 LOG(FATAL) << "Unexpected type conversion from " << input_type
2737 << " to " << result_type;
2738 }
2739 break;
2740
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002741 case DataType::Type::kUint16:
2742 switch (input_type) {
2743 case DataType::Type::kInt8:
2744 case DataType::Type::kInt16:
2745 case DataType::Type::kInt32:
2746 case DataType::Type::kInt64:
2747 if (in.IsRegister()) {
2748 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2749 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2750 __ movzxw(out.AsRegister<CpuRegister>(),
2751 Address(CpuRegister(RSP), in.GetStackIndex()));
2752 } else {
2753 __ movl(out.AsRegister<CpuRegister>(),
2754 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2755 }
2756 break;
2757
2758 default:
2759 LOG(FATAL) << "Unexpected type conversion from " << input_type
2760 << " to " << result_type;
2761 }
2762 break;
2763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002765 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002767 case DataType::Type::kInt32:
2768 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002769 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002771 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002773 Address(CpuRegister(RSP), in.GetStackIndex()));
2774 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002776 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002777 }
2778 break;
2779
2780 default:
2781 LOG(FATAL) << "Unexpected type conversion from " << input_type
2782 << " to " << result_type;
2783 }
2784 break;
2785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002787 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002789 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002791 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002792 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002793 Address(CpuRegister(RSP), in.GetStackIndex()));
2794 } else {
2795 DCHECK(in.IsConstant());
2796 DCHECK(in.GetConstant()->IsLongConstant());
2797 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002798 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002799 }
2800 break;
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002803 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2804 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002805 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002806
2807 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002808 // if input >= (float)INT_MAX goto done
2809 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002810 __ j(kAboveEqual, &done);
2811 // if input == NaN goto nan
2812 __ j(kUnordered, &nan);
2813 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002814 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002815 __ jmp(&done);
2816 __ Bind(&nan);
2817 // output = 0
2818 __ xorl(output, output);
2819 __ Bind(&done);
2820 break;
2821 }
2822
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002824 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2825 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002826 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002827
2828 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002829 // if input >= (double)INT_MAX goto done
2830 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002831 __ j(kAboveEqual, &done);
2832 // if input == NaN goto nan
2833 __ j(kUnordered, &nan);
2834 // output = double-to-int-truncate(input)
2835 __ cvttsd2si(output, input);
2836 __ jmp(&done);
2837 __ Bind(&nan);
2838 // output = 0
2839 __ xorl(output, output);
2840 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002841 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002842 }
Roland Levillain946e1432014-11-11 17:35:19 +00002843
2844 default:
2845 LOG(FATAL) << "Unexpected type conversion from " << input_type
2846 << " to " << result_type;
2847 }
2848 break;
2849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002851 switch (input_type) {
2852 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002854 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002856 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002857 case DataType::Type::kInt16:
2858 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002859 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002861 break;
2862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002863 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002864 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2865 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002866 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002867
Mark Mendell92e83bf2015-05-07 11:25:03 -04002868 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002869 // if input >= (float)LONG_MAX goto done
2870 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002871 __ j(kAboveEqual, &done);
2872 // if input == NaN goto nan
2873 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002874 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002875 __ cvttss2si(output, input, true);
2876 __ jmp(&done);
2877 __ Bind(&nan);
2878 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002879 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002880 __ Bind(&done);
2881 break;
2882 }
2883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002884 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002885 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2886 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002887 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002888
Mark Mendell92e83bf2015-05-07 11:25:03 -04002889 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002890 // if input >= (double)LONG_MAX goto done
2891 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002892 __ j(kAboveEqual, &done);
2893 // if input == NaN goto nan
2894 __ j(kUnordered, &nan);
2895 // output = double-to-long-truncate(input)
2896 __ cvttsd2si(output, input, true);
2897 __ jmp(&done);
2898 __ Bind(&nan);
2899 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002900 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002901 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002902 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002903 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002904
2905 default:
2906 LOG(FATAL) << "Unexpected type conversion from " << input_type
2907 << " to " << result_type;
2908 }
2909 break;
2910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002912 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002914 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002915 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002916 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 case DataType::Type::kInt16:
2918 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002919 if (in.IsRegister()) {
2920 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2921 } else if (in.IsConstant()) {
2922 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2923 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002924 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002925 } else {
2926 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2927 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2928 }
Roland Levillaincff13742014-11-17 14:32:17 +00002929 break;
2930
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002931 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002932 if (in.IsRegister()) {
2933 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2934 } else if (in.IsConstant()) {
2935 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2936 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002937 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002938 } else {
2939 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2940 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2941 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002942 break;
2943
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002945 if (in.IsFpuRegister()) {
2946 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2947 } else if (in.IsConstant()) {
2948 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2949 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002950 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002951 } else {
2952 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2953 Address(CpuRegister(RSP), in.GetStackIndex()));
2954 }
Roland Levillaincff13742014-11-17 14:32:17 +00002955 break;
2956
2957 default:
2958 LOG(FATAL) << "Unexpected type conversion from " << input_type
2959 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002960 }
Roland Levillaincff13742014-11-17 14:32:17 +00002961 break;
2962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002964 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002966 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002968 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 case DataType::Type::kInt16:
2970 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002971 if (in.IsRegister()) {
2972 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2973 } else if (in.IsConstant()) {
2974 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2975 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002976 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002977 } else {
2978 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2979 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2980 }
Roland Levillaincff13742014-11-17 14:32:17 +00002981 break;
2982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002984 if (in.IsRegister()) {
2985 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2986 } else if (in.IsConstant()) {
2987 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2988 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002989 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002990 } else {
2991 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2992 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2993 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002994 break;
2995
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002996 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002997 if (in.IsFpuRegister()) {
2998 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2999 } else if (in.IsConstant()) {
3000 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3001 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003002 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003003 } else {
3004 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3005 Address(CpuRegister(RSP), in.GetStackIndex()));
3006 }
Roland Levillaincff13742014-11-17 14:32:17 +00003007 break;
3008
3009 default:
3010 LOG(FATAL) << "Unexpected type conversion from " << input_type
3011 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003012 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003013 break;
3014
3015 default:
3016 LOG(FATAL) << "Unexpected type conversion from " << input_type
3017 << " to " << result_type;
3018 }
3019}
3020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003021void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003022 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003023 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003024 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003025 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003026 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003027 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003029 break;
3030 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003033 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003034 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003035 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003036 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037 break;
3038 }
3039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 case DataType::Type::kFloat64:
3041 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003042 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003043 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003044 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003045 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003046 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003047
3048 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003049 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003050 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003051}
3052
3053void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3054 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003055 Location first = locations->InAt(0);
3056 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003057 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003058
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003061 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003062 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3063 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003064 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3065 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003066 } else {
3067 __ leal(out.AsRegister<CpuRegister>(), Address(
3068 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3069 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003070 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003071 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3072 __ addl(out.AsRegister<CpuRegister>(),
3073 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3074 } else {
3075 __ leal(out.AsRegister<CpuRegister>(), Address(
3076 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3077 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003078 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003079 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003080 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003081 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003082 break;
3083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003086 if (second.IsRegister()) {
3087 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3088 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003089 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3090 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003091 } else {
3092 __ leaq(out.AsRegister<CpuRegister>(), Address(
3093 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3094 }
3095 } else {
3096 DCHECK(second.IsConstant());
3097 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3098 int32_t int32_value = Low32Bits(value);
3099 DCHECK_EQ(int32_value, value);
3100 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3101 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3102 } else {
3103 __ leaq(out.AsRegister<CpuRegister>(), Address(
3104 first.AsRegister<CpuRegister>(), int32_value));
3105 }
3106 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003107 break;
3108 }
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003111 if (second.IsFpuRegister()) {
3112 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3113 } else if (second.IsConstant()) {
3114 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003115 codegen_->LiteralFloatAddress(
3116 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003117 } else {
3118 DCHECK(second.IsStackSlot());
3119 __ addss(first.AsFpuRegister<XmmRegister>(),
3120 Address(CpuRegister(RSP), second.GetStackIndex()));
3121 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003122 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003126 if (second.IsFpuRegister()) {
3127 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3128 } else if (second.IsConstant()) {
3129 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003130 codegen_->LiteralDoubleAddress(
3131 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003132 } else {
3133 DCHECK(second.IsDoubleStackSlot());
3134 __ addsd(first.AsFpuRegister<XmmRegister>(),
3135 Address(CpuRegister(RSP), second.GetStackIndex()));
3136 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003137 break;
3138 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003139
3140 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003141 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003142 }
3143}
3144
3145void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003146 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003147 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003148 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003150 locations->SetInAt(0, Location::RequiresRegister());
3151 locations->SetInAt(1, Location::Any());
3152 locations->SetOut(Location::SameAsFirstInput());
3153 break;
3154 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003155 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003156 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003157 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003158 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003159 break;
3160 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kFloat32:
3162 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003163 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003164 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003165 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003166 break;
Calin Juravle11351682014-10-23 15:38:15 +01003167 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003168 default:
Calin Juravle11351682014-10-23 15:38:15 +01003169 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003170 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003171}
3172
3173void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3174 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003175 Location first = locations->InAt(0);
3176 Location second = locations->InAt(1);
3177 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003178 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003180 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003182 } else if (second.IsConstant()) {
3183 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003184 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003185 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003187 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003188 break;
3189 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003191 if (second.IsConstant()) {
3192 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3193 DCHECK(IsInt<32>(value));
3194 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3195 } else {
3196 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3197 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003198 break;
3199 }
3200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003202 if (second.IsFpuRegister()) {
3203 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3204 } else if (second.IsConstant()) {
3205 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003206 codegen_->LiteralFloatAddress(
3207 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003208 } else {
3209 DCHECK(second.IsStackSlot());
3210 __ subss(first.AsFpuRegister<XmmRegister>(),
3211 Address(CpuRegister(RSP), second.GetStackIndex()));
3212 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003213 break;
Calin Juravle11351682014-10-23 15:38:15 +01003214 }
3215
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003216 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003217 if (second.IsFpuRegister()) {
3218 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3219 } else if (second.IsConstant()) {
3220 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003221 codegen_->LiteralDoubleAddress(
3222 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003223 } else {
3224 DCHECK(second.IsDoubleStackSlot());
3225 __ subsd(first.AsFpuRegister<XmmRegister>(),
3226 Address(CpuRegister(RSP), second.GetStackIndex()));
3227 }
Calin Juravle11351682014-10-23 15:38:15 +01003228 break;
3229 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003230
3231 default:
Calin Juravle11351682014-10-23 15:38:15 +01003232 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003233 }
3234}
3235
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3237 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003238 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003239 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003241 locations->SetInAt(0, Location::RequiresRegister());
3242 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003243 if (mul->InputAt(1)->IsIntConstant()) {
3244 // Can use 3 operand multiply.
3245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3246 } else {
3247 locations->SetOut(Location::SameAsFirstInput());
3248 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249 break;
3250 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003251 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003253 locations->SetInAt(1, Location::Any());
3254 if (mul->InputAt(1)->IsLongConstant() &&
3255 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003256 // Can use 3 operand multiply.
3257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3258 } else {
3259 locations->SetOut(Location::SameAsFirstInput());
3260 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003261 break;
3262 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kFloat32:
3264 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003265 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003266 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003267 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003268 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003269 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003270
3271 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003272 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003273 }
3274}
3275
3276void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3277 LocationSummary* locations = mul->GetLocations();
3278 Location first = locations->InAt(0);
3279 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003280 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003281 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003283 // The constant may have ended up in a register, so test explicitly to avoid
3284 // problems where the output may not be the same as the first operand.
3285 if (mul->InputAt(1)->IsIntConstant()) {
3286 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3287 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3288 } else if (second.IsRegister()) {
3289 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003290 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003291 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003292 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003293 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003294 __ imull(first.AsRegister<CpuRegister>(),
3295 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003296 }
3297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003298 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003299 // The constant may have ended up in a register, so test explicitly to avoid
3300 // problems where the output may not be the same as the first operand.
3301 if (mul->InputAt(1)->IsLongConstant()) {
3302 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3303 if (IsInt<32>(value)) {
3304 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3305 Immediate(static_cast<int32_t>(value)));
3306 } else {
3307 // Have to use the constant area.
3308 DCHECK(first.Equals(out));
3309 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3310 }
3311 } else if (second.IsRegister()) {
3312 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003313 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003314 } else {
3315 DCHECK(second.IsDoubleStackSlot());
3316 DCHECK(first.Equals(out));
3317 __ imulq(first.AsRegister<CpuRegister>(),
3318 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003319 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320 break;
3321 }
3322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003323 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003324 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003325 if (second.IsFpuRegister()) {
3326 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3327 } else if (second.IsConstant()) {
3328 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003329 codegen_->LiteralFloatAddress(
3330 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003331 } else {
3332 DCHECK(second.IsStackSlot());
3333 __ mulss(first.AsFpuRegister<XmmRegister>(),
3334 Address(CpuRegister(RSP), second.GetStackIndex()));
3335 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003336 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003337 }
3338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003339 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003340 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003341 if (second.IsFpuRegister()) {
3342 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3343 } else if (second.IsConstant()) {
3344 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003345 codegen_->LiteralDoubleAddress(
3346 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003347 } else {
3348 DCHECK(second.IsDoubleStackSlot());
3349 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3350 Address(CpuRegister(RSP), second.GetStackIndex()));
3351 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003352 break;
3353 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354
3355 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003356 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003357 }
3358}
3359
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003360void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3361 uint32_t stack_adjustment, bool is_float) {
3362 if (source.IsStackSlot()) {
3363 DCHECK(is_float);
3364 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3365 } else if (source.IsDoubleStackSlot()) {
3366 DCHECK(!is_float);
3367 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3368 } else {
3369 // Write the value to the temporary location on the stack and load to FP stack.
3370 if (is_float) {
3371 Location stack_temp = Location::StackSlot(temp_offset);
3372 codegen_->Move(stack_temp, source);
3373 __ flds(Address(CpuRegister(RSP), temp_offset));
3374 } else {
3375 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3376 codegen_->Move(stack_temp, source);
3377 __ fldl(Address(CpuRegister(RSP), temp_offset));
3378 }
3379 }
3380}
3381
3382void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003383 DataType::Type type = rem->GetResultType();
3384 bool is_float = type == DataType::Type::kFloat32;
3385 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003386 LocationSummary* locations = rem->GetLocations();
3387 Location first = locations->InAt(0);
3388 Location second = locations->InAt(1);
3389 Location out = locations->Out();
3390
3391 // Create stack space for 2 elements.
3392 // TODO: enhance register allocator to ask for stack temporaries.
3393 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3394
3395 // Load the values to the FP stack in reverse order, using temporaries if needed.
3396 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3397 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3398
3399 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003400 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003401 __ Bind(&retry);
3402 __ fprem();
3403
3404 // Move FP status to AX.
3405 __ fstsw();
3406
3407 // And see if the argument reduction is complete. This is signaled by the
3408 // C2 FPU flag bit set to 0.
3409 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3410 __ j(kNotEqual, &retry);
3411
3412 // We have settled on the final value. Retrieve it into an XMM register.
3413 // Store FP top of stack to real stack.
3414 if (is_float) {
3415 __ fsts(Address(CpuRegister(RSP), 0));
3416 } else {
3417 __ fstl(Address(CpuRegister(RSP), 0));
3418 }
3419
3420 // Pop the 2 items from the FP stack.
3421 __ fucompp();
3422
3423 // Load the value from the stack into an XMM register.
3424 DCHECK(out.IsFpuRegister()) << out;
3425 if (is_float) {
3426 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3427 } else {
3428 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3429 }
3430
3431 // And remove the temporary stack space we allocated.
3432 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3433}
3434
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3436 DCHECK(instruction->IsDiv() || instruction->IsRem());
3437
3438 LocationSummary* locations = instruction->GetLocations();
3439 Location second = locations->InAt(1);
3440 DCHECK(second.IsConstant());
3441
3442 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3443 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003444 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445
3446 DCHECK(imm == 1 || imm == -1);
3447
3448 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003450 if (instruction->IsRem()) {
3451 __ xorl(output_register, output_register);
3452 } else {
3453 __ movl(output_register, input_register);
3454 if (imm == -1) {
3455 __ negl(output_register);
3456 }
3457 }
3458 break;
3459 }
3460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003462 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003463 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464 } else {
3465 __ movq(output_register, input_register);
3466 if (imm == -1) {
3467 __ negq(output_register);
3468 }
3469 }
3470 break;
3471 }
3472
3473 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003474 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475 }
3476}
3477
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003478void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479 LocationSummary* locations = instruction->GetLocations();
3480 Location second = locations->InAt(1);
3481
3482 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3483 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3484
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003485 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003486 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3487 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488
3489 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3490
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003491 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003492 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 __ testl(numerator, numerator);
3494 __ cmov(kGreaterEqual, tmp, numerator);
3495 int shift = CTZ(imm);
3496 __ sarl(tmp, Immediate(shift));
3497
3498 if (imm < 0) {
3499 __ negl(tmp);
3500 }
3501
3502 __ movl(output_register, tmp);
3503 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003505 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3506
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003507 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 __ addq(rdx, numerator);
3509 __ testq(numerator, numerator);
3510 __ cmov(kGreaterEqual, rdx, numerator);
3511 int shift = CTZ(imm);
3512 __ sarq(rdx, Immediate(shift));
3513
3514 if (imm < 0) {
3515 __ negq(rdx);
3516 }
3517
3518 __ movq(output_register, rdx);
3519 }
3520}
3521
3522void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3523 DCHECK(instruction->IsDiv() || instruction->IsRem());
3524
3525 LocationSummary* locations = instruction->GetLocations();
3526 Location second = locations->InAt(1);
3527
3528 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3529 : locations->GetTemp(0).AsRegister<CpuRegister>();
3530 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3531 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3532 : locations->Out().AsRegister<CpuRegister>();
3533 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3534
3535 DCHECK_EQ(RAX, eax.AsRegister());
3536 DCHECK_EQ(RDX, edx.AsRegister());
3537 if (instruction->IsDiv()) {
3538 DCHECK_EQ(RAX, out.AsRegister());
3539 } else {
3540 DCHECK_EQ(RDX, out.AsRegister());
3541 }
3542
3543 int64_t magic;
3544 int shift;
3545
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003546 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003547 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3549
3550 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3551
3552 __ movl(numerator, eax);
3553
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 __ movl(eax, Immediate(magic));
3555 __ imull(numerator);
3556
3557 if (imm > 0 && magic < 0) {
3558 __ addl(edx, numerator);
3559 } else if (imm < 0 && magic > 0) {
3560 __ subl(edx, numerator);
3561 }
3562
3563 if (shift != 0) {
3564 __ sarl(edx, Immediate(shift));
3565 }
3566
3567 __ movl(eax, edx);
3568 __ shrl(edx, Immediate(31));
3569 __ addl(edx, eax);
3570
3571 if (instruction->IsRem()) {
3572 __ movl(eax, numerator);
3573 __ imull(edx, Immediate(imm));
3574 __ subl(eax, edx);
3575 __ movl(edx, eax);
3576 } else {
3577 __ movl(eax, edx);
3578 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003579 } else {
3580 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003582 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583
3584 CpuRegister rax = eax;
3585 CpuRegister rdx = edx;
3586
3587 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3588
3589 // Save the numerator.
3590 __ movq(numerator, rax);
3591
3592 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003593 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003594
3595 // RDX:RAX = magic * numerator
3596 __ imulq(numerator);
3597
3598 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003599 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003600 __ addq(rdx, numerator);
3601 } else if (imm < 0 && magic > 0) {
3602 // RDX -= numerator
3603 __ subq(rdx, numerator);
3604 }
3605
3606 // Shift if needed.
3607 if (shift != 0) {
3608 __ sarq(rdx, Immediate(shift));
3609 }
3610
3611 // RDX += 1 if RDX < 0
3612 __ movq(rax, rdx);
3613 __ shrq(rdx, Immediate(63));
3614 __ addq(rdx, rax);
3615
3616 if (instruction->IsRem()) {
3617 __ movq(rax, numerator);
3618
3619 if (IsInt<32>(imm)) {
3620 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3621 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003622 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 }
3624
3625 __ subq(rax, rdx);
3626 __ movq(rdx, rax);
3627 } else {
3628 __ movq(rax, rdx);
3629 }
3630 }
3631}
3632
Calin Juravlebacfec32014-11-14 15:54:36 +00003633void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3634 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003635 DataType::Type type = instruction->GetResultType();
3636 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003637
3638 bool is_div = instruction->IsDiv();
3639 LocationSummary* locations = instruction->GetLocations();
3640
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003641 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3642 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003643
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003645 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003646
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003648 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003649
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003650 if (imm == 0) {
3651 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3652 } else if (imm == 1 || imm == -1) {
3653 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003654 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003655 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003656 } else {
3657 DCHECK(imm <= -2 || imm >= 2);
3658 GenerateDivRemWithAnyConstant(instruction);
3659 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003660 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003661 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003662 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003663 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003664 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003665
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003666 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3667 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3668 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3669 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003670 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003671 __ cmpl(second_reg, Immediate(-1));
3672 __ j(kEqual, slow_path->GetEntryLabel());
3673 // edx:eax <- sign-extended of eax
3674 __ cdq();
3675 // eax = quotient, edx = remainder
3676 __ idivl(second_reg);
3677 } else {
3678 __ cmpq(second_reg, Immediate(-1));
3679 __ j(kEqual, slow_path->GetEntryLabel());
3680 // rdx:rax <- sign-extended of rax
3681 __ cqo();
3682 // rax = quotient, rdx = remainder
3683 __ idivq(second_reg);
3684 }
3685 __ Bind(slow_path->GetExitLabel());
3686 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003687}
3688
Calin Juravle7c4954d2014-10-28 16:57:40 +00003689void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3690 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003691 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003692 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kInt32:
3694 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003695 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003696 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003697 locations->SetOut(Location::SameAsFirstInput());
3698 // Intel uses edx:eax as the dividend.
3699 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003700 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3701 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3702 // output and request another temp.
3703 if (div->InputAt(1)->IsConstant()) {
3704 locations->AddTemp(Location::RequiresRegister());
3705 }
Calin Juravled0d48522014-11-04 16:40:20 +00003706 break;
3707 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003709 case DataType::Type::kFloat32:
3710 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003711 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003712 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003713 locations->SetOut(Location::SameAsFirstInput());
3714 break;
3715 }
3716
3717 default:
3718 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3719 }
3720}
3721
3722void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3723 LocationSummary* locations = div->GetLocations();
3724 Location first = locations->InAt(0);
3725 Location second = locations->InAt(1);
3726 DCHECK(first.Equals(locations->Out()));
3727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003729 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 case DataType::Type::kInt32:
3731 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003732 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003733 break;
3734 }
3735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003737 if (second.IsFpuRegister()) {
3738 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3739 } else if (second.IsConstant()) {
3740 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003741 codegen_->LiteralFloatAddress(
3742 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003743 } else {
3744 DCHECK(second.IsStackSlot());
3745 __ divss(first.AsFpuRegister<XmmRegister>(),
3746 Address(CpuRegister(RSP), second.GetStackIndex()));
3747 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003748 break;
3749 }
3750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003751 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003752 if (second.IsFpuRegister()) {
3753 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3754 } else if (second.IsConstant()) {
3755 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003756 codegen_->LiteralDoubleAddress(
3757 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003758 } else {
3759 DCHECK(second.IsDoubleStackSlot());
3760 __ divsd(first.AsFpuRegister<XmmRegister>(),
3761 Address(CpuRegister(RSP), second.GetStackIndex()));
3762 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003763 break;
3764 }
3765
3766 default:
3767 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3768 }
3769}
3770
Calin Juravlebacfec32014-11-14 15:54:36 +00003771void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003772 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003773 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003774 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003775
3776 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kInt32:
3778 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003780 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3782 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003783 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3784 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3785 // output and request another temp.
3786 if (rem->InputAt(1)->IsConstant()) {
3787 locations->AddTemp(Location::RequiresRegister());
3788 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003789 break;
3790 }
3791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003792 case DataType::Type::kFloat32:
3793 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003794 locations->SetInAt(0, Location::Any());
3795 locations->SetInAt(1, Location::Any());
3796 locations->SetOut(Location::RequiresFpuRegister());
3797 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 break;
3799 }
3800
3801 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003802 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003803 }
3804}
3805
3806void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003808 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt32:
3810 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003811 GenerateDivRemIntegral(rem);
3812 break;
3813 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 case DataType::Type::kFloat32:
3815 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003816 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003817 break;
3818 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003819 default:
3820 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3821 }
3822}
3823
Aart Bik3dad3412018-02-28 12:01:46 -08003824void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
3825 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
3826 switch (abs->GetResultType()) {
3827 case DataType::Type::kInt32:
3828 case DataType::Type::kInt64:
3829 locations->SetInAt(0, Location::RequiresRegister());
3830 locations->SetOut(Location::SameAsFirstInput());
3831 locations->AddTemp(Location::RequiresRegister());
3832 break;
3833 case DataType::Type::kFloat32:
3834 case DataType::Type::kFloat64:
3835 locations->SetInAt(0, Location::RequiresFpuRegister());
3836 locations->SetOut(Location::SameAsFirstInput());
3837 locations->AddTemp(Location::RequiresFpuRegister());
3838 break;
3839 default:
3840 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
3841 }
3842}
3843
3844void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
3845 LocationSummary* locations = abs->GetLocations();
3846 switch (abs->GetResultType()) {
3847 case DataType::Type::kInt32: {
3848 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3849 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
3850 // Create mask.
3851 __ movl(mask, out);
3852 __ sarl(mask, Immediate(31));
3853 // Add mask.
3854 __ addl(out, mask);
3855 __ xorl(out, mask);
3856 break;
3857 }
3858 case DataType::Type::kInt64: {
3859 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3860 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
3861 // Create mask.
3862 __ movq(mask, out);
3863 __ sarq(mask, Immediate(63));
3864 // Add mask.
3865 __ addq(out, mask);
3866 __ xorq(out, mask);
3867 break;
3868 }
3869 case DataType::Type::kFloat32: {
3870 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3871 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3872 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
3873 __ andps(out, mask);
3874 break;
3875 }
3876 case DataType::Type::kFloat64: {
3877 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3878 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3879 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
3880 __ andpd(out, mask);
3881 break;
3882 }
3883 default:
3884 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
3885 }
3886}
3887
Calin Juravled0d48522014-11-04 16:40:20 +00003888void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003889 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003890 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003891}
3892
3893void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003894 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003895 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003896 codegen_->AddSlowPath(slow_path);
3897
3898 LocationSummary* locations = instruction->GetLocations();
3899 Location value = locations->InAt(0);
3900
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003901 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003902 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003903 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 case DataType::Type::kInt8:
3905 case DataType::Type::kUint16:
3906 case DataType::Type::kInt16:
3907 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003908 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003909 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003910 __ j(kEqual, slow_path->GetEntryLabel());
3911 } else if (value.IsStackSlot()) {
3912 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3913 __ j(kEqual, slow_path->GetEntryLabel());
3914 } else {
3915 DCHECK(value.IsConstant()) << value;
3916 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003917 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003918 }
3919 }
3920 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003921 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003923 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003924 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003925 __ j(kEqual, slow_path->GetEntryLabel());
3926 } else if (value.IsDoubleStackSlot()) {
3927 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3928 __ j(kEqual, slow_path->GetEntryLabel());
3929 } else {
3930 DCHECK(value.IsConstant()) << value;
3931 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003932 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003933 }
3934 }
3935 break;
3936 }
3937 default:
3938 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003939 }
Calin Juravled0d48522014-11-04 16:40:20 +00003940}
3941
Calin Juravle9aec02f2014-11-18 23:06:35 +00003942void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3943 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3944
3945 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003946 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003947
3948 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003949 case DataType::Type::kInt32:
3950 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951 locations->SetInAt(0, Location::RequiresRegister());
3952 // The shift count needs to be in CL.
3953 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3954 locations->SetOut(Location::SameAsFirstInput());
3955 break;
3956 }
3957 default:
3958 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3959 }
3960}
3961
3962void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3963 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3964
3965 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003966 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003967 Location second = locations->InAt(1);
3968
3969 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003971 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003972 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003973 if (op->IsShl()) {
3974 __ shll(first_reg, second_reg);
3975 } else if (op->IsShr()) {
3976 __ sarl(first_reg, second_reg);
3977 } else {
3978 __ shrl(first_reg, second_reg);
3979 }
3980 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003981 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003982 if (op->IsShl()) {
3983 __ shll(first_reg, imm);
3984 } else if (op->IsShr()) {
3985 __ sarl(first_reg, imm);
3986 } else {
3987 __ shrl(first_reg, imm);
3988 }
3989 }
3990 break;
3991 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003992 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003993 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003995 if (op->IsShl()) {
3996 __ shlq(first_reg, second_reg);
3997 } else if (op->IsShr()) {
3998 __ sarq(first_reg, second_reg);
3999 } else {
4000 __ shrq(first_reg, second_reg);
4001 }
4002 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004003 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004004 if (op->IsShl()) {
4005 __ shlq(first_reg, imm);
4006 } else if (op->IsShr()) {
4007 __ sarq(first_reg, imm);
4008 } else {
4009 __ shrq(first_reg, imm);
4010 }
4011 }
4012 break;
4013 }
4014 default:
4015 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004016 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004017 }
4018}
4019
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004020void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004022 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004023
4024 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004025 case DataType::Type::kInt32:
4026 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004027 locations->SetInAt(0, Location::RequiresRegister());
4028 // The shift count needs to be in CL (unless it is a constant).
4029 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4030 locations->SetOut(Location::SameAsFirstInput());
4031 break;
4032 }
4033 default:
4034 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4035 UNREACHABLE();
4036 }
4037}
4038
4039void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4040 LocationSummary* locations = ror->GetLocations();
4041 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4042 Location second = locations->InAt(1);
4043
4044 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004045 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004046 if (second.IsRegister()) {
4047 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4048 __ rorl(first_reg, second_reg);
4049 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004050 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004051 __ rorl(first_reg, imm);
4052 }
4053 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004055 if (second.IsRegister()) {
4056 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4057 __ rorq(first_reg, second_reg);
4058 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004059 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004060 __ rorq(first_reg, imm);
4061 }
4062 break;
4063 default:
4064 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4065 UNREACHABLE();
4066 }
4067}
4068
Calin Juravle9aec02f2014-11-18 23:06:35 +00004069void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4070 HandleShift(shl);
4071}
4072
4073void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4074 HandleShift(shl);
4075}
4076
4077void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4078 HandleShift(shr);
4079}
4080
4081void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4082 HandleShift(shr);
4083}
4084
4085void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4086 HandleShift(ushr);
4087}
4088
4089void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4090 HandleShift(ushr);
4091}
4092
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004093void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004094 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4095 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004096 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004097 if (instruction->IsStringAlloc()) {
4098 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4099 } else {
4100 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004101 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004102 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004103}
4104
4105void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004106 // Note: if heap poisoning is enabled, the entry point takes cares
4107 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004108 if (instruction->IsStringAlloc()) {
4109 // String is allocated through StringFactory. Call NewEmptyString entry point.
4110 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004111 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004112 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4113 __ call(Address(temp, code_offset.SizeValue()));
4114 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4115 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004116 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004117 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004118 DCHECK(!codegen_->IsLeafMethod());
4119 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004120}
4121
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004122void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004123 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4124 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004125 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004126 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004127 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4128 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004129}
4130
4131void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004132 // Note: if heap poisoning is enabled, the entry point takes cares
4133 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004134 QuickEntrypointEnum entrypoint =
4135 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4136 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004137 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004138 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004139}
4140
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004141void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004143 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004144 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4145 if (location.IsStackSlot()) {
4146 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4147 } else if (location.IsDoubleStackSlot()) {
4148 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4149 }
4150 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004151}
4152
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004153void InstructionCodeGeneratorX86_64::VisitParameterValue(
4154 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004155 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004156}
4157
4158void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4159 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004160 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004161 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4162}
4163
4164void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4165 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4166 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004167}
4168
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004169void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4170 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004171 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004172 locations->SetInAt(0, Location::RequiresRegister());
4173 locations->SetOut(Location::RequiresRegister());
4174}
4175
4176void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4177 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004178 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004179 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004180 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004181 __ movq(locations->Out().AsRegister<CpuRegister>(),
4182 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004183 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004184 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004185 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004186 __ movq(locations->Out().AsRegister<CpuRegister>(),
4187 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4188 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004189 __ movq(locations->Out().AsRegister<CpuRegister>(),
4190 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004191 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004192}
4193
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004194void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004196 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004197 locations->SetInAt(0, Location::RequiresRegister());
4198 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004199}
4200
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004201void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4202 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004203 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4204 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004205 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004206 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004207 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004208 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004209 break;
4210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004211 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004212 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004213 break;
4214
4215 default:
4216 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4217 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004218}
4219
David Brazdil66d126e2015-04-03 16:02:44 +01004220void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4221 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004222 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004223 locations->SetInAt(0, Location::RequiresRegister());
4224 locations->SetOut(Location::SameAsFirstInput());
4225}
4226
4227void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004228 LocationSummary* locations = bool_not->GetLocations();
4229 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4230 locations->Out().AsRegister<CpuRegister>().AsRegister());
4231 Location out = locations->Out();
4232 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4233}
4234
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004235void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004236 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004237 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004238 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004239 locations->SetInAt(i, Location::Any());
4240 }
4241 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004242}
4243
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004244void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004245 LOG(FATAL) << "Unimplemented";
4246}
4247
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004248void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004249 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004250 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004251 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004252 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4253 */
4254 switch (kind) {
4255 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004256 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004257 break;
4258 }
4259 case MemBarrierKind::kAnyStore:
4260 case MemBarrierKind::kLoadAny:
4261 case MemBarrierKind::kStoreStore: {
4262 // nop
4263 break;
4264 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004265 case MemBarrierKind::kNTStoreStore:
4266 // Non-Temporal Store/Store needs an explicit fence.
4267 MemoryFence(/* non-temporal */ true);
4268 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004269 }
4270}
4271
4272void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4273 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4274
Roland Levillain0d5a2812015-11-13 10:07:31 +00004275 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004276 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004277 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004278 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4279 object_field_get_with_read_barrier
4280 ? LocationSummary::kCallOnSlowPath
4281 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004282 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004283 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004284 }
Calin Juravle52c48962014-12-16 17:02:57 +00004285 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004286 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004287 locations->SetOut(Location::RequiresFpuRegister());
4288 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004289 // The output overlaps for an object field get when read barriers
4290 // are enabled: we do not want the move to overwrite the object's
4291 // location, as we need it to emit the read barrier.
4292 locations->SetOut(
4293 Location::RequiresRegister(),
4294 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004295 }
Calin Juravle52c48962014-12-16 17:02:57 +00004296}
4297
4298void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4299 const FieldInfo& field_info) {
4300 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4301
4302 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004303 Location base_loc = locations->InAt(0);
4304 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004305 Location out = locations->Out();
4306 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004307 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4308 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004309 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4310
Vladimir Marko61b92282017-10-11 13:23:17 +01004311 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004312 case DataType::Type::kBool:
4313 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004314 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4315 break;
4316 }
4317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004319 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4320 break;
4321 }
4322
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004323 case DataType::Type::kUint16: {
4324 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004325 break;
4326 }
4327
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004328 case DataType::Type::kInt16: {
4329 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004330 break;
4331 }
4332
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004333 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004334 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4335 break;
4336 }
4337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004338 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004339 // /* HeapReference<Object> */ out = *(base + offset)
4340 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004341 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004342 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004343 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004344 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004345 if (is_volatile) {
4346 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4347 }
4348 } else {
4349 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4350 codegen_->MaybeRecordImplicitNullCheck(instruction);
4351 if (is_volatile) {
4352 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4353 }
4354 // If read barriers are enabled, emit read barriers other than
4355 // Baker's using a slow path (and also unpoison the loaded
4356 // reference, if heap poisoning is enabled).
4357 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4358 }
4359 break;
4360 }
4361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004363 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4364 break;
4365 }
4366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004368 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4369 break;
4370 }
4371
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004372 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004373 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4374 break;
4375 }
4376
Aart Bik66c158e2018-01-31 12:55:04 -08004377 case DataType::Type::kUint32:
4378 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004379 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004380 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004381 UNREACHABLE();
4382 }
4383
Vladimir Marko61b92282017-10-11 13:23:17 +01004384 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004385 // Potential implicit null checks, in the case of reference
4386 // fields, are handled in the previous switch statement.
4387 } else {
4388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004389 }
Roland Levillain4d027112015-07-01 15:41:14 +01004390
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004391 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004392 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004393 // Memory barriers, in the case of references, are also handled
4394 // in the previous switch statement.
4395 } else {
4396 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4397 }
Roland Levillain4d027112015-07-01 15:41:14 +01004398 }
Calin Juravle52c48962014-12-16 17:02:57 +00004399}
4400
4401void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4402 const FieldInfo& field_info) {
4403 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4404
4405 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004406 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004407 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004408 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004409 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004410 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004411
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004412 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004413 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004414 if (is_volatile) {
4415 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4416 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4417 } else {
4418 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4419 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004420 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004421 if (is_volatile) {
4422 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4423 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4424 } else {
4425 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4426 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004427 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004428 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004429 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004430 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004431 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004432 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004433 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004434 locations->AddTemp(Location::RequiresRegister());
4435 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436}
4437
Calin Juravle52c48962014-12-16 17:02:57 +00004438void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004439 const FieldInfo& field_info,
4440 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004441 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4442
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004444 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4445 Location value = locations->InAt(1);
4446 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004447 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004448 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4449
4450 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004451 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004452 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453
Mark Mendellea5af682015-10-22 17:35:49 -04004454 bool maybe_record_implicit_null_check_done = false;
4455
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004456 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004457 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004458 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004460 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004461 __ movb(Address(base, offset),
4462 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004463 } else {
4464 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4465 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466 break;
4467 }
4468
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004469 case DataType::Type::kUint16:
4470 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004471 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004472 __ movw(Address(base, offset),
4473 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004474 } else {
4475 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4476 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 break;
4478 }
4479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004480 case DataType::Type::kInt32:
4481 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004482 if (value.IsConstant()) {
4483 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004484 // `field_type == DataType::Type::kReference` implies `v == 0`.
4485 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004486 // Note: if heap poisoning is enabled, no need to poison
4487 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004488 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004489 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004490 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004491 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4492 __ movl(temp, value.AsRegister<CpuRegister>());
4493 __ PoisonHeapReference(temp);
4494 __ movl(Address(base, offset), temp);
4495 } else {
4496 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4497 }
Mark Mendell40741f32015-04-20 22:10:34 -04004498 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004499 break;
4500 }
4501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004502 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004503 if (value.IsConstant()) {
4504 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004505 codegen_->MoveInt64ToAddress(Address(base, offset),
4506 Address(base, offset + sizeof(int32_t)),
4507 v,
4508 instruction);
4509 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004510 } else {
4511 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4512 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513 break;
4514 }
4515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004516 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004517 if (value.IsConstant()) {
4518 int32_t v =
4519 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4520 __ movl(Address(base, offset), Immediate(v));
4521 } else {
4522 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4523 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004524 break;
4525 }
4526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004527 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004528 if (value.IsConstant()) {
4529 int64_t v =
4530 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4531 codegen_->MoveInt64ToAddress(Address(base, offset),
4532 Address(base, offset + sizeof(int32_t)),
4533 v,
4534 instruction);
4535 maybe_record_implicit_null_check_done = true;
4536 } else {
4537 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4538 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004539 break;
4540 }
4541
Aart Bik66c158e2018-01-31 12:55:04 -08004542 case DataType::Type::kUint32:
4543 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004544 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004546 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547 }
Calin Juravle52c48962014-12-16 17:02:57 +00004548
Mark Mendellea5af682015-10-22 17:35:49 -04004549 if (!maybe_record_implicit_null_check_done) {
4550 codegen_->MaybeRecordImplicitNullCheck(instruction);
4551 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004552
4553 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4554 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4555 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004556 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004557 }
4558
Calin Juravle52c48962014-12-16 17:02:57 +00004559 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004560 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004561 }
4562}
4563
4564void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4565 HandleFieldSet(instruction, instruction->GetFieldInfo());
4566}
4567
4568void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004569 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004570}
4571
4572void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004573 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004574}
4575
4576void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004577 HandleFieldGet(instruction, instruction->GetFieldInfo());
4578}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004579
Calin Juravle52c48962014-12-16 17:02:57 +00004580void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4581 HandleFieldGet(instruction);
4582}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583
Calin Juravle52c48962014-12-16 17:02:57 +00004584void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4585 HandleFieldGet(instruction, instruction->GetFieldInfo());
4586}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004587
Calin Juravle52c48962014-12-16 17:02:57 +00004588void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4589 HandleFieldSet(instruction, instruction->GetFieldInfo());
4590}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591
Calin Juravle52c48962014-12-16 17:02:57 +00004592void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004593 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594}
4595
Calin Juravlee460d1d2015-09-29 04:52:17 +01004596void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4597 HUnresolvedInstanceFieldGet* instruction) {
4598 FieldAccessCallingConventionX86_64 calling_convention;
4599 codegen_->CreateUnresolvedFieldLocationSummary(
4600 instruction, instruction->GetFieldType(), calling_convention);
4601}
4602
4603void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4604 HUnresolvedInstanceFieldGet* instruction) {
4605 FieldAccessCallingConventionX86_64 calling_convention;
4606 codegen_->GenerateUnresolvedFieldAccess(instruction,
4607 instruction->GetFieldType(),
4608 instruction->GetFieldIndex(),
4609 instruction->GetDexPc(),
4610 calling_convention);
4611}
4612
4613void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4614 HUnresolvedInstanceFieldSet* instruction) {
4615 FieldAccessCallingConventionX86_64 calling_convention;
4616 codegen_->CreateUnresolvedFieldLocationSummary(
4617 instruction, instruction->GetFieldType(), calling_convention);
4618}
4619
4620void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4621 HUnresolvedInstanceFieldSet* instruction) {
4622 FieldAccessCallingConventionX86_64 calling_convention;
4623 codegen_->GenerateUnresolvedFieldAccess(instruction,
4624 instruction->GetFieldType(),
4625 instruction->GetFieldIndex(),
4626 instruction->GetDexPc(),
4627 calling_convention);
4628}
4629
4630void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4631 HUnresolvedStaticFieldGet* instruction) {
4632 FieldAccessCallingConventionX86_64 calling_convention;
4633 codegen_->CreateUnresolvedFieldLocationSummary(
4634 instruction, instruction->GetFieldType(), calling_convention);
4635}
4636
4637void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4638 HUnresolvedStaticFieldGet* instruction) {
4639 FieldAccessCallingConventionX86_64 calling_convention;
4640 codegen_->GenerateUnresolvedFieldAccess(instruction,
4641 instruction->GetFieldType(),
4642 instruction->GetFieldIndex(),
4643 instruction->GetDexPc(),
4644 calling_convention);
4645}
4646
4647void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4648 HUnresolvedStaticFieldSet* instruction) {
4649 FieldAccessCallingConventionX86_64 calling_convention;
4650 codegen_->CreateUnresolvedFieldLocationSummary(
4651 instruction, instruction->GetFieldType(), calling_convention);
4652}
4653
4654void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4655 HUnresolvedStaticFieldSet* instruction) {
4656 FieldAccessCallingConventionX86_64 calling_convention;
4657 codegen_->GenerateUnresolvedFieldAccess(instruction,
4658 instruction->GetFieldType(),
4659 instruction->GetFieldIndex(),
4660 instruction->GetDexPc(),
4661 calling_convention);
4662}
4663
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004664void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004665 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4666 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4667 ? Location::RequiresRegister()
4668 : Location::Any();
4669 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004670}
4671
Calin Juravle2ae48182016-03-16 14:05:09 +00004672void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4673 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004674 return;
4675 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004676 LocationSummary* locations = instruction->GetLocations();
4677 Location obj = locations->InAt(0);
4678
4679 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004680 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004681}
4682
Calin Juravle2ae48182016-03-16 14:05:09 +00004683void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004684 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004685 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004686
4687 LocationSummary* locations = instruction->GetLocations();
4688 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004689
4690 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004691 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004692 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004693 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004694 } else {
4695 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004696 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004697 __ jmp(slow_path->GetEntryLabel());
4698 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004699 }
4700 __ j(kEqual, slow_path->GetEntryLabel());
4701}
4702
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004703void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004704 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004705}
4706
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004708 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004709 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004710 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004711 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4712 object_array_get_with_read_barrier
4713 ? LocationSummary::kCallOnSlowPath
4714 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004715 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004716 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004717 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004718 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004719 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004720 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004721 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4722 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004723 // The output overlaps for an object array get when read barriers
4724 // are enabled: we do not want the move to overwrite the array's
4725 // location, as we need it to emit the read barrier.
4726 locations->SetOut(
4727 Location::RequiresRegister(),
4728 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004729 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004730}
4731
4732void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4733 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004734 Location obj_loc = locations->InAt(0);
4735 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004736 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004737 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004738 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004741 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004742 case DataType::Type::kBool:
4743 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004745 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 break;
4747 }
4748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004749 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004750 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004751 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 break;
4753 }
4754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004755 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004756 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004757 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4758 // Branch cases into compressed and uncompressed for each index's type.
4759 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4760 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004761 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004763 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4764 "Expecting 0=compressed, 1=uncompressed");
4765 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004766 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4767 __ jmp(&done);
4768 __ Bind(&not_compressed);
4769 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4770 __ Bind(&done);
4771 } else {
4772 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4773 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 break;
4775 }
4776
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004777 case DataType::Type::kInt16: {
4778 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4779 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4780 break;
4781 }
4782
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004784 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004785 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004786 break;
4787 }
4788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004790 static_assert(
4791 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4792 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004793 // /* HeapReference<Object> */ out =
4794 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4795 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004796 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004797 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004798 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004799 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004800 } else {
4801 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004802 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4803 codegen_->MaybeRecordImplicitNullCheck(instruction);
4804 // If read barriers are enabled, emit read barriers other than
4805 // Baker's using a slow path (and also unpoison the loaded
4806 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004807 if (index.IsConstant()) {
4808 uint32_t offset =
4809 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004810 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4811 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004812 codegen_->MaybeGenerateReadBarrierSlow(
4813 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4814 }
4815 }
4816 break;
4817 }
4818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004819 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004820 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004821 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004822 break;
4823 }
4824
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004825 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004826 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004827 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004828 break;
4829 }
4830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004831 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004832 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004833 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004834 break;
4835 }
4836
Aart Bik66c158e2018-01-31 12:55:04 -08004837 case DataType::Type::kUint32:
4838 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004839 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004840 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004841 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842 }
Roland Levillain4d027112015-07-01 15:41:14 +01004843
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004844 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004845 // Potential implicit null checks, in the case of reference
4846 // arrays, are handled in the previous switch statement.
4847 } else {
4848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004849 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850}
4851
4852void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004853 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004854
4855 bool needs_write_barrier =
4856 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004857 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004858
Vladimir Markoca6fff82017-10-03 14:49:14 +01004859 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004860 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004861 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004862 LocationSummary::kCallOnSlowPath :
4863 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004864
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004865 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004866 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004867 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04004868 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004869 } else {
4870 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4871 }
4872
4873 if (needs_write_barrier) {
4874 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004875 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878}
4879
4880void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4881 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004882 Location array_loc = locations->InAt(0);
4883 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004885 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004886 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004887 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004888 bool needs_write_barrier =
4889 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004890 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4891 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4892 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893
4894 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004895 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004896 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004897 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004898 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004899 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004900 if (value.IsRegister()) {
4901 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004903 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004905 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004906 break;
4907 }
4908
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004909 case DataType::Type::kUint16:
4910 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004912 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004913 if (value.IsRegister()) {
4914 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004915 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004916 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01004917 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004919 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004920 break;
4921 }
4922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004923 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004925 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004926
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004927 if (!value.IsRegister()) {
4928 // Just setting null.
4929 DCHECK(instruction->InputAt(2)->IsNullConstant());
4930 DCHECK(value.IsConstant()) << value;
4931 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004932 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004933 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004934 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004935 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004936 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004937
4938 DCHECK(needs_write_barrier);
4939 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004940 // We cannot use a NearLabel for `done`, as its range may be too
4941 // short when Baker read barriers are enabled.
4942 Label done;
4943 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004944 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004945 Location temp_loc = locations->GetTemp(0);
4946 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004947 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004948 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004949 codegen_->AddSlowPath(slow_path);
4950 if (instruction->GetValueCanBeNull()) {
4951 __ testl(register_value, register_value);
4952 __ j(kNotEqual, &not_null);
4953 __ movl(address, Immediate(0));
4954 codegen_->MaybeRecordImplicitNullCheck(instruction);
4955 __ jmp(&done);
4956 __ Bind(&not_null);
4957 }
4958
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004959 // Note that when Baker read barriers are enabled, the type
4960 // checks are performed without read barriers. This is fine,
4961 // even in the case where a class object is in the from-space
4962 // after the flip, as a comparison involving such a type would
4963 // not produce a false positive; it may of course produce a
4964 // false negative, in which case we would take the ArraySet
4965 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004966
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004967 // /* HeapReference<Class> */ temp = array->klass_
4968 __ movl(temp, Address(array, class_offset));
4969 codegen_->MaybeRecordImplicitNullCheck(instruction);
4970 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004971
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004972 // /* HeapReference<Class> */ temp = temp->component_type_
4973 __ movl(temp, Address(temp, component_offset));
4974 // If heap poisoning is enabled, no need to unpoison `temp`
4975 // nor the object reference in `register_value->klass`, as
4976 // we are comparing two poisoned references.
4977 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004978
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004979 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4980 __ j(kEqual, &do_put);
4981 // If heap poisoning is enabled, the `temp` reference has
4982 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004983 __ MaybeUnpoisonHeapReference(temp);
4984
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004985 // If heap poisoning is enabled, no need to unpoison the
4986 // heap reference loaded below, as it is only used for a
4987 // comparison with null.
4988 __ cmpl(Address(temp, super_offset), Immediate(0));
4989 __ j(kNotEqual, slow_path->GetEntryLabel());
4990 __ Bind(&do_put);
4991 } else {
4992 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004993 }
4994 }
4995
4996 if (kPoisonHeapReferences) {
4997 __ movl(temp, register_value);
4998 __ PoisonHeapReference(temp);
4999 __ movl(address, temp);
5000 } else {
5001 __ movl(address, register_value);
5002 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005003 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005004 codegen_->MaybeRecordImplicitNullCheck(instruction);
5005 }
5006
5007 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5008 codegen_->MarkGCCard(
5009 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
5010 __ Bind(&done);
5011
5012 if (slow_path != nullptr) {
5013 __ Bind(slow_path->GetExitLabel());
5014 }
5015
5016 break;
5017 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005019 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005020 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005021 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005022 if (value.IsRegister()) {
5023 __ movl(address, value.AsRegister<CpuRegister>());
5024 } else {
5025 DCHECK(value.IsConstant()) << value;
5026 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5027 __ movl(address, Immediate(v));
5028 }
5029 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005030 break;
5031 }
5032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005033 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005034 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005035 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005036 if (value.IsRegister()) {
5037 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005038 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005040 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005041 Address address_high =
5042 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005043 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 }
5045 break;
5046 }
5047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005048 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005049 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005050 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005051 if (value.IsFpuRegister()) {
5052 __ movss(address, value.AsFpuRegister<XmmRegister>());
5053 } else {
5054 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005055 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005056 __ movl(address, Immediate(v));
5057 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005058 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005059 break;
5060 }
5061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005063 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005064 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005065 if (value.IsFpuRegister()) {
5066 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5067 codegen_->MaybeRecordImplicitNullCheck(instruction);
5068 } else {
5069 int64_t v =
5070 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005071 Address address_high =
5072 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005073 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5074 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005075 break;
5076 }
5077
Aart Bik66c158e2018-01-31 12:55:04 -08005078 case DataType::Type::kUint32:
5079 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005080 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005082 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 }
5084}
5085
5086void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005087 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005088 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005089 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005090 if (!instruction->IsEmittedAtUseSite()) {
5091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5092 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093}
5094
5095void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005096 if (instruction->IsEmittedAtUseSite()) {
5097 return;
5098 }
5099
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005101 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005102 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5103 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005105 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005106 // Mask out most significant bit in case the array is String's array of char.
5107 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005108 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005109 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110}
5111
5112void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005113 RegisterSet caller_saves = RegisterSet::Empty();
5114 InvokeRuntimeCallingConvention calling_convention;
5115 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5116 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5117 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005118 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005119 HInstruction* length = instruction->InputAt(1);
5120 if (!length->IsEmittedAtUseSite()) {
5121 locations->SetInAt(1, Location::RegisterOrConstant(length));
5122 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005123}
5124
5125void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5126 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005127 Location index_loc = locations->InAt(0);
5128 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005129 SlowPathCode* slow_path =
5130 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131
Mark Mendell99dbd682015-04-22 16:18:52 -04005132 if (length_loc.IsConstant()) {
5133 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5134 if (index_loc.IsConstant()) {
5135 // BCE will remove the bounds check if we are guarenteed to pass.
5136 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5137 if (index < 0 || index >= length) {
5138 codegen_->AddSlowPath(slow_path);
5139 __ jmp(slow_path->GetEntryLabel());
5140 } else {
5141 // Some optimization after BCE may have generated this, and we should not
5142 // generate a bounds check if it is a valid range.
5143 }
5144 return;
5145 }
5146
5147 // We have to reverse the jump condition because the length is the constant.
5148 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5149 __ cmpl(index_reg, Immediate(length));
5150 codegen_->AddSlowPath(slow_path);
5151 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005152 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005153 HInstruction* array_length = instruction->InputAt(1);
5154 if (array_length->IsEmittedAtUseSite()) {
5155 // Address the length field in the array.
5156 DCHECK(array_length->IsArrayLength());
5157 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5158 Location array_loc = array_length->GetLocations()->InAt(0);
5159 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005160 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005161 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5162 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005163 CpuRegister length_reg = CpuRegister(TMP);
5164 __ movl(length_reg, array_len);
5165 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005166 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005167 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005168 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005169 // Checking the bound for general case:
5170 // Array of char or String's array when the compression feature off.
5171 if (index_loc.IsConstant()) {
5172 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5173 __ cmpl(array_len, Immediate(value));
5174 } else {
5175 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5176 }
5177 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005178 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005179 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005180 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005181 }
5182 codegen_->AddSlowPath(slow_path);
5183 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185}
5186
5187void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5188 CpuRegister card,
5189 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005190 CpuRegister value,
5191 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005192 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005193 if (value_can_be_null) {
5194 __ testl(value, value);
5195 __ j(kEqual, &is_null);
5196 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005197 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005198 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 __ movq(temp, object);
5200 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005201 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005202 if (value_can_be_null) {
5203 __ Bind(&is_null);
5204 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205}
5206
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005207void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005208 LOG(FATAL) << "Unimplemented";
5209}
5210
5211void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005212 if (instruction->GetNext()->IsSuspendCheck() &&
5213 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5214 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5215 // The back edge will generate the suspend check.
5216 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5217 }
5218
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005219 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5220}
5221
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005222void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005223 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5224 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005225 // In suspend check slow path, usually there are no caller-save registers at all.
5226 // If SIMD instructions are present, however, we force spilling all live SIMD
5227 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005228 locations->SetCustomSlowPathCallerSaves(
5229 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005230}
5231
5232void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005233 HBasicBlock* block = instruction->GetBlock();
5234 if (block->GetLoopInformation() != nullptr) {
5235 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5236 // The back edge will generate the suspend check.
5237 return;
5238 }
5239 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5240 // The goto will generate the suspend check.
5241 return;
5242 }
5243 GenerateSuspendCheck(instruction, nullptr);
5244}
5245
5246void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5247 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005248 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005249 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5250 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005251 slow_path =
5252 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005253 instruction->SetSlowPath(slow_path);
5254 codegen_->AddSlowPath(slow_path);
5255 if (successor != nullptr) {
5256 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005257 }
5258 } else {
5259 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5260 }
5261
Andreas Gampe542451c2016-07-26 09:02:02 -07005262 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005263 /* no_rip */ true),
5264 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005265 if (successor == nullptr) {
5266 __ j(kNotEqual, slow_path->GetEntryLabel());
5267 __ Bind(slow_path->GetReturnLabel());
5268 } else {
5269 __ j(kEqual, codegen_->GetLabelOf(successor));
5270 __ jmp(slow_path->GetEntryLabel());
5271 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005272}
5273
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005274X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5275 return codegen_->GetAssembler();
5276}
5277
5278void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005279 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005280 Location source = move->GetSource();
5281 Location destination = move->GetDestination();
5282
5283 if (source.IsRegister()) {
5284 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005286 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005287 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005288 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005289 } else {
5290 DCHECK(destination.IsDoubleStackSlot());
5291 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005292 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005293 }
5294 } else if (source.IsStackSlot()) {
5295 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005296 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005297 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005301 } else {
5302 DCHECK(destination.IsStackSlot());
5303 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5304 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5305 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005306 } else if (source.IsDoubleStackSlot()) {
5307 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005308 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005309 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005311 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5312 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005313 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005314 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005315 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5316 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5317 }
Aart Bik5576f372017-03-23 16:17:37 -07005318 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005319 if (destination.IsFpuRegister()) {
5320 __ movups(destination.AsFpuRegister<XmmRegister>(),
5321 Address(CpuRegister(RSP), source.GetStackIndex()));
5322 } else {
5323 DCHECK(destination.IsSIMDStackSlot());
5324 size_t high = kX86_64WordSize;
5325 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5326 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5327 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5328 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5329 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005330 } else if (source.IsConstant()) {
5331 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005332 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5333 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005334 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005335 if (value == 0) {
5336 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5337 } else {
5338 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5339 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005340 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005341 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005342 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005343 }
5344 } else if (constant->IsLongConstant()) {
5345 int64_t value = constant->AsLongConstant()->GetValue();
5346 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005347 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005348 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005349 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005350 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005351 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005352 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005353 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005354 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005355 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005356 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005357 } else {
5358 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005359 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005360 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5361 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005362 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005363 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005364 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005365 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005366 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005367 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005368 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005369 } else {
5370 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005371 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005372 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005373 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005374 } else if (source.IsFpuRegister()) {
5375 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005376 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005377 } else if (destination.IsStackSlot()) {
5378 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005379 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005380 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005381 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005382 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005383 } else {
5384 DCHECK(destination.IsSIMDStackSlot());
5385 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5386 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005387 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005388 }
5389}
5390
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005391void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005392 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005393 __ movl(Address(CpuRegister(RSP), mem), reg);
5394 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005395}
5396
Mark Mendell8a1c7282015-06-29 15:41:28 -04005397void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5398 __ movq(CpuRegister(TMP), reg1);
5399 __ movq(reg1, reg2);
5400 __ movq(reg2, CpuRegister(TMP));
5401}
5402
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005403void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5404 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5405 __ movq(Address(CpuRegister(RSP), mem), reg);
5406 __ movq(reg, CpuRegister(TMP));
5407}
5408
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005409void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5410 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5411 __ movss(Address(CpuRegister(RSP), mem), reg);
5412 __ movd(reg, CpuRegister(TMP));
5413}
5414
5415void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5416 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5417 __ movsd(Address(CpuRegister(RSP), mem), reg);
5418 __ movd(reg, CpuRegister(TMP));
5419}
5420
Aart Bikcfe50bb2017-12-12 14:54:12 -08005421void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5422 size_t extra_slot = 2 * kX86_64WordSize;
5423 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5424 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5425 ExchangeMemory64(0, mem + extra_slot, 2);
5426 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5427 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5428}
5429
5430void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5431 ScratchRegisterScope ensure_scratch(
5432 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5433
5434 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5435 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5436 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5437 Address(CpuRegister(RSP), mem2 + stack_offset));
5438 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5439 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5440 CpuRegister(ensure_scratch.GetRegister()));
5441}
5442
5443void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5444 ScratchRegisterScope ensure_scratch(
5445 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5446
5447 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5448
5449 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5450 for (int i = 0; i < num_of_qwords; i++) {
5451 __ movq(CpuRegister(TMP),
5452 Address(CpuRegister(RSP), mem1 + stack_offset));
5453 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5454 Address(CpuRegister(RSP), mem2 + stack_offset));
5455 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5456 CpuRegister(TMP));
5457 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5458 CpuRegister(ensure_scratch.GetRegister()));
5459 stack_offset += kX86_64WordSize;
5460 }
5461}
5462
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005463void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005464 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005465 Location source = move->GetSource();
5466 Location destination = move->GetDestination();
5467
5468 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005469 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005470 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005471 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005472 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005473 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005474 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005475 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005476 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005477 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005478 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005479 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005480 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005481 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005482 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005483 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5484 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5485 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005486 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005487 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005488 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005489 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005490 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005491 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005492 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005493 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005494 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5495 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5496 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5497 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5498 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5499 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005500 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005501 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005502 }
5503}
5504
5505
5506void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5507 __ pushq(CpuRegister(reg));
5508}
5509
5510
5511void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5512 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005513}
5514
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005515void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005516 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005517 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5518 const size_t status_byte_offset =
5519 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5520 constexpr uint32_t shifted_initialized_value =
5521 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5522
5523 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005524 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005525 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005526 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005527}
5528
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005529HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5530 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005532 case HLoadClass::LoadKind::kInvalid:
5533 LOG(FATAL) << "UNREACHABLE";
5534 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005535 case HLoadClass::LoadKind::kReferrersClass:
5536 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005537 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005538 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005539 case HLoadClass::LoadKind::kBssEntry:
5540 DCHECK(!Runtime::Current()->UseJitCompilation());
5541 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005542 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005543 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005544 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005545 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005546 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 break;
5548 }
5549 return desired_class_load_kind;
5550}
5551
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005552void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005553 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005554 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005555 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005556 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005558 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005559 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005560 return;
5561 }
Vladimir Marko41559982017-01-06 14:04:23 +00005562 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005563
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005564 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5565 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566 ? LocationSummary::kCallOnSlowPath
5567 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005568 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005569 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005570 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005571 }
5572
Vladimir Marko41559982017-01-06 14:04:23 +00005573 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005574 locations->SetInAt(0, Location::RequiresRegister());
5575 }
5576 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005577 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5578 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5579 // Rely on the type resolution and/or initialization to save everything.
5580 // Custom calling convention: RAX serves as both input and output.
5581 RegisterSet caller_saves = RegisterSet::Empty();
5582 caller_saves.Add(Location::RegisterLocation(RAX));
5583 locations->SetCustomSlowPathCallerSaves(caller_saves);
5584 } else {
5585 // For non-Baker read barrier we have a temp-clobbering call.
5586 }
5587 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005588}
5589
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005590Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005591 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005592 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005593 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005594 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005595 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005596 PatchInfo<Label>* info = &jit_class_patches_.back();
5597 return &info->label;
5598}
5599
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005600// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5601// move.
5602void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005603 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005604 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005605 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005606 return;
5607 }
Vladimir Marko41559982017-01-06 14:04:23 +00005608 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005609
Vladimir Marko41559982017-01-06 14:04:23 +00005610 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005611 Location out_loc = locations->Out();
5612 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005613
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005614 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5615 ? kWithoutReadBarrier
5616 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005617 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005618 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005619 case HLoadClass::LoadKind::kReferrersClass: {
5620 DCHECK(!cls->CanCallRuntime());
5621 DCHECK(!cls->MustGenerateClinitCheck());
5622 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5623 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5624 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005625 cls,
5626 out_loc,
5627 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005628 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005629 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005630 break;
5631 }
5632 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005633 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005634 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005635 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005636 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005637 break;
5638 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005639 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005640 uint32_t address = dchecked_integral_cast<uint32_t>(
5641 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5642 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005643 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005644 break;
5645 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005646 case HLoadClass::LoadKind::kBootImageClassTable: {
5647 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5648 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005649 codegen_->RecordBootImageTypePatch(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005650 // Extract the reference from the slot data, i.e. clear the hash bits.
5651 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
5652 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
5653 if (masked_hash != 0) {
5654 __ subl(out, Immediate(masked_hash));
5655 }
5656 break;
5657 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005658 case HLoadClass::LoadKind::kBssEntry: {
5659 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5660 /* no_rip */ false);
5661 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5662 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5663 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5664 generate_null_check = true;
5665 break;
5666 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005667 case HLoadClass::LoadKind::kJitTableAddress: {
5668 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5669 /* no_rip */ true);
5670 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005671 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005672 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005673 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005674 break;
5675 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005676 default:
5677 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5678 UNREACHABLE();
5679 }
5680
5681 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5682 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005683 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005684 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5685 codegen_->AddSlowPath(slow_path);
5686 if (generate_null_check) {
5687 __ testl(out, out);
5688 __ j(kEqual, slow_path->GetEntryLabel());
5689 }
5690 if (cls->MustGenerateClinitCheck()) {
5691 GenerateClassInitializationCheck(slow_path, out);
5692 } else {
5693 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005694 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005695 }
5696}
5697
5698void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5699 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005700 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005701 locations->SetInAt(0, Location::RequiresRegister());
5702 if (check->HasUses()) {
5703 locations->SetOut(Location::SameAsFirstInput());
5704 }
5705}
5706
5707void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005708 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005709 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005710 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005712 GenerateClassInitializationCheck(slow_path,
5713 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005714}
5715
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005716HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5717 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005718 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005719 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005720 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005721 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005722 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005723 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005724 case HLoadString::LoadKind::kJitTableAddress:
5725 DCHECK(Runtime::Current()->UseJitCompilation());
5726 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005727 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005728 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005729 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005730 }
5731 return desired_string_load_kind;
5732}
5733
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005734void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005735 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005736 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005737 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005738 locations->SetOut(Location::RegisterLocation(RAX));
5739 } else {
5740 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005741 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5742 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005743 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005744 // Custom calling convention: RAX serves as both input and output.
5745 RegisterSet caller_saves = RegisterSet::Empty();
5746 caller_saves.Add(Location::RegisterLocation(RAX));
5747 locations->SetCustomSlowPathCallerSaves(caller_saves);
5748 } else {
5749 // For non-Baker read barrier we have a temp-clobbering call.
5750 }
5751 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005752 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005753}
5754
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005755Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005756 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005757 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005758 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005759 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005760 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005761 PatchInfo<Label>* info = &jit_string_patches_.back();
5762 return &info->label;
5763}
5764
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005765// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5766// move.
5767void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005768 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005769 Location out_loc = locations->Out();
5770 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005771
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772 switch (load->GetLoadKind()) {
5773 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005774 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005776 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005777 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005778 }
5779 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005780 uint32_t address = dchecked_integral_cast<uint32_t>(
5781 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5782 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005783 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005784 return;
5785 }
5786 case HLoadString::LoadKind::kBootImageInternTable: {
5787 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5788 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005789 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005790 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005792 case HLoadString::LoadKind::kBssEntry: {
5793 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5794 /* no_rip */ false);
5795 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5796 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005797 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005798 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005799 codegen_->AddSlowPath(slow_path);
5800 __ testl(out, out);
5801 __ j(kEqual, slow_path->GetEntryLabel());
5802 __ Bind(slow_path->GetExitLabel());
5803 return;
5804 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005805 case HLoadString::LoadKind::kJitTableAddress: {
5806 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5807 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005808 Label* fixup_label = codegen_->NewJitRootStringPatch(
5809 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005810 // /* GcRoot<mirror::String> */ out = *address
5811 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5812 return;
5813 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005814 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005815 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005818 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005819 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005820 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005821 codegen_->InvokeRuntime(kQuickResolveString,
5822 load,
5823 load->GetDexPc());
5824 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005825}
5826
David Brazdilcb1c0552015-08-04 16:22:25 +01005827static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005828 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005829 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005830}
5831
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005832void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5833 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005834 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005835 locations->SetOut(Location::RequiresRegister());
5836}
5837
5838void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005839 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5840}
5841
5842void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005843 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005844}
5845
5846void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5847 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005848}
5849
5850void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005851 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5852 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005853 InvokeRuntimeCallingConvention calling_convention;
5854 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5855}
5856
5857void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005858 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005859 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005860}
5861
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005862static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00005863 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005864 // We need a temporary for holding the iftable length.
5865 return true;
5866 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005867 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005868 !kUseBakerReadBarrier &&
5869 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005870 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5871 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5872}
5873
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005874static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5875 return kEmitCompilerReadBarrier &&
5876 !kUseBakerReadBarrier &&
5877 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5878 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5879 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5880}
5881
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005882void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005885 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005886 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005887 case TypeCheckKind::kExactCheck:
5888 case TypeCheckKind::kAbstractClassCheck:
5889 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00005890 case TypeCheckKind::kArrayObjectCheck: {
5891 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5892 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5893 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005894 break;
Vladimir Marko87584542017-12-12 17:47:52 +00005895 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 case TypeCheckKind::kUnresolvedCheck:
5898 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 call_kind = LocationSummary::kCallOnSlowPath;
5900 break;
5901 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902
Vladimir Markoca6fff82017-10-03 14:49:14 +01005903 LocationSummary* locations =
5904 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005905 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005906 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005907 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005909 locations->SetInAt(1, Location::Any());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005910 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5911 locations->SetOut(Location::RequiresRegister());
5912 // When read barriers are enabled, we need a temporary register for
5913 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005914 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005917}
5918
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005919void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005920 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005921 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005922 Location obj_loc = locations->InAt(0);
5923 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925 Location out_loc = locations->Out();
5926 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005927 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005928 locations->GetTemp(0) :
5929 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005930 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5932 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5933 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005934 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005936
5937 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005938 // Avoid null check if we know obj is not null.
5939 if (instruction->MustDoNullCheck()) {
5940 __ testl(obj, obj);
5941 __ j(kEqual, &zero);
5942 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005944 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005946 ReadBarrierOption read_barrier_option =
5947 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005948 // /* HeapReference<Class> */ out = obj->klass_
5949 GenerateReferenceLoadTwoRegisters(instruction,
5950 out_loc,
5951 obj_loc,
5952 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005953 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005954 if (cls.IsRegister()) {
5955 __ cmpl(out, cls.AsRegister<CpuRegister>());
5956 } else {
5957 DCHECK(cls.IsStackSlot()) << cls;
5958 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5959 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005960 if (zero.IsLinked()) {
5961 // Classes must be equal for the instanceof to succeed.
5962 __ j(kNotEqual, &zero);
5963 __ movl(out, Immediate(1));
5964 __ jmp(&done);
5965 } else {
5966 __ setcc(kEqual, out);
5967 // setcc only sets the low byte.
5968 __ andl(out, Immediate(1));
5969 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 break;
5971 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005974 ReadBarrierOption read_barrier_option =
5975 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005976 // /* HeapReference<Class> */ out = obj->klass_
5977 GenerateReferenceLoadTwoRegisters(instruction,
5978 out_loc,
5979 obj_loc,
5980 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005981 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 // If the class is abstract, we eagerly fetch the super class of the
5983 // object to avoid doing a comparison we know will fail.
5984 NearLabel loop, success;
5985 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005987 GenerateReferenceLoadOneRegister(instruction,
5988 out_loc,
5989 super_offset,
5990 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00005991 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 __ testl(out, out);
5993 // If `out` is null, we use it for the result, and jump to `done`.
5994 __ j(kEqual, &done);
5995 if (cls.IsRegister()) {
5996 __ cmpl(out, cls.AsRegister<CpuRegister>());
5997 } else {
5998 DCHECK(cls.IsStackSlot()) << cls;
5999 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6000 }
6001 __ j(kNotEqual, &loop);
6002 __ movl(out, Immediate(1));
6003 if (zero.IsLinked()) {
6004 __ jmp(&done);
6005 }
6006 break;
6007 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006010 ReadBarrierOption read_barrier_option =
6011 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006012 // /* HeapReference<Class> */ out = obj->klass_
6013 GenerateReferenceLoadTwoRegisters(instruction,
6014 out_loc,
6015 obj_loc,
6016 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006017 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 // Walk over the class hierarchy to find a match.
6019 NearLabel loop, success;
6020 __ Bind(&loop);
6021 if (cls.IsRegister()) {
6022 __ cmpl(out, cls.AsRegister<CpuRegister>());
6023 } else {
6024 DCHECK(cls.IsStackSlot()) << cls;
6025 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6026 }
6027 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006028 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006029 GenerateReferenceLoadOneRegister(instruction,
6030 out_loc,
6031 super_offset,
6032 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006033 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 __ testl(out, out);
6035 __ j(kNotEqual, &loop);
6036 // If `out` is null, we use it for the result, and jump to `done`.
6037 __ jmp(&done);
6038 __ Bind(&success);
6039 __ movl(out, Immediate(1));
6040 if (zero.IsLinked()) {
6041 __ jmp(&done);
6042 }
6043 break;
6044 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006046 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006047 ReadBarrierOption read_barrier_option =
6048 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006049 // /* HeapReference<Class> */ out = obj->klass_
6050 GenerateReferenceLoadTwoRegisters(instruction,
6051 out_loc,
6052 obj_loc,
6053 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006054 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006055 // Do an exact check.
6056 NearLabel exact_check;
6057 if (cls.IsRegister()) {
6058 __ cmpl(out, cls.AsRegister<CpuRegister>());
6059 } else {
6060 DCHECK(cls.IsStackSlot()) << cls;
6061 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6062 }
6063 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006064 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006065 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006066 GenerateReferenceLoadOneRegister(instruction,
6067 out_loc,
6068 component_offset,
6069 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006070 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006071 __ testl(out, out);
6072 // If `out` is null, we use it for the result, and jump to `done`.
6073 __ j(kEqual, &done);
6074 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6075 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006076 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006077 __ movl(out, Immediate(1));
6078 __ jmp(&done);
6079 break;
6080 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006081
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006082 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006083 // No read barrier since the slow path will retry upon failure.
6084 // /* HeapReference<Class> */ out = obj->klass_
6085 GenerateReferenceLoadTwoRegisters(instruction,
6086 out_loc,
6087 obj_loc,
6088 class_offset,
6089 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006090 if (cls.IsRegister()) {
6091 __ cmpl(out, cls.AsRegister<CpuRegister>());
6092 } else {
6093 DCHECK(cls.IsStackSlot()) << cls;
6094 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6095 }
6096 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006097 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6098 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 codegen_->AddSlowPath(slow_path);
6100 __ j(kNotEqual, slow_path->GetEntryLabel());
6101 __ movl(out, Immediate(1));
6102 if (zero.IsLinked()) {
6103 __ jmp(&done);
6104 }
6105 break;
6106 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006107
Calin Juravle98893e12015-10-02 21:05:03 +01006108 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006109 case TypeCheckKind::kInterfaceCheck: {
6110 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006111 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006112 // cases.
6113 //
6114 // We cannot directly call the InstanceofNonTrivial runtime
6115 // entry point without resorting to a type checking slow path
6116 // here (i.e. by calling InvokeRuntime directly), as it would
6117 // require to assign fixed registers for the inputs of this
6118 // HInstanceOf instruction (following the runtime calling
6119 // convention), which might be cluttered by the potential first
6120 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006121 //
6122 // TODO: Introduce a new runtime entry point taking the object
6123 // to test (instead of its class) as argument, and let it deal
6124 // with the read barrier issues. This will let us refactor this
6125 // case of the `switch` code as it was previously (with a direct
6126 // call to the runtime not using a type checking slow path).
6127 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006128 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006129 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6130 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006131 codegen_->AddSlowPath(slow_path);
6132 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 if (zero.IsLinked()) {
6134 __ jmp(&done);
6135 }
6136 break;
6137 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006138 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006139
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006140 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006141 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 __ xorl(out, out);
6143 }
6144
6145 if (done.IsLinked()) {
6146 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006147 }
6148
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006149 if (slow_path != nullptr) {
6150 __ Bind(slow_path->GetExitLabel());
6151 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006152}
6153
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006154void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006155 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006156 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006157 LocationSummary* locations =
6158 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006160 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6161 // Require a register for the interface check since there is a loop that compares the class to
6162 // a memory address.
6163 locations->SetInAt(1, Location::RequiresRegister());
6164 } else {
6165 locations->SetInAt(1, Location::Any());
6166 }
6167
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6169 locations->AddTemp(Location::RequiresRegister());
6170 // When read barriers are enabled, we need an additional temporary
6171 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006172 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006173 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006175}
6176
6177void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006178 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006179 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006180 Location obj_loc = locations->InAt(0);
6181 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006182 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 Location temp_loc = locations->GetTemp(0);
6184 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006185 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006186 locations->GetTemp(1) :
6187 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006188 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6189 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6190 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6191 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6192 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6193 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006194 const uint32_t object_array_data_offset =
6195 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196
Vladimir Marko87584542017-12-12 17:47:52 +00006197 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006199 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6200 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006203
6204 NearLabel done;
6205 // Avoid null check if we know obj is not null.
6206 if (instruction->MustDoNullCheck()) {
6207 __ testl(obj, obj);
6208 __ j(kEqual, &done);
6209 }
6210
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006212 case TypeCheckKind::kExactCheck:
6213 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006214 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006215 GenerateReferenceLoadTwoRegisters(instruction,
6216 temp_loc,
6217 obj_loc,
6218 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006219 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 if (cls.IsRegister()) {
6221 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6222 } else {
6223 DCHECK(cls.IsStackSlot()) << cls;
6224 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6225 }
6226 // Jump to slow path for throwing the exception or doing a
6227 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006228 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006229 break;
6230 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006233 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006234 GenerateReferenceLoadTwoRegisters(instruction,
6235 temp_loc,
6236 obj_loc,
6237 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006238 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 // If the class is abstract, we eagerly fetch the super class of the
6240 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006241 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006243 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006244 GenerateReferenceLoadOneRegister(instruction,
6245 temp_loc,
6246 super_offset,
6247 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006248 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006249
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006250 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6251 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006253 // Otherwise, compare the classes.
6254 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 if (cls.IsRegister()) {
6256 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6257 } else {
6258 DCHECK(cls.IsStackSlot()) << cls;
6259 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6260 }
6261 __ j(kNotEqual, &loop);
6262 break;
6263 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006266 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006267 GenerateReferenceLoadTwoRegisters(instruction,
6268 temp_loc,
6269 obj_loc,
6270 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006271 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006273 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006274 __ Bind(&loop);
6275 if (cls.IsRegister()) {
6276 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6277 } else {
6278 DCHECK(cls.IsStackSlot()) << cls;
6279 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6280 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006281 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006282
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006284 GenerateReferenceLoadOneRegister(instruction,
6285 temp_loc,
6286 super_offset,
6287 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006288 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289
6290 // If the class reference currently in `temp` is not null, jump
6291 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006293 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006294 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006296 break;
6297 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006300 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006301 GenerateReferenceLoadTwoRegisters(instruction,
6302 temp_loc,
6303 obj_loc,
6304 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006305 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006306 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006308 if (cls.IsRegister()) {
6309 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6310 } else {
6311 DCHECK(cls.IsStackSlot()) << cls;
6312 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6313 }
6314 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315
6316 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006317 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006318 GenerateReferenceLoadOneRegister(instruction,
6319 temp_loc,
6320 component_offset,
6321 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006322 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006323
6324 // If the component type is not null (i.e. the object is indeed
6325 // an array), jump to label `check_non_primitive_component_type`
6326 // to further check that this component type is not a primitive
6327 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006330 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006332 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 break;
6334 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006335
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006336 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006337 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 //
6339 // We cannot directly call the CheckCast runtime entry point
6340 // without resorting to a type checking slow path here (i.e. by
6341 // calling InvokeRuntime directly), as it would require to
6342 // assign fixed registers for the inputs of this HInstanceOf
6343 // instruction (following the runtime calling convention), which
6344 // might be cluttered by the potential first read barrier
6345 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006346 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006347 break;
6348 }
6349
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006350 case TypeCheckKind::kInterfaceCheck:
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006351 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6352 // We can not get false positives by doing this.
6353 // /* HeapReference<Class> */ temp = obj->klass_
6354 GenerateReferenceLoadTwoRegisters(instruction,
6355 temp_loc,
6356 obj_loc,
6357 class_offset,
6358 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006359
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006360 // /* HeapReference<Class> */ temp = temp->iftable_
6361 GenerateReferenceLoadTwoRegisters(instruction,
6362 temp_loc,
6363 temp_loc,
6364 iftable_offset,
6365 kWithoutReadBarrier);
6366 // Iftable is never null.
6367 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6368 // Maybe poison the `cls` for direct comparison with memory.
6369 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6370 // Loop through the iftable and check if any class matches.
6371 NearLabel start_loop;
6372 __ Bind(&start_loop);
6373 // Need to subtract first to handle the empty array case.
6374 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6375 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6376 // Go to next interface if the classes do not match.
6377 __ cmpl(cls.AsRegister<CpuRegister>(),
6378 CodeGeneratorX86_64::ArrayAddress(temp,
6379 maybe_temp2_loc,
6380 TIMES_4,
6381 object_array_data_offset));
6382 __ j(kNotEqual, &start_loop); // Return if same class.
6383 // If `cls` was poisoned above, unpoison it.
6384 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006385 break;
6386 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006388 if (done.IsLinked()) {
6389 __ Bind(&done);
6390 }
6391
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006393}
6394
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006395void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006396 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6397 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006398 InvokeRuntimeCallingConvention calling_convention;
6399 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6400}
6401
6402void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006403 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006404 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006405 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006406 if (instruction->IsEnter()) {
6407 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6408 } else {
6409 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6410 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006411}
6412
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006413void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6414void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6415void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6416
6417void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6418 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006419 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006420 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6421 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006422 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006423 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006424 locations->SetOut(Location::SameAsFirstInput());
6425}
6426
6427void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6428 HandleBitwiseOperation(instruction);
6429}
6430
6431void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6432 HandleBitwiseOperation(instruction);
6433}
6434
6435void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6436 HandleBitwiseOperation(instruction);
6437}
6438
6439void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6440 LocationSummary* locations = instruction->GetLocations();
6441 Location first = locations->InAt(0);
6442 Location second = locations->InAt(1);
6443 DCHECK(first.Equals(locations->Out()));
6444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006446 if (second.IsRegister()) {
6447 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006448 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006449 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006450 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006451 } else {
6452 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006453 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006454 }
6455 } else if (second.IsConstant()) {
6456 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6457 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006458 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006459 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006460 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006461 } else {
6462 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006463 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006464 }
6465 } else {
6466 Address address(CpuRegister(RSP), second.GetStackIndex());
6467 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006468 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006469 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006470 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006471 } else {
6472 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006473 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006474 }
6475 }
6476 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006478 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6479 bool second_is_constant = false;
6480 int64_t value = 0;
6481 if (second.IsConstant()) {
6482 second_is_constant = true;
6483 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006484 }
Mark Mendell40741f32015-04-20 22:10:34 -04006485 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006486
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006487 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006488 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006489 if (is_int32_value) {
6490 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6491 } else {
6492 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6493 }
6494 } else if (second.IsDoubleStackSlot()) {
6495 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006496 } else {
6497 __ andq(first_reg, second.AsRegister<CpuRegister>());
6498 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006499 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006500 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006501 if (is_int32_value) {
6502 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6503 } else {
6504 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6505 }
6506 } else if (second.IsDoubleStackSlot()) {
6507 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006508 } else {
6509 __ orq(first_reg, second.AsRegister<CpuRegister>());
6510 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006511 } else {
6512 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006513 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006514 if (is_int32_value) {
6515 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6516 } else {
6517 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6518 }
6519 } else if (second.IsDoubleStackSlot()) {
6520 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006521 } else {
6522 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6523 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006524 }
6525 }
6526}
6527
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006528void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6529 HInstruction* instruction,
6530 Location out,
6531 uint32_t offset,
6532 Location maybe_temp,
6533 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006534 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006535 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006536 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006537 if (kUseBakerReadBarrier) {
6538 // Load with fast path based Baker's read barrier.
6539 // /* HeapReference<Object> */ out = *(out + offset)
6540 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006541 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006542 } else {
6543 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006544 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006545 // in the following move operation, as we will need it for the
6546 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006547 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006548 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006549 // /* HeapReference<Object> */ out = *(out + offset)
6550 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006551 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006552 }
6553 } else {
6554 // Plain load with no read barrier.
6555 // /* HeapReference<Object> */ out = *(out + offset)
6556 __ movl(out_reg, Address(out_reg, offset));
6557 __ MaybeUnpoisonHeapReference(out_reg);
6558 }
6559}
6560
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006561void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6562 HInstruction* instruction,
6563 Location out,
6564 Location obj,
6565 uint32_t offset,
6566 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006567 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6568 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006569 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006570 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006571 if (kUseBakerReadBarrier) {
6572 // Load with fast path based Baker's read barrier.
6573 // /* HeapReference<Object> */ out = *(obj + offset)
6574 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006575 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006576 } else {
6577 // Load with slow path based read barrier.
6578 // /* HeapReference<Object> */ out = *(obj + offset)
6579 __ movl(out_reg, Address(obj_reg, offset));
6580 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6581 }
6582 } else {
6583 // Plain load with no read barrier.
6584 // /* HeapReference<Object> */ out = *(obj + offset)
6585 __ movl(out_reg, Address(obj_reg, offset));
6586 __ MaybeUnpoisonHeapReference(out_reg);
6587 }
6588}
6589
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006590void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6591 HInstruction* instruction,
6592 Location root,
6593 const Address& address,
6594 Label* fixup_label,
6595 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006596 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006597 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006598 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006599 if (kUseBakerReadBarrier) {
6600 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6601 // Baker's read barrier are used:
6602 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006603 // root = obj.field;
6604 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6605 // if (temp != null) {
6606 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006607 // }
6608
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006609 // /* GcRoot<mirror::Object> */ root = *address
6610 __ movl(root_reg, address);
6611 if (fixup_label != nullptr) {
6612 __ Bind(fixup_label);
6613 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006614 static_assert(
6615 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6616 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6617 "have different sizes.");
6618 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6619 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6620 "have different sizes.");
6621
Vladimir Marko953437b2016-08-24 08:30:46 +00006622 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006623 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006624 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006625 codegen_->AddSlowPath(slow_path);
6626
Roland Levillaind966ce72017-02-09 16:20:14 +00006627 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6628 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006629 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006630 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6631 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006632 __ j(kNotEqual, slow_path->GetEntryLabel());
6633 __ Bind(slow_path->GetExitLabel());
6634 } else {
6635 // GC root loaded through a slow path for read barriers other
6636 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006637 // /* GcRoot<mirror::Object>* */ root = address
6638 __ leaq(root_reg, address);
6639 if (fixup_label != nullptr) {
6640 __ Bind(fixup_label);
6641 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006642 // /* mirror::Object* */ root = root->Read()
6643 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6644 }
6645 } else {
6646 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006647 // /* GcRoot<mirror::Object> */ root = *address
6648 __ movl(root_reg, address);
6649 if (fixup_label != nullptr) {
6650 __ Bind(fixup_label);
6651 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006652 // Note that GC roots are not affected by heap poisoning, thus we
6653 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006654 }
6655}
6656
6657void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6658 Location ref,
6659 CpuRegister obj,
6660 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006661 bool needs_null_check) {
6662 DCHECK(kEmitCompilerReadBarrier);
6663 DCHECK(kUseBakerReadBarrier);
6664
6665 // /* HeapReference<Object> */ ref = *(obj + offset)
6666 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006667 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006668}
6669
6670void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6671 Location ref,
6672 CpuRegister obj,
6673 uint32_t data_offset,
6674 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006675 bool needs_null_check) {
6676 DCHECK(kEmitCompilerReadBarrier);
6677 DCHECK(kUseBakerReadBarrier);
6678
Roland Levillain3d312422016-06-23 13:53:42 +01006679 static_assert(
6680 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6681 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006682 // /* HeapReference<Object> */ ref =
6683 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006684 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006685 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006686}
6687
6688void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6689 Location ref,
6690 CpuRegister obj,
6691 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006692 bool needs_null_check,
6693 bool always_update_field,
6694 CpuRegister* temp1,
6695 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006696 DCHECK(kEmitCompilerReadBarrier);
6697 DCHECK(kUseBakerReadBarrier);
6698
6699 // In slow path based read barriers, the read barrier call is
6700 // inserted after the original load. However, in fast path based
6701 // Baker's read barriers, we need to perform the load of
6702 // mirror::Object::monitor_ *before* the original reference load.
6703 // This load-load ordering is required by the read barrier.
6704 // The fast path/slow path (for Baker's algorithm) should look like:
6705 //
6706 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6707 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6708 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006709 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006710 // if (is_gray) {
6711 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6712 // }
6713 //
6714 // Note: the original implementation in ReadBarrier::Barrier is
6715 // slightly more complex as:
6716 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006717 // the high-bits of rb_state, which are expected to be all zeroes
6718 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6719 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006720 // - it performs additional checks that we do not do here for
6721 // performance reasons.
6722
6723 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006724 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6725
Vladimir Marko953437b2016-08-24 08:30:46 +00006726 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006727 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6728 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006729 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6730 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6731 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6732
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006733 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006734 // ref = ReadBarrier::Mark(ref);
6735 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6736 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006737 if (needs_null_check) {
6738 MaybeRecordImplicitNullCheck(instruction);
6739 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006740
6741 // Load fence to prevent load-load reordering.
6742 // Note that this is a no-op, thanks to the x86-64 memory model.
6743 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6744
6745 // The actual reference load.
6746 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006747 __ movl(ref_reg, src); // Flags are unaffected.
6748
6749 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6750 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006751 SlowPathCode* slow_path;
6752 if (always_update_field) {
6753 DCHECK(temp1 != nullptr);
6754 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006755 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006756 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6757 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006758 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006759 instruction, ref, /* unpoison_ref_before_marking */ true);
6760 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006761 AddSlowPath(slow_path);
6762
6763 // We have done the "if" of the gray bit check above, now branch based on the flags.
6764 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006765
6766 // Object* ref = ref_addr->AsMirrorPtr()
6767 __ MaybeUnpoisonHeapReference(ref_reg);
6768
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006769 __ Bind(slow_path->GetExitLabel());
6770}
6771
6772void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6773 Location out,
6774 Location ref,
6775 Location obj,
6776 uint32_t offset,
6777 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006778 DCHECK(kEmitCompilerReadBarrier);
6779
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006780 // Insert a slow path based read barrier *after* the reference load.
6781 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006782 // If heap poisoning is enabled, the unpoisoning of the loaded
6783 // reference will be carried out by the runtime within the slow
6784 // path.
6785 //
6786 // Note that `ref` currently does not get unpoisoned (when heap
6787 // poisoning is enabled), which is alright as the `ref` argument is
6788 // not used by the artReadBarrierSlow entry point.
6789 //
6790 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006791 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00006792 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6793 AddSlowPath(slow_path);
6794
Roland Levillain0d5a2812015-11-13 10:07:31 +00006795 __ jmp(slow_path->GetEntryLabel());
6796 __ Bind(slow_path->GetExitLabel());
6797}
6798
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006799void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6800 Location out,
6801 Location ref,
6802 Location obj,
6803 uint32_t offset,
6804 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006805 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006806 // Baker's read barriers shall be handled by the fast path
6807 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6808 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006809 // If heap poisoning is enabled, unpoisoning will be taken care of
6810 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006811 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006812 } else if (kPoisonHeapReferences) {
6813 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6814 }
6815}
6816
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006817void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6818 Location out,
6819 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006820 DCHECK(kEmitCompilerReadBarrier);
6821
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006822 // Insert a slow path based read barrier *after* the GC root load.
6823 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006824 // Note that GC roots are not affected by heap poisoning, so we do
6825 // not need to do anything special for this here.
6826 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006827 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006828 AddSlowPath(slow_path);
6829
Roland Levillain0d5a2812015-11-13 10:07:31 +00006830 __ jmp(slow_path->GetEntryLabel());
6831 __ Bind(slow_path->GetExitLabel());
6832}
6833
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006834void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006835 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006836 LOG(FATAL) << "Unreachable";
6837}
6838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006839void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006840 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006841 LOG(FATAL) << "Unreachable";
6842}
6843
Mark Mendellfe57faa2015-09-18 09:26:15 -04006844// Simple implementation of packed switch - generate cascaded compare/jumps.
6845void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6846 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006847 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006848 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006849 locations->AddTemp(Location::RequiresRegister());
6850 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006851}
6852
6853void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6854 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006855 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006856 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006857 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6858 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6859 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006860 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6861
6862 // Should we generate smaller inline compare/jumps?
6863 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6864 // Figure out the correct compare values and jump conditions.
6865 // Handle the first compare/branch as a special case because it might
6866 // jump to the default case.
6867 DCHECK_GT(num_entries, 2u);
6868 Condition first_condition;
6869 uint32_t index;
6870 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6871 if (lower_bound != 0) {
6872 first_condition = kLess;
6873 __ cmpl(value_reg_in, Immediate(lower_bound));
6874 __ j(first_condition, codegen_->GetLabelOf(default_block));
6875 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6876
6877 index = 1;
6878 } else {
6879 // Handle all the compare/jumps below.
6880 first_condition = kBelow;
6881 index = 0;
6882 }
6883
6884 // Handle the rest of the compare/jumps.
6885 for (; index + 1 < num_entries; index += 2) {
6886 int32_t compare_to_value = lower_bound + index + 1;
6887 __ cmpl(value_reg_in, Immediate(compare_to_value));
6888 // Jump to successors[index] if value < case_value[index].
6889 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6890 // Jump to successors[index + 1] if value == case_value[index + 1].
6891 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6892 }
6893
6894 if (index != num_entries) {
6895 // There are an odd number of entries. Handle the last one.
6896 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006897 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006898 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6899 }
6900
6901 // And the default for any other value.
6902 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6903 __ jmp(codegen_->GetLabelOf(default_block));
6904 }
6905 return;
6906 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006907
6908 // Remove the bias, if needed.
6909 Register value_reg_out = value_reg_in.AsRegister();
6910 if (lower_bound != 0) {
6911 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6912 value_reg_out = temp_reg.AsRegister();
6913 }
6914 CpuRegister value_reg(value_reg_out);
6915
6916 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006917 __ cmpl(value_reg, Immediate(num_entries - 1));
6918 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006919
Mark Mendell9c86b482015-09-18 13:36:07 -04006920 // We are in the range of the table.
6921 // Load the address of the jump table in the constant area.
6922 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006923
Mark Mendell9c86b482015-09-18 13:36:07 -04006924 // Load the (signed) offset from the jump table.
6925 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6926
6927 // Add the offset to the address of the table base.
6928 __ addq(temp_reg, base_reg);
6929
6930 // And jump.
6931 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006932}
6933
xueliang.zhonge0eb4832017-10-30 13:43:14 +00006934void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
6935 ATTRIBUTE_UNUSED) {
6936 LOG(FATAL) << "Unreachable";
6937}
6938
6939void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
6940 ATTRIBUTE_UNUSED) {
6941 LOG(FATAL) << "Unreachable";
6942}
6943
Aart Bikc5d47542016-01-27 17:00:35 -08006944void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6945 if (value == 0) {
6946 __ xorl(dest, dest);
6947 } else {
6948 __ movl(dest, Immediate(value));
6949 }
6950}
6951
Mark Mendell92e83bf2015-05-07 11:25:03 -04006952void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6953 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006954 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006955 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006956 } else if (IsUint<32>(value)) {
6957 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006958 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6959 } else {
6960 __ movq(dest, Immediate(value));
6961 }
6962}
6963
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006964void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6965 if (value == 0) {
6966 __ xorps(dest, dest);
6967 } else {
6968 __ movss(dest, LiteralInt32Address(value));
6969 }
6970}
6971
6972void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6973 if (value == 0) {
6974 __ xorpd(dest, dest);
6975 } else {
6976 __ movsd(dest, LiteralInt64Address(value));
6977 }
6978}
6979
6980void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6981 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6982}
6983
6984void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6985 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6986}
6987
Aart Bika19616e2016-02-01 18:57:58 -08006988void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6989 if (value == 0) {
6990 __ testl(dest, dest);
6991 } else {
6992 __ cmpl(dest, Immediate(value));
6993 }
6994}
6995
6996void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6997 if (IsInt<32>(value)) {
6998 if (value == 0) {
6999 __ testq(dest, dest);
7000 } else {
7001 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7002 }
7003 } else {
7004 // Value won't fit in an int.
7005 __ cmpq(dest, LiteralInt64Address(value));
7006 }
7007}
7008
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007009void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7010 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007011 GenerateIntCompare(lhs_reg, rhs);
7012}
7013
7014void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007015 if (rhs.IsConstant()) {
7016 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007017 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007018 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007019 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007020 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007021 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007022 }
7023}
7024
7025void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7026 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7027 if (rhs.IsConstant()) {
7028 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7029 Compare64BitValue(lhs_reg, value);
7030 } else if (rhs.IsDoubleStackSlot()) {
7031 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7032 } else {
7033 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7034 }
7035}
7036
7037Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7038 Location index,
7039 ScaleFactor scale,
7040 uint32_t data_offset) {
7041 return index.IsConstant() ?
7042 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7043 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7044}
7045
Mark Mendellcfa410b2015-05-25 16:02:44 -04007046void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7047 DCHECK(dest.IsDoubleStackSlot());
7048 if (IsInt<32>(value)) {
7049 // Can move directly as an int32 constant.
7050 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7051 Immediate(static_cast<int32_t>(value)));
7052 } else {
7053 Load64BitValue(CpuRegister(TMP), value);
7054 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7055 }
7056}
7057
Mark Mendell9c86b482015-09-18 13:36:07 -04007058/**
7059 * Class to handle late fixup of offsets into constant area.
7060 */
7061class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7062 public:
7063 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7064 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7065
7066 protected:
7067 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7068
7069 CodeGeneratorX86_64* codegen_;
7070
7071 private:
7072 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7073 // Patch the correct offset for the instruction. We use the address of the
7074 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7075 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7076 int32_t relative_position = constant_offset - pos;
7077
7078 // Patch in the right value.
7079 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7080 }
7081
7082 // Location in constant area that the fixup refers to.
7083 size_t offset_into_constant_area_;
7084};
7085
7086/**
7087 t * Class to handle late fixup of offsets to a jump table that will be created in the
7088 * constant area.
7089 */
7090class JumpTableRIPFixup : public RIPFixup {
7091 public:
7092 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7093 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7094
7095 void CreateJumpTable() {
7096 X86_64Assembler* assembler = codegen_->GetAssembler();
7097
7098 // Ensure that the reference to the jump table has the correct offset.
7099 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7100 SetOffset(offset_in_constant_table);
7101
7102 // Compute the offset from the start of the function to this jump table.
7103 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7104
7105 // Populate the jump table with the correct values for the jump table.
7106 int32_t num_entries = switch_instr_->GetNumEntries();
7107 HBasicBlock* block = switch_instr_->GetBlock();
7108 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7109 // The value that we want is the target offset - the position of the table.
7110 for (int32_t i = 0; i < num_entries; i++) {
7111 HBasicBlock* b = successors[i];
7112 Label* l = codegen_->GetLabelOf(b);
7113 DCHECK(l->IsBound());
7114 int32_t offset_to_block = l->Position() - current_table_offset;
7115 assembler->AppendInt32(offset_to_block);
7116 }
7117 }
7118
7119 private:
7120 const HPackedSwitch* switch_instr_;
7121};
7122
Mark Mendellf55c3e02015-03-26 21:07:46 -04007123void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7124 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007125 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007126 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7127 // 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 -04007128 assembler->Align(4, 0);
7129 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007130
7131 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007132 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007133 jump_table->CreateJumpTable();
7134 }
7135
7136 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007137 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007138 }
7139
7140 // And finish up.
7141 CodeGenerator::Finalize(allocator);
7142}
7143
Mark Mendellf55c3e02015-03-26 21:07:46 -04007144Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007145 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007146 return Address::RIP(fixup);
7147}
7148
7149Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007150 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007151 return Address::RIP(fixup);
7152}
7153
7154Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007155 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007156 return Address::RIP(fixup);
7157}
7158
7159Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007160 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007161 return Address::RIP(fixup);
7162}
7163
Andreas Gampe85b62f22015-09-09 13:15:38 -07007164// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007165void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007166 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007167 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007168 return;
7169 }
7170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007171 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007172
7173 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7174 if (trg.Equals(return_loc)) {
7175 return;
7176 }
7177
7178 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007179 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007180 parallel_move.AddMove(return_loc, trg, type, nullptr);
7181 GetMoveResolver()->EmitNativeCode(&parallel_move);
7182}
7183
Mark Mendell9c86b482015-09-18 13:36:07 -04007184Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7185 // Create a fixup to be used to create and address the jump table.
7186 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007187 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007188
7189 // We have to populate the jump tables.
7190 fixups_to_jump_tables_.push_back(table_fixup);
7191 return Address::RIP(table_fixup);
7192}
7193
Mark Mendellea5af682015-10-22 17:35:49 -04007194void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7195 const Address& addr_high,
7196 int64_t v,
7197 HInstruction* instruction) {
7198 if (IsInt<32>(v)) {
7199 int32_t v_32 = v;
7200 __ movq(addr_low, Immediate(v_32));
7201 MaybeRecordImplicitNullCheck(instruction);
7202 } else {
7203 // Didn't fit in a register. Do it in pieces.
7204 int32_t low_v = Low32Bits(v);
7205 int32_t high_v = High32Bits(v);
7206 __ movl(addr_low, Immediate(low_v));
7207 MaybeRecordImplicitNullCheck(instruction);
7208 __ movl(addr_high, Immediate(high_v));
7209 }
7210}
7211
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007212void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7213 const uint8_t* roots_data,
7214 const PatchInfo<Label>& info,
7215 uint64_t index_in_table) const {
7216 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7217 uintptr_t address =
7218 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7219 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7220 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7221 dchecked_integral_cast<uint32_t>(address);
7222}
7223
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007224void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7225 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007226 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007227 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007228 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007229 }
7230
7231 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007232 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007233 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007234 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007235 }
7236}
7237
Roland Levillain4d027112015-07-01 15:41:14 +01007238#undef __
7239
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007240} // namespace x86_64
7241} // namespace art