blob: ae35ab5983d28c2e77816e149bd8fe87f2290aa3 [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));
996 RecordBootMethodPatch(invoke);
997 break;
Vladimir Marko58155012015-08-19 12:49:41 +0000998 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +0000999 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +00001000 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001001 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001002 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001003 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001004 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001005 __ Bind(NewMethodBssEntryPatch(
1006 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko58155012015-08-19 12:49:41 +00001007 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001008 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001009 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1010 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1011 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001012 }
Vladimir Marko58155012015-08-19 12:49:41 +00001013 }
1014
1015 switch (invoke->GetCodePtrLocation()) {
1016 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1017 __ call(&frame_entry_label_);
1018 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001019 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1020 // (callee_method + offset_of_quick_compiled_code)()
1021 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1022 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001023 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001024 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001025 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001026 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001027
1028 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001029}
1030
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001031void CodeGeneratorX86_64::GenerateVirtualCall(
1032 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001033 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1034 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1035 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001036
1037 // Use the calling convention instead of the location of the receiver, as
1038 // intrinsics may have put the receiver in a different register. In the intrinsics
1039 // slow path, the arguments have been moved to the right place, so here we are
1040 // guaranteed that the receiver is the first register of the calling convention.
1041 InvokeDexCallingConvention calling_convention;
1042 Register receiver = calling_convention.GetRegisterAt(0);
1043
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001044 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001045 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001046 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001047 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001048 // Instead of simply (possibly) unpoisoning `temp` here, we should
1049 // emit a read barrier for the previous class reference load.
1050 // However this is not required in practice, as this is an
1051 // intermediate/temporary reference and because the current
1052 // concurrent copying collector keeps the from-space memory
1053 // intact/accessible until the end of the marking phase (the
1054 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001055 __ MaybeUnpoisonHeapReference(temp);
1056 // temp = temp->GetMethodAt(method_offset);
1057 __ movq(temp, Address(temp, method_offset));
1058 // call temp->GetEntryPoint();
1059 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001060 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001061 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001062}
1063
Vladimir Marko65979462017-05-19 17:25:12 +01001064void CodeGeneratorX86_64::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
1065 boot_image_method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001066 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001067 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001068}
1069
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001070Label* CodeGeneratorX86_64::NewMethodBssEntryPatch(MethodReference target_method) {
1071 // Add a patch entry and return the label.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001072 method_bss_entry_patches_.emplace_back(*target_method.dex_file, target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001073 return &method_bss_entry_patches_.back().label;
1074}
1075
Vladimir Marko1998cd02017-01-13 13:02:58 +00001076void CodeGeneratorX86_64::RecordBootTypePatch(HLoadClass* load_class) {
1077 boot_image_type_patches_.emplace_back(load_class->GetDexFile(),
1078 load_class->GetTypeIndex().index_);
1079 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001080}
1081
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001082Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001083 type_bss_entry_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
1084 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001085}
1086
Vladimir Marko65979462017-05-19 17:25:12 +01001087void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01001088 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
1089 __ Bind(&string_patches_.back().label);
1090}
1091
Vladimir Markoaad75c62016-10-03 08:46:48 +00001092Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1093 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001094 string_bss_entry_patches_.emplace_back(
1095 load_string->GetDexFile(), load_string->GetStringIndex().index_);
1096 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001097}
1098
Vladimir Markoaad75c62016-10-03 08:46:48 +00001099// The label points to the end of the "movl" or another instruction but the literal offset
1100// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1101constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1102
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001103template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001104inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1105 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001106 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001107 for (const PatchInfo<Label>& info : infos) {
1108 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1109 linker_patches->push_back(
1110 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1111 }
1112}
1113
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001114void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001115 DCHECK(linker_patches->empty());
1116 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001117 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001118 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001119 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001120 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001121 string_patches_.size() +
1122 string_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001123 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001124 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001125 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1126 boot_image_method_patches_, linker_patches);
1127 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1128 boot_image_type_patches_, linker_patches);
1129 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1130 string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001131 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01001132 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001133 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1134 boot_image_type_patches_, linker_patches);
1135 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1136 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001137 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001138 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1139 method_bss_entry_patches_, linker_patches);
1140 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1141 type_bss_entry_patches_, linker_patches);
1142 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1143 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001144 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001145}
1146
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001148 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149}
1150
1151void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001152 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001153}
1154
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001155size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1156 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1157 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001158}
1159
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001160size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1161 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1162 return kX86_64WordSize;
1163}
1164
1165size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001166 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001167 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001168 } else {
1169 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1170 }
1171 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001172}
1173
1174size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001175 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001176 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001177 } else {
1178 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1179 }
1180 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001181}
1182
Calin Juravle175dc732015-08-25 15:42:32 +01001183void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1184 HInstruction* instruction,
1185 uint32_t dex_pc,
1186 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001187 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001188 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1189 if (EntrypointRequiresStackMap(entrypoint)) {
1190 RecordPcInfo(instruction, dex_pc, slow_path);
1191 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001192}
1193
Roland Levillaindec8f632016-07-22 17:10:06 +01001194void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1195 HInstruction* instruction,
1196 SlowPathCode* slow_path) {
1197 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001198 GenerateInvokeRuntime(entry_point_offset);
1199}
1200
1201void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001202 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1203}
1204
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001205static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001206// Use a fake return address register to mimic Quick.
1207static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001208CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001209 const X86_64InstructionSetFeatures& isa_features,
1210 const CompilerOptions& compiler_options,
1211 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001212 : CodeGenerator(graph,
1213 kNumberOfCpuRegisters,
1214 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001215 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001216 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1217 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001218 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001219 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1220 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001221 compiler_options,
1222 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001223 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001225 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001226 move_resolver_(graph->GetAllocator(), this),
1227 assembler_(graph->GetAllocator()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001228 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001229 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001230 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1231 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1232 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1233 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1234 string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1235 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1236 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1237 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1238 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001239 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1240}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001242InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1243 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001244 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 assembler_(codegen->GetAssembler()),
1246 codegen_(codegen) {}
1247
David Brazdil58282f42016-01-14 12:45:10 +00001248void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001250 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001252 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001253 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001254}
1255
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001256static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001257 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001258}
David Srbecky9d8606d2015-04-12 09:35:32 +01001259
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001260static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001261 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001262}
1263
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001265 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001266 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001267 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001268 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001269 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001270
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001271 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1272 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1273 ArtMethod::HotnessCountOffset().Int32Value()),
1274 Immediate(1));
1275 }
1276
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001277 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001278 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1279 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001280 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001281 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001282
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001283 if (HasEmptyFrame()) {
1284 return;
1285 }
1286
Nicolas Geoffray98893962015-01-21 12:32:32 +00001287 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001288 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001289 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001290 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001291 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1292 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001293 }
1294 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001295
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001296 int adjust = GetFrameSize() - GetCoreSpillSize();
1297 __ subq(CpuRegister(RSP), Immediate(adjust));
1298 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001299 uint32_t xmm_spill_location = GetFpuSpillStart();
1300 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001301
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001302 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1303 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001304 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1305 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1306 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001307 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001308 }
1309
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001310 // Save the current method if we need it. Note that we do not
1311 // do this in HCurrentMethod, as the instruction might have been removed
1312 // in the SSA graph.
1313 if (RequiresCurrentMethod()) {
1314 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1315 CpuRegister(kMethodRegisterArgument));
1316 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001317
1318 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1319 // Initialize should_deoptimize flag to 0.
1320 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1321 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001322}
1323
1324void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001325 __ cfi().RememberState();
1326 if (!HasEmptyFrame()) {
1327 uint32_t xmm_spill_location = GetFpuSpillStart();
1328 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1329 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1330 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1331 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1332 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1333 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1334 }
1335 }
1336
1337 int adjust = GetFrameSize() - GetCoreSpillSize();
1338 __ addq(CpuRegister(RSP), Immediate(adjust));
1339 __ cfi().AdjustCFAOffset(-adjust);
1340
1341 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1342 Register reg = kCoreCalleeSaves[i];
1343 if (allocated_registers_.ContainsCoreRegister(reg)) {
1344 __ popq(CpuRegister(reg));
1345 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1346 __ cfi().Restore(DWARFReg(reg));
1347 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001348 }
1349 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001350 __ ret();
1351 __ cfi().RestoreState();
1352 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001353}
1354
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001355void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1356 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001357}
1358
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001359void CodeGeneratorX86_64::Move(Location destination, Location source) {
1360 if (source.Equals(destination)) {
1361 return;
1362 }
1363 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001364 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001365 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001366 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001367 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001368 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001369 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001370 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1371 } else if (source.IsConstant()) {
1372 HConstant* constant = source.GetConstant();
1373 if (constant->IsLongConstant()) {
1374 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1375 } else {
1376 Load32BitValue(dest, GetInt32ValueOf(constant));
1377 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001378 } else {
1379 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001380 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001381 }
1382 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001383 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001384 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001385 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001386 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001387 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1388 } else if (source.IsConstant()) {
1389 HConstant* constant = source.GetConstant();
1390 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1391 if (constant->IsFloatConstant()) {
1392 Load32BitValue(dest, static_cast<int32_t>(value));
1393 } else {
1394 Load64BitValue(dest, value);
1395 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001396 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001397 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001398 } else {
1399 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001400 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001401 }
1402 } else if (destination.IsStackSlot()) {
1403 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001404 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001405 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001406 } else if (source.IsFpuRegister()) {
1407 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001408 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001409 } else if (source.IsConstant()) {
1410 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001411 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001412 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001413 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001414 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001415 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1416 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001417 }
1418 } else {
1419 DCHECK(destination.IsDoubleStackSlot());
1420 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001421 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001422 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 } else if (source.IsFpuRegister()) {
1424 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001426 } else if (source.IsConstant()) {
1427 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001428 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1429 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001430 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001431 } else {
1432 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001433 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1434 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001435 }
1436 }
1437}
1438
Calin Juravle175dc732015-08-25 15:42:32 +01001439void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1440 DCHECK(location.IsRegister());
1441 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1442}
1443
Calin Juravlee460d1d2015-09-29 04:52:17 +01001444void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001445 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001446 Move(dst, src);
1447}
1448
1449void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1450 if (location.IsRegister()) {
1451 locations->AddTemp(location);
1452 } else {
1453 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1454 }
1455}
1456
David Brazdilfc6a86a2015-06-26 10:33:45 +00001457void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001458 if (successor->IsExitBlock()) {
1459 DCHECK(got->GetPrevious()->AlwaysThrows());
1460 return; // no code needed
1461 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001462
1463 HBasicBlock* block = got->GetBlock();
1464 HInstruction* previous = got->GetPrevious();
1465
1466 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001467 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001468 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1469 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1470 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1471 Immediate(1));
1472 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001473 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1474 return;
1475 }
1476
1477 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1478 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1479 }
1480 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001481 __ jmp(codegen_->GetLabelOf(successor));
1482 }
1483}
1484
David Brazdilfc6a86a2015-06-26 10:33:45 +00001485void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1486 got->SetLocations(nullptr);
1487}
1488
1489void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1490 HandleGoto(got, got->GetSuccessor());
1491}
1492
1493void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1494 try_boundary->SetLocations(nullptr);
1495}
1496
1497void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1498 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1499 if (!successor->IsExitBlock()) {
1500 HandleGoto(try_boundary, successor);
1501 }
1502}
1503
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001504void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1505 exit->SetLocations(nullptr);
1506}
1507
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001508void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509}
1510
Mark Mendell152408f2015-12-31 12:28:50 -05001511template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001512void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001513 LabelType* true_label,
1514 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001515 if (cond->IsFPConditionTrueIfNaN()) {
1516 __ j(kUnordered, true_label);
1517 } else if (cond->IsFPConditionFalseIfNaN()) {
1518 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001519 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001520 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001521}
1522
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001523void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001524 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001525
Mark Mendellc4701932015-04-10 13:18:51 -04001526 Location left = locations->InAt(0);
1527 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001528 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001529 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001531 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001532 case DataType::Type::kInt8:
1533 case DataType::Type::kUint16:
1534 case DataType::Type::kInt16:
1535 case DataType::Type::kInt32:
1536 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001537 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001538 break;
1539 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001540 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001541 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001542 break;
1543 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001544 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001545 if (right.IsFpuRegister()) {
1546 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1547 } else if (right.IsConstant()) {
1548 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1549 codegen_->LiteralFloatAddress(
1550 right.GetConstant()->AsFloatConstant()->GetValue()));
1551 } else {
1552 DCHECK(right.IsStackSlot());
1553 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1554 Address(CpuRegister(RSP), right.GetStackIndex()));
1555 }
Mark Mendellc4701932015-04-10 13:18:51 -04001556 break;
1557 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001559 if (right.IsFpuRegister()) {
1560 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1561 } else if (right.IsConstant()) {
1562 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1563 codegen_->LiteralDoubleAddress(
1564 right.GetConstant()->AsDoubleConstant()->GetValue()));
1565 } else {
1566 DCHECK(right.IsDoubleStackSlot());
1567 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1568 Address(CpuRegister(RSP), right.GetStackIndex()));
1569 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001570 break;
1571 }
1572 default:
1573 LOG(FATAL) << "Unexpected condition type " << type;
1574 }
1575}
1576
1577template<class LabelType>
1578void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1579 LabelType* true_target_in,
1580 LabelType* false_target_in) {
1581 // Generated branching requires both targets to be explicit. If either of the
1582 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1583 LabelType fallthrough_target;
1584 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1585 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1586
1587 // Generate the comparison to set the CC.
1588 GenerateCompareTest(condition);
1589
1590 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001591 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001592 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001593 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001594 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1595 break;
1596 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001597 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001598 GenerateFPJumps(condition, true_target, false_target);
1599 break;
1600 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001601 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001602 GenerateFPJumps(condition, true_target, false_target);
1603 break;
1604 }
1605 default:
1606 LOG(FATAL) << "Unexpected condition type " << type;
1607 }
1608
David Brazdil0debae72015-11-12 18:37:00 +00001609 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001610 __ jmp(false_target);
1611 }
David Brazdil0debae72015-11-12 18:37:00 +00001612
1613 if (fallthrough_target.IsLinked()) {
1614 __ Bind(&fallthrough_target);
1615 }
Mark Mendellc4701932015-04-10 13:18:51 -04001616}
1617
David Brazdil0debae72015-11-12 18:37:00 +00001618static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1619 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1620 // are set only strictly before `branch`. We can't use the eflags on long
1621 // conditions if they are materialized due to the complex branching.
1622 return cond->IsCondition() &&
1623 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001624 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001625}
1626
Mark Mendell152408f2015-12-31 12:28:50 -05001627template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001628void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001629 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001630 LabelType* true_target,
1631 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001632 HInstruction* cond = instruction->InputAt(condition_input_index);
1633
1634 if (true_target == nullptr && false_target == nullptr) {
1635 // Nothing to do. The code always falls through.
1636 return;
1637 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001638 // Constant condition, statically compared against "true" (integer value 1).
1639 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001640 if (true_target != nullptr) {
1641 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001642 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001643 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001644 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001645 if (false_target != nullptr) {
1646 __ jmp(false_target);
1647 }
1648 }
1649 return;
1650 }
1651
1652 // The following code generates these patterns:
1653 // (1) true_target == nullptr && false_target != nullptr
1654 // - opposite condition true => branch to false_target
1655 // (2) true_target != nullptr && false_target == nullptr
1656 // - condition true => branch to true_target
1657 // (3) true_target != nullptr && false_target != nullptr
1658 // - condition true => branch to true_target
1659 // - branch to false_target
1660 if (IsBooleanValueOrMaterializedCondition(cond)) {
1661 if (AreEflagsSetFrom(cond, instruction)) {
1662 if (true_target == nullptr) {
1663 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1664 } else {
1665 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1666 }
1667 } else {
1668 // Materialized condition, compare against 0.
1669 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1670 if (lhs.IsRegister()) {
1671 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1672 } else {
1673 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1674 }
1675 if (true_target == nullptr) {
1676 __ j(kEqual, false_target);
1677 } else {
1678 __ j(kNotEqual, true_target);
1679 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001680 }
1681 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001682 // Condition has not been materialized, use its inputs as the
1683 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001684 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001685
David Brazdil0debae72015-11-12 18:37:00 +00001686 // If this is a long or FP comparison that has been folded into
1687 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 DataType::Type type = condition->InputAt(0)->GetType();
1689 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001690 GenerateCompareTestAndBranch(condition, true_target, false_target);
1691 return;
1692 }
1693
1694 Location lhs = condition->GetLocations()->InAt(0);
1695 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001696 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001697 if (true_target == nullptr) {
1698 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1699 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001700 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001701 }
Dave Allison20dfc792014-06-16 20:44:29 -07001702 }
David Brazdil0debae72015-11-12 18:37:00 +00001703
1704 // If neither branch falls through (case 3), the conditional branch to `true_target`
1705 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1706 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001707 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001708 }
1709}
1710
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001711void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001712 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001713 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001714 locations->SetInAt(0, Location::Any());
1715 }
1716}
1717
1718void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001719 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1720 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1721 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1722 nullptr : codegen_->GetLabelOf(true_successor);
1723 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1724 nullptr : codegen_->GetLabelOf(false_successor);
1725 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001726}
1727
1728void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001729 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001730 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001731 InvokeRuntimeCallingConvention calling_convention;
1732 RegisterSet caller_saves = RegisterSet::Empty();
1733 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1734 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001735 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001736 locations->SetInAt(0, Location::Any());
1737 }
1738}
1739
1740void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001741 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001742 GenerateTestAndBranch<Label>(deoptimize,
1743 /* condition_input_index */ 0,
1744 slow_path->GetEntryLabel(),
1745 /* false_target */ nullptr);
1746}
1747
Mingyao Yang063fc772016-08-02 11:02:54 -07001748void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001749 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001750 LocationSummary(flag, LocationSummary::kNoCall);
1751 locations->SetOut(Location::RequiresRegister());
1752}
1753
1754void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1755 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1756 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1757}
1758
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001759static bool SelectCanUseCMOV(HSelect* select) {
1760 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001762 return false;
1763 }
1764
1765 // A FP condition doesn't generate the single CC that we need.
1766 HInstruction* condition = select->GetCondition();
1767 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001768 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001769 return false;
1770 }
1771
1772 // We can generate a CMOV for this Select.
1773 return true;
1774}
1775
David Brazdil74eb1b22015-12-14 11:44:01 +00001776void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001777 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001779 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001780 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001781 } else {
1782 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001783 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001784 if (select->InputAt(1)->IsConstant()) {
1785 locations->SetInAt(1, Location::RequiresRegister());
1786 } else {
1787 locations->SetInAt(1, Location::Any());
1788 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001789 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001790 locations->SetInAt(1, Location::Any());
1791 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001792 }
1793 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1794 locations->SetInAt(2, Location::RequiresRegister());
1795 }
1796 locations->SetOut(Location::SameAsFirstInput());
1797}
1798
1799void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1800 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001801 if (SelectCanUseCMOV(select)) {
1802 // If both the condition and the source types are integer, we can generate
1803 // a CMOV to implement Select.
1804 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001805 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001806 DCHECK(locations->InAt(0).Equals(locations->Out()));
1807
1808 HInstruction* select_condition = select->GetCondition();
1809 Condition cond = kNotEqual;
1810
1811 // Figure out how to test the 'condition'.
1812 if (select_condition->IsCondition()) {
1813 HCondition* condition = select_condition->AsCondition();
1814 if (!condition->IsEmittedAtUseSite()) {
1815 // This was a previously materialized condition.
1816 // Can we use the existing condition code?
1817 if (AreEflagsSetFrom(condition, select)) {
1818 // Materialization was the previous instruction. Condition codes are right.
1819 cond = X86_64IntegerCondition(condition->GetCondition());
1820 } else {
1821 // No, we have to recreate the condition code.
1822 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1823 __ testl(cond_reg, cond_reg);
1824 }
1825 } else {
1826 GenerateCompareTest(condition);
1827 cond = X86_64IntegerCondition(condition->GetCondition());
1828 }
1829 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001830 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001831 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1832 __ testl(cond_reg, cond_reg);
1833 }
1834
1835 // If the condition is true, overwrite the output, which already contains false.
1836 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001837 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001838 if (value_true_loc.IsRegister()) {
1839 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1840 } else {
1841 __ cmov(cond,
1842 value_false,
1843 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1844 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001845 } else {
1846 NearLabel false_target;
1847 GenerateTestAndBranch<NearLabel>(select,
1848 /* condition_input_index */ 2,
1849 /* true_target */ nullptr,
1850 &false_target);
1851 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1852 __ Bind(&false_target);
1853 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001854}
1855
David Srbecky0cf44932015-12-09 14:09:59 +00001856void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001857 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001858}
1859
David Srbeckyd28f4a02016-03-14 17:14:24 +00001860void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1861 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001862}
1863
1864void CodeGeneratorX86_64::GenerateNop() {
1865 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001866}
1867
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001868void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001870 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001871 // Handle the long/FP comparisons made in instruction simplification.
1872 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001874 locations->SetInAt(0, Location::RequiresRegister());
1875 locations->SetInAt(1, Location::Any());
1876 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001877 case DataType::Type::kFloat32:
1878 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001879 locations->SetInAt(0, Location::RequiresFpuRegister());
1880 locations->SetInAt(1, Location::Any());
1881 break;
1882 default:
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 locations->SetInAt(1, Location::Any());
1885 break;
1886 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001887 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001888 locations->SetOut(Location::RequiresRegister());
1889 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001890}
1891
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001893 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001894 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001895 }
Mark Mendellc4701932015-04-10 13:18:51 -04001896
1897 LocationSummary* locations = cond->GetLocations();
1898 Location lhs = locations->InAt(0);
1899 Location rhs = locations->InAt(1);
1900 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001901 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001902
1903 switch (cond->InputAt(0)->GetType()) {
1904 default:
1905 // Integer case.
1906
1907 // Clear output register: setcc only sets the low byte.
1908 __ xorl(reg, reg);
1909
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001910 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001911 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001913 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001914 // Clear output register: setcc only sets the low byte.
1915 __ xorl(reg, reg);
1916
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001917 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001918 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001919 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001921 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1922 if (rhs.IsConstant()) {
1923 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1924 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1925 } else if (rhs.IsStackSlot()) {
1926 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1927 } else {
1928 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1929 }
1930 GenerateFPJumps(cond, &true_label, &false_label);
1931 break;
1932 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001933 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001934 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1935 if (rhs.IsConstant()) {
1936 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1937 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1938 } else if (rhs.IsDoubleStackSlot()) {
1939 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1940 } else {
1941 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1942 }
1943 GenerateFPJumps(cond, &true_label, &false_label);
1944 break;
1945 }
1946 }
1947
1948 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001949 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001950
Roland Levillain4fa13f62015-07-06 18:11:54 +01001951 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001952 __ Bind(&false_label);
1953 __ xorl(reg, reg);
1954 __ jmp(&done_label);
1955
Roland Levillain4fa13f62015-07-06 18:11:54 +01001956 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001957 __ Bind(&true_label);
1958 __ movl(reg, Immediate(1));
1959 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002004}
2005
2006void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008}
2009
Aart Bike9f37602015-10-09 11:15:55 -07002010void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
2038void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002040}
2041
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002042void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002044 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002045 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002047 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002050 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kInt32:
2052 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002053 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002054 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2056 break;
2057 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 case DataType::Type::kFloat32:
2059 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002060 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002061 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002062 locations->SetOut(Location::RequiresRegister());
2063 break;
2064 }
2065 default:
2066 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2067 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002068}
2069
2070void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002071 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002072 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002073 Location left = locations->InAt(0);
2074 Location right = locations->InAt(1);
2075
Mark Mendell0c9497d2015-08-21 09:30:05 -04002076 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002078 Condition less_cond = kLess;
2079
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002081 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002082 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002085 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002087 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002088 break;
2089 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002091 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002092 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002093 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002095 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2096 if (right.IsConstant()) {
2097 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2098 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2099 } else if (right.IsStackSlot()) {
2100 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2101 } else {
2102 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2103 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002104 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002105 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002106 break;
2107 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002109 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2110 if (right.IsConstant()) {
2111 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2112 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2113 } else if (right.IsDoubleStackSlot()) {
2114 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2115 } else {
2116 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2117 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002118 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002119 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002120 break;
2121 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002122 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002123 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002124 }
Aart Bika19616e2016-02-01 18:57:58 -08002125
Calin Juravleddb7df22014-11-25 20:56:51 +00002126 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002127 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002128 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002129
Calin Juravle91debbc2014-11-26 19:01:09 +00002130 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002131 __ movl(out, Immediate(1));
2132 __ jmp(&done);
2133
2134 __ Bind(&less);
2135 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002136
2137 __ Bind(&done);
2138}
2139
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002140void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002141 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002142 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002143 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144}
2145
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002146void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002147 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002148}
2149
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002150void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2151 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002152 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002153 locations->SetOut(Location::ConstantLocation(constant));
2154}
2155
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002156void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002157 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002158}
2159
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002160void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002161 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002162 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002163 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002164}
2165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002166void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002167 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002168}
2169
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002170void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2171 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002172 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002173 locations->SetOut(Location::ConstantLocation(constant));
2174}
2175
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002176void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002177 // Will be generated at use site.
2178}
2179
2180void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2181 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002182 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 locations->SetOut(Location::ConstantLocation(constant));
2184}
2185
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002186void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2187 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 // Will be generated at use site.
2189}
2190
Igor Murashkind01745e2017-04-05 16:40:31 -07002191void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2192 constructor_fence->SetLocations(nullptr);
2193}
2194
2195void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2196 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2197 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2198}
2199
Calin Juravle27df7582015-04-17 19:12:31 +01002200void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2201 memory_barrier->SetLocations(nullptr);
2202}
2203
2204void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002205 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002206}
2207
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002208void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2209 ret->SetLocations(nullptr);
2210}
2211
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002212void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002214}
2215
2216void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002218 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002219 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002220 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002221 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002222 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 case DataType::Type::kInt8:
2224 case DataType::Type::kUint16:
2225 case DataType::Type::kInt16:
2226 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002228 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002229 break;
2230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kFloat32:
2232 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002233 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002234 break;
2235
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002236 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002237 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002238 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002239}
2240
2241void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2242 if (kIsDebugBuild) {
2243 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002244 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002246 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 case DataType::Type::kInt8:
2248 case DataType::Type::kUint16:
2249 case DataType::Type::kInt16:
2250 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002252 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002253 break;
2254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 case DataType::Type::kFloat32:
2256 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002257 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002258 XMM0);
2259 break;
2260
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002261 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002262 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002263 }
2264 }
2265 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002266}
2267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002269 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002270 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002272 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kInt8:
2274 case DataType::Type::kUint16:
2275 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002276 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002278 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002280 return Location::RegisterLocation(RAX);
2281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002283 return Location::NoLocation();
2284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002285 case DataType::Type::kFloat64:
2286 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002287 return Location::FpuRegisterLocation(XMM0);
2288 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002289
2290 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002291}
2292
2293Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2294 return Location::RegisterLocation(kMethodRegisterArgument);
2295}
2296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002298 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002299 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002301 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 case DataType::Type::kInt8:
2303 case DataType::Type::kUint16:
2304 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002305 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002306 uint32_t index = gp_index_++;
2307 stack_index_++;
2308 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002309 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002310 } else {
2311 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2312 }
2313 }
2314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002316 uint32_t index = gp_index_;
2317 stack_index_ += 2;
2318 if (index < calling_convention.GetNumberOfRegisters()) {
2319 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002321 } else {
2322 gp_index_ += 2;
2323 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2324 }
2325 }
2326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002328 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002329 stack_index_++;
2330 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002331 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002332 } else {
2333 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2334 }
2335 }
2336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002338 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002339 stack_index_ += 2;
2340 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002342 } else {
2343 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2344 }
2345 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002346
Aart Bik66c158e2018-01-31 12:55:04 -08002347 case DataType::Type::kUint32:
2348 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002350 LOG(FATAL) << "Unexpected parameter type " << type;
2351 break;
2352 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002353 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002354}
2355
Calin Juravle175dc732015-08-25 15:42:32 +01002356void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2357 // The trampoline uses the same calling convention as dex calling conventions,
2358 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2359 // the method_idx.
2360 HandleInvoke(invoke);
2361}
2362
2363void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2364 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2365}
2366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002367void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002368 // Explicit clinit checks triggered by static invokes must have been pruned by
2369 // art::PrepareForRegisterAllocation.
2370 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002371
Mark Mendellfb8d2792015-03-31 22:16:59 -04002372 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002373 if (intrinsic.TryDispatch(invoke)) {
2374 return;
2375 }
2376
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002377 HandleInvoke(invoke);
2378}
2379
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002380static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2381 if (invoke->GetLocations()->Intrinsified()) {
2382 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2383 intrinsic.Dispatch(invoke);
2384 return true;
2385 }
2386 return false;
2387}
2388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002389void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002390 // Explicit clinit checks triggered by static invokes must have been pruned by
2391 // art::PrepareForRegisterAllocation.
2392 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002393
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002394 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2395 return;
2396 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002397
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002398 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002399 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002400 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002401}
2402
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002403void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002404 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002405 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002406}
2407
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002408void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002409 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002410 if (intrinsic.TryDispatch(invoke)) {
2411 return;
2412 }
2413
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002414 HandleInvoke(invoke);
2415}
2416
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002417void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002418 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2419 return;
2420 }
2421
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002422 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002423 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002424}
2425
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002426void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2427 HandleInvoke(invoke);
2428 // Add the hidden argument.
2429 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2430}
2431
2432void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2433 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002434 LocationSummary* locations = invoke->GetLocations();
2435 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2436 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002437 Location receiver = locations->InAt(0);
2438 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2439
Roland Levillain0d5a2812015-11-13 10:07:31 +00002440 // Set the hidden argument. This is safe to do this here, as RAX
2441 // won't be modified thereafter, before the `call` instruction.
2442 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002443 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002445 if (receiver.IsStackSlot()) {
2446 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002447 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002448 __ movl(temp, Address(temp, class_offset));
2449 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002450 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002451 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002452 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002453 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002454 // Instead of simply (possibly) unpoisoning `temp` here, we should
2455 // emit a read barrier for the previous class reference load.
2456 // However this is not required in practice, as this is an
2457 // intermediate/temporary reference and because the current
2458 // concurrent copying collector keeps the from-space memory
2459 // intact/accessible until the end of the marking phase (the
2460 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002461 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002462 // temp = temp->GetAddressOfIMT()
2463 __ movq(temp,
2464 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2465 // temp = temp->GetImtEntryAt(method_offset);
2466 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002467 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002468 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002469 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002470 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002471 __ call(Address(
2472 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002473
2474 DCHECK(!codegen_->IsLeafMethod());
2475 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2476}
2477
Orion Hodsonac141392017-01-13 11:53:47 +00002478void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2479 HandleInvoke(invoke);
2480}
2481
2482void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2483 codegen_->GenerateInvokePolymorphicCall(invoke);
2484}
2485
Roland Levillain88cb1752014-10-20 16:36:47 +01002486void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2487 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002488 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002489 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kInt32:
2491 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetOut(Location::SameAsFirstInput());
2494 break;
2495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kFloat32:
2497 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002498 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002499 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002500 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002501 break;
2502
2503 default:
2504 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2505 }
2506}
2507
2508void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2509 LocationSummary* locations = neg->GetLocations();
2510 Location out = locations->Out();
2511 Location in = locations->InAt(0);
2512 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002514 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002515 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002517 break;
2518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002520 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002521 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002522 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002523 break;
2524
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002526 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002527 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002528 // Implement float negation with an exclusive or with value
2529 // 0x80000000 (mask for bit 31, representing the sign of a
2530 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002531 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002532 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002533 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002534 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002537 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002538 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002539 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002540 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002541 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002542 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002544 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002545 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002546
2547 default:
2548 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2549 }
2550}
2551
Roland Levillaindff1f282014-11-05 14:15:05 +00002552void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2553 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002554 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 DataType::Type result_type = conversion->GetResultType();
2556 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002557 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2558 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002559
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002561 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002563 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002565 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2566 locations->SetInAt(0, Location::Any());
2567 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002568 break;
2569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002571 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002573 locations->SetInAt(0, Location::Any());
2574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2575 break;
2576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002578 locations->SetInAt(0, Location::RequiresFpuRegister());
2579 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002580 break;
2581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002583 locations->SetInAt(0, Location::RequiresFpuRegister());
2584 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002585 break;
2586
2587 default:
2588 LOG(FATAL) << "Unexpected type conversion from " << input_type
2589 << " to " << result_type;
2590 }
2591 break;
2592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002596 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002598 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002599 case DataType::Type::kInt16:
2600 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 // TODO: We would benefit from a (to-be-implemented)
2602 // Location::RegisterOrStackSlot requirement for this input.
2603 locations->SetInAt(0, Location::RequiresRegister());
2604 locations->SetOut(Location::RequiresRegister());
2605 break;
2606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002608 locations->SetInAt(0, Location::RequiresFpuRegister());
2609 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002610 break;
2611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002613 locations->SetInAt(0, Location::RequiresFpuRegister());
2614 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002615 break;
2616
2617 default:
2618 LOG(FATAL) << "Unexpected type conversion from " << input_type
2619 << " to " << result_type;
2620 }
2621 break;
2622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002623 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002624 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002625 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002626 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002628 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 case DataType::Type::kInt16:
2630 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002631 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002632 locations->SetOut(Location::RequiresFpuRegister());
2633 break;
2634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002636 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002637 locations->SetOut(Location::RequiresFpuRegister());
2638 break;
2639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002641 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002642 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002643 break;
2644
2645 default:
2646 LOG(FATAL) << "Unexpected type conversion from " << input_type
2647 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002648 }
Roland Levillaincff13742014-11-17 14:32:17 +00002649 break;
2650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002652 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002653 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002654 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002656 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kInt16:
2658 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002659 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002660 locations->SetOut(Location::RequiresFpuRegister());
2661 break;
2662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002664 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002665 locations->SetOut(Location::RequiresFpuRegister());
2666 break;
2667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002669 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002670 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002671 break;
2672
2673 default:
2674 LOG(FATAL) << "Unexpected type conversion from " << input_type
2675 << " to " << result_type;
2676 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002677 break;
2678
2679 default:
2680 LOG(FATAL) << "Unexpected type conversion from " << input_type
2681 << " to " << result_type;
2682 }
2683}
2684
2685void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2686 LocationSummary* locations = conversion->GetLocations();
2687 Location out = locations->Out();
2688 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 DataType::Type result_type = conversion->GetResultType();
2690 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002691 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2692 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002694 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002695 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002696 case DataType::Type::kInt8:
2697 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 case DataType::Type::kInt16:
2699 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002700 case DataType::Type::kInt64:
2701 if (in.IsRegister()) {
2702 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2703 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2704 __ movzxb(out.AsRegister<CpuRegister>(),
2705 Address(CpuRegister(RSP), in.GetStackIndex()));
2706 } else {
2707 __ movl(out.AsRegister<CpuRegister>(),
2708 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2709 }
2710 break;
2711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716 break;
2717
2718 case DataType::Type::kInt8:
2719 switch (input_type) {
2720 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002722 case DataType::Type::kInt16:
2723 case DataType::Type::kInt32:
2724 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002725 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002726 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002727 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002729 Address(CpuRegister(RSP), in.GetStackIndex()));
2730 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002732 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002733 }
2734 break;
2735
2736 default:
2737 LOG(FATAL) << "Unexpected type conversion from " << input_type
2738 << " to " << result_type;
2739 }
2740 break;
2741
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002742 case DataType::Type::kUint16:
2743 switch (input_type) {
2744 case DataType::Type::kInt8:
2745 case DataType::Type::kInt16:
2746 case DataType::Type::kInt32:
2747 case DataType::Type::kInt64:
2748 if (in.IsRegister()) {
2749 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2750 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2751 __ movzxw(out.AsRegister<CpuRegister>(),
2752 Address(CpuRegister(RSP), in.GetStackIndex()));
2753 } else {
2754 __ movl(out.AsRegister<CpuRegister>(),
2755 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2756 }
2757 break;
2758
2759 default:
2760 LOG(FATAL) << "Unexpected type conversion from " << input_type
2761 << " to " << result_type;
2762 }
2763 break;
2764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002765 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002766 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002767 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002768 case DataType::Type::kInt32:
2769 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002770 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002772 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002774 Address(CpuRegister(RSP), in.GetStackIndex()));
2775 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002777 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002778 }
2779 break;
2780
2781 default:
2782 LOG(FATAL) << "Unexpected type conversion from " << input_type
2783 << " to " << result_type;
2784 }
2785 break;
2786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002788 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002790 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002791 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002792 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002794 Address(CpuRegister(RSP), in.GetStackIndex()));
2795 } else {
2796 DCHECK(in.IsConstant());
2797 DCHECK(in.GetConstant()->IsLongConstant());
2798 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002799 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002800 }
2801 break;
2802
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002804 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2805 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002806 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002807
2808 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002809 // if input >= (float)INT_MAX goto done
2810 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002811 __ j(kAboveEqual, &done);
2812 // if input == NaN goto nan
2813 __ j(kUnordered, &nan);
2814 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002815 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002816 __ jmp(&done);
2817 __ Bind(&nan);
2818 // output = 0
2819 __ xorl(output, output);
2820 __ Bind(&done);
2821 break;
2822 }
2823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002825 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2826 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002827 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002828
2829 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002830 // if input >= (double)INT_MAX goto done
2831 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002832 __ j(kAboveEqual, &done);
2833 // if input == NaN goto nan
2834 __ j(kUnordered, &nan);
2835 // output = double-to-int-truncate(input)
2836 __ cvttsd2si(output, input);
2837 __ jmp(&done);
2838 __ Bind(&nan);
2839 // output = 0
2840 __ xorl(output, output);
2841 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002842 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002843 }
Roland Levillain946e1432014-11-11 17:35:19 +00002844
2845 default:
2846 LOG(FATAL) << "Unexpected type conversion from " << input_type
2847 << " to " << result_type;
2848 }
2849 break;
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002852 switch (input_type) {
2853 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002854 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002855 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002856 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002857 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 case DataType::Type::kInt16:
2859 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002860 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002861 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002862 break;
2863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002864 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002865 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2866 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002867 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002868
Mark Mendell92e83bf2015-05-07 11:25:03 -04002869 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002870 // if input >= (float)LONG_MAX goto done
2871 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002872 __ j(kAboveEqual, &done);
2873 // if input == NaN goto nan
2874 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002875 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002876 __ cvttss2si(output, input, true);
2877 __ jmp(&done);
2878 __ Bind(&nan);
2879 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002880 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002881 __ Bind(&done);
2882 break;
2883 }
2884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002886 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2887 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002888 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002889
Mark Mendell92e83bf2015-05-07 11:25:03 -04002890 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002891 // if input >= (double)LONG_MAX goto done
2892 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002893 __ j(kAboveEqual, &done);
2894 // if input == NaN goto nan
2895 __ j(kUnordered, &nan);
2896 // output = double-to-long-truncate(input)
2897 __ cvttsd2si(output, input, true);
2898 __ jmp(&done);
2899 __ Bind(&nan);
2900 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002901 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002902 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002903 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002904 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002905
2906 default:
2907 LOG(FATAL) << "Unexpected type conversion from " << input_type
2908 << " to " << result_type;
2909 }
2910 break;
2911
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002913 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002915 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002917 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 case DataType::Type::kInt16:
2919 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002920 if (in.IsRegister()) {
2921 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2922 } else if (in.IsConstant()) {
2923 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2924 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002925 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002926 } else {
2927 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2928 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2929 }
Roland Levillaincff13742014-11-17 14:32:17 +00002930 break;
2931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002933 if (in.IsRegister()) {
2934 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2935 } else if (in.IsConstant()) {
2936 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2937 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002938 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002939 } else {
2940 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2941 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2942 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002943 break;
2944
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002946 if (in.IsFpuRegister()) {
2947 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2948 } else if (in.IsConstant()) {
2949 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2950 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002951 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002952 } else {
2953 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2954 Address(CpuRegister(RSP), in.GetStackIndex()));
2955 }
Roland Levillaincff13742014-11-17 14:32:17 +00002956 break;
2957
2958 default:
2959 LOG(FATAL) << "Unexpected type conversion from " << input_type
2960 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002961 }
Roland Levillaincff13742014-11-17 14:32:17 +00002962 break;
2963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002965 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002967 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002969 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt16:
2971 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002972 if (in.IsRegister()) {
2973 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2974 } else if (in.IsConstant()) {
2975 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2976 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002977 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002978 } else {
2979 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2980 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2981 }
Roland Levillaincff13742014-11-17 14:32:17 +00002982 break;
2983
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002985 if (in.IsRegister()) {
2986 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2987 } else if (in.IsConstant()) {
2988 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2989 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002990 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002991 } else {
2992 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2993 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2994 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002995 break;
2996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002998 if (in.IsFpuRegister()) {
2999 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3000 } else if (in.IsConstant()) {
3001 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3002 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003003 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003004 } else {
3005 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3006 Address(CpuRegister(RSP), in.GetStackIndex()));
3007 }
Roland Levillaincff13742014-11-17 14:32:17 +00003008 break;
3009
3010 default:
3011 LOG(FATAL) << "Unexpected type conversion from " << input_type
3012 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003013 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003014 break;
3015
3016 default:
3017 LOG(FATAL) << "Unexpected type conversion from " << input_type
3018 << " to " << result_type;
3019 }
3020}
3021
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003022void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003023 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003024 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003025 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003027 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003028 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3029 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003030 break;
3031 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003033 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003034 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003035 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003036 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003038 break;
3039 }
3040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 case DataType::Type::kFloat64:
3042 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003043 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003044 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003046 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003048
3049 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003050 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003051 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003052}
3053
3054void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3055 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003056 Location first = locations->InAt(0);
3057 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003058 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003060 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003062 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003063 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3064 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003065 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3066 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003067 } else {
3068 __ leal(out.AsRegister<CpuRegister>(), Address(
3069 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3070 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003071 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003072 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3073 __ addl(out.AsRegister<CpuRegister>(),
3074 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3075 } else {
3076 __ leal(out.AsRegister<CpuRegister>(), Address(
3077 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3078 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003079 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003080 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003081 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003082 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003083 break;
3084 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003085
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003087 if (second.IsRegister()) {
3088 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3089 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003090 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3091 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003092 } else {
3093 __ leaq(out.AsRegister<CpuRegister>(), Address(
3094 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3095 }
3096 } else {
3097 DCHECK(second.IsConstant());
3098 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3099 int32_t int32_value = Low32Bits(value);
3100 DCHECK_EQ(int32_value, value);
3101 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3102 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3103 } else {
3104 __ leaq(out.AsRegister<CpuRegister>(), Address(
3105 first.AsRegister<CpuRegister>(), int32_value));
3106 }
3107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003108 break;
3109 }
3110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003112 if (second.IsFpuRegister()) {
3113 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3114 } else if (second.IsConstant()) {
3115 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003116 codegen_->LiteralFloatAddress(
3117 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003118 } else {
3119 DCHECK(second.IsStackSlot());
3120 __ addss(first.AsFpuRegister<XmmRegister>(),
3121 Address(CpuRegister(RSP), second.GetStackIndex()));
3122 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003124 }
3125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003127 if (second.IsFpuRegister()) {
3128 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3129 } else if (second.IsConstant()) {
3130 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003131 codegen_->LiteralDoubleAddress(
3132 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003133 } else {
3134 DCHECK(second.IsDoubleStackSlot());
3135 __ addsd(first.AsFpuRegister<XmmRegister>(),
3136 Address(CpuRegister(RSP), second.GetStackIndex()));
3137 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003138 break;
3139 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003140
3141 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003142 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003143 }
3144}
3145
3146void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003148 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003149 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003151 locations->SetInAt(0, Location::RequiresRegister());
3152 locations->SetInAt(1, Location::Any());
3153 locations->SetOut(Location::SameAsFirstInput());
3154 break;
3155 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003156 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003157 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003158 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003159 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003160 break;
3161 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003162 case DataType::Type::kFloat32:
3163 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003164 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003165 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003166 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003167 break;
Calin Juravle11351682014-10-23 15:38:15 +01003168 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003169 default:
Calin Juravle11351682014-10-23 15:38:15 +01003170 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003171 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003172}
3173
3174void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3175 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003176 Location first = locations->InAt(0);
3177 Location second = locations->InAt(1);
3178 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003179 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003181 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003183 } else if (second.IsConstant()) {
3184 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003185 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003186 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003187 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003188 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003189 break;
3190 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003192 if (second.IsConstant()) {
3193 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3194 DCHECK(IsInt<32>(value));
3195 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3196 } else {
3197 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3198 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003199 break;
3200 }
3201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003202 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003203 if (second.IsFpuRegister()) {
3204 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3205 } else if (second.IsConstant()) {
3206 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003207 codegen_->LiteralFloatAddress(
3208 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003209 } else {
3210 DCHECK(second.IsStackSlot());
3211 __ subss(first.AsFpuRegister<XmmRegister>(),
3212 Address(CpuRegister(RSP), second.GetStackIndex()));
3213 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003214 break;
Calin Juravle11351682014-10-23 15:38:15 +01003215 }
3216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003218 if (second.IsFpuRegister()) {
3219 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3220 } else if (second.IsConstant()) {
3221 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003222 codegen_->LiteralDoubleAddress(
3223 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003224 } else {
3225 DCHECK(second.IsDoubleStackSlot());
3226 __ subsd(first.AsFpuRegister<XmmRegister>(),
3227 Address(CpuRegister(RSP), second.GetStackIndex()));
3228 }
Calin Juravle11351682014-10-23 15:38:15 +01003229 break;
3230 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003231
3232 default:
Calin Juravle11351682014-10-23 15:38:15 +01003233 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003234 }
3235}
3236
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3238 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003239 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003240 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003242 locations->SetInAt(0, Location::RequiresRegister());
3243 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003244 if (mul->InputAt(1)->IsIntConstant()) {
3245 // Can use 3 operand multiply.
3246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3247 } else {
3248 locations->SetOut(Location::SameAsFirstInput());
3249 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003250 break;
3251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003252 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003254 locations->SetInAt(1, Location::Any());
3255 if (mul->InputAt(1)->IsLongConstant() &&
3256 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003257 // Can use 3 operand multiply.
3258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3259 } else {
3260 locations->SetOut(Location::SameAsFirstInput());
3261 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003262 break;
3263 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003264 case DataType::Type::kFloat32:
3265 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003266 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003267 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003268 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003269 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003270 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271
3272 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003273 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003274 }
3275}
3276
3277void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3278 LocationSummary* locations = mul->GetLocations();
3279 Location first = locations->InAt(0);
3280 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003281 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003282 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003283 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003284 // The constant may have ended up in a register, so test explicitly to avoid
3285 // problems where the output may not be the same as the first operand.
3286 if (mul->InputAt(1)->IsIntConstant()) {
3287 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3288 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3289 } else if (second.IsRegister()) {
3290 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003291 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003292 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003293 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003294 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003295 __ imull(first.AsRegister<CpuRegister>(),
3296 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003297 }
3298 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003299 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003300 // The constant may have ended up in a register, so test explicitly to avoid
3301 // problems where the output may not be the same as the first operand.
3302 if (mul->InputAt(1)->IsLongConstant()) {
3303 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3304 if (IsInt<32>(value)) {
3305 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3306 Immediate(static_cast<int32_t>(value)));
3307 } else {
3308 // Have to use the constant area.
3309 DCHECK(first.Equals(out));
3310 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3311 }
3312 } else if (second.IsRegister()) {
3313 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003314 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003315 } else {
3316 DCHECK(second.IsDoubleStackSlot());
3317 DCHECK(first.Equals(out));
3318 __ imulq(first.AsRegister<CpuRegister>(),
3319 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003320 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003321 break;
3322 }
3323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003324 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003325 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003326 if (second.IsFpuRegister()) {
3327 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3328 } else if (second.IsConstant()) {
3329 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003330 codegen_->LiteralFloatAddress(
3331 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003332 } else {
3333 DCHECK(second.IsStackSlot());
3334 __ mulss(first.AsFpuRegister<XmmRegister>(),
3335 Address(CpuRegister(RSP), second.GetStackIndex()));
3336 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003337 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003338 }
3339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003340 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003341 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003342 if (second.IsFpuRegister()) {
3343 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3344 } else if (second.IsConstant()) {
3345 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003346 codegen_->LiteralDoubleAddress(
3347 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003348 } else {
3349 DCHECK(second.IsDoubleStackSlot());
3350 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3351 Address(CpuRegister(RSP), second.GetStackIndex()));
3352 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003353 break;
3354 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355
3356 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003357 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003358 }
3359}
3360
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003361void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3362 uint32_t stack_adjustment, bool is_float) {
3363 if (source.IsStackSlot()) {
3364 DCHECK(is_float);
3365 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3366 } else if (source.IsDoubleStackSlot()) {
3367 DCHECK(!is_float);
3368 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3369 } else {
3370 // Write the value to the temporary location on the stack and load to FP stack.
3371 if (is_float) {
3372 Location stack_temp = Location::StackSlot(temp_offset);
3373 codegen_->Move(stack_temp, source);
3374 __ flds(Address(CpuRegister(RSP), temp_offset));
3375 } else {
3376 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3377 codegen_->Move(stack_temp, source);
3378 __ fldl(Address(CpuRegister(RSP), temp_offset));
3379 }
3380 }
3381}
3382
3383void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003384 DataType::Type type = rem->GetResultType();
3385 bool is_float = type == DataType::Type::kFloat32;
3386 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003387 LocationSummary* locations = rem->GetLocations();
3388 Location first = locations->InAt(0);
3389 Location second = locations->InAt(1);
3390 Location out = locations->Out();
3391
3392 // Create stack space for 2 elements.
3393 // TODO: enhance register allocator to ask for stack temporaries.
3394 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3395
3396 // Load the values to the FP stack in reverse order, using temporaries if needed.
3397 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3398 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3399
3400 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003401 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003402 __ Bind(&retry);
3403 __ fprem();
3404
3405 // Move FP status to AX.
3406 __ fstsw();
3407
3408 // And see if the argument reduction is complete. This is signaled by the
3409 // C2 FPU flag bit set to 0.
3410 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3411 __ j(kNotEqual, &retry);
3412
3413 // We have settled on the final value. Retrieve it into an XMM register.
3414 // Store FP top of stack to real stack.
3415 if (is_float) {
3416 __ fsts(Address(CpuRegister(RSP), 0));
3417 } else {
3418 __ fstl(Address(CpuRegister(RSP), 0));
3419 }
3420
3421 // Pop the 2 items from the FP stack.
3422 __ fucompp();
3423
3424 // Load the value from the stack into an XMM register.
3425 DCHECK(out.IsFpuRegister()) << out;
3426 if (is_float) {
3427 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3428 } else {
3429 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3430 }
3431
3432 // And remove the temporary stack space we allocated.
3433 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3434}
3435
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003436void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3437 DCHECK(instruction->IsDiv() || instruction->IsRem());
3438
3439 LocationSummary* locations = instruction->GetLocations();
3440 Location second = locations->InAt(1);
3441 DCHECK(second.IsConstant());
3442
3443 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3444 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003445 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446
3447 DCHECK(imm == 1 || imm == -1);
3448
3449 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003450 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 if (instruction->IsRem()) {
3452 __ xorl(output_register, output_register);
3453 } else {
3454 __ movl(output_register, input_register);
3455 if (imm == -1) {
3456 __ negl(output_register);
3457 }
3458 }
3459 break;
3460 }
3461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003463 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003464 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003465 } else {
3466 __ movq(output_register, input_register);
3467 if (imm == -1) {
3468 __ negq(output_register);
3469 }
3470 }
3471 break;
3472 }
3473
3474 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003475 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476 }
3477}
3478
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003479void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 LocationSummary* locations = instruction->GetLocations();
3481 Location second = locations->InAt(1);
3482
3483 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3484 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3485
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003487 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3488 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489
3490 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3491
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003492 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003493 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003494 __ testl(numerator, numerator);
3495 __ cmov(kGreaterEqual, tmp, numerator);
3496 int shift = CTZ(imm);
3497 __ sarl(tmp, Immediate(shift));
3498
3499 if (imm < 0) {
3500 __ negl(tmp);
3501 }
3502
3503 __ movl(output_register, tmp);
3504 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003505 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3507
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003508 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 __ addq(rdx, numerator);
3510 __ testq(numerator, numerator);
3511 __ cmov(kGreaterEqual, rdx, numerator);
3512 int shift = CTZ(imm);
3513 __ sarq(rdx, Immediate(shift));
3514
3515 if (imm < 0) {
3516 __ negq(rdx);
3517 }
3518
3519 __ movq(output_register, rdx);
3520 }
3521}
3522
3523void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3524 DCHECK(instruction->IsDiv() || instruction->IsRem());
3525
3526 LocationSummary* locations = instruction->GetLocations();
3527 Location second = locations->InAt(1);
3528
3529 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3530 : locations->GetTemp(0).AsRegister<CpuRegister>();
3531 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3532 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3533 : locations->Out().AsRegister<CpuRegister>();
3534 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3535
3536 DCHECK_EQ(RAX, eax.AsRegister());
3537 DCHECK_EQ(RDX, edx.AsRegister());
3538 if (instruction->IsDiv()) {
3539 DCHECK_EQ(RAX, out.AsRegister());
3540 } else {
3541 DCHECK_EQ(RDX, out.AsRegister());
3542 }
3543
3544 int64_t magic;
3545 int shift;
3546
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003547 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003548 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003549 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3550
3551 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3552
3553 __ movl(numerator, eax);
3554
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 __ movl(eax, Immediate(magic));
3556 __ imull(numerator);
3557
3558 if (imm > 0 && magic < 0) {
3559 __ addl(edx, numerator);
3560 } else if (imm < 0 && magic > 0) {
3561 __ subl(edx, numerator);
3562 }
3563
3564 if (shift != 0) {
3565 __ sarl(edx, Immediate(shift));
3566 }
3567
3568 __ movl(eax, edx);
3569 __ shrl(edx, Immediate(31));
3570 __ addl(edx, eax);
3571
3572 if (instruction->IsRem()) {
3573 __ movl(eax, numerator);
3574 __ imull(edx, Immediate(imm));
3575 __ subl(eax, edx);
3576 __ movl(edx, eax);
3577 } else {
3578 __ movl(eax, edx);
3579 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 } else {
3581 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3582
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003583 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584
3585 CpuRegister rax = eax;
3586 CpuRegister rdx = edx;
3587
3588 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3589
3590 // Save the numerator.
3591 __ movq(numerator, rax);
3592
3593 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003594 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595
3596 // RDX:RAX = magic * numerator
3597 __ imulq(numerator);
3598
3599 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003600 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601 __ addq(rdx, numerator);
3602 } else if (imm < 0 && magic > 0) {
3603 // RDX -= numerator
3604 __ subq(rdx, numerator);
3605 }
3606
3607 // Shift if needed.
3608 if (shift != 0) {
3609 __ sarq(rdx, Immediate(shift));
3610 }
3611
3612 // RDX += 1 if RDX < 0
3613 __ movq(rax, rdx);
3614 __ shrq(rdx, Immediate(63));
3615 __ addq(rdx, rax);
3616
3617 if (instruction->IsRem()) {
3618 __ movq(rax, numerator);
3619
3620 if (IsInt<32>(imm)) {
3621 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3622 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003623 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 }
3625
3626 __ subq(rax, rdx);
3627 __ movq(rdx, rax);
3628 } else {
3629 __ movq(rax, rdx);
3630 }
3631 }
3632}
3633
Calin Juravlebacfec32014-11-14 15:54:36 +00003634void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3635 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 DataType::Type type = instruction->GetResultType();
3637 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003638
3639 bool is_div = instruction->IsDiv();
3640 LocationSummary* locations = instruction->GetLocations();
3641
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003642 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3643 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003644
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003646 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003647
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003649 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003650
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651 if (imm == 0) {
3652 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3653 } else if (imm == 1 || imm == -1) {
3654 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003655 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003656 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003657 } else {
3658 DCHECK(imm <= -2 || imm >= 2);
3659 GenerateDivRemWithAnyConstant(instruction);
3660 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003661 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003662 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003663 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003664 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003665 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003666
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003667 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3668 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3669 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3670 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003672 __ cmpl(second_reg, Immediate(-1));
3673 __ j(kEqual, slow_path->GetEntryLabel());
3674 // edx:eax <- sign-extended of eax
3675 __ cdq();
3676 // eax = quotient, edx = remainder
3677 __ idivl(second_reg);
3678 } else {
3679 __ cmpq(second_reg, Immediate(-1));
3680 __ j(kEqual, slow_path->GetEntryLabel());
3681 // rdx:rax <- sign-extended of rax
3682 __ cqo();
3683 // rax = quotient, rdx = remainder
3684 __ idivq(second_reg);
3685 }
3686 __ Bind(slow_path->GetExitLabel());
3687 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003688}
3689
Calin Juravle7c4954d2014-10-28 16:57:40 +00003690void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3691 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003692 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003693 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003694 case DataType::Type::kInt32:
3695 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003696 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003697 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003698 locations->SetOut(Location::SameAsFirstInput());
3699 // Intel uses edx:eax as the dividend.
3700 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003701 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3702 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3703 // output and request another temp.
3704 if (div->InputAt(1)->IsConstant()) {
3705 locations->AddTemp(Location::RequiresRegister());
3706 }
Calin Juravled0d48522014-11-04 16:40:20 +00003707 break;
3708 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003710 case DataType::Type::kFloat32:
3711 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003712 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003713 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003714 locations->SetOut(Location::SameAsFirstInput());
3715 break;
3716 }
3717
3718 default:
3719 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3720 }
3721}
3722
3723void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3724 LocationSummary* locations = div->GetLocations();
3725 Location first = locations->InAt(0);
3726 Location second = locations->InAt(1);
3727 DCHECK(first.Equals(locations->Out()));
3728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003729 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003730 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003731 case DataType::Type::kInt32:
3732 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003733 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003734 break;
3735 }
3736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003738 if (second.IsFpuRegister()) {
3739 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3740 } else if (second.IsConstant()) {
3741 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003742 codegen_->LiteralFloatAddress(
3743 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003744 } else {
3745 DCHECK(second.IsStackSlot());
3746 __ divss(first.AsFpuRegister<XmmRegister>(),
3747 Address(CpuRegister(RSP), second.GetStackIndex()));
3748 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003749 break;
3750 }
3751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003752 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003753 if (second.IsFpuRegister()) {
3754 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3755 } else if (second.IsConstant()) {
3756 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003757 codegen_->LiteralDoubleAddress(
3758 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003759 } else {
3760 DCHECK(second.IsDoubleStackSlot());
3761 __ divsd(first.AsFpuRegister<XmmRegister>(),
3762 Address(CpuRegister(RSP), second.GetStackIndex()));
3763 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003764 break;
3765 }
3766
3767 default:
3768 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3769 }
3770}
3771
Calin Juravlebacfec32014-11-14 15:54:36 +00003772void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003773 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003774 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003775 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003776
3777 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003778 case DataType::Type::kInt32:
3779 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003781 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003782 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3783 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003784 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3785 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3786 // output and request another temp.
3787 if (rem->InputAt(1)->IsConstant()) {
3788 locations->AddTemp(Location::RequiresRegister());
3789 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003790 break;
3791 }
3792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 case DataType::Type::kFloat32:
3794 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003795 locations->SetInAt(0, Location::Any());
3796 locations->SetInAt(1, Location::Any());
3797 locations->SetOut(Location::RequiresFpuRegister());
3798 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003799 break;
3800 }
3801
3802 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003803 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003804 }
3805}
3806
3807void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003808 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kInt32:
3811 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003812 GenerateDivRemIntegral(rem);
3813 break;
3814 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 case DataType::Type::kFloat32:
3816 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003817 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003818 break;
3819 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003820 default:
3821 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3822 }
3823}
3824
Calin Juravled0d48522014-11-04 16:40:20 +00003825void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003826 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003827 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003828}
3829
3830void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003831 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003832 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003833 codegen_->AddSlowPath(slow_path);
3834
3835 LocationSummary* locations = instruction->GetLocations();
3836 Location value = locations->InAt(0);
3837
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003838 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003839 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003840 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kInt8:
3842 case DataType::Type::kUint16:
3843 case DataType::Type::kInt16:
3844 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003845 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003846 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003847 __ j(kEqual, slow_path->GetEntryLabel());
3848 } else if (value.IsStackSlot()) {
3849 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3850 __ j(kEqual, slow_path->GetEntryLabel());
3851 } else {
3852 DCHECK(value.IsConstant()) << value;
3853 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003854 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003855 }
3856 }
3857 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003858 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003860 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003861 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003862 __ j(kEqual, slow_path->GetEntryLabel());
3863 } else if (value.IsDoubleStackSlot()) {
3864 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3865 __ j(kEqual, slow_path->GetEntryLabel());
3866 } else {
3867 DCHECK(value.IsConstant()) << value;
3868 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003869 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003870 }
3871 }
3872 break;
3873 }
3874 default:
3875 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003876 }
Calin Juravled0d48522014-11-04 16:40:20 +00003877}
3878
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3880 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3881
3882 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003883 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003884
3885 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 case DataType::Type::kInt32:
3887 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003888 locations->SetInAt(0, Location::RequiresRegister());
3889 // The shift count needs to be in CL.
3890 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3891 locations->SetOut(Location::SameAsFirstInput());
3892 break;
3893 }
3894 default:
3895 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3896 }
3897}
3898
3899void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3900 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3901
3902 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003903 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 Location second = locations->InAt(1);
3905
3906 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003907 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003909 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910 if (op->IsShl()) {
3911 __ shll(first_reg, second_reg);
3912 } else if (op->IsShr()) {
3913 __ sarl(first_reg, second_reg);
3914 } else {
3915 __ shrl(first_reg, second_reg);
3916 }
3917 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003918 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003919 if (op->IsShl()) {
3920 __ shll(first_reg, imm);
3921 } else if (op->IsShr()) {
3922 __ sarl(first_reg, imm);
3923 } else {
3924 __ shrl(first_reg, imm);
3925 }
3926 }
3927 break;
3928 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003930 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932 if (op->IsShl()) {
3933 __ shlq(first_reg, second_reg);
3934 } else if (op->IsShr()) {
3935 __ sarq(first_reg, second_reg);
3936 } else {
3937 __ shrq(first_reg, second_reg);
3938 }
3939 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003940 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941 if (op->IsShl()) {
3942 __ shlq(first_reg, imm);
3943 } else if (op->IsShr()) {
3944 __ sarq(first_reg, imm);
3945 } else {
3946 __ shrq(first_reg, imm);
3947 }
3948 }
3949 break;
3950 }
3951 default:
3952 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003953 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954 }
3955}
3956
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003957void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3958 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003959 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003960
3961 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kInt32:
3963 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003964 locations->SetInAt(0, Location::RequiresRegister());
3965 // The shift count needs to be in CL (unless it is a constant).
3966 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3967 locations->SetOut(Location::SameAsFirstInput());
3968 break;
3969 }
3970 default:
3971 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3972 UNREACHABLE();
3973 }
3974}
3975
3976void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3977 LocationSummary* locations = ror->GetLocations();
3978 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3979 Location second = locations->InAt(1);
3980
3981 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003982 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003983 if (second.IsRegister()) {
3984 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3985 __ rorl(first_reg, second_reg);
3986 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003987 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003988 __ rorl(first_reg, imm);
3989 }
3990 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003991 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003992 if (second.IsRegister()) {
3993 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3994 __ rorq(first_reg, second_reg);
3995 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003996 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003997 __ rorq(first_reg, imm);
3998 }
3999 break;
4000 default:
4001 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4002 UNREACHABLE();
4003 }
4004}
4005
Calin Juravle9aec02f2014-11-18 23:06:35 +00004006void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4007 HandleShift(shl);
4008}
4009
4010void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4011 HandleShift(shl);
4012}
4013
4014void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4015 HandleShift(shr);
4016}
4017
4018void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4019 HandleShift(shr);
4020}
4021
4022void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4023 HandleShift(ushr);
4024}
4025
4026void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4027 HandleShift(ushr);
4028}
4029
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004031 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4032 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004033 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004034 if (instruction->IsStringAlloc()) {
4035 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4036 } else {
4037 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004038 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004039 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004040}
4041
4042void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004043 // Note: if heap poisoning is enabled, the entry point takes cares
4044 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004045 if (instruction->IsStringAlloc()) {
4046 // String is allocated through StringFactory. Call NewEmptyString entry point.
4047 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004048 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004049 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4050 __ call(Address(temp, code_offset.SizeValue()));
4051 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4052 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004053 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004054 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004055 DCHECK(!codegen_->IsLeafMethod());
4056 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004057}
4058
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004059void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004060 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4061 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004062 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004063 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004064 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4065 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004066}
4067
4068void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004069 // Note: if heap poisoning is enabled, the entry point takes cares
4070 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004071 QuickEntrypointEnum entrypoint =
4072 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4073 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004074 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004075 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004076}
4077
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004078void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004080 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004081 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4082 if (location.IsStackSlot()) {
4083 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4084 } else if (location.IsDoubleStackSlot()) {
4085 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4086 }
4087 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004088}
4089
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004090void InstructionCodeGeneratorX86_64::VisitParameterValue(
4091 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004092 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004093}
4094
4095void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4096 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004097 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004098 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4099}
4100
4101void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4102 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4103 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004104}
4105
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004106void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004108 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004109 locations->SetInAt(0, Location::RequiresRegister());
4110 locations->SetOut(Location::RequiresRegister());
4111}
4112
4113void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4114 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004115 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004116 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004117 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004118 __ movq(locations->Out().AsRegister<CpuRegister>(),
4119 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004120 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004121 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004122 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004123 __ movq(locations->Out().AsRegister<CpuRegister>(),
4124 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4125 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004126 __ movq(locations->Out().AsRegister<CpuRegister>(),
4127 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004128 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004129}
4130
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004131void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004132 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004133 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004134 locations->SetInAt(0, Location::RequiresRegister());
4135 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004136}
4137
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004138void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4139 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004140 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4141 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004142 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004143 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004146 break;
4147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004148 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004149 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004150 break;
4151
4152 default:
4153 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4154 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004155}
4156
David Brazdil66d126e2015-04-03 16:02:44 +01004157void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4158 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004159 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004160 locations->SetInAt(0, Location::RequiresRegister());
4161 locations->SetOut(Location::SameAsFirstInput());
4162}
4163
4164void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004165 LocationSummary* locations = bool_not->GetLocations();
4166 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4167 locations->Out().AsRegister<CpuRegister>().AsRegister());
4168 Location out = locations->Out();
4169 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4170}
4171
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004172void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004173 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004174 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004175 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004176 locations->SetInAt(i, Location::Any());
4177 }
4178 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004179}
4180
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004181void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004182 LOG(FATAL) << "Unimplemented";
4183}
4184
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004185void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004186 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004187 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004188 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004189 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4190 */
4191 switch (kind) {
4192 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004193 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004194 break;
4195 }
4196 case MemBarrierKind::kAnyStore:
4197 case MemBarrierKind::kLoadAny:
4198 case MemBarrierKind::kStoreStore: {
4199 // nop
4200 break;
4201 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004202 case MemBarrierKind::kNTStoreStore:
4203 // Non-Temporal Store/Store needs an explicit fence.
4204 MemoryFence(/* non-temporal */ true);
4205 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004206 }
4207}
4208
4209void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4210 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4211
Roland Levillain0d5a2812015-11-13 10:07:31 +00004212 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004215 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4216 object_field_get_with_read_barrier
4217 ? LocationSummary::kCallOnSlowPath
4218 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004219 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004220 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004221 }
Calin Juravle52c48962014-12-16 17:02:57 +00004222 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004223 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004224 locations->SetOut(Location::RequiresFpuRegister());
4225 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004226 // The output overlaps for an object field get when read barriers
4227 // are enabled: we do not want the move to overwrite the object's
4228 // location, as we need it to emit the read barrier.
4229 locations->SetOut(
4230 Location::RequiresRegister(),
4231 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004232 }
Calin Juravle52c48962014-12-16 17:02:57 +00004233}
4234
4235void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4236 const FieldInfo& field_info) {
4237 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4238
4239 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004240 Location base_loc = locations->InAt(0);
4241 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004242 Location out = locations->Out();
4243 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004244 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4245 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004246 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4247
Vladimir Marko61b92282017-10-11 13:23:17 +01004248 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004249 case DataType::Type::kBool:
4250 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4252 break;
4253 }
4254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004255 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004256 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4257 break;
4258 }
4259
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004260 case DataType::Type::kUint16: {
4261 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004262 break;
4263 }
4264
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004265 case DataType::Type::kInt16: {
4266 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004267 break;
4268 }
4269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004270 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004271 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4272 break;
4273 }
4274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004275 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004276 // /* HeapReference<Object> */ out = *(base + offset)
4277 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004278 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004279 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004280 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004281 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004282 if (is_volatile) {
4283 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4284 }
4285 } else {
4286 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4287 codegen_->MaybeRecordImplicitNullCheck(instruction);
4288 if (is_volatile) {
4289 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4290 }
4291 // If read barriers are enabled, emit read barriers other than
4292 // Baker's using a slow path (and also unpoison the loaded
4293 // reference, if heap poisoning is enabled).
4294 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4295 }
4296 break;
4297 }
4298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004299 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004300 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4301 break;
4302 }
4303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004304 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004305 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4306 break;
4307 }
4308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004309 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004310 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4311 break;
4312 }
4313
Aart Bik66c158e2018-01-31 12:55:04 -08004314 case DataType::Type::kUint32:
4315 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004316 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004317 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004318 UNREACHABLE();
4319 }
4320
Vladimir Marko61b92282017-10-11 13:23:17 +01004321 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004322 // Potential implicit null checks, in the case of reference
4323 // fields, are handled in the previous switch statement.
4324 } else {
4325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004326 }
Roland Levillain4d027112015-07-01 15:41:14 +01004327
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004328 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004329 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004330 // Memory barriers, in the case of references, are also handled
4331 // in the previous switch statement.
4332 } else {
4333 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4334 }
Roland Levillain4d027112015-07-01 15:41:14 +01004335 }
Calin Juravle52c48962014-12-16 17:02:57 +00004336}
4337
4338void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4339 const FieldInfo& field_info) {
4340 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4341
4342 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004343 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004344 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004345 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004346 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004347 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004348
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004349 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004350 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004351 if (is_volatile) {
4352 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4353 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4354 } else {
4355 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4356 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004357 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004358 if (is_volatile) {
4359 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4360 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4361 } else {
4362 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4363 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004364 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004365 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004366 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004367 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004368 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004369 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004370 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004371 locations->AddTemp(Location::RequiresRegister());
4372 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373}
4374
Calin Juravle52c48962014-12-16 17:02:57 +00004375void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004376 const FieldInfo& field_info,
4377 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004378 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4379
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004381 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4382 Location value = locations->InAt(1);
4383 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004384 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004385 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4386
4387 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004388 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004389 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390
Mark Mendellea5af682015-10-22 17:35:49 -04004391 bool maybe_record_implicit_null_check_done = false;
4392
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004393 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004395 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004396 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004397 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004398 __ movb(Address(base, offset),
4399 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004400 } else {
4401 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4402 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004403 break;
4404 }
4405
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004406 case DataType::Type::kUint16:
4407 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004408 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004409 __ movw(Address(base, offset),
4410 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004411 } else {
4412 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4413 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414 break;
4415 }
4416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 case DataType::Type::kInt32:
4418 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004419 if (value.IsConstant()) {
4420 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004421 // `field_type == DataType::Type::kReference` implies `v == 0`.
4422 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004423 // Note: if heap poisoning is enabled, no need to poison
4424 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004425 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004426 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004427 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004428 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4429 __ movl(temp, value.AsRegister<CpuRegister>());
4430 __ PoisonHeapReference(temp);
4431 __ movl(Address(base, offset), temp);
4432 } else {
4433 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4434 }
Mark Mendell40741f32015-04-20 22:10:34 -04004435 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436 break;
4437 }
4438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004439 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004440 if (value.IsConstant()) {
4441 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004442 codegen_->MoveInt64ToAddress(Address(base, offset),
4443 Address(base, offset + sizeof(int32_t)),
4444 v,
4445 instruction);
4446 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004447 } else {
4448 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4449 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004450 break;
4451 }
4452
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004453 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004454 if (value.IsConstant()) {
4455 int32_t v =
4456 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4457 __ movl(Address(base, offset), Immediate(v));
4458 } else {
4459 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4460 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004461 break;
4462 }
4463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004465 if (value.IsConstant()) {
4466 int64_t v =
4467 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4468 codegen_->MoveInt64ToAddress(Address(base, offset),
4469 Address(base, offset + sizeof(int32_t)),
4470 v,
4471 instruction);
4472 maybe_record_implicit_null_check_done = true;
4473 } else {
4474 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4475 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004476 break;
4477 }
4478
Aart Bik66c158e2018-01-31 12:55:04 -08004479 case DataType::Type::kUint32:
4480 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004481 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004483 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484 }
Calin Juravle52c48962014-12-16 17:02:57 +00004485
Mark Mendellea5af682015-10-22 17:35:49 -04004486 if (!maybe_record_implicit_null_check_done) {
4487 codegen_->MaybeRecordImplicitNullCheck(instruction);
4488 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004489
4490 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4491 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4492 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004493 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004494 }
4495
Calin Juravle52c48962014-12-16 17:02:57 +00004496 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004497 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004498 }
4499}
4500
4501void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4502 HandleFieldSet(instruction, instruction->GetFieldInfo());
4503}
4504
4505void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004506 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004507}
4508
4509void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004510 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004511}
4512
4513void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004514 HandleFieldGet(instruction, instruction->GetFieldInfo());
4515}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004516
Calin Juravle52c48962014-12-16 17:02:57 +00004517void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4518 HandleFieldGet(instruction);
4519}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520
Calin Juravle52c48962014-12-16 17:02:57 +00004521void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4522 HandleFieldGet(instruction, instruction->GetFieldInfo());
4523}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524
Calin Juravle52c48962014-12-16 17:02:57 +00004525void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4526 HandleFieldSet(instruction, instruction->GetFieldInfo());
4527}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528
Calin Juravle52c48962014-12-16 17:02:57 +00004529void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004530 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531}
4532
Calin Juravlee460d1d2015-09-29 04:52:17 +01004533void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4534 HUnresolvedInstanceFieldGet* instruction) {
4535 FieldAccessCallingConventionX86_64 calling_convention;
4536 codegen_->CreateUnresolvedFieldLocationSummary(
4537 instruction, instruction->GetFieldType(), calling_convention);
4538}
4539
4540void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4541 HUnresolvedInstanceFieldGet* instruction) {
4542 FieldAccessCallingConventionX86_64 calling_convention;
4543 codegen_->GenerateUnresolvedFieldAccess(instruction,
4544 instruction->GetFieldType(),
4545 instruction->GetFieldIndex(),
4546 instruction->GetDexPc(),
4547 calling_convention);
4548}
4549
4550void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4551 HUnresolvedInstanceFieldSet* instruction) {
4552 FieldAccessCallingConventionX86_64 calling_convention;
4553 codegen_->CreateUnresolvedFieldLocationSummary(
4554 instruction, instruction->GetFieldType(), calling_convention);
4555}
4556
4557void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4558 HUnresolvedInstanceFieldSet* instruction) {
4559 FieldAccessCallingConventionX86_64 calling_convention;
4560 codegen_->GenerateUnresolvedFieldAccess(instruction,
4561 instruction->GetFieldType(),
4562 instruction->GetFieldIndex(),
4563 instruction->GetDexPc(),
4564 calling_convention);
4565}
4566
4567void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4568 HUnresolvedStaticFieldGet* instruction) {
4569 FieldAccessCallingConventionX86_64 calling_convention;
4570 codegen_->CreateUnresolvedFieldLocationSummary(
4571 instruction, instruction->GetFieldType(), calling_convention);
4572}
4573
4574void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4575 HUnresolvedStaticFieldGet* instruction) {
4576 FieldAccessCallingConventionX86_64 calling_convention;
4577 codegen_->GenerateUnresolvedFieldAccess(instruction,
4578 instruction->GetFieldType(),
4579 instruction->GetFieldIndex(),
4580 instruction->GetDexPc(),
4581 calling_convention);
4582}
4583
4584void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4585 HUnresolvedStaticFieldSet* instruction) {
4586 FieldAccessCallingConventionX86_64 calling_convention;
4587 codegen_->CreateUnresolvedFieldLocationSummary(
4588 instruction, instruction->GetFieldType(), calling_convention);
4589}
4590
4591void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4592 HUnresolvedStaticFieldSet* instruction) {
4593 FieldAccessCallingConventionX86_64 calling_convention;
4594 codegen_->GenerateUnresolvedFieldAccess(instruction,
4595 instruction->GetFieldType(),
4596 instruction->GetFieldIndex(),
4597 instruction->GetDexPc(),
4598 calling_convention);
4599}
4600
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004602 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4603 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4604 ? Location::RequiresRegister()
4605 : Location::Any();
4606 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607}
4608
Calin Juravle2ae48182016-03-16 14:05:09 +00004609void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4610 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004611 return;
4612 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004613 LocationSummary* locations = instruction->GetLocations();
4614 Location obj = locations->InAt(0);
4615
4616 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004617 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004618}
4619
Calin Juravle2ae48182016-03-16 14:05:09 +00004620void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004621 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004622 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004623
4624 LocationSummary* locations = instruction->GetLocations();
4625 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004626
4627 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004628 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004629 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004631 } else {
4632 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004633 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004634 __ jmp(slow_path->GetEntryLabel());
4635 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004636 }
4637 __ j(kEqual, slow_path->GetEntryLabel());
4638}
4639
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004640void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004641 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004642}
4643
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004644void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004645 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004646 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004647 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004648 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4649 object_array_get_with_read_barrier
4650 ? LocationSummary::kCallOnSlowPath
4651 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004652 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004653 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004654 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004655 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004656 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004657 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4659 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004660 // The output overlaps for an object array get when read barriers
4661 // are enabled: we do not want the move to overwrite the array's
4662 // location, as we need it to emit the read barrier.
4663 locations->SetOut(
4664 Location::RequiresRegister(),
4665 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667}
4668
4669void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4670 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004671 Location obj_loc = locations->InAt(0);
4672 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004674 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004675 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004678 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004679 case DataType::Type::kBool:
4680 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004681 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004682 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004683 break;
4684 }
4685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004686 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004687 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004688 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 break;
4690 }
4691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004693 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004694 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4695 // Branch cases into compressed and uncompressed for each index's type.
4696 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4697 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004698 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004699 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004700 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4701 "Expecting 0=compressed, 1=uncompressed");
4702 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004703 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4704 __ jmp(&done);
4705 __ Bind(&not_compressed);
4706 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4707 __ Bind(&done);
4708 } else {
4709 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4710 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 break;
4712 }
4713
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004714 case DataType::Type::kInt16: {
4715 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4716 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4717 break;
4718 }
4719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004720 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004721 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004722 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004723 break;
4724 }
4725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004726 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004727 static_assert(
4728 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4729 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004730 // /* HeapReference<Object> */ out =
4731 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4732 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004733 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004734 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004735 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004736 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004737 } else {
4738 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004739 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4740 codegen_->MaybeRecordImplicitNullCheck(instruction);
4741 // If read barriers are enabled, emit read barriers other than
4742 // Baker's using a slow path (and also unpoison the loaded
4743 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 if (index.IsConstant()) {
4745 uint32_t offset =
4746 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004747 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4748 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004749 codegen_->MaybeGenerateReadBarrierSlow(
4750 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4751 }
4752 }
4753 break;
4754 }
4755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004756 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004757 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004758 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 break;
4760 }
4761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004762 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004763 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004764 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004765 break;
4766 }
4767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004769 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004770 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004771 break;
4772 }
4773
Aart Bik66c158e2018-01-31 12:55:04 -08004774 case DataType::Type::kUint32:
4775 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004777 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004778 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 }
Roland Levillain4d027112015-07-01 15:41:14 +01004780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004781 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004782 // Potential implicit null checks, in the case of reference
4783 // arrays, are handled in the previous switch statement.
4784 } else {
4785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004786 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787}
4788
4789void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004790 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004791
4792 bool needs_write_barrier =
4793 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004794 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004795
Vladimir Markoca6fff82017-10-03 14:49:14 +01004796 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004798 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004799 LocationSummary::kCallOnSlowPath :
4800 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004801
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004802 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004803 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004804 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04004805 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 } else {
4807 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4808 }
4809
4810 if (needs_write_barrier) {
4811 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004812 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004813 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004814 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815}
4816
4817void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004819 Location array_loc = locations->InAt(0);
4820 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004822 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004824 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004825 bool needs_write_barrier =
4826 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4828 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4829 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830
4831 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004833 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004836 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 if (value.IsRegister()) {
4838 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004839 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004840 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004842 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004843 break;
4844 }
4845
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004846 case DataType::Type::kUint16:
4847 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004849 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004850 if (value.IsRegister()) {
4851 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01004854 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004855 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857 break;
4858 }
4859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004862 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004863
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004864 if (!value.IsRegister()) {
4865 // Just setting null.
4866 DCHECK(instruction->InputAt(2)->IsNullConstant());
4867 DCHECK(value.IsConstant()) << value;
4868 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004871 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004872 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004873 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004874
4875 DCHECK(needs_write_barrier);
4876 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004877 // We cannot use a NearLabel for `done`, as its range may be too
4878 // short when Baker read barriers are enabled.
4879 Label done;
4880 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004881 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004882 Location temp_loc = locations->GetTemp(0);
4883 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004884 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004885 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004886 codegen_->AddSlowPath(slow_path);
4887 if (instruction->GetValueCanBeNull()) {
4888 __ testl(register_value, register_value);
4889 __ j(kNotEqual, &not_null);
4890 __ movl(address, Immediate(0));
4891 codegen_->MaybeRecordImplicitNullCheck(instruction);
4892 __ jmp(&done);
4893 __ Bind(&not_null);
4894 }
4895
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004896 // Note that when Baker read barriers are enabled, the type
4897 // checks are performed without read barriers. This is fine,
4898 // even in the case where a class object is in the from-space
4899 // after the flip, as a comparison involving such a type would
4900 // not produce a false positive; it may of course produce a
4901 // false negative, in which case we would take the ArraySet
4902 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004903
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004904 // /* HeapReference<Class> */ temp = array->klass_
4905 __ movl(temp, Address(array, class_offset));
4906 codegen_->MaybeRecordImplicitNullCheck(instruction);
4907 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004908
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004909 // /* HeapReference<Class> */ temp = temp->component_type_
4910 __ movl(temp, Address(temp, component_offset));
4911 // If heap poisoning is enabled, no need to unpoison `temp`
4912 // nor the object reference in `register_value->klass`, as
4913 // we are comparing two poisoned references.
4914 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004915
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004916 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4917 __ j(kEqual, &do_put);
4918 // If heap poisoning is enabled, the `temp` reference has
4919 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004920 __ MaybeUnpoisonHeapReference(temp);
4921
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004922 // If heap poisoning is enabled, no need to unpoison the
4923 // heap reference loaded below, as it is only used for a
4924 // comparison with null.
4925 __ cmpl(Address(temp, super_offset), Immediate(0));
4926 __ j(kNotEqual, slow_path->GetEntryLabel());
4927 __ Bind(&do_put);
4928 } else {
4929 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004930 }
4931 }
4932
4933 if (kPoisonHeapReferences) {
4934 __ movl(temp, register_value);
4935 __ PoisonHeapReference(temp);
4936 __ movl(address, temp);
4937 } else {
4938 __ movl(address, register_value);
4939 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004940 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004941 codegen_->MaybeRecordImplicitNullCheck(instruction);
4942 }
4943
4944 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4945 codegen_->MarkGCCard(
4946 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4947 __ Bind(&done);
4948
4949 if (slow_path != nullptr) {
4950 __ Bind(slow_path->GetExitLabel());
4951 }
4952
4953 break;
4954 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004956 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004957 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004958 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004959 if (value.IsRegister()) {
4960 __ movl(address, value.AsRegister<CpuRegister>());
4961 } else {
4962 DCHECK(value.IsConstant()) << value;
4963 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4964 __ movl(address, Immediate(v));
4965 }
4966 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004967 break;
4968 }
4969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004970 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004971 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004972 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004973 if (value.IsRegister()) {
4974 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004975 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004977 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004978 Address address_high =
4979 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004980 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 }
4982 break;
4983 }
4984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004985 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004986 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004987 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004988 if (value.IsFpuRegister()) {
4989 __ movss(address, value.AsFpuRegister<XmmRegister>());
4990 } else {
4991 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004992 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004993 __ movl(address, Immediate(v));
4994 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004995 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004996 break;
4997 }
4998
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004999 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005000 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005001 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005002 if (value.IsFpuRegister()) {
5003 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5004 codegen_->MaybeRecordImplicitNullCheck(instruction);
5005 } else {
5006 int64_t v =
5007 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005008 Address address_high =
5009 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005010 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5011 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005012 break;
5013 }
5014
Aart Bik66c158e2018-01-31 12:55:04 -08005015 case DataType::Type::kUint32:
5016 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005019 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020 }
5021}
5022
5023void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005024 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005025 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005026 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005027 if (!instruction->IsEmittedAtUseSite()) {
5028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5029 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005030}
5031
5032void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005033 if (instruction->IsEmittedAtUseSite()) {
5034 return;
5035 }
5036
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005038 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005039 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5040 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005042 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005043 // Mask out most significant bit in case the array is String's array of char.
5044 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005045 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047}
5048
5049void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005050 RegisterSet caller_saves = RegisterSet::Empty();
5051 InvokeRuntimeCallingConvention calling_convention;
5052 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5053 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5054 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005055 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005056 HInstruction* length = instruction->InputAt(1);
5057 if (!length->IsEmittedAtUseSite()) {
5058 locations->SetInAt(1, Location::RegisterOrConstant(length));
5059 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060}
5061
5062void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5063 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005064 Location index_loc = locations->InAt(0);
5065 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005066 SlowPathCode* slow_path =
5067 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068
Mark Mendell99dbd682015-04-22 16:18:52 -04005069 if (length_loc.IsConstant()) {
5070 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5071 if (index_loc.IsConstant()) {
5072 // BCE will remove the bounds check if we are guarenteed to pass.
5073 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5074 if (index < 0 || index >= length) {
5075 codegen_->AddSlowPath(slow_path);
5076 __ jmp(slow_path->GetEntryLabel());
5077 } else {
5078 // Some optimization after BCE may have generated this, and we should not
5079 // generate a bounds check if it is a valid range.
5080 }
5081 return;
5082 }
5083
5084 // We have to reverse the jump condition because the length is the constant.
5085 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5086 __ cmpl(index_reg, Immediate(length));
5087 codegen_->AddSlowPath(slow_path);
5088 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005089 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005090 HInstruction* array_length = instruction->InputAt(1);
5091 if (array_length->IsEmittedAtUseSite()) {
5092 // Address the length field in the array.
5093 DCHECK(array_length->IsArrayLength());
5094 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5095 Location array_loc = array_length->GetLocations()->InAt(0);
5096 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005097 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005098 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5099 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005100 CpuRegister length_reg = CpuRegister(TMP);
5101 __ movl(length_reg, array_len);
5102 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005103 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005104 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005105 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005106 // Checking the bound for general case:
5107 // Array of char or String's array when the compression feature off.
5108 if (index_loc.IsConstant()) {
5109 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5110 __ cmpl(array_len, Immediate(value));
5111 } else {
5112 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5113 }
5114 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005115 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005116 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005117 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005118 }
5119 codegen_->AddSlowPath(slow_path);
5120 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005121 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122}
5123
5124void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5125 CpuRegister card,
5126 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005127 CpuRegister value,
5128 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005129 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005130 if (value_can_be_null) {
5131 __ testl(value, value);
5132 __ j(kEqual, &is_null);
5133 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005134 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005135 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 __ movq(temp, object);
5137 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005138 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005139 if (value_can_be_null) {
5140 __ Bind(&is_null);
5141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142}
5143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005144void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005145 LOG(FATAL) << "Unimplemented";
5146}
5147
5148void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005149 if (instruction->GetNext()->IsSuspendCheck() &&
5150 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5151 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5152 // The back edge will generate the suspend check.
5153 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5154 }
5155
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005156 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5157}
5158
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005159void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005160 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5161 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005162 // In suspend check slow path, usually there are no caller-save registers at all.
5163 // If SIMD instructions are present, however, we force spilling all live SIMD
5164 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005165 locations->SetCustomSlowPathCallerSaves(
5166 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005167}
5168
5169void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005170 HBasicBlock* block = instruction->GetBlock();
5171 if (block->GetLoopInformation() != nullptr) {
5172 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5173 // The back edge will generate the suspend check.
5174 return;
5175 }
5176 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5177 // The goto will generate the suspend check.
5178 return;
5179 }
5180 GenerateSuspendCheck(instruction, nullptr);
5181}
5182
5183void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5184 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005185 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005186 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5187 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005188 slow_path =
5189 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005190 instruction->SetSlowPath(slow_path);
5191 codegen_->AddSlowPath(slow_path);
5192 if (successor != nullptr) {
5193 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005194 }
5195 } else {
5196 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5197 }
5198
Andreas Gampe542451c2016-07-26 09:02:02 -07005199 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005200 /* no_rip */ true),
5201 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005202 if (successor == nullptr) {
5203 __ j(kNotEqual, slow_path->GetEntryLabel());
5204 __ Bind(slow_path->GetReturnLabel());
5205 } else {
5206 __ j(kEqual, codegen_->GetLabelOf(successor));
5207 __ jmp(slow_path->GetEntryLabel());
5208 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005209}
5210
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005211X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5212 return codegen_->GetAssembler();
5213}
5214
5215void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005216 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005217 Location source = move->GetSource();
5218 Location destination = move->GetDestination();
5219
5220 if (source.IsRegister()) {
5221 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005222 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005223 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005224 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005225 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005226 } else {
5227 DCHECK(destination.IsDoubleStackSlot());
5228 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005229 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 }
5231 } else if (source.IsStackSlot()) {
5232 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005233 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005234 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005235 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005236 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005237 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005238 } else {
5239 DCHECK(destination.IsStackSlot());
5240 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5241 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5242 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005243 } else if (source.IsDoubleStackSlot()) {
5244 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005245 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005246 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005247 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005248 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5249 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005250 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005251 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005252 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5253 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5254 }
Aart Bik5576f372017-03-23 16:17:37 -07005255 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005256 if (destination.IsFpuRegister()) {
5257 __ movups(destination.AsFpuRegister<XmmRegister>(),
5258 Address(CpuRegister(RSP), source.GetStackIndex()));
5259 } else {
5260 DCHECK(destination.IsSIMDStackSlot());
5261 size_t high = kX86_64WordSize;
5262 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5263 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5264 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5265 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5266 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005267 } else if (source.IsConstant()) {
5268 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005269 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5270 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005271 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005272 if (value == 0) {
5273 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5274 } else {
5275 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5276 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005277 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005278 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005279 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005280 }
5281 } else if (constant->IsLongConstant()) {
5282 int64_t value = constant->AsLongConstant()->GetValue();
5283 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005284 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005285 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005286 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005287 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005288 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005289 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005290 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005292 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005293 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 } else {
5295 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005296 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005297 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5298 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005299 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005301 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005302 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005303 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005304 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005305 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306 } else {
5307 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005308 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005309 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005310 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 } else if (source.IsFpuRegister()) {
5312 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005313 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 } else if (destination.IsStackSlot()) {
5315 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005316 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005317 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005318 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005319 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005320 } else {
5321 DCHECK(destination.IsSIMDStackSlot());
5322 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5323 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005324 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005325 }
5326}
5327
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005328void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005329 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005330 __ movl(Address(CpuRegister(RSP), mem), reg);
5331 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005332}
5333
Mark Mendell8a1c7282015-06-29 15:41:28 -04005334void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5335 __ movq(CpuRegister(TMP), reg1);
5336 __ movq(reg1, reg2);
5337 __ movq(reg2, CpuRegister(TMP));
5338}
5339
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005340void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5341 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5342 __ movq(Address(CpuRegister(RSP), mem), reg);
5343 __ movq(reg, CpuRegister(TMP));
5344}
5345
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005346void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5347 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5348 __ movss(Address(CpuRegister(RSP), mem), reg);
5349 __ movd(reg, CpuRegister(TMP));
5350}
5351
5352void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5353 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5354 __ movsd(Address(CpuRegister(RSP), mem), reg);
5355 __ movd(reg, CpuRegister(TMP));
5356}
5357
Aart Bikcfe50bb2017-12-12 14:54:12 -08005358void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5359 size_t extra_slot = 2 * kX86_64WordSize;
5360 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5361 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5362 ExchangeMemory64(0, mem + extra_slot, 2);
5363 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5364 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5365}
5366
5367void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5368 ScratchRegisterScope ensure_scratch(
5369 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5370
5371 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5372 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5373 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5374 Address(CpuRegister(RSP), mem2 + stack_offset));
5375 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5376 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5377 CpuRegister(ensure_scratch.GetRegister()));
5378}
5379
5380void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5381 ScratchRegisterScope ensure_scratch(
5382 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5383
5384 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5385
5386 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5387 for (int i = 0; i < num_of_qwords; i++) {
5388 __ movq(CpuRegister(TMP),
5389 Address(CpuRegister(RSP), mem1 + stack_offset));
5390 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5391 Address(CpuRegister(RSP), mem2 + stack_offset));
5392 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5393 CpuRegister(TMP));
5394 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5395 CpuRegister(ensure_scratch.GetRegister()));
5396 stack_offset += kX86_64WordSize;
5397 }
5398}
5399
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005400void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005401 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005402 Location source = move->GetSource();
5403 Location destination = move->GetDestination();
5404
5405 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005406 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005407 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005408 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005409 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005410 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005411 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005412 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005413 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005414 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005415 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005416 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005417 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005418 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005419 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005420 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5421 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5422 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005423 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005424 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005425 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005426 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005427 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005428 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005429 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005430 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005431 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5432 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5433 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5434 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5435 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5436 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005437 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005438 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005439 }
5440}
5441
5442
5443void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5444 __ pushq(CpuRegister(reg));
5445}
5446
5447
5448void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5449 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005450}
5451
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005452void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005453 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005454 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5455 const size_t status_byte_offset =
5456 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5457 constexpr uint32_t shifted_initialized_value =
5458 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5459
5460 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005461 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005462 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005463 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005464}
5465
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005466HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5467 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005468 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005469 case HLoadClass::LoadKind::kInvalid:
5470 LOG(FATAL) << "UNREACHABLE";
5471 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005472 case HLoadClass::LoadKind::kReferrersClass:
5473 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005474 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005475 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005476 case HLoadClass::LoadKind::kBssEntry:
5477 DCHECK(!Runtime::Current()->UseJitCompilation());
5478 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005479 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005480 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005482 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005483 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005484 break;
5485 }
5486 return desired_class_load_kind;
5487}
5488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005489void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005490 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005491 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005492 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005493 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005494 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005495 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005496 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005497 return;
5498 }
Vladimir Marko41559982017-01-06 14:04:23 +00005499 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005500
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005501 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5502 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005503 ? LocationSummary::kCallOnSlowPath
5504 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005505 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005506 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005507 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005508 }
5509
Vladimir Marko41559982017-01-06 14:04:23 +00005510 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005511 locations->SetInAt(0, Location::RequiresRegister());
5512 }
5513 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005514 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5515 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5516 // Rely on the type resolution and/or initialization to save everything.
5517 // Custom calling convention: RAX serves as both input and output.
5518 RegisterSet caller_saves = RegisterSet::Empty();
5519 caller_saves.Add(Location::RegisterLocation(RAX));
5520 locations->SetCustomSlowPathCallerSaves(caller_saves);
5521 } else {
5522 // For non-Baker read barrier we have a temp-clobbering call.
5523 }
5524 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005525}
5526
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005527Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005528 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005529 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005530 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005531 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005532 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005533 PatchInfo<Label>* info = &jit_class_patches_.back();
5534 return &info->label;
5535}
5536
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005537// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5538// move.
5539void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005540 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005541 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005542 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005543 return;
5544 }
Vladimir Marko41559982017-01-06 14:04:23 +00005545 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005546
Vladimir Marko41559982017-01-06 14:04:23 +00005547 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005548 Location out_loc = locations->Out();
5549 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005550
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005551 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5552 ? kWithoutReadBarrier
5553 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005554 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005555 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005556 case HLoadClass::LoadKind::kReferrersClass: {
5557 DCHECK(!cls->CanCallRuntime());
5558 DCHECK(!cls->MustGenerateClinitCheck());
5559 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5560 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5561 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005562 cls,
5563 out_loc,
5564 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005565 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005566 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005567 break;
5568 }
5569 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005570 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005571 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005573 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005574 break;
5575 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005576 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005577 uint32_t address = dchecked_integral_cast<uint32_t>(
5578 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5579 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005580 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 break;
5582 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005583 case HLoadClass::LoadKind::kBootImageClassTable: {
5584 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5585 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5586 codegen_->RecordBootTypePatch(cls);
5587 // Extract the reference from the slot data, i.e. clear the hash bits.
5588 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
5589 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
5590 if (masked_hash != 0) {
5591 __ subl(out, Immediate(masked_hash));
5592 }
5593 break;
5594 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005595 case HLoadClass::LoadKind::kBssEntry: {
5596 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5597 /* no_rip */ false);
5598 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5599 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5600 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5601 generate_null_check = true;
5602 break;
5603 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005604 case HLoadClass::LoadKind::kJitTableAddress: {
5605 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5606 /* no_rip */ true);
5607 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005608 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005609 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005610 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005611 break;
5612 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005613 default:
5614 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5615 UNREACHABLE();
5616 }
5617
5618 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5619 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005620 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005621 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5622 codegen_->AddSlowPath(slow_path);
5623 if (generate_null_check) {
5624 __ testl(out, out);
5625 __ j(kEqual, slow_path->GetEntryLabel());
5626 }
5627 if (cls->MustGenerateClinitCheck()) {
5628 GenerateClassInitializationCheck(slow_path, out);
5629 } else {
5630 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005631 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005632 }
5633}
5634
5635void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5636 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005637 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005638 locations->SetInAt(0, Location::RequiresRegister());
5639 if (check->HasUses()) {
5640 locations->SetOut(Location::SameAsFirstInput());
5641 }
5642}
5643
5644void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005645 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005646 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005647 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005648 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005649 GenerateClassInitializationCheck(slow_path,
5650 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005651}
5652
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005653HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5654 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005655 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005656 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005657 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005658 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005659 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005660 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005661 case HLoadString::LoadKind::kJitTableAddress:
5662 DCHECK(Runtime::Current()->UseJitCompilation());
5663 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005664 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005665 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005666 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005667 }
5668 return desired_string_load_kind;
5669}
5670
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005671void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005672 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005673 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005674 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005675 locations->SetOut(Location::RegisterLocation(RAX));
5676 } else {
5677 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005678 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5679 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005680 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005681 // Custom calling convention: RAX serves as both input and output.
5682 RegisterSet caller_saves = RegisterSet::Empty();
5683 caller_saves.Add(Location::RegisterLocation(RAX));
5684 locations->SetCustomSlowPathCallerSaves(caller_saves);
5685 } else {
5686 // For non-Baker read barrier we have a temp-clobbering call.
5687 }
5688 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005689 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005690}
5691
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005692Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005693 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005694 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005695 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005696 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005697 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005698 PatchInfo<Label>* info = &jit_string_patches_.back();
5699 return &info->label;
5700}
5701
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005702// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5703// move.
5704void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005705 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 Location out_loc = locations->Out();
5707 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005708
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005709 switch (load->GetLoadKind()) {
5710 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005711 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005712 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005713 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005714 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005715 }
5716 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005717 uint32_t address = dchecked_integral_cast<uint32_t>(
5718 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5719 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005720 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005721 return;
5722 }
5723 case HLoadString::LoadKind::kBootImageInternTable: {
5724 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5725 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5726 codegen_->RecordBootStringPatch(load);
5727 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005728 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005729 case HLoadString::LoadKind::kBssEntry: {
5730 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5731 /* no_rip */ false);
5732 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5733 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005734 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005735 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005736 codegen_->AddSlowPath(slow_path);
5737 __ testl(out, out);
5738 __ j(kEqual, slow_path->GetEntryLabel());
5739 __ Bind(slow_path->GetExitLabel());
5740 return;
5741 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005742 case HLoadString::LoadKind::kJitTableAddress: {
5743 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5744 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005745 Label* fixup_label = codegen_->NewJitRootStringPatch(
5746 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005747 // /* GcRoot<mirror::String> */ out = *address
5748 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5749 return;
5750 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005751 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005752 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005754
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005755 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005756 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005757 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005758 codegen_->InvokeRuntime(kQuickResolveString,
5759 load,
5760 load->GetDexPc());
5761 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005762}
5763
David Brazdilcb1c0552015-08-04 16:22:25 +01005764static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005765 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005766 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005767}
5768
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005769void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5770 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005771 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005772 locations->SetOut(Location::RequiresRegister());
5773}
5774
5775void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005776 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5777}
5778
5779void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005780 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005781}
5782
5783void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5784 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005785}
5786
5787void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005788 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5789 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005790 InvokeRuntimeCallingConvention calling_convention;
5791 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5792}
5793
5794void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005795 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005796 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005797}
5798
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005799static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00005800 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005801 // We need a temporary for holding the iftable length.
5802 return true;
5803 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005804 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005805 !kUseBakerReadBarrier &&
5806 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005807 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5808 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5809}
5810
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005811static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5812 return kEmitCompilerReadBarrier &&
5813 !kUseBakerReadBarrier &&
5814 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5815 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5816 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5817}
5818
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005819void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005820 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005822 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005824 case TypeCheckKind::kExactCheck:
5825 case TypeCheckKind::kAbstractClassCheck:
5826 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00005827 case TypeCheckKind::kArrayObjectCheck: {
5828 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5829 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5830 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 break;
Vladimir Marko87584542017-12-12 17:47:52 +00005832 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005834 case TypeCheckKind::kUnresolvedCheck:
5835 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 call_kind = LocationSummary::kCallOnSlowPath;
5837 break;
5838 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839
Vladimir Markoca6fff82017-10-03 14:49:14 +01005840 LocationSummary* locations =
5841 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005842 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005843 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005844 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005845 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005846 locations->SetInAt(1, Location::Any());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5848 locations->SetOut(Location::RequiresRegister());
5849 // When read barriers are enabled, we need a temporary register for
5850 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005851 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005852 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005854}
5855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005856void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005857 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005858 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859 Location obj_loc = locations->InAt(0);
5860 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005861 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 Location out_loc = locations->Out();
5863 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005864 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005865 locations->GetTemp(0) :
5866 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005867 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005868 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5869 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5870 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005871 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005873
5874 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005875 // Avoid null check if we know obj is not null.
5876 if (instruction->MustDoNullCheck()) {
5877 __ testl(obj, obj);
5878 __ j(kEqual, &zero);
5879 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005881 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005883 ReadBarrierOption read_barrier_option =
5884 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005885 // /* HeapReference<Class> */ out = obj->klass_
5886 GenerateReferenceLoadTwoRegisters(instruction,
5887 out_loc,
5888 obj_loc,
5889 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005890 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 if (cls.IsRegister()) {
5892 __ cmpl(out, cls.AsRegister<CpuRegister>());
5893 } else {
5894 DCHECK(cls.IsStackSlot()) << cls;
5895 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5896 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005897 if (zero.IsLinked()) {
5898 // Classes must be equal for the instanceof to succeed.
5899 __ j(kNotEqual, &zero);
5900 __ movl(out, Immediate(1));
5901 __ jmp(&done);
5902 } else {
5903 __ setcc(kEqual, out);
5904 // setcc only sets the low byte.
5905 __ andl(out, Immediate(1));
5906 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907 break;
5908 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005911 ReadBarrierOption read_barrier_option =
5912 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005913 // /* HeapReference<Class> */ out = obj->klass_
5914 GenerateReferenceLoadTwoRegisters(instruction,
5915 out_loc,
5916 obj_loc,
5917 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005918 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919 // If the class is abstract, we eagerly fetch the super class of the
5920 // object to avoid doing a comparison we know will fail.
5921 NearLabel loop, success;
5922 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005923 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005924 GenerateReferenceLoadOneRegister(instruction,
5925 out_loc,
5926 super_offset,
5927 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00005928 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 __ testl(out, out);
5930 // If `out` is null, we use it for the result, and jump to `done`.
5931 __ j(kEqual, &done);
5932 if (cls.IsRegister()) {
5933 __ cmpl(out, cls.AsRegister<CpuRegister>());
5934 } else {
5935 DCHECK(cls.IsStackSlot()) << cls;
5936 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5937 }
5938 __ j(kNotEqual, &loop);
5939 __ movl(out, Immediate(1));
5940 if (zero.IsLinked()) {
5941 __ jmp(&done);
5942 }
5943 break;
5944 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005945
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005947 ReadBarrierOption read_barrier_option =
5948 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005949 // /* HeapReference<Class> */ out = obj->klass_
5950 GenerateReferenceLoadTwoRegisters(instruction,
5951 out_loc,
5952 obj_loc,
5953 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005954 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 // Walk over the class hierarchy to find a match.
5956 NearLabel loop, success;
5957 __ Bind(&loop);
5958 if (cls.IsRegister()) {
5959 __ cmpl(out, cls.AsRegister<CpuRegister>());
5960 } else {
5961 DCHECK(cls.IsStackSlot()) << cls;
5962 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5963 }
5964 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005966 GenerateReferenceLoadOneRegister(instruction,
5967 out_loc,
5968 super_offset,
5969 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00005970 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005971 __ testl(out, out);
5972 __ j(kNotEqual, &loop);
5973 // If `out` is null, we use it for the result, and jump to `done`.
5974 __ jmp(&done);
5975 __ Bind(&success);
5976 __ movl(out, Immediate(1));
5977 if (zero.IsLinked()) {
5978 __ jmp(&done);
5979 }
5980 break;
5981 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005982
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005984 ReadBarrierOption read_barrier_option =
5985 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005986 // /* HeapReference<Class> */ out = obj->klass_
5987 GenerateReferenceLoadTwoRegisters(instruction,
5988 out_loc,
5989 obj_loc,
5990 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005991 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005992 // Do an exact check.
5993 NearLabel exact_check;
5994 if (cls.IsRegister()) {
5995 __ cmpl(out, cls.AsRegister<CpuRegister>());
5996 } else {
5997 DCHECK(cls.IsStackSlot()) << cls;
5998 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5999 }
6000 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006001 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006002 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006003 GenerateReferenceLoadOneRegister(instruction,
6004 out_loc,
6005 component_offset,
6006 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006007 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006008 __ testl(out, out);
6009 // If `out` is null, we use it for the result, and jump to `done`.
6010 __ j(kEqual, &done);
6011 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6012 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006013 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 __ movl(out, Immediate(1));
6015 __ jmp(&done);
6016 break;
6017 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006020 // No read barrier since the slow path will retry upon failure.
6021 // /* HeapReference<Class> */ out = obj->klass_
6022 GenerateReferenceLoadTwoRegisters(instruction,
6023 out_loc,
6024 obj_loc,
6025 class_offset,
6026 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006027 if (cls.IsRegister()) {
6028 __ cmpl(out, cls.AsRegister<CpuRegister>());
6029 } else {
6030 DCHECK(cls.IsStackSlot()) << cls;
6031 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6032 }
6033 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006034 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6035 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 codegen_->AddSlowPath(slow_path);
6037 __ j(kNotEqual, slow_path->GetEntryLabel());
6038 __ movl(out, Immediate(1));
6039 if (zero.IsLinked()) {
6040 __ jmp(&done);
6041 }
6042 break;
6043 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044
Calin Juravle98893e12015-10-02 21:05:03 +01006045 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006046 case TypeCheckKind::kInterfaceCheck: {
6047 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006048 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049 // cases.
6050 //
6051 // We cannot directly call the InstanceofNonTrivial runtime
6052 // entry point without resorting to a type checking slow path
6053 // here (i.e. by calling InvokeRuntime directly), as it would
6054 // require to assign fixed registers for the inputs of this
6055 // HInstanceOf instruction (following the runtime calling
6056 // convention), which might be cluttered by the potential first
6057 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006058 //
6059 // TODO: Introduce a new runtime entry point taking the object
6060 // to test (instead of its class) as argument, and let it deal
6061 // with the read barrier issues. This will let us refactor this
6062 // case of the `switch` code as it was previously (with a direct
6063 // call to the runtime not using a type checking slow path).
6064 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006065 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006066 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6067 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068 codegen_->AddSlowPath(slow_path);
6069 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 if (zero.IsLinked()) {
6071 __ jmp(&done);
6072 }
6073 break;
6074 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006075 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006076
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006077 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006078 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006079 __ xorl(out, out);
6080 }
6081
6082 if (done.IsLinked()) {
6083 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006084 }
6085
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006086 if (slow_path != nullptr) {
6087 __ Bind(slow_path->GetExitLabel());
6088 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006089}
6090
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006091void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006092 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006093 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006094 LocationSummary* locations =
6095 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006097 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6098 // Require a register for the interface check since there is a loop that compares the class to
6099 // a memory address.
6100 locations->SetInAt(1, Location::RequiresRegister());
6101 } else {
6102 locations->SetInAt(1, Location::Any());
6103 }
6104
Roland Levillain0d5a2812015-11-13 10:07:31 +00006105 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6106 locations->AddTemp(Location::RequiresRegister());
6107 // When read barriers are enabled, we need an additional temporary
6108 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006109 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006112}
6113
6114void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006115 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006116 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117 Location obj_loc = locations->InAt(0);
6118 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006119 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006120 Location temp_loc = locations->GetTemp(0);
6121 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006122 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006123 locations->GetTemp(1) :
6124 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006125 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6126 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6127 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6128 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6129 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6130 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006131 const uint32_t object_array_data_offset =
6132 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133
Vladimir Marko87584542017-12-12 17:47:52 +00006134 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006136 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6137 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006138 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006140
6141 NearLabel done;
6142 // Avoid null check if we know obj is not null.
6143 if (instruction->MustDoNullCheck()) {
6144 __ testl(obj, obj);
6145 __ j(kEqual, &done);
6146 }
6147
Roland Levillain0d5a2812015-11-13 10:07:31 +00006148 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 case TypeCheckKind::kExactCheck:
6150 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006151 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006152 GenerateReferenceLoadTwoRegisters(instruction,
6153 temp_loc,
6154 obj_loc,
6155 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006156 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 if (cls.IsRegister()) {
6158 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6159 } else {
6160 DCHECK(cls.IsStackSlot()) << cls;
6161 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6162 }
6163 // Jump to slow path for throwing the exception or doing a
6164 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006165 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 break;
6167 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006170 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006171 GenerateReferenceLoadTwoRegisters(instruction,
6172 temp_loc,
6173 obj_loc,
6174 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006175 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 // If the class is abstract, we eagerly fetch the super class of the
6177 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006178 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006180 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006181 GenerateReferenceLoadOneRegister(instruction,
6182 temp_loc,
6183 super_offset,
6184 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006185 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006186
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006187 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6188 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006190 // Otherwise, compare the classes.
6191 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 if (cls.IsRegister()) {
6193 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6194 } else {
6195 DCHECK(cls.IsStackSlot()) << cls;
6196 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6197 }
6198 __ j(kNotEqual, &loop);
6199 break;
6200 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006203 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006204 GenerateReferenceLoadTwoRegisters(instruction,
6205 temp_loc,
6206 obj_loc,
6207 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006208 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006210 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006211 __ Bind(&loop);
6212 if (cls.IsRegister()) {
6213 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6214 } else {
6215 DCHECK(cls.IsStackSlot()) << cls;
6216 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6217 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006218 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006221 GenerateReferenceLoadOneRegister(instruction,
6222 temp_loc,
6223 super_offset,
6224 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006225 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006226
6227 // If the class reference currently in `temp` is not null, jump
6228 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006229 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006230 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006232 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 break;
6234 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006237 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006238 GenerateReferenceLoadTwoRegisters(instruction,
6239 temp_loc,
6240 obj_loc,
6241 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006242 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006243 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006245 if (cls.IsRegister()) {
6246 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6247 } else {
6248 DCHECK(cls.IsStackSlot()) << cls;
6249 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6250 }
6251 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252
6253 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006255 GenerateReferenceLoadOneRegister(instruction,
6256 temp_loc,
6257 component_offset,
6258 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006259 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006260
6261 // If the component type is not null (i.e. the object is indeed
6262 // an array), jump to label `check_non_primitive_component_type`
6263 // to further check that this component type is not a primitive
6264 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006267 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006269 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 break;
6271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006273 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006274 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 //
6276 // We cannot directly call the CheckCast runtime entry point
6277 // without resorting to a type checking slow path here (i.e. by
6278 // calling InvokeRuntime directly), as it would require to
6279 // assign fixed registers for the inputs of this HInstanceOf
6280 // instruction (following the runtime calling convention), which
6281 // might be cluttered by the potential first read barrier
6282 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006283 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006284 break;
6285 }
6286
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006287 case TypeCheckKind::kInterfaceCheck:
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006288 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6289 // We can not get false positives by doing this.
6290 // /* HeapReference<Class> */ temp = obj->klass_
6291 GenerateReferenceLoadTwoRegisters(instruction,
6292 temp_loc,
6293 obj_loc,
6294 class_offset,
6295 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006296
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006297 // /* HeapReference<Class> */ temp = temp->iftable_
6298 GenerateReferenceLoadTwoRegisters(instruction,
6299 temp_loc,
6300 temp_loc,
6301 iftable_offset,
6302 kWithoutReadBarrier);
6303 // Iftable is never null.
6304 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6305 // Maybe poison the `cls` for direct comparison with memory.
6306 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6307 // Loop through the iftable and check if any class matches.
6308 NearLabel start_loop;
6309 __ Bind(&start_loop);
6310 // Need to subtract first to handle the empty array case.
6311 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6312 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6313 // Go to next interface if the classes do not match.
6314 __ cmpl(cls.AsRegister<CpuRegister>(),
6315 CodeGeneratorX86_64::ArrayAddress(temp,
6316 maybe_temp2_loc,
6317 TIMES_4,
6318 object_array_data_offset));
6319 __ j(kNotEqual, &start_loop); // Return if same class.
6320 // If `cls` was poisoned above, unpoison it.
6321 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322 break;
6323 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006325 if (done.IsLinked()) {
6326 __ Bind(&done);
6327 }
6328
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006330}
6331
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006332void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006333 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6334 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006335 InvokeRuntimeCallingConvention calling_convention;
6336 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6337}
6338
6339void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006340 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006341 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006342 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006343 if (instruction->IsEnter()) {
6344 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6345 } else {
6346 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6347 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006348}
6349
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006350void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6351void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6352void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6353
6354void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6355 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006356 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6358 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006359 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006360 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006361 locations->SetOut(Location::SameAsFirstInput());
6362}
6363
6364void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6365 HandleBitwiseOperation(instruction);
6366}
6367
6368void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6369 HandleBitwiseOperation(instruction);
6370}
6371
6372void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6373 HandleBitwiseOperation(instruction);
6374}
6375
6376void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6377 LocationSummary* locations = instruction->GetLocations();
6378 Location first = locations->InAt(0);
6379 Location second = locations->InAt(1);
6380 DCHECK(first.Equals(locations->Out()));
6381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006383 if (second.IsRegister()) {
6384 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006385 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006386 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006387 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006388 } else {
6389 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006390 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006391 }
6392 } else if (second.IsConstant()) {
6393 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6394 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006395 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006396 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006397 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006398 } else {
6399 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006400 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006401 }
6402 } else {
6403 Address address(CpuRegister(RSP), second.GetStackIndex());
6404 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006405 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006406 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006407 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006408 } else {
6409 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006410 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006411 }
6412 }
6413 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006415 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6416 bool second_is_constant = false;
6417 int64_t value = 0;
6418 if (second.IsConstant()) {
6419 second_is_constant = true;
6420 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006421 }
Mark Mendell40741f32015-04-20 22:10:34 -04006422 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006423
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006424 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006425 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006426 if (is_int32_value) {
6427 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6428 } else {
6429 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6430 }
6431 } else if (second.IsDoubleStackSlot()) {
6432 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006433 } else {
6434 __ andq(first_reg, second.AsRegister<CpuRegister>());
6435 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006436 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006437 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006438 if (is_int32_value) {
6439 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6440 } else {
6441 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6442 }
6443 } else if (second.IsDoubleStackSlot()) {
6444 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006445 } else {
6446 __ orq(first_reg, second.AsRegister<CpuRegister>());
6447 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006448 } else {
6449 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006450 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006451 if (is_int32_value) {
6452 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6453 } else {
6454 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6455 }
6456 } else if (second.IsDoubleStackSlot()) {
6457 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006458 } else {
6459 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6460 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006461 }
6462 }
6463}
6464
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006465void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6466 HInstruction* instruction,
6467 Location out,
6468 uint32_t offset,
6469 Location maybe_temp,
6470 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006471 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006472 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006473 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006474 if (kUseBakerReadBarrier) {
6475 // Load with fast path based Baker's read barrier.
6476 // /* HeapReference<Object> */ out = *(out + offset)
6477 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006478 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006479 } else {
6480 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006481 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006482 // in the following move operation, as we will need it for the
6483 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006484 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006485 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006486 // /* HeapReference<Object> */ out = *(out + offset)
6487 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006488 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006489 }
6490 } else {
6491 // Plain load with no read barrier.
6492 // /* HeapReference<Object> */ out = *(out + offset)
6493 __ movl(out_reg, Address(out_reg, offset));
6494 __ MaybeUnpoisonHeapReference(out_reg);
6495 }
6496}
6497
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006498void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6499 HInstruction* instruction,
6500 Location out,
6501 Location obj,
6502 uint32_t offset,
6503 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006504 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6505 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006506 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006507 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006508 if (kUseBakerReadBarrier) {
6509 // Load with fast path based Baker's read barrier.
6510 // /* HeapReference<Object> */ out = *(obj + offset)
6511 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006512 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006513 } else {
6514 // Load with slow path based read barrier.
6515 // /* HeapReference<Object> */ out = *(obj + offset)
6516 __ movl(out_reg, Address(obj_reg, offset));
6517 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6518 }
6519 } else {
6520 // Plain load with no read barrier.
6521 // /* HeapReference<Object> */ out = *(obj + offset)
6522 __ movl(out_reg, Address(obj_reg, offset));
6523 __ MaybeUnpoisonHeapReference(out_reg);
6524 }
6525}
6526
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006527void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6528 HInstruction* instruction,
6529 Location root,
6530 const Address& address,
6531 Label* fixup_label,
6532 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006533 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006534 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006535 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006536 if (kUseBakerReadBarrier) {
6537 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6538 // Baker's read barrier are used:
6539 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006540 // root = obj.field;
6541 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6542 // if (temp != null) {
6543 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006544 // }
6545
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006546 // /* GcRoot<mirror::Object> */ root = *address
6547 __ movl(root_reg, address);
6548 if (fixup_label != nullptr) {
6549 __ Bind(fixup_label);
6550 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006551 static_assert(
6552 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6553 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6554 "have different sizes.");
6555 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6556 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6557 "have different sizes.");
6558
Vladimir Marko953437b2016-08-24 08:30:46 +00006559 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006560 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006561 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006562 codegen_->AddSlowPath(slow_path);
6563
Roland Levillaind966ce72017-02-09 16:20:14 +00006564 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6565 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006566 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006567 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6568 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 __ j(kNotEqual, slow_path->GetEntryLabel());
6570 __ Bind(slow_path->GetExitLabel());
6571 } else {
6572 // GC root loaded through a slow path for read barriers other
6573 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006574 // /* GcRoot<mirror::Object>* */ root = address
6575 __ leaq(root_reg, address);
6576 if (fixup_label != nullptr) {
6577 __ Bind(fixup_label);
6578 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006579 // /* mirror::Object* */ root = root->Read()
6580 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6581 }
6582 } else {
6583 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006584 // /* GcRoot<mirror::Object> */ root = *address
6585 __ movl(root_reg, address);
6586 if (fixup_label != nullptr) {
6587 __ Bind(fixup_label);
6588 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006589 // Note that GC roots are not affected by heap poisoning, thus we
6590 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006591 }
6592}
6593
6594void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6595 Location ref,
6596 CpuRegister obj,
6597 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006598 bool needs_null_check) {
6599 DCHECK(kEmitCompilerReadBarrier);
6600 DCHECK(kUseBakerReadBarrier);
6601
6602 // /* HeapReference<Object> */ ref = *(obj + offset)
6603 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006604 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006605}
6606
6607void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6608 Location ref,
6609 CpuRegister obj,
6610 uint32_t data_offset,
6611 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006612 bool needs_null_check) {
6613 DCHECK(kEmitCompilerReadBarrier);
6614 DCHECK(kUseBakerReadBarrier);
6615
Roland Levillain3d312422016-06-23 13:53:42 +01006616 static_assert(
6617 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6618 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006619 // /* HeapReference<Object> */ ref =
6620 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006621 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006622 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006623}
6624
6625void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6626 Location ref,
6627 CpuRegister obj,
6628 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006629 bool needs_null_check,
6630 bool always_update_field,
6631 CpuRegister* temp1,
6632 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006633 DCHECK(kEmitCompilerReadBarrier);
6634 DCHECK(kUseBakerReadBarrier);
6635
6636 // In slow path based read barriers, the read barrier call is
6637 // inserted after the original load. However, in fast path based
6638 // Baker's read barriers, we need to perform the load of
6639 // mirror::Object::monitor_ *before* the original reference load.
6640 // This load-load ordering is required by the read barrier.
6641 // The fast path/slow path (for Baker's algorithm) should look like:
6642 //
6643 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6644 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6645 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006646 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006647 // if (is_gray) {
6648 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6649 // }
6650 //
6651 // Note: the original implementation in ReadBarrier::Barrier is
6652 // slightly more complex as:
6653 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006654 // the high-bits of rb_state, which are expected to be all zeroes
6655 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6656 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006657 // - it performs additional checks that we do not do here for
6658 // performance reasons.
6659
6660 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006661 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6662
Vladimir Marko953437b2016-08-24 08:30:46 +00006663 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006664 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6665 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006666 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6667 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6668 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6669
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006670 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006671 // ref = ReadBarrier::Mark(ref);
6672 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6673 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006674 if (needs_null_check) {
6675 MaybeRecordImplicitNullCheck(instruction);
6676 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006677
6678 // Load fence to prevent load-load reordering.
6679 // Note that this is a no-op, thanks to the x86-64 memory model.
6680 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6681
6682 // The actual reference load.
6683 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006684 __ movl(ref_reg, src); // Flags are unaffected.
6685
6686 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6687 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006688 SlowPathCode* slow_path;
6689 if (always_update_field) {
6690 DCHECK(temp1 != nullptr);
6691 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006692 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006693 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6694 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006695 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006696 instruction, ref, /* unpoison_ref_before_marking */ true);
6697 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006698 AddSlowPath(slow_path);
6699
6700 // We have done the "if" of the gray bit check above, now branch based on the flags.
6701 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006702
6703 // Object* ref = ref_addr->AsMirrorPtr()
6704 __ MaybeUnpoisonHeapReference(ref_reg);
6705
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006706 __ Bind(slow_path->GetExitLabel());
6707}
6708
6709void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6710 Location out,
6711 Location ref,
6712 Location obj,
6713 uint32_t offset,
6714 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 DCHECK(kEmitCompilerReadBarrier);
6716
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006717 // Insert a slow path based read barrier *after* the reference load.
6718 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719 // If heap poisoning is enabled, the unpoisoning of the loaded
6720 // reference will be carried out by the runtime within the slow
6721 // path.
6722 //
6723 // Note that `ref` currently does not get unpoisoned (when heap
6724 // poisoning is enabled), which is alright as the `ref` argument is
6725 // not used by the artReadBarrierSlow entry point.
6726 //
6727 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006728 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00006729 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6730 AddSlowPath(slow_path);
6731
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732 __ jmp(slow_path->GetEntryLabel());
6733 __ Bind(slow_path->GetExitLabel());
6734}
6735
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006736void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6737 Location out,
6738 Location ref,
6739 Location obj,
6740 uint32_t offset,
6741 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006742 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006743 // Baker's read barriers shall be handled by the fast path
6744 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6745 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006746 // If heap poisoning is enabled, unpoisoning will be taken care of
6747 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006748 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006749 } else if (kPoisonHeapReferences) {
6750 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6751 }
6752}
6753
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006754void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6755 Location out,
6756 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757 DCHECK(kEmitCompilerReadBarrier);
6758
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006759 // Insert a slow path based read barrier *after* the GC root load.
6760 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761 // Note that GC roots are not affected by heap poisoning, so we do
6762 // not need to do anything special for this here.
6763 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006764 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006765 AddSlowPath(slow_path);
6766
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767 __ jmp(slow_path->GetEntryLabel());
6768 __ Bind(slow_path->GetExitLabel());
6769}
6770
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006771void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006772 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006773 LOG(FATAL) << "Unreachable";
6774}
6775
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006776void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006777 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006778 LOG(FATAL) << "Unreachable";
6779}
6780
Mark Mendellfe57faa2015-09-18 09:26:15 -04006781// Simple implementation of packed switch - generate cascaded compare/jumps.
6782void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6783 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006784 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006785 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006786 locations->AddTemp(Location::RequiresRegister());
6787 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006788}
6789
6790void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6791 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006792 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006793 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006794 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6795 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6796 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006797 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6798
6799 // Should we generate smaller inline compare/jumps?
6800 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6801 // Figure out the correct compare values and jump conditions.
6802 // Handle the first compare/branch as a special case because it might
6803 // jump to the default case.
6804 DCHECK_GT(num_entries, 2u);
6805 Condition first_condition;
6806 uint32_t index;
6807 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6808 if (lower_bound != 0) {
6809 first_condition = kLess;
6810 __ cmpl(value_reg_in, Immediate(lower_bound));
6811 __ j(first_condition, codegen_->GetLabelOf(default_block));
6812 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6813
6814 index = 1;
6815 } else {
6816 // Handle all the compare/jumps below.
6817 first_condition = kBelow;
6818 index = 0;
6819 }
6820
6821 // Handle the rest of the compare/jumps.
6822 for (; index + 1 < num_entries; index += 2) {
6823 int32_t compare_to_value = lower_bound + index + 1;
6824 __ cmpl(value_reg_in, Immediate(compare_to_value));
6825 // Jump to successors[index] if value < case_value[index].
6826 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6827 // Jump to successors[index + 1] if value == case_value[index + 1].
6828 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6829 }
6830
6831 if (index != num_entries) {
6832 // There are an odd number of entries. Handle the last one.
6833 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006834 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006835 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6836 }
6837
6838 // And the default for any other value.
6839 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6840 __ jmp(codegen_->GetLabelOf(default_block));
6841 }
6842 return;
6843 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006844
6845 // Remove the bias, if needed.
6846 Register value_reg_out = value_reg_in.AsRegister();
6847 if (lower_bound != 0) {
6848 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6849 value_reg_out = temp_reg.AsRegister();
6850 }
6851 CpuRegister value_reg(value_reg_out);
6852
6853 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006854 __ cmpl(value_reg, Immediate(num_entries - 1));
6855 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006856
Mark Mendell9c86b482015-09-18 13:36:07 -04006857 // We are in the range of the table.
6858 // Load the address of the jump table in the constant area.
6859 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006860
Mark Mendell9c86b482015-09-18 13:36:07 -04006861 // Load the (signed) offset from the jump table.
6862 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6863
6864 // Add the offset to the address of the table base.
6865 __ addq(temp_reg, base_reg);
6866
6867 // And jump.
6868 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006869}
6870
xueliang.zhonge0eb4832017-10-30 13:43:14 +00006871void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
6872 ATTRIBUTE_UNUSED) {
6873 LOG(FATAL) << "Unreachable";
6874}
6875
6876void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
6877 ATTRIBUTE_UNUSED) {
6878 LOG(FATAL) << "Unreachable";
6879}
6880
Aart Bikc5d47542016-01-27 17:00:35 -08006881void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6882 if (value == 0) {
6883 __ xorl(dest, dest);
6884 } else {
6885 __ movl(dest, Immediate(value));
6886 }
6887}
6888
Mark Mendell92e83bf2015-05-07 11:25:03 -04006889void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6890 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006891 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006892 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006893 } else if (IsUint<32>(value)) {
6894 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006895 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6896 } else {
6897 __ movq(dest, Immediate(value));
6898 }
6899}
6900
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006901void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6902 if (value == 0) {
6903 __ xorps(dest, dest);
6904 } else {
6905 __ movss(dest, LiteralInt32Address(value));
6906 }
6907}
6908
6909void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6910 if (value == 0) {
6911 __ xorpd(dest, dest);
6912 } else {
6913 __ movsd(dest, LiteralInt64Address(value));
6914 }
6915}
6916
6917void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6918 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6919}
6920
6921void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6922 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6923}
6924
Aart Bika19616e2016-02-01 18:57:58 -08006925void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6926 if (value == 0) {
6927 __ testl(dest, dest);
6928 } else {
6929 __ cmpl(dest, Immediate(value));
6930 }
6931}
6932
6933void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6934 if (IsInt<32>(value)) {
6935 if (value == 0) {
6936 __ testq(dest, dest);
6937 } else {
6938 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6939 }
6940 } else {
6941 // Value won't fit in an int.
6942 __ cmpq(dest, LiteralInt64Address(value));
6943 }
6944}
6945
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006946void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6947 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006948 GenerateIntCompare(lhs_reg, rhs);
6949}
6950
6951void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006952 if (rhs.IsConstant()) {
6953 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006954 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006955 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006956 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006957 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006958 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006959 }
6960}
6961
6962void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6963 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6964 if (rhs.IsConstant()) {
6965 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6966 Compare64BitValue(lhs_reg, value);
6967 } else if (rhs.IsDoubleStackSlot()) {
6968 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6969 } else {
6970 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6971 }
6972}
6973
6974Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6975 Location index,
6976 ScaleFactor scale,
6977 uint32_t data_offset) {
6978 return index.IsConstant() ?
6979 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6980 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6981}
6982
Mark Mendellcfa410b2015-05-25 16:02:44 -04006983void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6984 DCHECK(dest.IsDoubleStackSlot());
6985 if (IsInt<32>(value)) {
6986 // Can move directly as an int32 constant.
6987 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6988 Immediate(static_cast<int32_t>(value)));
6989 } else {
6990 Load64BitValue(CpuRegister(TMP), value);
6991 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6992 }
6993}
6994
Mark Mendell9c86b482015-09-18 13:36:07 -04006995/**
6996 * Class to handle late fixup of offsets into constant area.
6997 */
6998class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6999 public:
7000 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7001 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7002
7003 protected:
7004 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7005
7006 CodeGeneratorX86_64* codegen_;
7007
7008 private:
7009 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7010 // Patch the correct offset for the instruction. We use the address of the
7011 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7012 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7013 int32_t relative_position = constant_offset - pos;
7014
7015 // Patch in the right value.
7016 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7017 }
7018
7019 // Location in constant area that the fixup refers to.
7020 size_t offset_into_constant_area_;
7021};
7022
7023/**
7024 t * Class to handle late fixup of offsets to a jump table that will be created in the
7025 * constant area.
7026 */
7027class JumpTableRIPFixup : public RIPFixup {
7028 public:
7029 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7030 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7031
7032 void CreateJumpTable() {
7033 X86_64Assembler* assembler = codegen_->GetAssembler();
7034
7035 // Ensure that the reference to the jump table has the correct offset.
7036 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7037 SetOffset(offset_in_constant_table);
7038
7039 // Compute the offset from the start of the function to this jump table.
7040 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7041
7042 // Populate the jump table with the correct values for the jump table.
7043 int32_t num_entries = switch_instr_->GetNumEntries();
7044 HBasicBlock* block = switch_instr_->GetBlock();
7045 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7046 // The value that we want is the target offset - the position of the table.
7047 for (int32_t i = 0; i < num_entries; i++) {
7048 HBasicBlock* b = successors[i];
7049 Label* l = codegen_->GetLabelOf(b);
7050 DCHECK(l->IsBound());
7051 int32_t offset_to_block = l->Position() - current_table_offset;
7052 assembler->AppendInt32(offset_to_block);
7053 }
7054 }
7055
7056 private:
7057 const HPackedSwitch* switch_instr_;
7058};
7059
Mark Mendellf55c3e02015-03-26 21:07:46 -04007060void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7061 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007062 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007063 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7064 // 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 -04007065 assembler->Align(4, 0);
7066 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007067
7068 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007069 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007070 jump_table->CreateJumpTable();
7071 }
7072
7073 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007074 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007075 }
7076
7077 // And finish up.
7078 CodeGenerator::Finalize(allocator);
7079}
7080
Mark Mendellf55c3e02015-03-26 21:07:46 -04007081Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007082 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007083 return Address::RIP(fixup);
7084}
7085
7086Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007087 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007088 return Address::RIP(fixup);
7089}
7090
7091Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007092 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007093 return Address::RIP(fixup);
7094}
7095
7096Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007097 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007098 return Address::RIP(fixup);
7099}
7100
Andreas Gampe85b62f22015-09-09 13:15:38 -07007101// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007102void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007103 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007104 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007105 return;
7106 }
7107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007108 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007109
7110 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7111 if (trg.Equals(return_loc)) {
7112 return;
7113 }
7114
7115 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007116 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007117 parallel_move.AddMove(return_loc, trg, type, nullptr);
7118 GetMoveResolver()->EmitNativeCode(&parallel_move);
7119}
7120
Mark Mendell9c86b482015-09-18 13:36:07 -04007121Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7122 // Create a fixup to be used to create and address the jump table.
7123 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007124 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007125
7126 // We have to populate the jump tables.
7127 fixups_to_jump_tables_.push_back(table_fixup);
7128 return Address::RIP(table_fixup);
7129}
7130
Mark Mendellea5af682015-10-22 17:35:49 -04007131void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7132 const Address& addr_high,
7133 int64_t v,
7134 HInstruction* instruction) {
7135 if (IsInt<32>(v)) {
7136 int32_t v_32 = v;
7137 __ movq(addr_low, Immediate(v_32));
7138 MaybeRecordImplicitNullCheck(instruction);
7139 } else {
7140 // Didn't fit in a register. Do it in pieces.
7141 int32_t low_v = Low32Bits(v);
7142 int32_t high_v = High32Bits(v);
7143 __ movl(addr_low, Immediate(low_v));
7144 MaybeRecordImplicitNullCheck(instruction);
7145 __ movl(addr_high, Immediate(high_v));
7146 }
7147}
7148
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007149void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7150 const uint8_t* roots_data,
7151 const PatchInfo<Label>& info,
7152 uint64_t index_in_table) const {
7153 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7154 uintptr_t address =
7155 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7156 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7157 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7158 dchecked_integral_cast<uint32_t>(address);
7159}
7160
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007161void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7162 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007163 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7164 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007165 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007166 }
7167
7168 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007169 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7170 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007171 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007172 }
7173}
7174
Roland Levillain4d027112015-07-01 15:41:14 +01007175#undef __
7176
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007177} // namespace x86_64
7178} // namespace art