blob: 322b0cfc4c12b7c8f5a5a0d29b0681e08bd53103 [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 Markob066d432018-01-03 13:14:37 +00001001 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
1002 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1003 __ movl(temp.AsRegister<CpuRegister>(),
1004 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001005 RecordBootImageRelRoPatch(GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00001006 break;
1007 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001008 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001009 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001010 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001011 RecordMethodBssEntryPatch(invoke);
Vladimir Marko58155012015-08-19 12:49:41 +00001012 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001013 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001014 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1015 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1016 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001017 }
Vladimir Marko58155012015-08-19 12:49:41 +00001018 }
1019
1020 switch (invoke->GetCodePtrLocation()) {
1021 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1022 __ call(&frame_entry_label_);
1023 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001024 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1025 // (callee_method + offset_of_quick_compiled_code)()
1026 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1027 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001028 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001029 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001030 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001031 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001032
1033 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001034}
1035
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001036void CodeGeneratorX86_64::GenerateVirtualCall(
1037 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001038 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1039 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1040 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001041
1042 // Use the calling convention instead of the location of the receiver, as
1043 // intrinsics may have put the receiver in a different register. In the intrinsics
1044 // slow path, the arguments have been moved to the right place, so here we are
1045 // guaranteed that the receiver is the first register of the calling convention.
1046 InvokeDexCallingConvention calling_convention;
1047 Register receiver = calling_convention.GetRegisterAt(0);
1048
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001049 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001050 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001051 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001052 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001053 // Instead of simply (possibly) unpoisoning `temp` here, we should
1054 // emit a read barrier for the previous class reference load.
1055 // However this is not required in practice, as this is an
1056 // intermediate/temporary reference and because the current
1057 // concurrent copying collector keeps the from-space memory
1058 // intact/accessible until the end of the marking phase (the
1059 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001060 __ MaybeUnpoisonHeapReference(temp);
1061 // temp = temp->GetMethodAt(method_offset);
1062 __ movq(temp, Address(temp, method_offset));
1063 // call temp->GetEntryPoint();
1064 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001065 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001066 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001067}
1068
Vladimir Markob066d432018-01-03 13:14:37 +00001069void CodeGeneratorX86_64::RecordBootImageRelRoPatch(uint32_t boot_image_offset) {
1070 boot_image_method_patches_.emplace_back(/* target_dex_file */ nullptr, boot_image_offset);
1071 __ Bind(&boot_image_method_patches_.back().label);
1072}
1073
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001074void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
1075 boot_image_method_patches_.emplace_back(
1076 invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001077 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001078}
1079
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001080void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
1081 method_bss_entry_patches_.emplace_back(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
1082 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001083}
1084
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001085void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1086 boot_image_type_patches_.emplace_back(
1087 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001088 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001089}
1090
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001091Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001092 type_bss_entry_patches_.emplace_back(
1093 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001094 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001095}
1096
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001097void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1098 boot_image_string_patches_.emplace_back(
1099 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1100 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001101}
1102
Vladimir Markoaad75c62016-10-03 08:46:48 +00001103Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1104 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001105 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001106 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001107 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001108}
1109
Vladimir Markoaad75c62016-10-03 08:46:48 +00001110// The label points to the end of the "movl" or another instruction but the literal offset
1111// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1112constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1113
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001114template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001115inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1116 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001117 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001118 for (const PatchInfo<Label>& info : infos) {
1119 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1120 linker_patches->push_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001121 Factory(literal_offset, info.target_dex_file, info.label.Position(), info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001122 }
1123}
1124
Vladimir Markob066d432018-01-03 13:14:37 +00001125linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1126 const DexFile* target_dex_file,
1127 uint32_t pc_insn_offset,
1128 uint32_t boot_image_offset) {
1129 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1130 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1131}
1132
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001133void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001134 DCHECK(linker_patches->empty());
1135 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001136 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001137 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001138 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001139 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001140 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001141 string_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001142 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001143 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001144 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1145 boot_image_method_patches_, linker_patches);
1146 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1147 boot_image_type_patches_, linker_patches);
1148 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001149 boot_image_string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001150 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001151 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1152 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001153 DCHECK(boot_image_type_patches_.empty());
1154 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001155 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001156 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1157 method_bss_entry_patches_, linker_patches);
1158 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1159 type_bss_entry_patches_, linker_patches);
1160 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1161 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001162 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001163}
1164
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001165void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001166 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001167}
1168
1169void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001170 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001171}
1172
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001173size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1174 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1175 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001176}
1177
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001178size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1179 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1180 return kX86_64WordSize;
1181}
1182
1183size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001184 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001185 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001186 } else {
1187 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1188 }
1189 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001190}
1191
1192size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001193 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001194 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001195 } else {
1196 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1197 }
1198 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001199}
1200
Calin Juravle175dc732015-08-25 15:42:32 +01001201void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1202 HInstruction* instruction,
1203 uint32_t dex_pc,
1204 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001205 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001206 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1207 if (EntrypointRequiresStackMap(entrypoint)) {
1208 RecordPcInfo(instruction, dex_pc, slow_path);
1209 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001210}
1211
Roland Levillaindec8f632016-07-22 17:10:06 +01001212void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1213 HInstruction* instruction,
1214 SlowPathCode* slow_path) {
1215 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001216 GenerateInvokeRuntime(entry_point_offset);
1217}
1218
1219void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001220 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1221}
1222
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001223static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001224// Use a fake return address register to mimic Quick.
1225static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001226CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001227 const X86_64InstructionSetFeatures& isa_features,
1228 const CompilerOptions& compiler_options,
1229 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001230 : CodeGenerator(graph,
1231 kNumberOfCpuRegisters,
1232 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001233 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001234 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1235 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001236 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001237 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1238 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001239 compiler_options,
1240 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001241 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001243 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001244 move_resolver_(graph->GetAllocator(), this),
1245 assembler_(graph->GetAllocator()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001246 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001247 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001248 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1249 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1250 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1251 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001252 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001253 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1254 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1255 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1256 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001257 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1258}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001260InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1261 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001262 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001263 assembler_(codegen->GetAssembler()),
1264 codegen_(codegen) {}
1265
David Brazdil58282f42016-01-14 12:45:10 +00001266void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001267 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001268 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001269
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001270 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001271 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001272}
1273
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001274static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001275 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001276}
David Srbecky9d8606d2015-04-12 09:35:32 +01001277
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001278static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001279 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001280}
1281
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001283 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001284 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001285 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001286 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001287 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001288
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001289 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1290 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1291 ArtMethod::HotnessCountOffset().Int32Value()),
1292 Immediate(1));
1293 }
1294
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001295 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1297 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001298 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001299 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001300
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001301 if (HasEmptyFrame()) {
1302 return;
1303 }
1304
Nicolas Geoffray98893962015-01-21 12:32:32 +00001305 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001306 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001307 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001308 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001309 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1310 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001311 }
1312 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001313
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001314 int adjust = GetFrameSize() - GetCoreSpillSize();
1315 __ subq(CpuRegister(RSP), Immediate(adjust));
1316 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001317 uint32_t xmm_spill_location = GetFpuSpillStart();
1318 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001319
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001320 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1321 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001322 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1323 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1324 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001325 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001326 }
1327
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001328 // Save the current method if we need it. Note that we do not
1329 // do this in HCurrentMethod, as the instruction might have been removed
1330 // in the SSA graph.
1331 if (RequiresCurrentMethod()) {
1332 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1333 CpuRegister(kMethodRegisterArgument));
1334 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001335
1336 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1337 // Initialize should_deoptimize flag to 0.
1338 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1339 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001340}
1341
1342void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001343 __ cfi().RememberState();
1344 if (!HasEmptyFrame()) {
1345 uint32_t xmm_spill_location = GetFpuSpillStart();
1346 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1347 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1348 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1349 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1350 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1351 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1352 }
1353 }
1354
1355 int adjust = GetFrameSize() - GetCoreSpillSize();
1356 __ addq(CpuRegister(RSP), Immediate(adjust));
1357 __ cfi().AdjustCFAOffset(-adjust);
1358
1359 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1360 Register reg = kCoreCalleeSaves[i];
1361 if (allocated_registers_.ContainsCoreRegister(reg)) {
1362 __ popq(CpuRegister(reg));
1363 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1364 __ cfi().Restore(DWARFReg(reg));
1365 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001366 }
1367 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001368 __ ret();
1369 __ cfi().RestoreState();
1370 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001371}
1372
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001373void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1374 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001375}
1376
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001377void CodeGeneratorX86_64::Move(Location destination, Location source) {
1378 if (source.Equals(destination)) {
1379 return;
1380 }
1381 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001382 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001383 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001384 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001385 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001386 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001387 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001388 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1389 } else if (source.IsConstant()) {
1390 HConstant* constant = source.GetConstant();
1391 if (constant->IsLongConstant()) {
1392 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1393 } else {
1394 Load32BitValue(dest, GetInt32ValueOf(constant));
1395 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001396 } else {
1397 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001398 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001399 }
1400 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001401 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001402 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001403 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001404 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001405 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1406 } else if (source.IsConstant()) {
1407 HConstant* constant = source.GetConstant();
1408 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1409 if (constant->IsFloatConstant()) {
1410 Load32BitValue(dest, static_cast<int32_t>(value));
1411 } else {
1412 Load64BitValue(dest, value);
1413 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001414 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001415 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001416 } else {
1417 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001418 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001419 }
1420 } else if (destination.IsStackSlot()) {
1421 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001422 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001423 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 } else if (source.IsFpuRegister()) {
1425 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001426 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001427 } else if (source.IsConstant()) {
1428 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001429 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001430 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001431 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001432 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001433 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1434 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001435 }
1436 } else {
1437 DCHECK(destination.IsDoubleStackSlot());
1438 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001439 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001440 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001441 } else if (source.IsFpuRegister()) {
1442 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001443 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001444 } else if (source.IsConstant()) {
1445 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001446 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1447 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001448 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001449 } else {
1450 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001451 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1452 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001453 }
1454 }
1455}
1456
Calin Juravle175dc732015-08-25 15:42:32 +01001457void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1458 DCHECK(location.IsRegister());
1459 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1460}
1461
Calin Juravlee460d1d2015-09-29 04:52:17 +01001462void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001464 Move(dst, src);
1465}
1466
1467void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1468 if (location.IsRegister()) {
1469 locations->AddTemp(location);
1470 } else {
1471 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1472 }
1473}
1474
David Brazdilfc6a86a2015-06-26 10:33:45 +00001475void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001476 if (successor->IsExitBlock()) {
1477 DCHECK(got->GetPrevious()->AlwaysThrows());
1478 return; // no code needed
1479 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001480
1481 HBasicBlock* block = got->GetBlock();
1482 HInstruction* previous = got->GetPrevious();
1483
1484 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001485 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001486 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1487 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1488 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1489 Immediate(1));
1490 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001491 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1492 return;
1493 }
1494
1495 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1496 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1497 }
1498 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001499 __ jmp(codegen_->GetLabelOf(successor));
1500 }
1501}
1502
David Brazdilfc6a86a2015-06-26 10:33:45 +00001503void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1504 got->SetLocations(nullptr);
1505}
1506
1507void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1508 HandleGoto(got, got->GetSuccessor());
1509}
1510
1511void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1512 try_boundary->SetLocations(nullptr);
1513}
1514
1515void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1516 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1517 if (!successor->IsExitBlock()) {
1518 HandleGoto(try_boundary, successor);
1519 }
1520}
1521
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001522void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1523 exit->SetLocations(nullptr);
1524}
1525
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001526void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001527}
1528
Mark Mendell152408f2015-12-31 12:28:50 -05001529template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001530void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001531 LabelType* true_label,
1532 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001533 if (cond->IsFPConditionTrueIfNaN()) {
1534 __ j(kUnordered, true_label);
1535 } else if (cond->IsFPConditionFalseIfNaN()) {
1536 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001537 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001538 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001539}
1540
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001541void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001542 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001543
Mark Mendellc4701932015-04-10 13:18:51 -04001544 Location left = locations->InAt(0);
1545 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001546 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001547 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001548 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001549 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001550 case DataType::Type::kInt8:
1551 case DataType::Type::kUint16:
1552 case DataType::Type::kInt16:
1553 case DataType::Type::kInt32:
1554 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001555 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001556 break;
1557 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001559 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001560 break;
1561 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001562 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001563 if (right.IsFpuRegister()) {
1564 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1565 } else if (right.IsConstant()) {
1566 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1567 codegen_->LiteralFloatAddress(
1568 right.GetConstant()->AsFloatConstant()->GetValue()));
1569 } else {
1570 DCHECK(right.IsStackSlot());
1571 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1572 Address(CpuRegister(RSP), right.GetStackIndex()));
1573 }
Mark Mendellc4701932015-04-10 13:18:51 -04001574 break;
1575 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001576 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001577 if (right.IsFpuRegister()) {
1578 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1579 } else if (right.IsConstant()) {
1580 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1581 codegen_->LiteralDoubleAddress(
1582 right.GetConstant()->AsDoubleConstant()->GetValue()));
1583 } else {
1584 DCHECK(right.IsDoubleStackSlot());
1585 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1586 Address(CpuRegister(RSP), right.GetStackIndex()));
1587 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001588 break;
1589 }
1590 default:
1591 LOG(FATAL) << "Unexpected condition type " << type;
1592 }
1593}
1594
1595template<class LabelType>
1596void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1597 LabelType* true_target_in,
1598 LabelType* false_target_in) {
1599 // Generated branching requires both targets to be explicit. If either of the
1600 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1601 LabelType fallthrough_target;
1602 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1603 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1604
1605 // Generate the comparison to set the CC.
1606 GenerateCompareTest(condition);
1607
1608 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001610 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001612 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1613 break;
1614 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001615 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001616 GenerateFPJumps(condition, true_target, false_target);
1617 break;
1618 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001619 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001620 GenerateFPJumps(condition, true_target, false_target);
1621 break;
1622 }
1623 default:
1624 LOG(FATAL) << "Unexpected condition type " << type;
1625 }
1626
David Brazdil0debae72015-11-12 18:37:00 +00001627 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001628 __ jmp(false_target);
1629 }
David Brazdil0debae72015-11-12 18:37:00 +00001630
1631 if (fallthrough_target.IsLinked()) {
1632 __ Bind(&fallthrough_target);
1633 }
Mark Mendellc4701932015-04-10 13:18:51 -04001634}
1635
David Brazdil0debae72015-11-12 18:37:00 +00001636static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1637 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1638 // are set only strictly before `branch`. We can't use the eflags on long
1639 // conditions if they are materialized due to the complex branching.
1640 return cond->IsCondition() &&
1641 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001642 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001643}
1644
Mark Mendell152408f2015-12-31 12:28:50 -05001645template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001646void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001647 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001648 LabelType* true_target,
1649 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001650 HInstruction* cond = instruction->InputAt(condition_input_index);
1651
1652 if (true_target == nullptr && false_target == nullptr) {
1653 // Nothing to do. The code always falls through.
1654 return;
1655 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001656 // Constant condition, statically compared against "true" (integer value 1).
1657 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001658 if (true_target != nullptr) {
1659 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001660 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001661 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001662 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001663 if (false_target != nullptr) {
1664 __ jmp(false_target);
1665 }
1666 }
1667 return;
1668 }
1669
1670 // The following code generates these patterns:
1671 // (1) true_target == nullptr && false_target != nullptr
1672 // - opposite condition true => branch to false_target
1673 // (2) true_target != nullptr && false_target == nullptr
1674 // - condition true => branch to true_target
1675 // (3) true_target != nullptr && false_target != nullptr
1676 // - condition true => branch to true_target
1677 // - branch to false_target
1678 if (IsBooleanValueOrMaterializedCondition(cond)) {
1679 if (AreEflagsSetFrom(cond, instruction)) {
1680 if (true_target == nullptr) {
1681 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1682 } else {
1683 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1684 }
1685 } else {
1686 // Materialized condition, compare against 0.
1687 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1688 if (lhs.IsRegister()) {
1689 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1690 } else {
1691 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1692 }
1693 if (true_target == nullptr) {
1694 __ j(kEqual, false_target);
1695 } else {
1696 __ j(kNotEqual, true_target);
1697 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001698 }
1699 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001700 // Condition has not been materialized, use its inputs as the
1701 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001702 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001703
David Brazdil0debae72015-11-12 18:37:00 +00001704 // If this is a long or FP comparison that has been folded into
1705 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001706 DataType::Type type = condition->InputAt(0)->GetType();
1707 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001708 GenerateCompareTestAndBranch(condition, true_target, false_target);
1709 return;
1710 }
1711
1712 Location lhs = condition->GetLocations()->InAt(0);
1713 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001714 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001715 if (true_target == nullptr) {
1716 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1717 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001718 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001719 }
Dave Allison20dfc792014-06-16 20:44:29 -07001720 }
David Brazdil0debae72015-11-12 18:37:00 +00001721
1722 // If neither branch falls through (case 3), the conditional branch to `true_target`
1723 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1724 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001725 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001726 }
1727}
1728
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001729void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001730 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001731 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001732 locations->SetInAt(0, Location::Any());
1733 }
1734}
1735
1736void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001737 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1738 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1739 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1740 nullptr : codegen_->GetLabelOf(true_successor);
1741 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1742 nullptr : codegen_->GetLabelOf(false_successor);
1743 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001744}
1745
1746void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001747 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001748 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001749 InvokeRuntimeCallingConvention calling_convention;
1750 RegisterSet caller_saves = RegisterSet::Empty();
1751 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1752 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001753 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001754 locations->SetInAt(0, Location::Any());
1755 }
1756}
1757
1758void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001759 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001760 GenerateTestAndBranch<Label>(deoptimize,
1761 /* condition_input_index */ 0,
1762 slow_path->GetEntryLabel(),
1763 /* false_target */ nullptr);
1764}
1765
Mingyao Yang063fc772016-08-02 11:02:54 -07001766void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001767 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001768 LocationSummary(flag, LocationSummary::kNoCall);
1769 locations->SetOut(Location::RequiresRegister());
1770}
1771
1772void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1773 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1774 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1775}
1776
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001777static bool SelectCanUseCMOV(HSelect* select) {
1778 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001779 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001780 return false;
1781 }
1782
1783 // A FP condition doesn't generate the single CC that we need.
1784 HInstruction* condition = select->GetCondition();
1785 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001787 return false;
1788 }
1789
1790 // We can generate a CMOV for this Select.
1791 return true;
1792}
1793
David Brazdil74eb1b22015-12-14 11:44:01 +00001794void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001795 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001796 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001797 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001798 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001799 } else {
1800 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001801 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001802 if (select->InputAt(1)->IsConstant()) {
1803 locations->SetInAt(1, Location::RequiresRegister());
1804 } else {
1805 locations->SetInAt(1, Location::Any());
1806 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001807 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001808 locations->SetInAt(1, Location::Any());
1809 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001810 }
1811 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1812 locations->SetInAt(2, Location::RequiresRegister());
1813 }
1814 locations->SetOut(Location::SameAsFirstInput());
1815}
1816
1817void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1818 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001819 if (SelectCanUseCMOV(select)) {
1820 // If both the condition and the source types are integer, we can generate
1821 // a CMOV to implement Select.
1822 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001823 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001824 DCHECK(locations->InAt(0).Equals(locations->Out()));
1825
1826 HInstruction* select_condition = select->GetCondition();
1827 Condition cond = kNotEqual;
1828
1829 // Figure out how to test the 'condition'.
1830 if (select_condition->IsCondition()) {
1831 HCondition* condition = select_condition->AsCondition();
1832 if (!condition->IsEmittedAtUseSite()) {
1833 // This was a previously materialized condition.
1834 // Can we use the existing condition code?
1835 if (AreEflagsSetFrom(condition, select)) {
1836 // Materialization was the previous instruction. Condition codes are right.
1837 cond = X86_64IntegerCondition(condition->GetCondition());
1838 } else {
1839 // No, we have to recreate the condition code.
1840 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1841 __ testl(cond_reg, cond_reg);
1842 }
1843 } else {
1844 GenerateCompareTest(condition);
1845 cond = X86_64IntegerCondition(condition->GetCondition());
1846 }
1847 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001848 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001849 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1850 __ testl(cond_reg, cond_reg);
1851 }
1852
1853 // If the condition is true, overwrite the output, which already contains false.
1854 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001855 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001856 if (value_true_loc.IsRegister()) {
1857 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1858 } else {
1859 __ cmov(cond,
1860 value_false,
1861 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1862 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001863 } else {
1864 NearLabel false_target;
1865 GenerateTestAndBranch<NearLabel>(select,
1866 /* condition_input_index */ 2,
1867 /* true_target */ nullptr,
1868 &false_target);
1869 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1870 __ Bind(&false_target);
1871 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001872}
1873
David Srbecky0cf44932015-12-09 14:09:59 +00001874void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001875 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001876}
1877
David Srbeckyd28f4a02016-03-14 17:14:24 +00001878void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1879 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001880}
1881
1882void CodeGeneratorX86_64::GenerateNop() {
1883 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001884}
1885
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001886void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001887 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001888 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001889 // Handle the long/FP comparisons made in instruction simplification.
1890 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001891 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001892 locations->SetInAt(0, Location::RequiresRegister());
1893 locations->SetInAt(1, Location::Any());
1894 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001895 case DataType::Type::kFloat32:
1896 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001897 locations->SetInAt(0, Location::RequiresFpuRegister());
1898 locations->SetInAt(1, Location::Any());
1899 break;
1900 default:
1901 locations->SetInAt(0, Location::RequiresRegister());
1902 locations->SetInAt(1, Location::Any());
1903 break;
1904 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001905 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001906 locations->SetOut(Location::RequiresRegister());
1907 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001908}
1909
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001910void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001911 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001913 }
Mark Mendellc4701932015-04-10 13:18:51 -04001914
1915 LocationSummary* locations = cond->GetLocations();
1916 Location lhs = locations->InAt(0);
1917 Location rhs = locations->InAt(1);
1918 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001919 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001920
1921 switch (cond->InputAt(0)->GetType()) {
1922 default:
1923 // Integer case.
1924
1925 // Clear output register: setcc only sets the low byte.
1926 __ xorl(reg, reg);
1927
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001928 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001929 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001930 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001932 // Clear output register: setcc only sets the low byte.
1933 __ xorl(reg, reg);
1934
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001935 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001936 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001937 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001939 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1940 if (rhs.IsConstant()) {
1941 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1942 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1943 } else if (rhs.IsStackSlot()) {
1944 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1945 } else {
1946 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1947 }
1948 GenerateFPJumps(cond, &true_label, &false_label);
1949 break;
1950 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001952 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1953 if (rhs.IsConstant()) {
1954 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1955 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1956 } else if (rhs.IsDoubleStackSlot()) {
1957 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1958 } else {
1959 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1960 }
1961 GenerateFPJumps(cond, &true_label, &false_label);
1962 break;
1963 }
1964 }
1965
1966 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001967 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001968
Roland Levillain4fa13f62015-07-06 18:11:54 +01001969 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001970 __ Bind(&false_label);
1971 __ xorl(reg, reg);
1972 __ jmp(&done_label);
1973
Roland Levillain4fa13f62015-07-06 18:11:54 +01001974 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001975 __ Bind(&true_label);
1976 __ movl(reg, Immediate(1));
1977 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001978}
1979
1980void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001982}
1983
1984void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001986}
1987
1988void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001990}
1991
1992void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001994}
1995
1996void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001998}
1999
2000void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002001 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002002}
2003
2004void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002006}
2007
2008void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002010}
2011
2012void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002014}
2015
2016void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002018}
2019
2020void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002021 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002022}
2023
2024void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002025 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002026}
2027
Aart Bike9f37602015-10-09 11:15:55 -07002028void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002029 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002030}
2031
2032void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002033 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002034}
2035
2036void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002037 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002038}
2039
2040void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002041 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002042}
2043
2044void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002045 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002046}
2047
2048void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002049 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002050}
2051
2052void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002053 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002054}
2055
2056void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002058}
2059
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002060void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002061 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002062 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002063 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002065 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002068 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 case DataType::Type::kInt32:
2070 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002071 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002072 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2074 break;
2075 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 case DataType::Type::kFloat32:
2077 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002078 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002079 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 locations->SetOut(Location::RequiresRegister());
2081 break;
2082 }
2083 default:
2084 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2085 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002086}
2087
2088void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002089 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002091 Location left = locations->InAt(0);
2092 Location right = locations->InAt(1);
2093
Mark Mendell0c9497d2015-08-21 09:30:05 -04002094 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002095 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002096 Condition less_cond = kLess;
2097
Calin Juravleddb7df22014-11-25 20:56:51 +00002098 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002100 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002101 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002103 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002105 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002106 break;
2107 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002109 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002110 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002111 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002112 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002113 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2114 if (right.IsConstant()) {
2115 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2116 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2117 } else if (right.IsStackSlot()) {
2118 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2119 } else {
2120 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2121 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002122 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002123 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002124 break;
2125 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002127 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2128 if (right.IsConstant()) {
2129 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2130 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2131 } else if (right.IsDoubleStackSlot()) {
2132 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2133 } else {
2134 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2135 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002136 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002137 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002138 break;
2139 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002140 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002141 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002142 }
Aart Bika19616e2016-02-01 18:57:58 -08002143
Calin Juravleddb7df22014-11-25 20:56:51 +00002144 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002145 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002146 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002147
Calin Juravle91debbc2014-11-26 19:01:09 +00002148 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002149 __ movl(out, Immediate(1));
2150 __ jmp(&done);
2151
2152 __ Bind(&less);
2153 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002154
2155 __ Bind(&done);
2156}
2157
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002158void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002159 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002160 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002161 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002162}
2163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002164void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002165 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002166}
2167
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002168void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002170 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002171 locations->SetOut(Location::ConstantLocation(constant));
2172}
2173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002174void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002175 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002176}
2177
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002178void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002179 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002181 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002182}
2183
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002184void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002185 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002186}
2187
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002190 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002191 locations->SetOut(Location::ConstantLocation(constant));
2192}
2193
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002194void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002195 // Will be generated at use site.
2196}
2197
2198void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2199 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002200 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002201 locations->SetOut(Location::ConstantLocation(constant));
2202}
2203
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002204void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2205 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002206 // Will be generated at use site.
2207}
2208
Igor Murashkind01745e2017-04-05 16:40:31 -07002209void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2210 constructor_fence->SetLocations(nullptr);
2211}
2212
2213void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2214 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2215 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2216}
2217
Calin Juravle27df7582015-04-17 19:12:31 +01002218void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2219 memory_barrier->SetLocations(nullptr);
2220}
2221
2222void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002223 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002224}
2225
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002226void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2227 ret->SetLocations(nullptr);
2228}
2229
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002230void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002231 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002232}
2233
2234void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002235 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002236 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002238 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002240 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002241 case DataType::Type::kInt8:
2242 case DataType::Type::kUint16:
2243 case DataType::Type::kInt16:
2244 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002246 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002247 break;
2248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 case DataType::Type::kFloat32:
2250 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002251 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002252 break;
2253
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002254 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002255 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002256 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002257}
2258
2259void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2260 if (kIsDebugBuild) {
2261 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002262 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002264 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002265 case DataType::Type::kInt8:
2266 case DataType::Type::kUint16:
2267 case DataType::Type::kInt16:
2268 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002270 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002271 break;
2272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kFloat32:
2274 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002276 XMM0);
2277 break;
2278
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002279 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002280 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002281 }
2282 }
2283 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002284}
2285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002287 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002288 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002290 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 case DataType::Type::kInt8:
2292 case DataType::Type::kUint16:
2293 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002294 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002296 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002298 return Location::RegisterLocation(RAX);
2299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002301 return Location::NoLocation();
2302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 case DataType::Type::kFloat64:
2304 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002305 return Location::FpuRegisterLocation(XMM0);
2306 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002307
2308 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002309}
2310
2311Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2312 return Location::RegisterLocation(kMethodRegisterArgument);
2313}
2314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002316 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002317 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002319 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 case DataType::Type::kInt8:
2321 case DataType::Type::kUint16:
2322 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002323 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002324 uint32_t index = gp_index_++;
2325 stack_index_++;
2326 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002328 } else {
2329 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2330 }
2331 }
2332
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002334 uint32_t index = gp_index_;
2335 stack_index_ += 2;
2336 if (index < calling_convention.GetNumberOfRegisters()) {
2337 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002338 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002339 } else {
2340 gp_index_ += 2;
2341 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2342 }
2343 }
2344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002346 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002347 stack_index_++;
2348 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002349 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002350 } else {
2351 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2352 }
2353 }
2354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002356 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002357 stack_index_ += 2;
2358 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002359 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002360 } else {
2361 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2362 }
2363 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002364
Aart Bik66c158e2018-01-31 12:55:04 -08002365 case DataType::Type::kUint32:
2366 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002368 LOG(FATAL) << "Unexpected parameter type " << type;
2369 break;
2370 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002371 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002372}
2373
Calin Juravle175dc732015-08-25 15:42:32 +01002374void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2375 // The trampoline uses the same calling convention as dex calling conventions,
2376 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2377 // the method_idx.
2378 HandleInvoke(invoke);
2379}
2380
2381void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2382 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2383}
2384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002385void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002386 // Explicit clinit checks triggered by static invokes must have been pruned by
2387 // art::PrepareForRegisterAllocation.
2388 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002389
Mark Mendellfb8d2792015-03-31 22:16:59 -04002390 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002391 if (intrinsic.TryDispatch(invoke)) {
2392 return;
2393 }
2394
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002395 HandleInvoke(invoke);
2396}
2397
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002398static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2399 if (invoke->GetLocations()->Intrinsified()) {
2400 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2401 intrinsic.Dispatch(invoke);
2402 return true;
2403 }
2404 return false;
2405}
2406
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002407void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002408 // Explicit clinit checks triggered by static invokes must have been pruned by
2409 // art::PrepareForRegisterAllocation.
2410 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002411
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002412 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2413 return;
2414 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002415
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002416 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002417 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002418 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002419}
2420
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002421void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002422 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002423 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002424}
2425
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002426void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002427 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002428 if (intrinsic.TryDispatch(invoke)) {
2429 return;
2430 }
2431
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002432 HandleInvoke(invoke);
2433}
2434
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002435void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002436 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2437 return;
2438 }
2439
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002440 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002441 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002442}
2443
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2445 HandleInvoke(invoke);
2446 // Add the hidden argument.
2447 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2448}
2449
2450void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2451 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002452 LocationSummary* locations = invoke->GetLocations();
2453 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2454 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002455 Location receiver = locations->InAt(0);
2456 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2457
Roland Levillain0d5a2812015-11-13 10:07:31 +00002458 // Set the hidden argument. This is safe to do this here, as RAX
2459 // won't be modified thereafter, before the `call` instruction.
2460 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002461 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002462
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002463 if (receiver.IsStackSlot()) {
2464 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002465 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002466 __ movl(temp, Address(temp, class_offset));
2467 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002468 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002469 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002470 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002471 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002472 // Instead of simply (possibly) unpoisoning `temp` here, we should
2473 // emit a read barrier for the previous class reference load.
2474 // However this is not required in practice, as this is an
2475 // intermediate/temporary reference and because the current
2476 // concurrent copying collector keeps the from-space memory
2477 // intact/accessible until the end of the marking phase (the
2478 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002479 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002480 // temp = temp->GetAddressOfIMT()
2481 __ movq(temp,
2482 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2483 // temp = temp->GetImtEntryAt(method_offset);
2484 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002485 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002486 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002487 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002488 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002489 __ call(Address(
2490 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002491
2492 DCHECK(!codegen_->IsLeafMethod());
2493 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2494}
2495
Orion Hodsonac141392017-01-13 11:53:47 +00002496void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2497 HandleInvoke(invoke);
2498}
2499
2500void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2501 codegen_->GenerateInvokePolymorphicCall(invoke);
2502}
2503
Roland Levillain88cb1752014-10-20 16:36:47 +01002504void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2505 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002506 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002507 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 case DataType::Type::kInt32:
2509 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002510 locations->SetInAt(0, Location::RequiresRegister());
2511 locations->SetOut(Location::SameAsFirstInput());
2512 break;
2513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002514 case DataType::Type::kFloat32:
2515 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002516 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002517 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002518 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2523 }
2524}
2525
2526void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2527 LocationSummary* locations = neg->GetLocations();
2528 Location out = locations->Out();
2529 Location in = locations->InAt(0);
2530 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002532 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002533 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002534 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002535 break;
2536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002538 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002539 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002540 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002541 break;
2542
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002543 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002544 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002545 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002546 // Implement float negation with an exclusive or with value
2547 // 0x80000000 (mask for bit 31, representing the sign of a
2548 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002549 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002550 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002551 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002552 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002555 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002556 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002557 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002558 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002559 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002560 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002561 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002562 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002563 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002564
2565 default:
2566 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2567 }
2568}
2569
Roland Levillaindff1f282014-11-05 14:15:05 +00002570void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2571 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002572 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002573 DataType::Type result_type = conversion->GetResultType();
2574 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002575 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2576 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002577
Roland Levillaindff1f282014-11-05 14:15:05 +00002578 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002581 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002583 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2584 locations->SetInAt(0, Location::Any());
2585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002586 break;
2587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002589 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002591 locations->SetInAt(0, Location::Any());
2592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2593 break;
2594
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002596 locations->SetInAt(0, Location::RequiresFpuRegister());
2597 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002598 break;
2599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002601 locations->SetInAt(0, Location::RequiresFpuRegister());
2602 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002603 break;
2604
2605 default:
2606 LOG(FATAL) << "Unexpected type conversion from " << input_type
2607 << " to " << result_type;
2608 }
2609 break;
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002612 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002614 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002616 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 case DataType::Type::kInt16:
2618 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002619 // TODO: We would benefit from a (to-be-implemented)
2620 // Location::RegisterOrStackSlot requirement for this input.
2621 locations->SetInAt(0, Location::RequiresRegister());
2622 locations->SetOut(Location::RequiresRegister());
2623 break;
2624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002625 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002626 locations->SetInAt(0, Location::RequiresFpuRegister());
2627 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002628 break;
2629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631 locations->SetInAt(0, Location::RequiresFpuRegister());
2632 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002633 break;
2634
2635 default:
2636 LOG(FATAL) << "Unexpected type conversion from " << input_type
2637 << " to " << result_type;
2638 }
2639 break;
2640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002641 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002642 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002643 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002644 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002646 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 case DataType::Type::kInt16:
2648 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002649 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002650 locations->SetOut(Location::RequiresFpuRegister());
2651 break;
2652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002653 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002654 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002655 locations->SetOut(Location::RequiresFpuRegister());
2656 break;
2657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002659 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002660 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002661 break;
2662
2663 default:
2664 LOG(FATAL) << "Unexpected type conversion from " << input_type
2665 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002666 }
Roland Levillaincff13742014-11-17 14:32:17 +00002667 break;
2668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002670 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002672 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002674 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002675 case DataType::Type::kInt16:
2676 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002677 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002678 locations->SetOut(Location::RequiresFpuRegister());
2679 break;
2680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002682 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002683 locations->SetOut(Location::RequiresFpuRegister());
2684 break;
2685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002687 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002688 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002689 break;
2690
2691 default:
2692 LOG(FATAL) << "Unexpected type conversion from " << input_type
2693 << " to " << result_type;
2694 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002695 break;
2696
2697 default:
2698 LOG(FATAL) << "Unexpected type conversion from " << input_type
2699 << " to " << result_type;
2700 }
2701}
2702
2703void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2704 LocationSummary* locations = conversion->GetLocations();
2705 Location out = locations->Out();
2706 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 DataType::Type result_type = conversion->GetResultType();
2708 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002709 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2710 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002711 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002712 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002713 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002714 case DataType::Type::kInt8:
2715 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 case DataType::Type::kInt16:
2717 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002718 case DataType::Type::kInt64:
2719 if (in.IsRegister()) {
2720 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2721 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2722 __ movzxb(out.AsRegister<CpuRegister>(),
2723 Address(CpuRegister(RSP), in.GetStackIndex()));
2724 } else {
2725 __ movl(out.AsRegister<CpuRegister>(),
2726 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2727 }
2728 break;
2729
2730 default:
2731 LOG(FATAL) << "Unexpected type conversion from " << input_type
2732 << " to " << result_type;
2733 }
2734 break;
2735
2736 case DataType::Type::kInt8:
2737 switch (input_type) {
2738 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002740 case DataType::Type::kInt16:
2741 case DataType::Type::kInt32:
2742 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002743 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002744 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002745 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002746 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002747 Address(CpuRegister(RSP), in.GetStackIndex()));
2748 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002750 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002751 }
2752 break;
2753
2754 default:
2755 LOG(FATAL) << "Unexpected type conversion from " << input_type
2756 << " to " << result_type;
2757 }
2758 break;
2759
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002760 case DataType::Type::kUint16:
2761 switch (input_type) {
2762 case DataType::Type::kInt8:
2763 case DataType::Type::kInt16:
2764 case DataType::Type::kInt32:
2765 case DataType::Type::kInt64:
2766 if (in.IsRegister()) {
2767 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2768 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2769 __ movzxw(out.AsRegister<CpuRegister>(),
2770 Address(CpuRegister(RSP), in.GetStackIndex()));
2771 } else {
2772 __ movl(out.AsRegister<CpuRegister>(),
2773 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2774 }
2775 break;
2776
2777 default:
2778 LOG(FATAL) << "Unexpected type conversion from " << input_type
2779 << " to " << result_type;
2780 }
2781 break;
2782
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002784 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002786 case DataType::Type::kInt32:
2787 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002788 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002790 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002791 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002792 Address(CpuRegister(RSP), in.GetStackIndex()));
2793 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002794 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002795 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002796 }
2797 break;
2798
2799 default:
2800 LOG(FATAL) << "Unexpected type conversion from " << input_type
2801 << " to " << result_type;
2802 }
2803 break;
2804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002806 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002808 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002810 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002812 Address(CpuRegister(RSP), in.GetStackIndex()));
2813 } else {
2814 DCHECK(in.IsConstant());
2815 DCHECK(in.GetConstant()->IsLongConstant());
2816 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002817 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002818 }
2819 break;
2820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002822 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2823 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002824 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002825
2826 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002827 // if input >= (float)INT_MAX goto done
2828 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002829 __ j(kAboveEqual, &done);
2830 // if input == NaN goto nan
2831 __ j(kUnordered, &nan);
2832 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002833 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002834 __ jmp(&done);
2835 __ Bind(&nan);
2836 // output = 0
2837 __ xorl(output, output);
2838 __ Bind(&done);
2839 break;
2840 }
2841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002843 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2844 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002845 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002846
2847 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002848 // if input >= (double)INT_MAX goto done
2849 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002850 __ j(kAboveEqual, &done);
2851 // if input == NaN goto nan
2852 __ j(kUnordered, &nan);
2853 // output = double-to-int-truncate(input)
2854 __ cvttsd2si(output, input);
2855 __ jmp(&done);
2856 __ Bind(&nan);
2857 // output = 0
2858 __ xorl(output, output);
2859 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002860 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002861 }
Roland Levillain946e1432014-11-11 17:35:19 +00002862
2863 default:
2864 LOG(FATAL) << "Unexpected type conversion from " << input_type
2865 << " to " << result_type;
2866 }
2867 break;
2868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002870 switch (input_type) {
2871 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002873 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002875 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 case DataType::Type::kInt16:
2877 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002878 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002880 break;
2881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002883 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2884 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002885 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002886
Mark Mendell92e83bf2015-05-07 11:25:03 -04002887 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002888 // if input >= (float)LONG_MAX goto done
2889 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002890 __ j(kAboveEqual, &done);
2891 // if input == NaN goto nan
2892 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002893 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002894 __ cvttss2si(output, input, true);
2895 __ jmp(&done);
2896 __ Bind(&nan);
2897 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002898 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002899 __ Bind(&done);
2900 break;
2901 }
2902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002904 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2905 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002906 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002907
Mark Mendell92e83bf2015-05-07 11:25:03 -04002908 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002909 // if input >= (double)LONG_MAX goto done
2910 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002911 __ j(kAboveEqual, &done);
2912 // if input == NaN goto nan
2913 __ j(kUnordered, &nan);
2914 // output = double-to-long-truncate(input)
2915 __ cvttsd2si(output, input, true);
2916 __ jmp(&done);
2917 __ Bind(&nan);
2918 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002919 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002920 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002921 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002922 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002923
2924 default:
2925 LOG(FATAL) << "Unexpected type conversion from " << input_type
2926 << " to " << result_type;
2927 }
2928 break;
2929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002931 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002933 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002935 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt16:
2937 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002938 if (in.IsRegister()) {
2939 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2940 } else if (in.IsConstant()) {
2941 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2942 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002943 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002944 } else {
2945 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2946 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2947 }
Roland Levillaincff13742014-11-17 14:32:17 +00002948 break;
2949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002951 if (in.IsRegister()) {
2952 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2953 } else if (in.IsConstant()) {
2954 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2955 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002956 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002957 } else {
2958 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2959 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2960 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002961 break;
2962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002964 if (in.IsFpuRegister()) {
2965 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2966 } else if (in.IsConstant()) {
2967 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2968 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002969 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002970 } else {
2971 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2972 Address(CpuRegister(RSP), in.GetStackIndex()));
2973 }
Roland Levillaincff13742014-11-17 14:32:17 +00002974 break;
2975
2976 default:
2977 LOG(FATAL) << "Unexpected type conversion from " << input_type
2978 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002979 }
Roland Levillaincff13742014-11-17 14:32:17 +00002980 break;
2981
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002983 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002985 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002987 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kInt16:
2989 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002990 if (in.IsRegister()) {
2991 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2992 } else if (in.IsConstant()) {
2993 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2994 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002995 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002996 } else {
2997 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2998 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2999 }
Roland Levillaincff13742014-11-17 14:32:17 +00003000 break;
3001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003003 if (in.IsRegister()) {
3004 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3005 } else if (in.IsConstant()) {
3006 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3007 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003008 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003009 } else {
3010 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3011 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3012 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003013 break;
3014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003016 if (in.IsFpuRegister()) {
3017 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3018 } else if (in.IsConstant()) {
3019 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3020 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003021 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003022 } else {
3023 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3024 Address(CpuRegister(RSP), in.GetStackIndex()));
3025 }
Roland Levillaincff13742014-11-17 14:32:17 +00003026 break;
3027
3028 default:
3029 LOG(FATAL) << "Unexpected type conversion from " << input_type
3030 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003031 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003032 break;
3033
3034 default:
3035 LOG(FATAL) << "Unexpected type conversion from " << input_type
3036 << " to " << result_type;
3037 }
3038}
3039
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003040void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003041 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003042 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003043 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003045 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003046 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003048 break;
3049 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003052 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003053 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003054 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003056 break;
3057 }
3058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003059 case DataType::Type::kFloat64:
3060 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003061 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003062 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003063 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003064 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003065 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003066
3067 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003068 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003069 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070}
3071
3072void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3073 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003074 Location first = locations->InAt(0);
3075 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003076 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003077
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003080 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003081 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3082 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003083 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3084 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003085 } else {
3086 __ leal(out.AsRegister<CpuRegister>(), Address(
3087 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3088 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003089 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003090 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3091 __ addl(out.AsRegister<CpuRegister>(),
3092 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3093 } else {
3094 __ leal(out.AsRegister<CpuRegister>(), Address(
3095 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3096 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003097 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003098 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003099 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003100 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003101 break;
3102 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003104 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003105 if (second.IsRegister()) {
3106 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3107 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003108 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3109 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003110 } else {
3111 __ leaq(out.AsRegister<CpuRegister>(), Address(
3112 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3113 }
3114 } else {
3115 DCHECK(second.IsConstant());
3116 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3117 int32_t int32_value = Low32Bits(value);
3118 DCHECK_EQ(int32_value, value);
3119 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3120 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3121 } else {
3122 __ leaq(out.AsRegister<CpuRegister>(), Address(
3123 first.AsRegister<CpuRegister>(), int32_value));
3124 }
3125 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003126 break;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003130 if (second.IsFpuRegister()) {
3131 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3132 } else if (second.IsConstant()) {
3133 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003134 codegen_->LiteralFloatAddress(
3135 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003136 } else {
3137 DCHECK(second.IsStackSlot());
3138 __ addss(first.AsFpuRegister<XmmRegister>(),
3139 Address(CpuRegister(RSP), second.GetStackIndex()));
3140 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003141 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003142 }
3143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003144 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003145 if (second.IsFpuRegister()) {
3146 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3147 } else if (second.IsConstant()) {
3148 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003149 codegen_->LiteralDoubleAddress(
3150 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003151 } else {
3152 DCHECK(second.IsDoubleStackSlot());
3153 __ addsd(first.AsFpuRegister<XmmRegister>(),
3154 Address(CpuRegister(RSP), second.GetStackIndex()));
3155 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003156 break;
3157 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003158
3159 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003160 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003161 }
3162}
3163
3164void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003165 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003166 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003167 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003168 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003169 locations->SetInAt(0, Location::RequiresRegister());
3170 locations->SetInAt(1, Location::Any());
3171 locations->SetOut(Location::SameAsFirstInput());
3172 break;
3173 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003175 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003176 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003177 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003178 break;
3179 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 case DataType::Type::kFloat32:
3181 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003182 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003183 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003184 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003185 break;
Calin Juravle11351682014-10-23 15:38:15 +01003186 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003187 default:
Calin Juravle11351682014-10-23 15:38:15 +01003188 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003189 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003190}
3191
3192void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3193 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003194 Location first = locations->InAt(0);
3195 Location second = locations->InAt(1);
3196 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003197 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003198 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003199 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003200 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003201 } else if (second.IsConstant()) {
3202 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003203 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003204 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003206 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003207 break;
3208 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003210 if (second.IsConstant()) {
3211 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3212 DCHECK(IsInt<32>(value));
3213 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3214 } else {
3215 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3216 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003217 break;
3218 }
3219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003221 if (second.IsFpuRegister()) {
3222 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3223 } else if (second.IsConstant()) {
3224 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003225 codegen_->LiteralFloatAddress(
3226 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003227 } else {
3228 DCHECK(second.IsStackSlot());
3229 __ subss(first.AsFpuRegister<XmmRegister>(),
3230 Address(CpuRegister(RSP), second.GetStackIndex()));
3231 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003232 break;
Calin Juravle11351682014-10-23 15:38:15 +01003233 }
3234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003235 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003236 if (second.IsFpuRegister()) {
3237 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3238 } else if (second.IsConstant()) {
3239 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003240 codegen_->LiteralDoubleAddress(
3241 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003242 } else {
3243 DCHECK(second.IsDoubleStackSlot());
3244 __ subsd(first.AsFpuRegister<XmmRegister>(),
3245 Address(CpuRegister(RSP), second.GetStackIndex()));
3246 }
Calin Juravle11351682014-10-23 15:38:15 +01003247 break;
3248 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003249
3250 default:
Calin Juravle11351682014-10-23 15:38:15 +01003251 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003252 }
3253}
3254
Calin Juravle34bacdf2014-10-07 20:23:36 +01003255void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3256 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003257 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003258 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003259 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003260 locations->SetInAt(0, Location::RequiresRegister());
3261 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003262 if (mul->InputAt(1)->IsIntConstant()) {
3263 // Can use 3 operand multiply.
3264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3265 } else {
3266 locations->SetOut(Location::SameAsFirstInput());
3267 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003268 break;
3269 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003270 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003272 locations->SetInAt(1, Location::Any());
3273 if (mul->InputAt(1)->IsLongConstant() &&
3274 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003275 // Can use 3 operand multiply.
3276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3277 } else {
3278 locations->SetOut(Location::SameAsFirstInput());
3279 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003280 break;
3281 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kFloat32:
3283 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003284 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003285 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003286 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003287 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003288 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003289
3290 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003291 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003292 }
3293}
3294
3295void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3296 LocationSummary* locations = mul->GetLocations();
3297 Location first = locations->InAt(0);
3298 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003299 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003300 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003301 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003302 // The constant may have ended up in a register, so test explicitly to avoid
3303 // problems where the output may not be the same as the first operand.
3304 if (mul->InputAt(1)->IsIntConstant()) {
3305 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3306 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3307 } else if (second.IsRegister()) {
3308 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003309 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003311 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003312 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003313 __ imull(first.AsRegister<CpuRegister>(),
3314 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003315 }
3316 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003317 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003318 // The constant may have ended up in a register, so test explicitly to avoid
3319 // problems where the output may not be the same as the first operand.
3320 if (mul->InputAt(1)->IsLongConstant()) {
3321 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3322 if (IsInt<32>(value)) {
3323 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3324 Immediate(static_cast<int32_t>(value)));
3325 } else {
3326 // Have to use the constant area.
3327 DCHECK(first.Equals(out));
3328 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3329 }
3330 } else if (second.IsRegister()) {
3331 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003332 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003333 } else {
3334 DCHECK(second.IsDoubleStackSlot());
3335 DCHECK(first.Equals(out));
3336 __ imulq(first.AsRegister<CpuRegister>(),
3337 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003338 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339 break;
3340 }
3341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003343 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003344 if (second.IsFpuRegister()) {
3345 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3346 } else if (second.IsConstant()) {
3347 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003348 codegen_->LiteralFloatAddress(
3349 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003350 } else {
3351 DCHECK(second.IsStackSlot());
3352 __ mulss(first.AsFpuRegister<XmmRegister>(),
3353 Address(CpuRegister(RSP), second.GetStackIndex()));
3354 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003356 }
3357
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003358 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003359 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003360 if (second.IsFpuRegister()) {
3361 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3362 } else if (second.IsConstant()) {
3363 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003364 codegen_->LiteralDoubleAddress(
3365 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003366 } else {
3367 DCHECK(second.IsDoubleStackSlot());
3368 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3369 Address(CpuRegister(RSP), second.GetStackIndex()));
3370 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003371 break;
3372 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003373
3374 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003375 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003376 }
3377}
3378
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003379void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3380 uint32_t stack_adjustment, bool is_float) {
3381 if (source.IsStackSlot()) {
3382 DCHECK(is_float);
3383 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3384 } else if (source.IsDoubleStackSlot()) {
3385 DCHECK(!is_float);
3386 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3387 } else {
3388 // Write the value to the temporary location on the stack and load to FP stack.
3389 if (is_float) {
3390 Location stack_temp = Location::StackSlot(temp_offset);
3391 codegen_->Move(stack_temp, source);
3392 __ flds(Address(CpuRegister(RSP), temp_offset));
3393 } else {
3394 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3395 codegen_->Move(stack_temp, source);
3396 __ fldl(Address(CpuRegister(RSP), temp_offset));
3397 }
3398 }
3399}
3400
3401void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 DataType::Type type = rem->GetResultType();
3403 bool is_float = type == DataType::Type::kFloat32;
3404 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003405 LocationSummary* locations = rem->GetLocations();
3406 Location first = locations->InAt(0);
3407 Location second = locations->InAt(1);
3408 Location out = locations->Out();
3409
3410 // Create stack space for 2 elements.
3411 // TODO: enhance register allocator to ask for stack temporaries.
3412 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3413
3414 // Load the values to the FP stack in reverse order, using temporaries if needed.
3415 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3416 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3417
3418 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003419 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003420 __ Bind(&retry);
3421 __ fprem();
3422
3423 // Move FP status to AX.
3424 __ fstsw();
3425
3426 // And see if the argument reduction is complete. This is signaled by the
3427 // C2 FPU flag bit set to 0.
3428 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3429 __ j(kNotEqual, &retry);
3430
3431 // We have settled on the final value. Retrieve it into an XMM register.
3432 // Store FP top of stack to real stack.
3433 if (is_float) {
3434 __ fsts(Address(CpuRegister(RSP), 0));
3435 } else {
3436 __ fstl(Address(CpuRegister(RSP), 0));
3437 }
3438
3439 // Pop the 2 items from the FP stack.
3440 __ fucompp();
3441
3442 // Load the value from the stack into an XMM register.
3443 DCHECK(out.IsFpuRegister()) << out;
3444 if (is_float) {
3445 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3446 } else {
3447 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3448 }
3449
3450 // And remove the temporary stack space we allocated.
3451 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3452}
3453
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003454void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3455 DCHECK(instruction->IsDiv() || instruction->IsRem());
3456
3457 LocationSummary* locations = instruction->GetLocations();
3458 Location second = locations->InAt(1);
3459 DCHECK(second.IsConstant());
3460
3461 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3462 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003463 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464
3465 DCHECK(imm == 1 || imm == -1);
3466
3467 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003468 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 if (instruction->IsRem()) {
3470 __ xorl(output_register, output_register);
3471 } else {
3472 __ movl(output_register, input_register);
3473 if (imm == -1) {
3474 __ negl(output_register);
3475 }
3476 }
3477 break;
3478 }
3479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003480 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003482 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483 } else {
3484 __ movq(output_register, input_register);
3485 if (imm == -1) {
3486 __ negq(output_register);
3487 }
3488 }
3489 break;
3490 }
3491
3492 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003493 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003494 }
3495}
3496
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003497void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 LocationSummary* locations = instruction->GetLocations();
3499 Location second = locations->InAt(1);
3500
3501 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3502 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3503
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003504 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003505 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3506 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507
3508 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003511 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 __ testl(numerator, numerator);
3513 __ cmov(kGreaterEqual, tmp, numerator);
3514 int shift = CTZ(imm);
3515 __ sarl(tmp, Immediate(shift));
3516
3517 if (imm < 0) {
3518 __ negl(tmp);
3519 }
3520
3521 __ movl(output_register, tmp);
3522 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003524 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3525
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003526 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 __ addq(rdx, numerator);
3528 __ testq(numerator, numerator);
3529 __ cmov(kGreaterEqual, rdx, numerator);
3530 int shift = CTZ(imm);
3531 __ sarq(rdx, Immediate(shift));
3532
3533 if (imm < 0) {
3534 __ negq(rdx);
3535 }
3536
3537 __ movq(output_register, rdx);
3538 }
3539}
3540
3541void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3542 DCHECK(instruction->IsDiv() || instruction->IsRem());
3543
3544 LocationSummary* locations = instruction->GetLocations();
3545 Location second = locations->InAt(1);
3546
3547 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3548 : locations->GetTemp(0).AsRegister<CpuRegister>();
3549 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3550 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3551 : locations->Out().AsRegister<CpuRegister>();
3552 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3553
3554 DCHECK_EQ(RAX, eax.AsRegister());
3555 DCHECK_EQ(RDX, edx.AsRegister());
3556 if (instruction->IsDiv()) {
3557 DCHECK_EQ(RAX, out.AsRegister());
3558 } else {
3559 DCHECK_EQ(RDX, out.AsRegister());
3560 }
3561
3562 int64_t magic;
3563 int shift;
3564
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003565 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003566 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003567 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3568
3569 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3570
3571 __ movl(numerator, eax);
3572
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573 __ movl(eax, Immediate(magic));
3574 __ imull(numerator);
3575
3576 if (imm > 0 && magic < 0) {
3577 __ addl(edx, numerator);
3578 } else if (imm < 0 && magic > 0) {
3579 __ subl(edx, numerator);
3580 }
3581
3582 if (shift != 0) {
3583 __ sarl(edx, Immediate(shift));
3584 }
3585
3586 __ movl(eax, edx);
3587 __ shrl(edx, Immediate(31));
3588 __ addl(edx, eax);
3589
3590 if (instruction->IsRem()) {
3591 __ movl(eax, numerator);
3592 __ imull(edx, Immediate(imm));
3593 __ subl(eax, edx);
3594 __ movl(edx, eax);
3595 } else {
3596 __ movl(eax, edx);
3597 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598 } else {
3599 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003601 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003602
3603 CpuRegister rax = eax;
3604 CpuRegister rdx = edx;
3605
3606 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3607
3608 // Save the numerator.
3609 __ movq(numerator, rax);
3610
3611 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003612 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613
3614 // RDX:RAX = magic * numerator
3615 __ imulq(numerator);
3616
3617 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003618 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003619 __ addq(rdx, numerator);
3620 } else if (imm < 0 && magic > 0) {
3621 // RDX -= numerator
3622 __ subq(rdx, numerator);
3623 }
3624
3625 // Shift if needed.
3626 if (shift != 0) {
3627 __ sarq(rdx, Immediate(shift));
3628 }
3629
3630 // RDX += 1 if RDX < 0
3631 __ movq(rax, rdx);
3632 __ shrq(rdx, Immediate(63));
3633 __ addq(rdx, rax);
3634
3635 if (instruction->IsRem()) {
3636 __ movq(rax, numerator);
3637
3638 if (IsInt<32>(imm)) {
3639 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3640 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003641 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003642 }
3643
3644 __ subq(rax, rdx);
3645 __ movq(rdx, rax);
3646 } else {
3647 __ movq(rax, rdx);
3648 }
3649 }
3650}
3651
Calin Juravlebacfec32014-11-14 15:54:36 +00003652void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3653 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003654 DataType::Type type = instruction->GetResultType();
3655 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003656
3657 bool is_div = instruction->IsDiv();
3658 LocationSummary* locations = instruction->GetLocations();
3659
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003660 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3661 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003662
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003664 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003665
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003666 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003667 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003668
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003669 if (imm == 0) {
3670 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3671 } else if (imm == 1 || imm == -1) {
3672 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003673 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003674 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003675 } else {
3676 DCHECK(imm <= -2 || imm >= 2);
3677 GenerateDivRemWithAnyConstant(instruction);
3678 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003679 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003680 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003681 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003682 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003683 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003684
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003685 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3686 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3687 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3688 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003689 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003690 __ cmpl(second_reg, Immediate(-1));
3691 __ j(kEqual, slow_path->GetEntryLabel());
3692 // edx:eax <- sign-extended of eax
3693 __ cdq();
3694 // eax = quotient, edx = remainder
3695 __ idivl(second_reg);
3696 } else {
3697 __ cmpq(second_reg, Immediate(-1));
3698 __ j(kEqual, slow_path->GetEntryLabel());
3699 // rdx:rax <- sign-extended of rax
3700 __ cqo();
3701 // rax = quotient, rdx = remainder
3702 __ idivq(second_reg);
3703 }
3704 __ Bind(slow_path->GetExitLabel());
3705 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003706}
3707
Calin Juravle7c4954d2014-10-28 16:57:40 +00003708void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3709 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003710 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003711 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003712 case DataType::Type::kInt32:
3713 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003714 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003715 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003716 locations->SetOut(Location::SameAsFirstInput());
3717 // Intel uses edx:eax as the dividend.
3718 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003719 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3720 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3721 // output and request another temp.
3722 if (div->InputAt(1)->IsConstant()) {
3723 locations->AddTemp(Location::RequiresRegister());
3724 }
Calin Juravled0d48522014-11-04 16:40:20 +00003725 break;
3726 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 case DataType::Type::kFloat32:
3729 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003730 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003731 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003732 locations->SetOut(Location::SameAsFirstInput());
3733 break;
3734 }
3735
3736 default:
3737 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3738 }
3739}
3740
3741void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3742 LocationSummary* locations = div->GetLocations();
3743 Location first = locations->InAt(0);
3744 Location second = locations->InAt(1);
3745 DCHECK(first.Equals(locations->Out()));
3746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003748 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003749 case DataType::Type::kInt32:
3750 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003751 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003752 break;
3753 }
3754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003756 if (second.IsFpuRegister()) {
3757 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3758 } else if (second.IsConstant()) {
3759 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003760 codegen_->LiteralFloatAddress(
3761 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003762 } else {
3763 DCHECK(second.IsStackSlot());
3764 __ divss(first.AsFpuRegister<XmmRegister>(),
3765 Address(CpuRegister(RSP), second.GetStackIndex()));
3766 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003767 break;
3768 }
3769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003771 if (second.IsFpuRegister()) {
3772 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3773 } else if (second.IsConstant()) {
3774 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003775 codegen_->LiteralDoubleAddress(
3776 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003777 } else {
3778 DCHECK(second.IsDoubleStackSlot());
3779 __ divsd(first.AsFpuRegister<XmmRegister>(),
3780 Address(CpuRegister(RSP), second.GetStackIndex()));
3781 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003782 break;
3783 }
3784
3785 default:
3786 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3787 }
3788}
3789
Calin Juravlebacfec32014-11-14 15:54:36 +00003790void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003792 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003793 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003794
3795 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 case DataType::Type::kInt32:
3797 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003799 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003800 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3801 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003802 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3803 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3804 // output and request another temp.
3805 if (rem->InputAt(1)->IsConstant()) {
3806 locations->AddTemp(Location::RequiresRegister());
3807 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003808 break;
3809 }
3810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kFloat32:
3812 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003813 locations->SetInAt(0, Location::Any());
3814 locations->SetInAt(1, Location::Any());
3815 locations->SetOut(Location::RequiresFpuRegister());
3816 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003817 break;
3818 }
3819
3820 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003821 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003822 }
3823}
3824
3825void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003826 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003827 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003828 case DataType::Type::kInt32:
3829 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003830 GenerateDivRemIntegral(rem);
3831 break;
3832 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kFloat32:
3834 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003835 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003836 break;
3837 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003838 default:
3839 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3840 }
3841}
3842
Aart Bik1f8d51b2018-02-15 10:42:37 -08003843static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3844 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3845 switch (minmax->GetResultType()) {
3846 case DataType::Type::kInt32:
3847 case DataType::Type::kInt64:
3848 locations->SetInAt(0, Location::RequiresRegister());
3849 locations->SetInAt(1, Location::RequiresRegister());
3850 locations->SetOut(Location::SameAsFirstInput());
3851 break;
3852 case DataType::Type::kFloat32:
3853 case DataType::Type::kFloat64:
3854 locations->SetInAt(0, Location::RequiresFpuRegister());
3855 locations->SetInAt(1, Location::RequiresFpuRegister());
3856 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
3857 // the second input to be the output (we can simply swap inputs).
3858 locations->SetOut(Location::SameAsFirstInput());
3859 break;
3860 default:
3861 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3862 }
3863}
3864
Aart Bik351df3e2018-03-07 11:54:57 -08003865void InstructionCodeGeneratorX86_64::GenerateMinMaxInt(LocationSummary* locations,
3866 bool is_min,
3867 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003868 Location op1_loc = locations->InAt(0);
3869 Location op2_loc = locations->InAt(1);
3870
3871 // Shortcut for same input locations.
3872 if (op1_loc.Equals(op2_loc)) {
3873 // Can return immediately, as op1_loc == out_loc.
3874 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3875 // a copy here.
3876 DCHECK(locations->Out().Equals(op1_loc));
3877 return;
3878 }
3879
3880 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3881 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
3882
3883 // (out := op1)
3884 // out <=? op2
3885 // if out is min jmp done
3886 // out := op2
3887 // done:
3888
3889 if (type == DataType::Type::kInt64) {
3890 __ cmpq(out, op2);
3891 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ true);
3892 } else {
3893 DCHECK_EQ(type, DataType::Type::kInt32);
3894 __ cmpl(out, op2);
3895 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ false);
3896 }
3897}
3898
3899void InstructionCodeGeneratorX86_64::GenerateMinMaxFP(LocationSummary* locations,
3900 bool is_min,
3901 DataType::Type type) {
3902 Location op1_loc = locations->InAt(0);
3903 Location op2_loc = locations->InAt(1);
3904 Location out_loc = locations->Out();
3905 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3906
3907 // Shortcut for same input locations.
3908 if (op1_loc.Equals(op2_loc)) {
3909 DCHECK(out_loc.Equals(op1_loc));
3910 return;
3911 }
3912
3913 // (out := op1)
3914 // out <=? op2
3915 // if Nan jmp Nan_label
3916 // if out is min jmp done
3917 // if op2 is min jmp op2_label
3918 // handle -0/+0
3919 // jmp done
3920 // Nan_label:
3921 // out := NaN
3922 // op2_label:
3923 // out := op2
3924 // done:
3925 //
3926 // This removes one jmp, but needs to copy one input (op1) to out.
3927 //
3928 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
3929
3930 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3931
3932 NearLabel nan, done, op2_label;
3933 if (type == DataType::Type::kFloat64) {
3934 __ ucomisd(out, op2);
3935 } else {
3936 DCHECK_EQ(type, DataType::Type::kFloat32);
3937 __ ucomiss(out, op2);
3938 }
3939
3940 __ j(Condition::kParityEven, &nan);
3941
3942 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
3943 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
3944
3945 // Handle 0.0/-0.0.
3946 if (is_min) {
3947 if (type == DataType::Type::kFloat64) {
3948 __ orpd(out, op2);
3949 } else {
3950 __ orps(out, op2);
3951 }
3952 } else {
3953 if (type == DataType::Type::kFloat64) {
3954 __ andpd(out, op2);
3955 } else {
3956 __ andps(out, op2);
3957 }
3958 }
3959 __ jmp(&done);
3960
3961 // NaN handling.
3962 __ Bind(&nan);
3963 if (type == DataType::Type::kFloat64) {
3964 __ movsd(out, codegen_->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
3965 } else {
3966 __ movss(out, codegen_->LiteralInt32Address(INT32_C(0x7FC00000)));
3967 }
3968 __ jmp(&done);
3969
3970 // out := op2;
3971 __ Bind(&op2_label);
3972 if (type == DataType::Type::kFloat64) {
3973 __ movsd(out, op2);
3974 } else {
3975 __ movss(out, op2);
3976 }
3977
3978 // Done.
3979 __ Bind(&done);
3980}
3981
Aart Bik351df3e2018-03-07 11:54:57 -08003982void InstructionCodeGeneratorX86_64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
3983 DataType::Type type = minmax->GetResultType();
3984 switch (type) {
3985 case DataType::Type::kInt32:
3986 case DataType::Type::kInt64:
3987 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
3988 break;
3989 case DataType::Type::kFloat32:
3990 case DataType::Type::kFloat64:
3991 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
3992 break;
3993 default:
3994 LOG(FATAL) << "Unexpected type for HMinMax " << type;
3995 }
3996}
3997
Aart Bik1f8d51b2018-02-15 10:42:37 -08003998void LocationsBuilderX86_64::VisitMin(HMin* min) {
3999 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4000}
4001
4002void InstructionCodeGeneratorX86_64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004003 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004004}
4005
4006void LocationsBuilderX86_64::VisitMax(HMax* max) {
4007 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4008}
4009
4010void InstructionCodeGeneratorX86_64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004011 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004012}
4013
Aart Bik3dad3412018-02-28 12:01:46 -08004014void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
4015 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4016 switch (abs->GetResultType()) {
4017 case DataType::Type::kInt32:
4018 case DataType::Type::kInt64:
4019 locations->SetInAt(0, Location::RequiresRegister());
4020 locations->SetOut(Location::SameAsFirstInput());
4021 locations->AddTemp(Location::RequiresRegister());
4022 break;
4023 case DataType::Type::kFloat32:
4024 case DataType::Type::kFloat64:
4025 locations->SetInAt(0, Location::RequiresFpuRegister());
4026 locations->SetOut(Location::SameAsFirstInput());
4027 locations->AddTemp(Location::RequiresFpuRegister());
4028 break;
4029 default:
4030 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4031 }
4032}
4033
4034void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
4035 LocationSummary* locations = abs->GetLocations();
4036 switch (abs->GetResultType()) {
4037 case DataType::Type::kInt32: {
4038 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4039 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4040 // Create mask.
4041 __ movl(mask, out);
4042 __ sarl(mask, Immediate(31));
4043 // Add mask.
4044 __ addl(out, mask);
4045 __ xorl(out, mask);
4046 break;
4047 }
4048 case DataType::Type::kInt64: {
4049 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4050 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4051 // Create mask.
4052 __ movq(mask, out);
4053 __ sarq(mask, Immediate(63));
4054 // Add mask.
4055 __ addq(out, mask);
4056 __ xorq(out, mask);
4057 break;
4058 }
4059 case DataType::Type::kFloat32: {
4060 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4061 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4062 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
4063 __ andps(out, mask);
4064 break;
4065 }
4066 case DataType::Type::kFloat64: {
4067 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4068 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4069 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
4070 __ andpd(out, mask);
4071 break;
4072 }
4073 default:
4074 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4075 }
4076}
4077
Calin Juravled0d48522014-11-04 16:40:20 +00004078void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004079 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004080 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00004081}
4082
4083void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004084 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004085 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004086 codegen_->AddSlowPath(slow_path);
4087
4088 LocationSummary* locations = instruction->GetLocations();
4089 Location value = locations->InAt(0);
4090
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004091 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004092 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004093 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004094 case DataType::Type::kInt8:
4095 case DataType::Type::kUint16:
4096 case DataType::Type::kInt16:
4097 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004098 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004100 __ j(kEqual, slow_path->GetEntryLabel());
4101 } else if (value.IsStackSlot()) {
4102 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4103 __ j(kEqual, slow_path->GetEntryLabel());
4104 } else {
4105 DCHECK(value.IsConstant()) << value;
4106 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004107 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004108 }
4109 }
4110 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004111 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004112 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004113 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004114 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004115 __ j(kEqual, slow_path->GetEntryLabel());
4116 } else if (value.IsDoubleStackSlot()) {
4117 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4118 __ j(kEqual, slow_path->GetEntryLabel());
4119 } else {
4120 DCHECK(value.IsConstant()) << value;
4121 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004122 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004123 }
4124 }
4125 break;
4126 }
4127 default:
4128 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004129 }
Calin Juravled0d48522014-11-04 16:40:20 +00004130}
4131
Calin Juravle9aec02f2014-11-18 23:06:35 +00004132void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
4133 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4134
4135 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004136 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004137
4138 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 case DataType::Type::kInt32:
4140 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004141 locations->SetInAt(0, Location::RequiresRegister());
4142 // The shift count needs to be in CL.
4143 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
4144 locations->SetOut(Location::SameAsFirstInput());
4145 break;
4146 }
4147 default:
4148 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4149 }
4150}
4151
4152void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
4153 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4154
4155 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004156 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004157 Location second = locations->InAt(1);
4158
4159 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004160 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004161 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004162 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004163 if (op->IsShl()) {
4164 __ shll(first_reg, second_reg);
4165 } else if (op->IsShr()) {
4166 __ sarl(first_reg, second_reg);
4167 } else {
4168 __ shrl(first_reg, second_reg);
4169 }
4170 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004171 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004172 if (op->IsShl()) {
4173 __ shll(first_reg, imm);
4174 } else if (op->IsShr()) {
4175 __ sarl(first_reg, imm);
4176 } else {
4177 __ shrl(first_reg, imm);
4178 }
4179 }
4180 break;
4181 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004183 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004184 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004185 if (op->IsShl()) {
4186 __ shlq(first_reg, second_reg);
4187 } else if (op->IsShr()) {
4188 __ sarq(first_reg, second_reg);
4189 } else {
4190 __ shrq(first_reg, second_reg);
4191 }
4192 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004193 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004194 if (op->IsShl()) {
4195 __ shlq(first_reg, imm);
4196 } else if (op->IsShr()) {
4197 __ sarq(first_reg, imm);
4198 } else {
4199 __ shrq(first_reg, imm);
4200 }
4201 }
4202 break;
4203 }
4204 default:
4205 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004206 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004207 }
4208}
4209
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004210void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4211 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004212 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004213
4214 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004215 case DataType::Type::kInt32:
4216 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004217 locations->SetInAt(0, Location::RequiresRegister());
4218 // The shift count needs to be in CL (unless it is a constant).
4219 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4220 locations->SetOut(Location::SameAsFirstInput());
4221 break;
4222 }
4223 default:
4224 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4225 UNREACHABLE();
4226 }
4227}
4228
4229void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4230 LocationSummary* locations = ror->GetLocations();
4231 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4232 Location second = locations->InAt(1);
4233
4234 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004235 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004236 if (second.IsRegister()) {
4237 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4238 __ rorl(first_reg, second_reg);
4239 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004240 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004241 __ rorl(first_reg, imm);
4242 }
4243 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004245 if (second.IsRegister()) {
4246 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4247 __ rorq(first_reg, second_reg);
4248 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004249 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004250 __ rorq(first_reg, imm);
4251 }
4252 break;
4253 default:
4254 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4255 UNREACHABLE();
4256 }
4257}
4258
Calin Juravle9aec02f2014-11-18 23:06:35 +00004259void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4260 HandleShift(shl);
4261}
4262
4263void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4264 HandleShift(shl);
4265}
4266
4267void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4268 HandleShift(shr);
4269}
4270
4271void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4272 HandleShift(shr);
4273}
4274
4275void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4276 HandleShift(ushr);
4277}
4278
4279void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4280 HandleShift(ushr);
4281}
4282
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004283void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004284 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4285 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004286 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004287 if (instruction->IsStringAlloc()) {
4288 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4289 } else {
4290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004291 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004292 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004293}
4294
4295void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004296 // Note: if heap poisoning is enabled, the entry point takes cares
4297 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004298 if (instruction->IsStringAlloc()) {
4299 // String is allocated through StringFactory. Call NewEmptyString entry point.
4300 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004301 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004302 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4303 __ call(Address(temp, code_offset.SizeValue()));
4304 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4305 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004306 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004307 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004308 DCHECK(!codegen_->IsLeafMethod());
4309 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004310}
4311
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004312void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004313 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4314 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004315 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004316 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004317 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4318 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004319}
4320
4321void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004322 // Note: if heap poisoning is enabled, the entry point takes cares
4323 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004324 QuickEntrypointEnum entrypoint =
4325 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4326 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004327 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004328 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004329}
4330
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004331void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004332 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004333 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004334 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4335 if (location.IsStackSlot()) {
4336 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4337 } else if (location.IsDoubleStackSlot()) {
4338 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4339 }
4340 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004341}
4342
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004343void InstructionCodeGeneratorX86_64::VisitParameterValue(
4344 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004345 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004346}
4347
4348void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4349 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004350 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004351 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4352}
4353
4354void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4355 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4356 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004357}
4358
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004359void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4360 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004361 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004362 locations->SetInAt(0, Location::RequiresRegister());
4363 locations->SetOut(Location::RequiresRegister());
4364}
4365
4366void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4367 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004368 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004369 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004370 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004371 __ movq(locations->Out().AsRegister<CpuRegister>(),
4372 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004373 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004374 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004375 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004376 __ movq(locations->Out().AsRegister<CpuRegister>(),
4377 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4378 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004379 __ movq(locations->Out().AsRegister<CpuRegister>(),
4380 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004381 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004382}
4383
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004384void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004385 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004386 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004387 locations->SetInAt(0, Location::RequiresRegister());
4388 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004389}
4390
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004391void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4392 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4394 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004395 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004396 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004397 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004399 break;
4400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004401 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004403 break;
4404
4405 default:
4406 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4407 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004408}
4409
David Brazdil66d126e2015-04-03 16:02:44 +01004410void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4411 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004412 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004413 locations->SetInAt(0, Location::RequiresRegister());
4414 locations->SetOut(Location::SameAsFirstInput());
4415}
4416
4417void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004418 LocationSummary* locations = bool_not->GetLocations();
4419 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4420 locations->Out().AsRegister<CpuRegister>().AsRegister());
4421 Location out = locations->Out();
4422 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4423}
4424
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004425void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004426 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004427 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004428 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004429 locations->SetInAt(i, Location::Any());
4430 }
4431 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004432}
4433
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004434void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004435 LOG(FATAL) << "Unimplemented";
4436}
4437
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004438void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004439 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004440 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004441 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004442 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4443 */
4444 switch (kind) {
4445 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004446 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004447 break;
4448 }
4449 case MemBarrierKind::kAnyStore:
4450 case MemBarrierKind::kLoadAny:
4451 case MemBarrierKind::kStoreStore: {
4452 // nop
4453 break;
4454 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004455 case MemBarrierKind::kNTStoreStore:
4456 // Non-Temporal Store/Store needs an explicit fence.
4457 MemoryFence(/* non-temporal */ true);
4458 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004459 }
4460}
4461
4462void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4463 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4464
Roland Levillain0d5a2812015-11-13 10:07:31 +00004465 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004466 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004468 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4469 object_field_get_with_read_barrier
4470 ? LocationSummary::kCallOnSlowPath
4471 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004472 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004473 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004474 }
Calin Juravle52c48962014-12-16 17:02:57 +00004475 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004476 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004477 locations->SetOut(Location::RequiresFpuRegister());
4478 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004479 // The output overlaps for an object field get when read barriers
4480 // are enabled: we do not want the move to overwrite the object's
4481 // location, as we need it to emit the read barrier.
4482 locations->SetOut(
4483 Location::RequiresRegister(),
4484 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004485 }
Calin Juravle52c48962014-12-16 17:02:57 +00004486}
4487
4488void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4489 const FieldInfo& field_info) {
4490 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4491
4492 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004493 Location base_loc = locations->InAt(0);
4494 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004495 Location out = locations->Out();
4496 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004497 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4498 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004499 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4500
Vladimir Marko61b92282017-10-11 13:23:17 +01004501 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004502 case DataType::Type::kBool:
4503 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004504 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4505 break;
4506 }
4507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004508 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004509 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4510 break;
4511 }
4512
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004513 case DataType::Type::kUint16: {
4514 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004515 break;
4516 }
4517
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004518 case DataType::Type::kInt16: {
4519 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004520 break;
4521 }
4522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004524 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4525 break;
4526 }
4527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004529 // /* HeapReference<Object> */ out = *(base + offset)
4530 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004531 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004532 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004533 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004534 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004535 if (is_volatile) {
4536 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4537 }
4538 } else {
4539 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4540 codegen_->MaybeRecordImplicitNullCheck(instruction);
4541 if (is_volatile) {
4542 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4543 }
4544 // If read barriers are enabled, emit read barriers other than
4545 // Baker's using a slow path (and also unpoison the loaded
4546 // reference, if heap poisoning is enabled).
4547 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4548 }
4549 break;
4550 }
4551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004552 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004553 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4554 break;
4555 }
4556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004557 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004558 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4559 break;
4560 }
4561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004562 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004563 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4564 break;
4565 }
4566
Aart Bik66c158e2018-01-31 12:55:04 -08004567 case DataType::Type::kUint32:
4568 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004570 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004571 UNREACHABLE();
4572 }
4573
Vladimir Marko61b92282017-10-11 13:23:17 +01004574 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004575 // Potential implicit null checks, in the case of reference
4576 // fields, are handled in the previous switch statement.
4577 } else {
4578 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004579 }
Roland Levillain4d027112015-07-01 15:41:14 +01004580
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004581 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004582 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004583 // Memory barriers, in the case of references, are also handled
4584 // in the previous switch statement.
4585 } else {
4586 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4587 }
Roland Levillain4d027112015-07-01 15:41:14 +01004588 }
Calin Juravle52c48962014-12-16 17:02:57 +00004589}
4590
4591void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4592 const FieldInfo& field_info) {
4593 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4594
4595 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004596 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004597 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004598 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004599 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004600 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004601
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004602 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004603 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004604 if (is_volatile) {
4605 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4606 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4607 } else {
4608 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4609 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004610 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004611 if (is_volatile) {
4612 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4613 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4614 } else {
4615 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4616 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004617 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004618 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004619 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004620 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004621 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004622 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004623 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004624 locations->AddTemp(Location::RequiresRegister());
4625 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004626}
4627
Calin Juravle52c48962014-12-16 17:02:57 +00004628void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004629 const FieldInfo& field_info,
4630 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004631 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4632
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004633 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004634 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4635 Location value = locations->InAt(1);
4636 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004638 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4639
4640 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004641 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004642 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643
Mark Mendellea5af682015-10-22 17:35:49 -04004644 bool maybe_record_implicit_null_check_done = false;
4645
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004646 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004647 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004648 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004649 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004650 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004651 __ movb(Address(base, offset),
4652 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004653 } else {
4654 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4655 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656 break;
4657 }
4658
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004659 case DataType::Type::kUint16:
4660 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004661 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004662 __ movw(Address(base, offset),
4663 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004664 } else {
4665 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4666 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667 break;
4668 }
4669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kInt32:
4671 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004672 if (value.IsConstant()) {
4673 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 // `field_type == DataType::Type::kReference` implies `v == 0`.
4675 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004676 // Note: if heap poisoning is enabled, no need to poison
4677 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004678 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004679 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004680 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004681 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4682 __ movl(temp, value.AsRegister<CpuRegister>());
4683 __ PoisonHeapReference(temp);
4684 __ movl(Address(base, offset), temp);
4685 } else {
4686 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4687 }
Mark Mendell40741f32015-04-20 22:10:34 -04004688 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004689 break;
4690 }
4691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004693 if (value.IsConstant()) {
4694 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004695 codegen_->MoveInt64ToAddress(Address(base, offset),
4696 Address(base, offset + sizeof(int32_t)),
4697 v,
4698 instruction);
4699 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004700 } else {
4701 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4702 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004703 break;
4704 }
4705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004706 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004707 if (value.IsConstant()) {
4708 int32_t v =
4709 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4710 __ movl(Address(base, offset), Immediate(v));
4711 } else {
4712 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4713 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004714 break;
4715 }
4716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004718 if (value.IsConstant()) {
4719 int64_t v =
4720 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4721 codegen_->MoveInt64ToAddress(Address(base, offset),
4722 Address(base, offset + sizeof(int32_t)),
4723 v,
4724 instruction);
4725 maybe_record_implicit_null_check_done = true;
4726 } else {
4727 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4728 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004729 break;
4730 }
4731
Aart Bik66c158e2018-01-31 12:55:04 -08004732 case DataType::Type::kUint32:
4733 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004734 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004736 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737 }
Calin Juravle52c48962014-12-16 17:02:57 +00004738
Mark Mendellea5af682015-10-22 17:35:49 -04004739 if (!maybe_record_implicit_null_check_done) {
4740 codegen_->MaybeRecordImplicitNullCheck(instruction);
4741 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004742
4743 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4744 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4745 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004746 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004747 }
4748
Calin Juravle52c48962014-12-16 17:02:57 +00004749 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004750 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004751 }
4752}
4753
4754void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4755 HandleFieldSet(instruction, instruction->GetFieldInfo());
4756}
4757
4758void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004759 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004760}
4761
4762void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004763 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004764}
4765
4766void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004767 HandleFieldGet(instruction, instruction->GetFieldInfo());
4768}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004769
Calin Juravle52c48962014-12-16 17:02:57 +00004770void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4771 HandleFieldGet(instruction);
4772}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004773
Calin Juravle52c48962014-12-16 17:02:57 +00004774void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4775 HandleFieldGet(instruction, instruction->GetFieldInfo());
4776}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004777
Calin Juravle52c48962014-12-16 17:02:57 +00004778void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4779 HandleFieldSet(instruction, instruction->GetFieldInfo());
4780}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004781
Calin Juravle52c48962014-12-16 17:02:57 +00004782void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004783 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784}
4785
Calin Juravlee460d1d2015-09-29 04:52:17 +01004786void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4787 HUnresolvedInstanceFieldGet* instruction) {
4788 FieldAccessCallingConventionX86_64 calling_convention;
4789 codegen_->CreateUnresolvedFieldLocationSummary(
4790 instruction, instruction->GetFieldType(), calling_convention);
4791}
4792
4793void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4794 HUnresolvedInstanceFieldGet* instruction) {
4795 FieldAccessCallingConventionX86_64 calling_convention;
4796 codegen_->GenerateUnresolvedFieldAccess(instruction,
4797 instruction->GetFieldType(),
4798 instruction->GetFieldIndex(),
4799 instruction->GetDexPc(),
4800 calling_convention);
4801}
4802
4803void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4804 HUnresolvedInstanceFieldSet* instruction) {
4805 FieldAccessCallingConventionX86_64 calling_convention;
4806 codegen_->CreateUnresolvedFieldLocationSummary(
4807 instruction, instruction->GetFieldType(), calling_convention);
4808}
4809
4810void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4811 HUnresolvedInstanceFieldSet* instruction) {
4812 FieldAccessCallingConventionX86_64 calling_convention;
4813 codegen_->GenerateUnresolvedFieldAccess(instruction,
4814 instruction->GetFieldType(),
4815 instruction->GetFieldIndex(),
4816 instruction->GetDexPc(),
4817 calling_convention);
4818}
4819
4820void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4821 HUnresolvedStaticFieldGet* instruction) {
4822 FieldAccessCallingConventionX86_64 calling_convention;
4823 codegen_->CreateUnresolvedFieldLocationSummary(
4824 instruction, instruction->GetFieldType(), calling_convention);
4825}
4826
4827void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4828 HUnresolvedStaticFieldGet* instruction) {
4829 FieldAccessCallingConventionX86_64 calling_convention;
4830 codegen_->GenerateUnresolvedFieldAccess(instruction,
4831 instruction->GetFieldType(),
4832 instruction->GetFieldIndex(),
4833 instruction->GetDexPc(),
4834 calling_convention);
4835}
4836
4837void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4838 HUnresolvedStaticFieldSet* instruction) {
4839 FieldAccessCallingConventionX86_64 calling_convention;
4840 codegen_->CreateUnresolvedFieldLocationSummary(
4841 instruction, instruction->GetFieldType(), calling_convention);
4842}
4843
4844void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4845 HUnresolvedStaticFieldSet* instruction) {
4846 FieldAccessCallingConventionX86_64 calling_convention;
4847 codegen_->GenerateUnresolvedFieldAccess(instruction,
4848 instruction->GetFieldType(),
4849 instruction->GetFieldIndex(),
4850 instruction->GetDexPc(),
4851 calling_convention);
4852}
4853
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004854void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004855 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4856 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4857 ? Location::RequiresRegister()
4858 : Location::Any();
4859 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004860}
4861
Calin Juravle2ae48182016-03-16 14:05:09 +00004862void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4863 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004864 return;
4865 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004866 LocationSummary* locations = instruction->GetLocations();
4867 Location obj = locations->InAt(0);
4868
4869 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004870 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004871}
4872
Calin Juravle2ae48182016-03-16 14:05:09 +00004873void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004874 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004875 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004876
4877 LocationSummary* locations = instruction->GetLocations();
4878 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004879
4880 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004881 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004882 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004883 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004884 } else {
4885 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004886 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004887 __ jmp(slow_path->GetEntryLabel());
4888 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004889 }
4890 __ j(kEqual, slow_path->GetEntryLabel());
4891}
4892
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004893void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004894 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004895}
4896
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004897void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004898 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004899 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004900 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004901 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4902 object_array_get_with_read_barrier
4903 ? LocationSummary::kCallOnSlowPath
4904 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004905 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004906 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004907 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004908 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004909 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004910 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4912 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004913 // The output overlaps for an object array get when read barriers
4914 // are enabled: we do not want the move to overwrite the array's
4915 // location, as we need it to emit the read barrier.
4916 locations->SetOut(
4917 Location::RequiresRegister(),
4918 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004919 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004920}
4921
4922void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4923 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004924 Location obj_loc = locations->InAt(0);
4925 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004927 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004928 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004931 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004932 case DataType::Type::kBool:
4933 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004934 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004935 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004936 break;
4937 }
4938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004939 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004940 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004941 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942 break;
4943 }
4944
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004946 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004947 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4948 // Branch cases into compressed and uncompressed for each index's type.
4949 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4950 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004951 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004952 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004953 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4954 "Expecting 0=compressed, 1=uncompressed");
4955 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004956 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4957 __ jmp(&done);
4958 __ Bind(&not_compressed);
4959 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4960 __ Bind(&done);
4961 } else {
4962 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4963 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004964 break;
4965 }
4966
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004967 case DataType::Type::kInt16: {
4968 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4969 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4970 break;
4971 }
4972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004974 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004975 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976 break;
4977 }
4978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004979 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004980 static_assert(
4981 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4982 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004983 // /* HeapReference<Object> */ out =
4984 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4985 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004986 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004987 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004988 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004989 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004990 } else {
4991 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004992 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4993 codegen_->MaybeRecordImplicitNullCheck(instruction);
4994 // If read barriers are enabled, emit read barriers other than
4995 // Baker's using a slow path (and also unpoison the loaded
4996 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004997 if (index.IsConstant()) {
4998 uint32_t offset =
4999 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005000 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5001 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005002 codegen_->MaybeGenerateReadBarrierSlow(
5003 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5004 }
5005 }
5006 break;
5007 }
5008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005009 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005010 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005011 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005012 break;
5013 }
5014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005016 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005017 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005018 break;
5019 }
5020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005021 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005022 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005023 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005024 break;
5025 }
5026
Aart Bik66c158e2018-01-31 12:55:04 -08005027 case DataType::Type::kUint32:
5028 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005029 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005030 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005031 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 }
Roland Levillain4d027112015-07-01 15:41:14 +01005033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005034 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005035 // Potential implicit null checks, in the case of reference
5036 // arrays, are handled in the previous switch statement.
5037 } else {
5038 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005039 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040}
5041
5042void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005043 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005044
5045 bool needs_write_barrier =
5046 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005047 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005048
Vladimir Markoca6fff82017-10-03 14:49:14 +01005049 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005050 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005051 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005052 LocationSummary::kCallOnSlowPath :
5053 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005054
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005055 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04005056 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005057 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04005058 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005059 } else {
5060 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5061 }
5062
5063 if (needs_write_barrier) {
5064 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005065 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005066 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068}
5069
5070void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
5071 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005072 Location array_loc = locations->InAt(0);
5073 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005075 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005076 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005077 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005078 bool needs_write_barrier =
5079 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005080 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5081 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5082 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083
5084 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005085 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005086 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005087 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005088 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005089 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005090 if (value.IsRegister()) {
5091 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005093 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005095 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096 break;
5097 }
5098
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005099 case DataType::Type::kUint16:
5100 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005101 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005102 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005103 if (value.IsRegister()) {
5104 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005106 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01005107 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 break;
5111 }
5112
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005113 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005114 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005115 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005116
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005117 if (!value.IsRegister()) {
5118 // Just setting null.
5119 DCHECK(instruction->InputAt(2)->IsNullConstant());
5120 DCHECK(value.IsConstant()) << value;
5121 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005122 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005123 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005124 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005126 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005127
5128 DCHECK(needs_write_barrier);
5129 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005130 // We cannot use a NearLabel for `done`, as its range may be too
5131 // short when Baker read barriers are enabled.
5132 Label done;
5133 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005134 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005135 Location temp_loc = locations->GetTemp(0);
5136 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005137 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005138 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005139 codegen_->AddSlowPath(slow_path);
5140 if (instruction->GetValueCanBeNull()) {
5141 __ testl(register_value, register_value);
5142 __ j(kNotEqual, &not_null);
5143 __ movl(address, Immediate(0));
5144 codegen_->MaybeRecordImplicitNullCheck(instruction);
5145 __ jmp(&done);
5146 __ Bind(&not_null);
5147 }
5148
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005149 // Note that when Baker read barriers are enabled, the type
5150 // checks are performed without read barriers. This is fine,
5151 // even in the case where a class object is in the from-space
5152 // after the flip, as a comparison involving such a type would
5153 // not produce a false positive; it may of course produce a
5154 // false negative, in which case we would take the ArraySet
5155 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005156
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005157 // /* HeapReference<Class> */ temp = array->klass_
5158 __ movl(temp, Address(array, class_offset));
5159 codegen_->MaybeRecordImplicitNullCheck(instruction);
5160 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005161
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005162 // /* HeapReference<Class> */ temp = temp->component_type_
5163 __ movl(temp, Address(temp, component_offset));
5164 // If heap poisoning is enabled, no need to unpoison `temp`
5165 // nor the object reference in `register_value->klass`, as
5166 // we are comparing two poisoned references.
5167 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005168
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005169 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5170 __ j(kEqual, &do_put);
5171 // If heap poisoning is enabled, the `temp` reference has
5172 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005173 __ MaybeUnpoisonHeapReference(temp);
5174
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005175 // If heap poisoning is enabled, no need to unpoison the
5176 // heap reference loaded below, as it is only used for a
5177 // comparison with null.
5178 __ cmpl(Address(temp, super_offset), Immediate(0));
5179 __ j(kNotEqual, slow_path->GetEntryLabel());
5180 __ Bind(&do_put);
5181 } else {
5182 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005183 }
5184 }
5185
5186 if (kPoisonHeapReferences) {
5187 __ movl(temp, register_value);
5188 __ PoisonHeapReference(temp);
5189 __ movl(address, temp);
5190 } else {
5191 __ movl(address, register_value);
5192 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005193 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005194 codegen_->MaybeRecordImplicitNullCheck(instruction);
5195 }
5196
5197 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5198 codegen_->MarkGCCard(
5199 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
5200 __ Bind(&done);
5201
5202 if (slow_path != nullptr) {
5203 __ Bind(slow_path->GetExitLabel());
5204 }
5205
5206 break;
5207 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005210 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005211 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005212 if (value.IsRegister()) {
5213 __ movl(address, value.AsRegister<CpuRegister>());
5214 } else {
5215 DCHECK(value.IsConstant()) << value;
5216 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5217 __ movl(address, Immediate(v));
5218 }
5219 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 break;
5221 }
5222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005224 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005225 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005226 if (value.IsRegister()) {
5227 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005230 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005231 Address address_high =
5232 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005233 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234 }
5235 break;
5236 }
5237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005238 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005240 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005241 if (value.IsFpuRegister()) {
5242 __ movss(address, value.AsFpuRegister<XmmRegister>());
5243 } else {
5244 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005245 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005246 __ movl(address, Immediate(v));
5247 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 break;
5250 }
5251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005254 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005255 if (value.IsFpuRegister()) {
5256 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5257 codegen_->MaybeRecordImplicitNullCheck(instruction);
5258 } else {
5259 int64_t v =
5260 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005261 Address address_high =
5262 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005263 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5264 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005265 break;
5266 }
5267
Aart Bik66c158e2018-01-31 12:55:04 -08005268 case DataType::Type::kUint32:
5269 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005270 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005272 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 }
5274}
5275
5276void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005277 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005278 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005279 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005280 if (!instruction->IsEmittedAtUseSite()) {
5281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5282 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283}
5284
5285void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005286 if (instruction->IsEmittedAtUseSite()) {
5287 return;
5288 }
5289
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005291 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005292 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5293 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005295 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005296 // Mask out most significant bit in case the array is String's array of char.
5297 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005298 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005299 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300}
5301
5302void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005303 RegisterSet caller_saves = RegisterSet::Empty();
5304 InvokeRuntimeCallingConvention calling_convention;
5305 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5306 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5307 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005308 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005309 HInstruction* length = instruction->InputAt(1);
5310 if (!length->IsEmittedAtUseSite()) {
5311 locations->SetInAt(1, Location::RegisterOrConstant(length));
5312 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313}
5314
5315void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5316 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005317 Location index_loc = locations->InAt(0);
5318 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005319 SlowPathCode* slow_path =
5320 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321
Mark Mendell99dbd682015-04-22 16:18:52 -04005322 if (length_loc.IsConstant()) {
5323 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5324 if (index_loc.IsConstant()) {
5325 // BCE will remove the bounds check if we are guarenteed to pass.
5326 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5327 if (index < 0 || index >= length) {
5328 codegen_->AddSlowPath(slow_path);
5329 __ jmp(slow_path->GetEntryLabel());
5330 } else {
5331 // Some optimization after BCE may have generated this, and we should not
5332 // generate a bounds check if it is a valid range.
5333 }
5334 return;
5335 }
5336
5337 // We have to reverse the jump condition because the length is the constant.
5338 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5339 __ cmpl(index_reg, Immediate(length));
5340 codegen_->AddSlowPath(slow_path);
5341 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005342 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005343 HInstruction* array_length = instruction->InputAt(1);
5344 if (array_length->IsEmittedAtUseSite()) {
5345 // Address the length field in the array.
5346 DCHECK(array_length->IsArrayLength());
5347 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5348 Location array_loc = array_length->GetLocations()->InAt(0);
5349 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005350 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005351 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5352 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005353 CpuRegister length_reg = CpuRegister(TMP);
5354 __ movl(length_reg, array_len);
5355 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005356 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005357 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005358 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005359 // Checking the bound for general case:
5360 // Array of char or String's array when the compression feature off.
5361 if (index_loc.IsConstant()) {
5362 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5363 __ cmpl(array_len, Immediate(value));
5364 } else {
5365 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5366 }
5367 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005368 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005369 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005370 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005371 }
5372 codegen_->AddSlowPath(slow_path);
5373 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375}
5376
5377void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5378 CpuRegister card,
5379 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005380 CpuRegister value,
5381 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005382 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005383 if (value_can_be_null) {
5384 __ testl(value, value);
5385 __ j(kEqual, &is_null);
5386 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005387 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005388 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 __ movq(temp, object);
5390 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005391 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005392 if (value_can_be_null) {
5393 __ Bind(&is_null);
5394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395}
5396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005397void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005398 LOG(FATAL) << "Unimplemented";
5399}
5400
5401void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005402 if (instruction->GetNext()->IsSuspendCheck() &&
5403 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5404 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5405 // The back edge will generate the suspend check.
5406 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5407 }
5408
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005409 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5410}
5411
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005412void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005413 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5414 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005415 // In suspend check slow path, usually there are no caller-save registers at all.
5416 // If SIMD instructions are present, however, we force spilling all live SIMD
5417 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005418 locations->SetCustomSlowPathCallerSaves(
5419 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005420}
5421
5422void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005423 HBasicBlock* block = instruction->GetBlock();
5424 if (block->GetLoopInformation() != nullptr) {
5425 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5426 // The back edge will generate the suspend check.
5427 return;
5428 }
5429 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5430 // The goto will generate the suspend check.
5431 return;
5432 }
5433 GenerateSuspendCheck(instruction, nullptr);
5434}
5435
5436void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5437 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005438 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005439 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5440 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005441 slow_path =
5442 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005443 instruction->SetSlowPath(slow_path);
5444 codegen_->AddSlowPath(slow_path);
5445 if (successor != nullptr) {
5446 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005447 }
5448 } else {
5449 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5450 }
5451
Andreas Gampe542451c2016-07-26 09:02:02 -07005452 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005453 /* no_rip */ true),
5454 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005455 if (successor == nullptr) {
5456 __ j(kNotEqual, slow_path->GetEntryLabel());
5457 __ Bind(slow_path->GetReturnLabel());
5458 } else {
5459 __ j(kEqual, codegen_->GetLabelOf(successor));
5460 __ jmp(slow_path->GetEntryLabel());
5461 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005462}
5463
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005464X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5465 return codegen_->GetAssembler();
5466}
5467
5468void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005469 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005470 Location source = move->GetSource();
5471 Location destination = move->GetDestination();
5472
5473 if (source.IsRegister()) {
5474 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005475 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005476 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005477 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005478 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005479 } else {
5480 DCHECK(destination.IsDoubleStackSlot());
5481 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005482 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005483 }
5484 } else if (source.IsStackSlot()) {
5485 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005486 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005487 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005488 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005489 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005490 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005491 } else {
5492 DCHECK(destination.IsStackSlot());
5493 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5494 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5495 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005496 } else if (source.IsDoubleStackSlot()) {
5497 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005498 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005499 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005500 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005501 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5502 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005503 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005504 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005505 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5506 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5507 }
Aart Bik5576f372017-03-23 16:17:37 -07005508 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005509 if (destination.IsFpuRegister()) {
5510 __ movups(destination.AsFpuRegister<XmmRegister>(),
5511 Address(CpuRegister(RSP), source.GetStackIndex()));
5512 } else {
5513 DCHECK(destination.IsSIMDStackSlot());
5514 size_t high = kX86_64WordSize;
5515 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5516 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5517 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5518 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5519 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005520 } else if (source.IsConstant()) {
5521 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005522 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5523 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005524 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005525 if (value == 0) {
5526 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5527 } else {
5528 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5529 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005530 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005531 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005532 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005533 }
5534 } else if (constant->IsLongConstant()) {
5535 int64_t value = constant->AsLongConstant()->GetValue();
5536 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005537 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005538 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005539 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005540 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005541 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005542 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005543 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005544 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005545 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005546 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005547 } else {
5548 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005549 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005550 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5551 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005552 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005553 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005554 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005555 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005556 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005557 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005558 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005559 } else {
5560 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005561 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005562 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005563 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005564 } else if (source.IsFpuRegister()) {
5565 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005566 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005567 } else if (destination.IsStackSlot()) {
5568 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005569 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005570 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005571 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005572 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005573 } else {
5574 DCHECK(destination.IsSIMDStackSlot());
5575 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5576 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005577 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005578 }
5579}
5580
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005581void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005582 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005583 __ movl(Address(CpuRegister(RSP), mem), reg);
5584 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005585}
5586
Mark Mendell8a1c7282015-06-29 15:41:28 -04005587void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5588 __ movq(CpuRegister(TMP), reg1);
5589 __ movq(reg1, reg2);
5590 __ movq(reg2, CpuRegister(TMP));
5591}
5592
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005593void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5594 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5595 __ movq(Address(CpuRegister(RSP), mem), reg);
5596 __ movq(reg, CpuRegister(TMP));
5597}
5598
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005599void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5600 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5601 __ movss(Address(CpuRegister(RSP), mem), reg);
5602 __ movd(reg, CpuRegister(TMP));
5603}
5604
5605void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5606 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5607 __ movsd(Address(CpuRegister(RSP), mem), reg);
5608 __ movd(reg, CpuRegister(TMP));
5609}
5610
Aart Bikcfe50bb2017-12-12 14:54:12 -08005611void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5612 size_t extra_slot = 2 * kX86_64WordSize;
5613 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5614 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5615 ExchangeMemory64(0, mem + extra_slot, 2);
5616 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5617 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5618}
5619
5620void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5621 ScratchRegisterScope ensure_scratch(
5622 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5623
5624 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5625 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5626 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5627 Address(CpuRegister(RSP), mem2 + stack_offset));
5628 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5629 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5630 CpuRegister(ensure_scratch.GetRegister()));
5631}
5632
5633void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5634 ScratchRegisterScope ensure_scratch(
5635 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5636
5637 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5638
5639 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5640 for (int i = 0; i < num_of_qwords; i++) {
5641 __ movq(CpuRegister(TMP),
5642 Address(CpuRegister(RSP), mem1 + stack_offset));
5643 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5644 Address(CpuRegister(RSP), mem2 + stack_offset));
5645 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5646 CpuRegister(TMP));
5647 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5648 CpuRegister(ensure_scratch.GetRegister()));
5649 stack_offset += kX86_64WordSize;
5650 }
5651}
5652
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005653void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005654 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005655 Location source = move->GetSource();
5656 Location destination = move->GetDestination();
5657
5658 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005659 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005660 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005661 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005662 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005663 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005664 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005665 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005666 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005667 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005668 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005669 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005670 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005671 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005672 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005673 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5674 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5675 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005676 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005677 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005678 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005679 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005680 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005681 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005682 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005683 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005684 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5685 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5686 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5687 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5688 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5689 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005690 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005691 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005692 }
5693}
5694
5695
5696void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5697 __ pushq(CpuRegister(reg));
5698}
5699
5700
5701void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5702 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005703}
5704
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005705void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005706 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005707 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5708 const size_t status_byte_offset =
5709 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5710 constexpr uint32_t shifted_initialized_value =
5711 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5712
5713 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005714 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005715 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005716 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005717}
5718
Vladimir Marko175e7862018-03-27 09:03:13 +00005719void InstructionCodeGeneratorX86_64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
5720 CpuRegister temp) {
5721 uint32_t path_to_root = check->GetBitstringPathToRoot();
5722 uint32_t mask = check->GetBitstringMask();
5723 DCHECK(IsPowerOfTwo(mask + 1));
5724 size_t mask_bits = WhichPowerOf2(mask + 1);
5725
5726 if (mask_bits == 16u) {
5727 // Compare the bitstring in memory.
5728 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
5729 } else {
5730 // /* uint32_t */ temp = temp->status_
5731 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
5732 // Compare the bitstring bits using SUB.
5733 __ subl(temp, Immediate(path_to_root));
5734 // Shift out bits that do not contribute to the comparison.
5735 __ shll(temp, Immediate(32u - mask_bits));
5736 }
5737}
5738
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005739HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5740 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005741 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005742 case HLoadClass::LoadKind::kInvalid:
5743 LOG(FATAL) << "UNREACHABLE";
5744 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005745 case HLoadClass::LoadKind::kReferrersClass:
5746 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005747 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005748 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005749 case HLoadClass::LoadKind::kBssEntry:
5750 DCHECK(!Runtime::Current()->UseJitCompilation());
5751 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005752 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005753 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005754 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005755 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005756 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005757 break;
5758 }
5759 return desired_class_load_kind;
5760}
5761
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005762void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005763 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005764 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005765 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005766 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005767 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005768 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005769 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005770 return;
5771 }
Vladimir Marko41559982017-01-06 14:04:23 +00005772 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005773
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005774 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5775 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005776 ? LocationSummary::kCallOnSlowPath
5777 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005778 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005779 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005780 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005781 }
5782
Vladimir Marko41559982017-01-06 14:04:23 +00005783 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005784 locations->SetInAt(0, Location::RequiresRegister());
5785 }
5786 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005787 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5788 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5789 // Rely on the type resolution and/or initialization to save everything.
5790 // Custom calling convention: RAX serves as both input and output.
5791 RegisterSet caller_saves = RegisterSet::Empty();
5792 caller_saves.Add(Location::RegisterLocation(RAX));
5793 locations->SetCustomSlowPathCallerSaves(caller_saves);
5794 } else {
5795 // For non-Baker read barrier we have a temp-clobbering call.
5796 }
5797 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005798}
5799
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005800Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005801 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005802 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005803 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005804 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005805 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005806 PatchInfo<Label>* info = &jit_class_patches_.back();
5807 return &info->label;
5808}
5809
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005810// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5811// move.
5812void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005813 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005814 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005815 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005816 return;
5817 }
Vladimir Marko41559982017-01-06 14:04:23 +00005818 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005819
Vladimir Marko41559982017-01-06 14:04:23 +00005820 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821 Location out_loc = locations->Out();
5822 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005824 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5825 ? kWithoutReadBarrier
5826 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005828 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005829 case HLoadClass::LoadKind::kReferrersClass: {
5830 DCHECK(!cls->CanCallRuntime());
5831 DCHECK(!cls->MustGenerateClinitCheck());
5832 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5833 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5834 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005835 cls,
5836 out_loc,
5837 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005838 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005839 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005840 break;
5841 }
5842 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005843 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005844 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005845 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005846 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005847 break;
5848 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005849 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005850 uint32_t address = dchecked_integral_cast<uint32_t>(
5851 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5852 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005853 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005854 break;
5855 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005856 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005857 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5858 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005859 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005860 break;
5861 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005862 case HLoadClass::LoadKind::kBssEntry: {
5863 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5864 /* no_rip */ false);
5865 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5866 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5867 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5868 generate_null_check = true;
5869 break;
5870 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005871 case HLoadClass::LoadKind::kJitTableAddress: {
5872 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5873 /* no_rip */ true);
5874 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005875 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005876 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005877 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005878 break;
5879 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005880 default:
5881 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5882 UNREACHABLE();
5883 }
5884
5885 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5886 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005887 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5889 codegen_->AddSlowPath(slow_path);
5890 if (generate_null_check) {
5891 __ testl(out, out);
5892 __ j(kEqual, slow_path->GetEntryLabel());
5893 }
5894 if (cls->MustGenerateClinitCheck()) {
5895 GenerateClassInitializationCheck(slow_path, out);
5896 } else {
5897 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005898 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899 }
5900}
5901
5902void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5903 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005904 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005905 locations->SetInAt(0, Location::RequiresRegister());
5906 if (check->HasUses()) {
5907 locations->SetOut(Location::SameAsFirstInput());
5908 }
5909}
5910
5911void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005912 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005913 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005914 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005915 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005916 GenerateClassInitializationCheck(slow_path,
5917 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918}
5919
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005920HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5921 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005922 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005923 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005924 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005925 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005926 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005928 case HLoadString::LoadKind::kJitTableAddress:
5929 DCHECK(Runtime::Current()->UseJitCompilation());
5930 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005931 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005932 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005933 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 }
5935 return desired_string_load_kind;
5936}
5937
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005938void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005939 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005940 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005941 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005942 locations->SetOut(Location::RegisterLocation(RAX));
5943 } else {
5944 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005945 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5946 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005947 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005948 // Custom calling convention: RAX serves as both input and output.
5949 RegisterSet caller_saves = RegisterSet::Empty();
5950 caller_saves.Add(Location::RegisterLocation(RAX));
5951 locations->SetCustomSlowPathCallerSaves(caller_saves);
5952 } else {
5953 // For non-Baker read barrier we have a temp-clobbering call.
5954 }
5955 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005956 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005957}
5958
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005959Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005960 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005961 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005962 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005963 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005964 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005965 PatchInfo<Label>* info = &jit_string_patches_.back();
5966 return &info->label;
5967}
5968
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005969// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5970// move.
5971void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005972 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005973 Location out_loc = locations->Out();
5974 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976 switch (load->GetLoadKind()) {
5977 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005978 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005980 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005981 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 }
5983 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005984 uint32_t address = dchecked_integral_cast<uint32_t>(
5985 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5986 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005987 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005988 return;
5989 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005990 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005991 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5992 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005993 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005994 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005995 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005996 case HLoadString::LoadKind::kBssEntry: {
5997 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5998 /* no_rip */ false);
5999 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6000 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006001 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006002 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006003 codegen_->AddSlowPath(slow_path);
6004 __ testl(out, out);
6005 __ j(kEqual, slow_path->GetEntryLabel());
6006 __ Bind(slow_path->GetExitLabel());
6007 return;
6008 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006009 case HLoadString::LoadKind::kJitTableAddress: {
6010 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6011 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006012 Label* fixup_label = codegen_->NewJitRootStringPatch(
6013 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006014 // /* GcRoot<mirror::String> */ out = *address
6015 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6016 return;
6017 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006018 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006019 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006021
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006022 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006023 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006024 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006025 codegen_->InvokeRuntime(kQuickResolveString,
6026 load,
6027 load->GetDexPc());
6028 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006029}
6030
David Brazdilcb1c0552015-08-04 16:22:25 +01006031static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006032 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006033 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01006034}
6035
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006036void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
6037 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006038 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006039 locations->SetOut(Location::RequiresRegister());
6040}
6041
6042void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006043 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
6044}
6045
6046void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006047 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006048}
6049
6050void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6051 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006052}
6053
6054void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006055 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6056 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006057 InvokeRuntimeCallingConvention calling_convention;
6058 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6059}
6060
6061void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006062 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006063 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006064}
6065
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006066// Temp is used for read barrier.
6067static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6068 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006069 !kUseBakerReadBarrier &&
6070 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006071 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006072 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6073 return 1;
6074 }
6075 return 0;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006076}
6077
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006078// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6079// interface pointer, the current interface is compared in memory.
6080// The other checks have one temp for loading the object's class.
6081static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6082 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6083 return 2;
6084 }
6085 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006086}
6087
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006088void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006090 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006091 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006092 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 case TypeCheckKind::kExactCheck:
6094 case TypeCheckKind::kAbstractClassCheck:
6095 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006096 case TypeCheckKind::kArrayObjectCheck: {
6097 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6098 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6099 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006101 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103 case TypeCheckKind::kUnresolvedCheck:
6104 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 call_kind = LocationSummary::kCallOnSlowPath;
6106 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006107 case TypeCheckKind::kBitstringCheck:
6108 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110
Vladimir Markoca6fff82017-10-03 14:49:14 +01006111 LocationSummary* locations =
6112 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006113 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006114 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006115 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006116 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006117 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6118 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6119 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6120 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6121 } else {
6122 locations->SetInAt(1, Location::Any());
6123 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
6125 locations->SetOut(Location::RequiresRegister());
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006126 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006127}
6128
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006129void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006130 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006131 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006132 Location obj_loc = locations->InAt(0);
6133 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006134 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 Location out_loc = locations->Out();
6136 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006137 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6138 DCHECK_LE(num_temps, 1u);
6139 Location maybe_temp_loc = (num_temps >= 1u) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006140 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6142 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6143 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006144 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006146
6147 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006148 // Avoid null check if we know obj is not null.
6149 if (instruction->MustDoNullCheck()) {
6150 __ testl(obj, obj);
6151 __ j(kEqual, &zero);
6152 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006154 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006156 ReadBarrierOption read_barrier_option =
6157 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006158 // /* HeapReference<Class> */ out = obj->klass_
6159 GenerateReferenceLoadTwoRegisters(instruction,
6160 out_loc,
6161 obj_loc,
6162 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006163 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 if (cls.IsRegister()) {
6165 __ cmpl(out, cls.AsRegister<CpuRegister>());
6166 } else {
6167 DCHECK(cls.IsStackSlot()) << cls;
6168 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6169 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006170 if (zero.IsLinked()) {
6171 // Classes must be equal for the instanceof to succeed.
6172 __ j(kNotEqual, &zero);
6173 __ movl(out, Immediate(1));
6174 __ jmp(&done);
6175 } else {
6176 __ setcc(kEqual, out);
6177 // setcc only sets the low byte.
6178 __ andl(out, Immediate(1));
6179 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180 break;
6181 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006184 ReadBarrierOption read_barrier_option =
6185 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006186 // /* HeapReference<Class> */ out = obj->klass_
6187 GenerateReferenceLoadTwoRegisters(instruction,
6188 out_loc,
6189 obj_loc,
6190 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006191 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 // If the class is abstract, we eagerly fetch the super class of the
6193 // object to avoid doing a comparison we know will fail.
6194 NearLabel loop, success;
6195 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006197 GenerateReferenceLoadOneRegister(instruction,
6198 out_loc,
6199 super_offset,
6200 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006201 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 __ testl(out, out);
6203 // If `out` is null, we use it for the result, and jump to `done`.
6204 __ j(kEqual, &done);
6205 if (cls.IsRegister()) {
6206 __ cmpl(out, cls.AsRegister<CpuRegister>());
6207 } else {
6208 DCHECK(cls.IsStackSlot()) << cls;
6209 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6210 }
6211 __ j(kNotEqual, &loop);
6212 __ movl(out, Immediate(1));
6213 if (zero.IsLinked()) {
6214 __ jmp(&done);
6215 }
6216 break;
6217 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006219 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006220 ReadBarrierOption read_barrier_option =
6221 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006222 // /* HeapReference<Class> */ out = obj->klass_
6223 GenerateReferenceLoadTwoRegisters(instruction,
6224 out_loc,
6225 obj_loc,
6226 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006227 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 // Walk over the class hierarchy to find a match.
6229 NearLabel loop, success;
6230 __ Bind(&loop);
6231 if (cls.IsRegister()) {
6232 __ cmpl(out, cls.AsRegister<CpuRegister>());
6233 } else {
6234 DCHECK(cls.IsStackSlot()) << cls;
6235 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6236 }
6237 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006238 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006239 GenerateReferenceLoadOneRegister(instruction,
6240 out_loc,
6241 super_offset,
6242 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006243 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 __ testl(out, out);
6245 __ j(kNotEqual, &loop);
6246 // If `out` is null, we use it for the result, and jump to `done`.
6247 __ jmp(&done);
6248 __ Bind(&success);
6249 __ movl(out, Immediate(1));
6250 if (zero.IsLinked()) {
6251 __ jmp(&done);
6252 }
6253 break;
6254 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006257 ReadBarrierOption read_barrier_option =
6258 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006259 // /* HeapReference<Class> */ out = obj->klass_
6260 GenerateReferenceLoadTwoRegisters(instruction,
6261 out_loc,
6262 obj_loc,
6263 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006264 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006265 // Do an exact check.
6266 NearLabel exact_check;
6267 if (cls.IsRegister()) {
6268 __ cmpl(out, cls.AsRegister<CpuRegister>());
6269 } else {
6270 DCHECK(cls.IsStackSlot()) << cls;
6271 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6272 }
6273 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006276 GenerateReferenceLoadOneRegister(instruction,
6277 out_loc,
6278 component_offset,
6279 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006280 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006281 __ testl(out, out);
6282 // If `out` is null, we use it for the result, and jump to `done`.
6283 __ j(kEqual, &done);
6284 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6285 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006286 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 __ movl(out, Immediate(1));
6288 __ jmp(&done);
6289 break;
6290 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006293 // No read barrier since the slow path will retry upon failure.
6294 // /* HeapReference<Class> */ out = obj->klass_
6295 GenerateReferenceLoadTwoRegisters(instruction,
6296 out_loc,
6297 obj_loc,
6298 class_offset,
6299 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 if (cls.IsRegister()) {
6301 __ cmpl(out, cls.AsRegister<CpuRegister>());
6302 } else {
6303 DCHECK(cls.IsStackSlot()) << cls;
6304 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6305 }
6306 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006307 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6308 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309 codegen_->AddSlowPath(slow_path);
6310 __ j(kNotEqual, slow_path->GetEntryLabel());
6311 __ movl(out, Immediate(1));
6312 if (zero.IsLinked()) {
6313 __ jmp(&done);
6314 }
6315 break;
6316 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006317
Calin Juravle98893e12015-10-02 21:05:03 +01006318 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319 case TypeCheckKind::kInterfaceCheck: {
6320 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006321 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006322 // cases.
6323 //
6324 // We cannot directly call the InstanceofNonTrivial runtime
6325 // entry point without resorting to a type checking slow path
6326 // here (i.e. by calling InvokeRuntime directly), as it would
6327 // require to assign fixed registers for the inputs of this
6328 // HInstanceOf instruction (following the runtime calling
6329 // convention), which might be cluttered by the potential first
6330 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006331 //
6332 // TODO: Introduce a new runtime entry point taking the object
6333 // to test (instead of its class) as argument, and let it deal
6334 // with the read barrier issues. This will let us refactor this
6335 // case of the `switch` code as it was previously (with a direct
6336 // call to the runtime not using a type checking slow path).
6337 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006339 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6340 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 codegen_->AddSlowPath(slow_path);
6342 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006343 if (zero.IsLinked()) {
6344 __ jmp(&done);
6345 }
6346 break;
6347 }
Vladimir Marko175e7862018-03-27 09:03:13 +00006348
6349 case TypeCheckKind::kBitstringCheck: {
6350 // /* HeapReference<Class> */ temp = obj->klass_
6351 GenerateReferenceLoadTwoRegisters(instruction,
6352 out_loc,
6353 obj_loc,
6354 class_offset,
6355 kWithoutReadBarrier);
6356
6357 GenerateBitstringTypeCheckCompare(instruction, out);
6358 if (zero.IsLinked()) {
6359 __ j(kNotEqual, &zero);
6360 __ movl(out, Immediate(1));
6361 __ jmp(&done);
6362 } else {
6363 __ setcc(kEqual, out);
6364 // setcc only sets the low byte.
6365 __ andl(out, Immediate(1));
6366 }
6367 break;
6368 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006369 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006372 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006373 __ xorl(out, out);
6374 }
6375
6376 if (done.IsLinked()) {
6377 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006378 }
6379
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006380 if (slow_path != nullptr) {
6381 __ Bind(slow_path->GetExitLabel());
6382 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006383}
6384
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006385void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006386 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006387 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006388 LocationSummary* locations =
6389 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006390 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006391 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6392 // Require a register for the interface check since there is a loop that compares the class to
6393 // a memory address.
6394 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006395 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6396 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6397 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6398 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006399 } else {
6400 locations->SetInAt(1, Location::Any());
6401 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006402 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
6403 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006404}
6405
6406void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006407 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006408 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 Location obj_loc = locations->InAt(0);
6410 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006411 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 Location temp_loc = locations->GetTemp(0);
6413 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006414 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6415 DCHECK_GE(num_temps, 1u);
6416 DCHECK_LE(num_temps, 2u);
6417 Location maybe_temp2_loc = (num_temps >= 2u) ? locations->GetTemp(1) : Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006418 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6419 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6420 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6421 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6422 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6423 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006424 const uint32_t object_array_data_offset =
6425 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426
Vladimir Marko87584542017-12-12 17:47:52 +00006427 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006429 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6430 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006433
6434 NearLabel done;
6435 // Avoid null check if we know obj is not null.
6436 if (instruction->MustDoNullCheck()) {
6437 __ testl(obj, obj);
6438 __ j(kEqual, &done);
6439 }
6440
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 case TypeCheckKind::kExactCheck:
6443 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006444 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006445 GenerateReferenceLoadTwoRegisters(instruction,
6446 temp_loc,
6447 obj_loc,
6448 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006449 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 if (cls.IsRegister()) {
6451 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6452 } else {
6453 DCHECK(cls.IsStackSlot()) << cls;
6454 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6455 }
6456 // Jump to slow path for throwing the exception or doing a
6457 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 break;
6460 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006463 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006464 GenerateReferenceLoadTwoRegisters(instruction,
6465 temp_loc,
6466 obj_loc,
6467 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006468 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 // If the class is abstract, we eagerly fetch the super class of the
6470 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006471 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006474 GenerateReferenceLoadOneRegister(instruction,
6475 temp_loc,
6476 super_offset,
6477 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006478 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006480 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6481 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006483 // Otherwise, compare the classes.
6484 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 if (cls.IsRegister()) {
6486 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6487 } else {
6488 DCHECK(cls.IsStackSlot()) << cls;
6489 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6490 }
6491 __ j(kNotEqual, &loop);
6492 break;
6493 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006495 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006496 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006497 GenerateReferenceLoadTwoRegisters(instruction,
6498 temp_loc,
6499 obj_loc,
6500 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006501 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006503 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 __ Bind(&loop);
6505 if (cls.IsRegister()) {
6506 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6507 } else {
6508 DCHECK(cls.IsStackSlot()) << cls;
6509 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6510 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006511 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006512
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006514 GenerateReferenceLoadOneRegister(instruction,
6515 temp_loc,
6516 super_offset,
6517 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006518 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006519
6520 // If the class reference currently in `temp` is not null, jump
6521 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006523 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006526 break;
6527 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006530 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006531 GenerateReferenceLoadTwoRegisters(instruction,
6532 temp_loc,
6533 obj_loc,
6534 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006535 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006536 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006538 if (cls.IsRegister()) {
6539 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6540 } else {
6541 DCHECK(cls.IsStackSlot()) << cls;
6542 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6543 }
6544 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006545
6546 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006548 GenerateReferenceLoadOneRegister(instruction,
6549 temp_loc,
6550 component_offset,
6551 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006552 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553
6554 // If the component type is not null (i.e. the object is indeed
6555 // an array), jump to label `check_non_primitive_component_type`
6556 // to further check that this component type is not a primitive
6557 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006560 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006562 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006563 break;
6564 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006565
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006566 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006567 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568 //
6569 // We cannot directly call the CheckCast runtime entry point
6570 // without resorting to a type checking slow path here (i.e. by
6571 // calling InvokeRuntime directly), as it would require to
6572 // assign fixed registers for the inputs of this HInstanceOf
6573 // instruction (following the runtime calling convention), which
6574 // might be cluttered by the potential first read barrier
6575 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006576 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006577 break;
6578 }
6579
Vladimir Marko175e7862018-03-27 09:03:13 +00006580 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006581 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6582 // We can not get false positives by doing this.
6583 // /* HeapReference<Class> */ temp = obj->klass_
6584 GenerateReferenceLoadTwoRegisters(instruction,
6585 temp_loc,
6586 obj_loc,
6587 class_offset,
6588 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006589
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006590 // /* HeapReference<Class> */ temp = temp->iftable_
6591 GenerateReferenceLoadTwoRegisters(instruction,
6592 temp_loc,
6593 temp_loc,
6594 iftable_offset,
6595 kWithoutReadBarrier);
6596 // Iftable is never null.
6597 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6598 // Maybe poison the `cls` for direct comparison with memory.
6599 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6600 // Loop through the iftable and check if any class matches.
6601 NearLabel start_loop;
6602 __ Bind(&start_loop);
6603 // Need to subtract first to handle the empty array case.
6604 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6605 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6606 // Go to next interface if the classes do not match.
6607 __ cmpl(cls.AsRegister<CpuRegister>(),
6608 CodeGeneratorX86_64::ArrayAddress(temp,
6609 maybe_temp2_loc,
6610 TIMES_4,
6611 object_array_data_offset));
6612 __ j(kNotEqual, &start_loop); // Return if same class.
6613 // If `cls` was poisoned above, unpoison it.
6614 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006616 }
6617
6618 case TypeCheckKind::kBitstringCheck: {
6619 // /* HeapReference<Class> */ temp = obj->klass_
6620 GenerateReferenceLoadTwoRegisters(instruction,
6621 temp_loc,
6622 obj_loc,
6623 class_offset,
6624 kWithoutReadBarrier);
6625
6626 GenerateBitstringTypeCheckCompare(instruction, temp);
6627 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
6628 break;
6629 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006630 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006631
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006632 if (done.IsLinked()) {
6633 __ Bind(&done);
6634 }
6635
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006637}
6638
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006639void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006640 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6641 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006642 InvokeRuntimeCallingConvention calling_convention;
6643 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6644}
6645
6646void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006647 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006648 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006649 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006650 if (instruction->IsEnter()) {
6651 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6652 } else {
6653 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6654 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006655}
6656
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006657void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6658void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6659void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6660
6661void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6662 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006663 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006664 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6665 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006666 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006667 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006668 locations->SetOut(Location::SameAsFirstInput());
6669}
6670
6671void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6672 HandleBitwiseOperation(instruction);
6673}
6674
6675void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6676 HandleBitwiseOperation(instruction);
6677}
6678
6679void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6680 HandleBitwiseOperation(instruction);
6681}
6682
6683void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6684 LocationSummary* locations = instruction->GetLocations();
6685 Location first = locations->InAt(0);
6686 Location second = locations->InAt(1);
6687 DCHECK(first.Equals(locations->Out()));
6688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006689 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006690 if (second.IsRegister()) {
6691 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006692 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006693 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006694 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006695 } else {
6696 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006697 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006698 }
6699 } else if (second.IsConstant()) {
6700 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6701 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006702 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006703 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006704 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006705 } else {
6706 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006707 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006708 }
6709 } else {
6710 Address address(CpuRegister(RSP), second.GetStackIndex());
6711 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006712 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006713 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006714 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006715 } else {
6716 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006717 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006718 }
6719 }
6720 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006721 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006722 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6723 bool second_is_constant = false;
6724 int64_t value = 0;
6725 if (second.IsConstant()) {
6726 second_is_constant = true;
6727 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006728 }
Mark Mendell40741f32015-04-20 22:10:34 -04006729 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006730
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006731 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006732 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006733 if (is_int32_value) {
6734 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6735 } else {
6736 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6737 }
6738 } else if (second.IsDoubleStackSlot()) {
6739 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006740 } else {
6741 __ andq(first_reg, second.AsRegister<CpuRegister>());
6742 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006743 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006744 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006745 if (is_int32_value) {
6746 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6747 } else {
6748 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6749 }
6750 } else if (second.IsDoubleStackSlot()) {
6751 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006752 } else {
6753 __ orq(first_reg, second.AsRegister<CpuRegister>());
6754 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006755 } else {
6756 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006757 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006758 if (is_int32_value) {
6759 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6760 } else {
6761 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6762 }
6763 } else if (second.IsDoubleStackSlot()) {
6764 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006765 } else {
6766 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6767 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006768 }
6769 }
6770}
6771
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006772void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6773 HInstruction* instruction,
6774 Location out,
6775 uint32_t offset,
6776 Location maybe_temp,
6777 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006778 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006779 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006780 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006781 if (kUseBakerReadBarrier) {
6782 // Load with fast path based Baker's read barrier.
6783 // /* HeapReference<Object> */ out = *(out + offset)
6784 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006785 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006786 } else {
6787 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006788 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006789 // in the following move operation, as we will need it for the
6790 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006791 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006792 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006793 // /* HeapReference<Object> */ out = *(out + offset)
6794 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006795 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006796 }
6797 } else {
6798 // Plain load with no read barrier.
6799 // /* HeapReference<Object> */ out = *(out + offset)
6800 __ movl(out_reg, Address(out_reg, offset));
6801 __ MaybeUnpoisonHeapReference(out_reg);
6802 }
6803}
6804
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006805void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6806 HInstruction* instruction,
6807 Location out,
6808 Location obj,
6809 uint32_t offset,
6810 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006811 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6812 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006813 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006814 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006815 if (kUseBakerReadBarrier) {
6816 // Load with fast path based Baker's read barrier.
6817 // /* HeapReference<Object> */ out = *(obj + offset)
6818 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006819 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006820 } else {
6821 // Load with slow path based read barrier.
6822 // /* HeapReference<Object> */ out = *(obj + offset)
6823 __ movl(out_reg, Address(obj_reg, offset));
6824 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6825 }
6826 } else {
6827 // Plain load with no read barrier.
6828 // /* HeapReference<Object> */ out = *(obj + offset)
6829 __ movl(out_reg, Address(obj_reg, offset));
6830 __ MaybeUnpoisonHeapReference(out_reg);
6831 }
6832}
6833
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006834void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6835 HInstruction* instruction,
6836 Location root,
6837 const Address& address,
6838 Label* fixup_label,
6839 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006840 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006841 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006842 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006843 if (kUseBakerReadBarrier) {
6844 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6845 // Baker's read barrier are used:
6846 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006847 // root = obj.field;
6848 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6849 // if (temp != null) {
6850 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006851 // }
6852
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006853 // /* GcRoot<mirror::Object> */ root = *address
6854 __ movl(root_reg, address);
6855 if (fixup_label != nullptr) {
6856 __ Bind(fixup_label);
6857 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006858 static_assert(
6859 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6860 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6861 "have different sizes.");
6862 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6863 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6864 "have different sizes.");
6865
Vladimir Marko953437b2016-08-24 08:30:46 +00006866 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006867 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006868 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006869 codegen_->AddSlowPath(slow_path);
6870
Roland Levillaind966ce72017-02-09 16:20:14 +00006871 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6872 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006873 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006874 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6875 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006876 __ j(kNotEqual, slow_path->GetEntryLabel());
6877 __ Bind(slow_path->GetExitLabel());
6878 } else {
6879 // GC root loaded through a slow path for read barriers other
6880 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006881 // /* GcRoot<mirror::Object>* */ root = address
6882 __ leaq(root_reg, address);
6883 if (fixup_label != nullptr) {
6884 __ Bind(fixup_label);
6885 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006886 // /* mirror::Object* */ root = root->Read()
6887 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6888 }
6889 } else {
6890 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006891 // /* GcRoot<mirror::Object> */ root = *address
6892 __ movl(root_reg, address);
6893 if (fixup_label != nullptr) {
6894 __ Bind(fixup_label);
6895 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006896 // Note that GC roots are not affected by heap poisoning, thus we
6897 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006898 }
6899}
6900
6901void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6902 Location ref,
6903 CpuRegister obj,
6904 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006905 bool needs_null_check) {
6906 DCHECK(kEmitCompilerReadBarrier);
6907 DCHECK(kUseBakerReadBarrier);
6908
6909 // /* HeapReference<Object> */ ref = *(obj + offset)
6910 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006911 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006912}
6913
6914void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6915 Location ref,
6916 CpuRegister obj,
6917 uint32_t data_offset,
6918 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006919 bool needs_null_check) {
6920 DCHECK(kEmitCompilerReadBarrier);
6921 DCHECK(kUseBakerReadBarrier);
6922
Roland Levillain3d312422016-06-23 13:53:42 +01006923 static_assert(
6924 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6925 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006926 // /* HeapReference<Object> */ ref =
6927 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006928 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006929 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006930}
6931
6932void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6933 Location ref,
6934 CpuRegister obj,
6935 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006936 bool needs_null_check,
6937 bool always_update_field,
6938 CpuRegister* temp1,
6939 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006940 DCHECK(kEmitCompilerReadBarrier);
6941 DCHECK(kUseBakerReadBarrier);
6942
6943 // In slow path based read barriers, the read barrier call is
6944 // inserted after the original load. However, in fast path based
6945 // Baker's read barriers, we need to perform the load of
6946 // mirror::Object::monitor_ *before* the original reference load.
6947 // This load-load ordering is required by the read barrier.
6948 // The fast path/slow path (for Baker's algorithm) should look like:
6949 //
6950 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6951 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6952 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006953 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006954 // if (is_gray) {
6955 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6956 // }
6957 //
6958 // Note: the original implementation in ReadBarrier::Barrier is
6959 // slightly more complex as:
6960 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006961 // the high-bits of rb_state, which are expected to be all zeroes
6962 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6963 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006964 // - it performs additional checks that we do not do here for
6965 // performance reasons.
6966
6967 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006968 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6969
Vladimir Marko953437b2016-08-24 08:30:46 +00006970 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006971 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6972 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006973 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6974 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6975 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6976
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006977 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006978 // ref = ReadBarrier::Mark(ref);
6979 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6980 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006981 if (needs_null_check) {
6982 MaybeRecordImplicitNullCheck(instruction);
6983 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006984
6985 // Load fence to prevent load-load reordering.
6986 // Note that this is a no-op, thanks to the x86-64 memory model.
6987 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6988
6989 // The actual reference load.
6990 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006991 __ movl(ref_reg, src); // Flags are unaffected.
6992
6993 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6994 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006995 SlowPathCode* slow_path;
6996 if (always_update_field) {
6997 DCHECK(temp1 != nullptr);
6998 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006999 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007000 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
7001 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007002 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007003 instruction, ref, /* unpoison_ref_before_marking */ true);
7004 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007005 AddSlowPath(slow_path);
7006
7007 // We have done the "if" of the gray bit check above, now branch based on the flags.
7008 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007009
7010 // Object* ref = ref_addr->AsMirrorPtr()
7011 __ MaybeUnpoisonHeapReference(ref_reg);
7012
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007013 __ Bind(slow_path->GetExitLabel());
7014}
7015
7016void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
7017 Location out,
7018 Location ref,
7019 Location obj,
7020 uint32_t offset,
7021 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007022 DCHECK(kEmitCompilerReadBarrier);
7023
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007024 // Insert a slow path based read barrier *after* the reference load.
7025 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007026 // If heap poisoning is enabled, the unpoisoning of the loaded
7027 // reference will be carried out by the runtime within the slow
7028 // path.
7029 //
7030 // Note that `ref` currently does not get unpoisoned (when heap
7031 // poisoning is enabled), which is alright as the `ref` argument is
7032 // not used by the artReadBarrierSlow entry point.
7033 //
7034 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007035 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007036 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
7037 AddSlowPath(slow_path);
7038
Roland Levillain0d5a2812015-11-13 10:07:31 +00007039 __ jmp(slow_path->GetEntryLabel());
7040 __ Bind(slow_path->GetExitLabel());
7041}
7042
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007043void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7044 Location out,
7045 Location ref,
7046 Location obj,
7047 uint32_t offset,
7048 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007049 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007050 // Baker's read barriers shall be handled by the fast path
7051 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
7052 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007053 // If heap poisoning is enabled, unpoisoning will be taken care of
7054 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007055 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007056 } else if (kPoisonHeapReferences) {
7057 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
7058 }
7059}
7060
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007061void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7062 Location out,
7063 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007064 DCHECK(kEmitCompilerReadBarrier);
7065
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007066 // Insert a slow path based read barrier *after* the GC root load.
7067 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007068 // Note that GC roots are not affected by heap poisoning, so we do
7069 // not need to do anything special for this here.
7070 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007071 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007072 AddSlowPath(slow_path);
7073
Roland Levillain0d5a2812015-11-13 10:07:31 +00007074 __ jmp(slow_path->GetEntryLabel());
7075 __ Bind(slow_path->GetExitLabel());
7076}
7077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007078void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007079 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007080 LOG(FATAL) << "Unreachable";
7081}
7082
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007083void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007084 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007085 LOG(FATAL) << "Unreachable";
7086}
7087
Mark Mendellfe57faa2015-09-18 09:26:15 -04007088// Simple implementation of packed switch - generate cascaded compare/jumps.
7089void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7090 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007091 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007092 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04007093 locations->AddTemp(Location::RequiresRegister());
7094 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04007095}
7096
7097void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7098 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007099 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007100 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04007101 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
7102 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
7103 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007104 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7105
7106 // Should we generate smaller inline compare/jumps?
7107 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7108 // Figure out the correct compare values and jump conditions.
7109 // Handle the first compare/branch as a special case because it might
7110 // jump to the default case.
7111 DCHECK_GT(num_entries, 2u);
7112 Condition first_condition;
7113 uint32_t index;
7114 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7115 if (lower_bound != 0) {
7116 first_condition = kLess;
7117 __ cmpl(value_reg_in, Immediate(lower_bound));
7118 __ j(first_condition, codegen_->GetLabelOf(default_block));
7119 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
7120
7121 index = 1;
7122 } else {
7123 // Handle all the compare/jumps below.
7124 first_condition = kBelow;
7125 index = 0;
7126 }
7127
7128 // Handle the rest of the compare/jumps.
7129 for (; index + 1 < num_entries; index += 2) {
7130 int32_t compare_to_value = lower_bound + index + 1;
7131 __ cmpl(value_reg_in, Immediate(compare_to_value));
7132 // Jump to successors[index] if value < case_value[index].
7133 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7134 // Jump to successors[index + 1] if value == case_value[index + 1].
7135 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7136 }
7137
7138 if (index != num_entries) {
7139 // There are an odd number of entries. Handle the last one.
7140 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00007141 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007142 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
7143 }
7144
7145 // And the default for any other value.
7146 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7147 __ jmp(codegen_->GetLabelOf(default_block));
7148 }
7149 return;
7150 }
Mark Mendell9c86b482015-09-18 13:36:07 -04007151
7152 // Remove the bias, if needed.
7153 Register value_reg_out = value_reg_in.AsRegister();
7154 if (lower_bound != 0) {
7155 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
7156 value_reg_out = temp_reg.AsRegister();
7157 }
7158 CpuRegister value_reg(value_reg_out);
7159
7160 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04007161 __ cmpl(value_reg, Immediate(num_entries - 1));
7162 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007163
Mark Mendell9c86b482015-09-18 13:36:07 -04007164 // We are in the range of the table.
7165 // Load the address of the jump table in the constant area.
7166 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007167
Mark Mendell9c86b482015-09-18 13:36:07 -04007168 // Load the (signed) offset from the jump table.
7169 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
7170
7171 // Add the offset to the address of the table base.
7172 __ addq(temp_reg, base_reg);
7173
7174 // And jump.
7175 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007176}
7177
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007178void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7179 ATTRIBUTE_UNUSED) {
7180 LOG(FATAL) << "Unreachable";
7181}
7182
7183void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7184 ATTRIBUTE_UNUSED) {
7185 LOG(FATAL) << "Unreachable";
7186}
7187
Aart Bikc5d47542016-01-27 17:00:35 -08007188void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
7189 if (value == 0) {
7190 __ xorl(dest, dest);
7191 } else {
7192 __ movl(dest, Immediate(value));
7193 }
7194}
7195
Mark Mendell92e83bf2015-05-07 11:25:03 -04007196void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
7197 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08007198 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007199 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00007200 } else if (IsUint<32>(value)) {
7201 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007202 __ movl(dest, Immediate(static_cast<int32_t>(value)));
7203 } else {
7204 __ movq(dest, Immediate(value));
7205 }
7206}
7207
Mark Mendell7c0b44f2016-02-01 10:08:35 -05007208void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
7209 if (value == 0) {
7210 __ xorps(dest, dest);
7211 } else {
7212 __ movss(dest, LiteralInt32Address(value));
7213 }
7214}
7215
7216void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
7217 if (value == 0) {
7218 __ xorpd(dest, dest);
7219 } else {
7220 __ movsd(dest, LiteralInt64Address(value));
7221 }
7222}
7223
7224void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
7225 Load32BitValue(dest, bit_cast<int32_t, float>(value));
7226}
7227
7228void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
7229 Load64BitValue(dest, bit_cast<int64_t, double>(value));
7230}
7231
Aart Bika19616e2016-02-01 18:57:58 -08007232void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
7233 if (value == 0) {
7234 __ testl(dest, dest);
7235 } else {
7236 __ cmpl(dest, Immediate(value));
7237 }
7238}
7239
7240void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
7241 if (IsInt<32>(value)) {
7242 if (value == 0) {
7243 __ testq(dest, dest);
7244 } else {
7245 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7246 }
7247 } else {
7248 // Value won't fit in an int.
7249 __ cmpq(dest, LiteralInt64Address(value));
7250 }
7251}
7252
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007253void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7254 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007255 GenerateIntCompare(lhs_reg, rhs);
7256}
7257
7258void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007259 if (rhs.IsConstant()) {
7260 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007261 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007262 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007263 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007264 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007265 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007266 }
7267}
7268
7269void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7270 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7271 if (rhs.IsConstant()) {
7272 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7273 Compare64BitValue(lhs_reg, value);
7274 } else if (rhs.IsDoubleStackSlot()) {
7275 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7276 } else {
7277 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7278 }
7279}
7280
7281Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7282 Location index,
7283 ScaleFactor scale,
7284 uint32_t data_offset) {
7285 return index.IsConstant() ?
7286 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7287 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7288}
7289
Mark Mendellcfa410b2015-05-25 16:02:44 -04007290void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7291 DCHECK(dest.IsDoubleStackSlot());
7292 if (IsInt<32>(value)) {
7293 // Can move directly as an int32 constant.
7294 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7295 Immediate(static_cast<int32_t>(value)));
7296 } else {
7297 Load64BitValue(CpuRegister(TMP), value);
7298 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7299 }
7300}
7301
Mark Mendell9c86b482015-09-18 13:36:07 -04007302/**
7303 * Class to handle late fixup of offsets into constant area.
7304 */
7305class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7306 public:
7307 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7308 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7309
7310 protected:
7311 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7312
7313 CodeGeneratorX86_64* codegen_;
7314
7315 private:
7316 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7317 // Patch the correct offset for the instruction. We use the address of the
7318 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7319 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7320 int32_t relative_position = constant_offset - pos;
7321
7322 // Patch in the right value.
7323 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7324 }
7325
7326 // Location in constant area that the fixup refers to.
7327 size_t offset_into_constant_area_;
7328};
7329
7330/**
7331 t * Class to handle late fixup of offsets to a jump table that will be created in the
7332 * constant area.
7333 */
7334class JumpTableRIPFixup : public RIPFixup {
7335 public:
7336 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7337 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7338
7339 void CreateJumpTable() {
7340 X86_64Assembler* assembler = codegen_->GetAssembler();
7341
7342 // Ensure that the reference to the jump table has the correct offset.
7343 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7344 SetOffset(offset_in_constant_table);
7345
7346 // Compute the offset from the start of the function to this jump table.
7347 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7348
7349 // Populate the jump table with the correct values for the jump table.
7350 int32_t num_entries = switch_instr_->GetNumEntries();
7351 HBasicBlock* block = switch_instr_->GetBlock();
7352 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7353 // The value that we want is the target offset - the position of the table.
7354 for (int32_t i = 0; i < num_entries; i++) {
7355 HBasicBlock* b = successors[i];
7356 Label* l = codegen_->GetLabelOf(b);
7357 DCHECK(l->IsBound());
7358 int32_t offset_to_block = l->Position() - current_table_offset;
7359 assembler->AppendInt32(offset_to_block);
7360 }
7361 }
7362
7363 private:
7364 const HPackedSwitch* switch_instr_;
7365};
7366
Mark Mendellf55c3e02015-03-26 21:07:46 -04007367void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7368 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007369 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007370 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7371 // 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 -04007372 assembler->Align(4, 0);
7373 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007374
7375 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007376 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007377 jump_table->CreateJumpTable();
7378 }
7379
7380 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007381 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007382 }
7383
7384 // And finish up.
7385 CodeGenerator::Finalize(allocator);
7386}
7387
Mark Mendellf55c3e02015-03-26 21:07:46 -04007388Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007389 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007390 return Address::RIP(fixup);
7391}
7392
7393Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007394 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007395 return Address::RIP(fixup);
7396}
7397
7398Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007399 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007400 return Address::RIP(fixup);
7401}
7402
7403Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007404 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007405 return Address::RIP(fixup);
7406}
7407
Andreas Gampe85b62f22015-09-09 13:15:38 -07007408// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007409void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007410 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007411 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007412 return;
7413 }
7414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007415 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007416
7417 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7418 if (trg.Equals(return_loc)) {
7419 return;
7420 }
7421
7422 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007423 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007424 parallel_move.AddMove(return_loc, trg, type, nullptr);
7425 GetMoveResolver()->EmitNativeCode(&parallel_move);
7426}
7427
Mark Mendell9c86b482015-09-18 13:36:07 -04007428Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7429 // Create a fixup to be used to create and address the jump table.
7430 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007431 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007432
7433 // We have to populate the jump tables.
7434 fixups_to_jump_tables_.push_back(table_fixup);
7435 return Address::RIP(table_fixup);
7436}
7437
Mark Mendellea5af682015-10-22 17:35:49 -04007438void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7439 const Address& addr_high,
7440 int64_t v,
7441 HInstruction* instruction) {
7442 if (IsInt<32>(v)) {
7443 int32_t v_32 = v;
7444 __ movq(addr_low, Immediate(v_32));
7445 MaybeRecordImplicitNullCheck(instruction);
7446 } else {
7447 // Didn't fit in a register. Do it in pieces.
7448 int32_t low_v = Low32Bits(v);
7449 int32_t high_v = High32Bits(v);
7450 __ movl(addr_low, Immediate(low_v));
7451 MaybeRecordImplicitNullCheck(instruction);
7452 __ movl(addr_high, Immediate(high_v));
7453 }
7454}
7455
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007456void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7457 const uint8_t* roots_data,
7458 const PatchInfo<Label>& info,
7459 uint64_t index_in_table) const {
7460 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7461 uintptr_t address =
7462 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7463 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7464 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7465 dchecked_integral_cast<uint32_t>(address);
7466}
7467
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007468void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7469 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007470 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007471 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007472 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007473 }
7474
7475 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007476 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007477 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007478 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007479 }
7480}
7481
Roland Levillain4d027112015-07-01 15:41:14 +01007482#undef __
7483
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007484} // namespace x86_64
7485} // namespace art