blob: 1f8d8225071daaa7ee53600c67d771b1f60ec05b [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 Geoffrayd97dc402015-01-22 13:50:01 +00001271 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001272 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1273 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001274 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001275 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001276
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001277 if (HasEmptyFrame()) {
1278 return;
1279 }
1280
Nicolas Geoffray98893962015-01-21 12:32:32 +00001281 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001282 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001283 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001284 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001285 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1286 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001287 }
1288 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001289
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001290 int adjust = GetFrameSize() - GetCoreSpillSize();
1291 __ subq(CpuRegister(RSP), Immediate(adjust));
1292 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001293 uint32_t xmm_spill_location = GetFpuSpillStart();
1294 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001295
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001296 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1297 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001298 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1299 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1300 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001301 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001302 }
1303
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001304 // Save the current method if we need it. Note that we do not
1305 // do this in HCurrentMethod, as the instruction might have been removed
1306 // in the SSA graph.
1307 if (RequiresCurrentMethod()) {
1308 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1309 CpuRegister(kMethodRegisterArgument));
1310 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001311
1312 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1313 // Initialize should_deoptimize flag to 0.
1314 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1315 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316}
1317
1318void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001319 __ cfi().RememberState();
1320 if (!HasEmptyFrame()) {
1321 uint32_t xmm_spill_location = GetFpuSpillStart();
1322 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1323 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1324 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1325 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1326 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1327 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1328 }
1329 }
1330
1331 int adjust = GetFrameSize() - GetCoreSpillSize();
1332 __ addq(CpuRegister(RSP), Immediate(adjust));
1333 __ cfi().AdjustCFAOffset(-adjust);
1334
1335 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1336 Register reg = kCoreCalleeSaves[i];
1337 if (allocated_registers_.ContainsCoreRegister(reg)) {
1338 __ popq(CpuRegister(reg));
1339 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1340 __ cfi().Restore(DWARFReg(reg));
1341 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001342 }
1343 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001344 __ ret();
1345 __ cfi().RestoreState();
1346 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001347}
1348
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001349void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1350 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001351}
1352
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001353void CodeGeneratorX86_64::Move(Location destination, Location source) {
1354 if (source.Equals(destination)) {
1355 return;
1356 }
1357 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001358 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001359 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001360 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001361 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001362 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001363 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001364 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1365 } else if (source.IsConstant()) {
1366 HConstant* constant = source.GetConstant();
1367 if (constant->IsLongConstant()) {
1368 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1369 } else {
1370 Load32BitValue(dest, GetInt32ValueOf(constant));
1371 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001372 } else {
1373 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001374 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001375 }
1376 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001377 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001379 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001380 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001381 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1382 } else if (source.IsConstant()) {
1383 HConstant* constant = source.GetConstant();
1384 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1385 if (constant->IsFloatConstant()) {
1386 Load32BitValue(dest, static_cast<int32_t>(value));
1387 } else {
1388 Load64BitValue(dest, value);
1389 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001390 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001391 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001392 } else {
1393 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001394 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001395 }
1396 } else if (destination.IsStackSlot()) {
1397 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001398 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001400 } else if (source.IsFpuRegister()) {
1401 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001402 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001403 } else if (source.IsConstant()) {
1404 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001405 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001406 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001407 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001408 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001409 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1410 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001411 }
1412 } else {
1413 DCHECK(destination.IsDoubleStackSlot());
1414 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001415 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001416 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001417 } else if (source.IsFpuRegister()) {
1418 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001419 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001420 } else if (source.IsConstant()) {
1421 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001422 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1423 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001424 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001425 } else {
1426 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001427 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1428 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001429 }
1430 }
1431}
1432
Calin Juravle175dc732015-08-25 15:42:32 +01001433void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1434 DCHECK(location.IsRegister());
1435 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1436}
1437
Calin Juravlee460d1d2015-09-29 04:52:17 +01001438void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001439 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001440 Move(dst, src);
1441}
1442
1443void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1444 if (location.IsRegister()) {
1445 locations->AddTemp(location);
1446 } else {
1447 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1448 }
1449}
1450
David Brazdilfc6a86a2015-06-26 10:33:45 +00001451void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001452 DCHECK(!successor->IsExitBlock());
1453
1454 HBasicBlock* block = got->GetBlock();
1455 HInstruction* previous = got->GetPrevious();
1456
1457 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001458 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001459 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1460 return;
1461 }
1462
1463 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1464 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1465 }
1466 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001467 __ jmp(codegen_->GetLabelOf(successor));
1468 }
1469}
1470
David Brazdilfc6a86a2015-06-26 10:33:45 +00001471void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1472 got->SetLocations(nullptr);
1473}
1474
1475void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1476 HandleGoto(got, got->GetSuccessor());
1477}
1478
1479void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1480 try_boundary->SetLocations(nullptr);
1481}
1482
1483void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1484 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1485 if (!successor->IsExitBlock()) {
1486 HandleGoto(try_boundary, successor);
1487 }
1488}
1489
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001490void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1491 exit->SetLocations(nullptr);
1492}
1493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001494void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001495}
1496
Mark Mendell152408f2015-12-31 12:28:50 -05001497template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001498void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001499 LabelType* true_label,
1500 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001501 if (cond->IsFPConditionTrueIfNaN()) {
1502 __ j(kUnordered, true_label);
1503 } else if (cond->IsFPConditionFalseIfNaN()) {
1504 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001505 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001506 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001507}
1508
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001509void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001510 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001511
Mark Mendellc4701932015-04-10 13:18:51 -04001512 Location left = locations->InAt(0);
1513 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001515 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001516 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001517 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001518 case DataType::Type::kInt8:
1519 case DataType::Type::kUint16:
1520 case DataType::Type::kInt16:
1521 case DataType::Type::kInt32:
1522 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001523 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001524 break;
1525 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001526 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001527 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001528 break;
1529 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001531 if (right.IsFpuRegister()) {
1532 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1533 } else if (right.IsConstant()) {
1534 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1535 codegen_->LiteralFloatAddress(
1536 right.GetConstant()->AsFloatConstant()->GetValue()));
1537 } else {
1538 DCHECK(right.IsStackSlot());
1539 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1540 Address(CpuRegister(RSP), right.GetStackIndex()));
1541 }
Mark Mendellc4701932015-04-10 13:18:51 -04001542 break;
1543 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001544 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001545 if (right.IsFpuRegister()) {
1546 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1547 } else if (right.IsConstant()) {
1548 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1549 codegen_->LiteralDoubleAddress(
1550 right.GetConstant()->AsDoubleConstant()->GetValue()));
1551 } else {
1552 DCHECK(right.IsDoubleStackSlot());
1553 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1554 Address(CpuRegister(RSP), right.GetStackIndex()));
1555 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001556 break;
1557 }
1558 default:
1559 LOG(FATAL) << "Unexpected condition type " << type;
1560 }
1561}
1562
1563template<class LabelType>
1564void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1565 LabelType* true_target_in,
1566 LabelType* false_target_in) {
1567 // Generated branching requires both targets to be explicit. If either of the
1568 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1569 LabelType fallthrough_target;
1570 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1571 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1572
1573 // Generate the comparison to set the CC.
1574 GenerateCompareTest(condition);
1575
1576 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001578 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001579 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001580 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1581 break;
1582 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001583 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001584 GenerateFPJumps(condition, true_target, false_target);
1585 break;
1586 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001587 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001588 GenerateFPJumps(condition, true_target, false_target);
1589 break;
1590 }
1591 default:
1592 LOG(FATAL) << "Unexpected condition type " << type;
1593 }
1594
David Brazdil0debae72015-11-12 18:37:00 +00001595 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001596 __ jmp(false_target);
1597 }
David Brazdil0debae72015-11-12 18:37:00 +00001598
1599 if (fallthrough_target.IsLinked()) {
1600 __ Bind(&fallthrough_target);
1601 }
Mark Mendellc4701932015-04-10 13:18:51 -04001602}
1603
David Brazdil0debae72015-11-12 18:37:00 +00001604static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1605 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1606 // are set only strictly before `branch`. We can't use the eflags on long
1607 // conditions if they are materialized due to the complex branching.
1608 return cond->IsCondition() &&
1609 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001611}
1612
Mark Mendell152408f2015-12-31 12:28:50 -05001613template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001614void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001615 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001616 LabelType* true_target,
1617 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001618 HInstruction* cond = instruction->InputAt(condition_input_index);
1619
1620 if (true_target == nullptr && false_target == nullptr) {
1621 // Nothing to do. The code always falls through.
1622 return;
1623 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001624 // Constant condition, statically compared against "true" (integer value 1).
1625 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001626 if (true_target != nullptr) {
1627 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001628 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001629 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001630 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001631 if (false_target != nullptr) {
1632 __ jmp(false_target);
1633 }
1634 }
1635 return;
1636 }
1637
1638 // The following code generates these patterns:
1639 // (1) true_target == nullptr && false_target != nullptr
1640 // - opposite condition true => branch to false_target
1641 // (2) true_target != nullptr && false_target == nullptr
1642 // - condition true => branch to true_target
1643 // (3) true_target != nullptr && false_target != nullptr
1644 // - condition true => branch to true_target
1645 // - branch to false_target
1646 if (IsBooleanValueOrMaterializedCondition(cond)) {
1647 if (AreEflagsSetFrom(cond, instruction)) {
1648 if (true_target == nullptr) {
1649 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1650 } else {
1651 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1652 }
1653 } else {
1654 // Materialized condition, compare against 0.
1655 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1656 if (lhs.IsRegister()) {
1657 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1658 } else {
1659 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1660 }
1661 if (true_target == nullptr) {
1662 __ j(kEqual, false_target);
1663 } else {
1664 __ j(kNotEqual, true_target);
1665 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001666 }
1667 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001668 // Condition has not been materialized, use its inputs as the
1669 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001670 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001671
David Brazdil0debae72015-11-12 18:37:00 +00001672 // If this is a long or FP comparison that has been folded into
1673 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 DataType::Type type = condition->InputAt(0)->GetType();
1675 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001676 GenerateCompareTestAndBranch(condition, true_target, false_target);
1677 return;
1678 }
1679
1680 Location lhs = condition->GetLocations()->InAt(0);
1681 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001682 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001683 if (true_target == nullptr) {
1684 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1685 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001686 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001687 }
Dave Allison20dfc792014-06-16 20:44:29 -07001688 }
David Brazdil0debae72015-11-12 18:37:00 +00001689
1690 // If neither branch falls through (case 3), the conditional branch to `true_target`
1691 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1692 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001694 }
1695}
1696
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001697void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001698 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001699 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700 locations->SetInAt(0, Location::Any());
1701 }
1702}
1703
1704void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001705 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1706 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1707 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1708 nullptr : codegen_->GetLabelOf(true_successor);
1709 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1710 nullptr : codegen_->GetLabelOf(false_successor);
1711 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001712}
1713
1714void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001715 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001716 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001717 InvokeRuntimeCallingConvention calling_convention;
1718 RegisterSet caller_saves = RegisterSet::Empty();
1719 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1720 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001721 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001722 locations->SetInAt(0, Location::Any());
1723 }
1724}
1725
1726void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001727 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001728 GenerateTestAndBranch<Label>(deoptimize,
1729 /* condition_input_index */ 0,
1730 slow_path->GetEntryLabel(),
1731 /* false_target */ nullptr);
1732}
1733
Mingyao Yang063fc772016-08-02 11:02:54 -07001734void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001735 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001736 LocationSummary(flag, LocationSummary::kNoCall);
1737 locations->SetOut(Location::RequiresRegister());
1738}
1739
1740void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1741 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1742 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1743}
1744
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001745static bool SelectCanUseCMOV(HSelect* select) {
1746 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001748 return false;
1749 }
1750
1751 // A FP condition doesn't generate the single CC that we need.
1752 HInstruction* condition = select->GetCondition();
1753 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001755 return false;
1756 }
1757
1758 // We can generate a CMOV for this Select.
1759 return true;
1760}
1761
David Brazdil74eb1b22015-12-14 11:44:01 +00001762void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001763 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001765 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001766 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001767 } else {
1768 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001769 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001770 if (select->InputAt(1)->IsConstant()) {
1771 locations->SetInAt(1, Location::RequiresRegister());
1772 } else {
1773 locations->SetInAt(1, Location::Any());
1774 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001775 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001776 locations->SetInAt(1, Location::Any());
1777 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001778 }
1779 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1780 locations->SetInAt(2, Location::RequiresRegister());
1781 }
1782 locations->SetOut(Location::SameAsFirstInput());
1783}
1784
1785void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1786 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001787 if (SelectCanUseCMOV(select)) {
1788 // If both the condition and the source types are integer, we can generate
1789 // a CMOV to implement Select.
1790 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001791 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001792 DCHECK(locations->InAt(0).Equals(locations->Out()));
1793
1794 HInstruction* select_condition = select->GetCondition();
1795 Condition cond = kNotEqual;
1796
1797 // Figure out how to test the 'condition'.
1798 if (select_condition->IsCondition()) {
1799 HCondition* condition = select_condition->AsCondition();
1800 if (!condition->IsEmittedAtUseSite()) {
1801 // This was a previously materialized condition.
1802 // Can we use the existing condition code?
1803 if (AreEflagsSetFrom(condition, select)) {
1804 // Materialization was the previous instruction. Condition codes are right.
1805 cond = X86_64IntegerCondition(condition->GetCondition());
1806 } else {
1807 // No, we have to recreate the condition code.
1808 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1809 __ testl(cond_reg, cond_reg);
1810 }
1811 } else {
1812 GenerateCompareTest(condition);
1813 cond = X86_64IntegerCondition(condition->GetCondition());
1814 }
1815 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001816 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001817 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1818 __ testl(cond_reg, cond_reg);
1819 }
1820
1821 // If the condition is true, overwrite the output, which already contains false.
1822 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001824 if (value_true_loc.IsRegister()) {
1825 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1826 } else {
1827 __ cmov(cond,
1828 value_false,
1829 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1830 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001831 } else {
1832 NearLabel false_target;
1833 GenerateTestAndBranch<NearLabel>(select,
1834 /* condition_input_index */ 2,
1835 /* true_target */ nullptr,
1836 &false_target);
1837 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1838 __ Bind(&false_target);
1839 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001840}
1841
David Srbecky0cf44932015-12-09 14:09:59 +00001842void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001843 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001844}
1845
David Srbeckyd28f4a02016-03-14 17:14:24 +00001846void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1847 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001848}
1849
1850void CodeGeneratorX86_64::GenerateNop() {
1851 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001852}
1853
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001854void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001855 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001856 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001857 // Handle the long/FP comparisons made in instruction simplification.
1858 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001859 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001860 locations->SetInAt(0, Location::RequiresRegister());
1861 locations->SetInAt(1, Location::Any());
1862 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 case DataType::Type::kFloat32:
1864 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetInAt(1, Location::Any());
1867 break;
1868 default:
1869 locations->SetInAt(0, Location::RequiresRegister());
1870 locations->SetInAt(1, Location::Any());
1871 break;
1872 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001873 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001874 locations->SetOut(Location::RequiresRegister());
1875 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001876}
1877
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001878void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001879 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001880 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001881 }
Mark Mendellc4701932015-04-10 13:18:51 -04001882
1883 LocationSummary* locations = cond->GetLocations();
1884 Location lhs = locations->InAt(0);
1885 Location rhs = locations->InAt(1);
1886 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001887 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001888
1889 switch (cond->InputAt(0)->GetType()) {
1890 default:
1891 // Integer case.
1892
1893 // Clear output register: setcc only sets the low byte.
1894 __ xorl(reg, reg);
1895
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001896 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001897 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001898 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001899 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001900 // Clear output register: setcc only sets the low byte.
1901 __ xorl(reg, reg);
1902
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001903 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001904 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001905 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001907 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1908 if (rhs.IsConstant()) {
1909 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1910 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1911 } else if (rhs.IsStackSlot()) {
1912 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1913 } else {
1914 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1915 }
1916 GenerateFPJumps(cond, &true_label, &false_label);
1917 break;
1918 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001920 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1921 if (rhs.IsConstant()) {
1922 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1923 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1924 } else if (rhs.IsDoubleStackSlot()) {
1925 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1926 } else {
1927 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1928 }
1929 GenerateFPJumps(cond, &true_label, &false_label);
1930 break;
1931 }
1932 }
1933
1934 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001935 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001936
Roland Levillain4fa13f62015-07-06 18:11:54 +01001937 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001938 __ Bind(&false_label);
1939 __ xorl(reg, reg);
1940 __ jmp(&done_label);
1941
Roland Levillain4fa13f62015-07-06 18:11:54 +01001942 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001943 __ Bind(&true_label);
1944 __ movl(reg, Immediate(1));
1945 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001946}
1947
1948void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001954}
1955
1956void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001958}
1959
1960void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001962}
1963
1964void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001966}
1967
1968void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001970}
1971
1972void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001974}
1975
1976void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001978}
1979
1980void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001982}
1983
1984void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001986}
1987
1988void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001990}
1991
1992void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001994}
1995
Aart Bike9f37602015-10-09 11:15:55 -07001996void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001998}
1999
2000void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002002}
2003
2004void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002006}
2007
2008void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002010}
2011
2012void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002014}
2015
2016void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002018}
2019
2020void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002021 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002022}
2023
2024void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002025 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002026}
2027
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002028void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002029 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002030 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002031 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002032 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002033 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002035 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002036 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037 case DataType::Type::kInt32:
2038 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002039 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002040 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2042 break;
2043 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kFloat32:
2045 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002046 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002047 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002048 locations->SetOut(Location::RequiresRegister());
2049 break;
2050 }
2051 default:
2052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002054}
2055
2056void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002057 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002059 Location left = locations->InAt(0);
2060 Location right = locations->InAt(1);
2061
Mark Mendell0c9497d2015-08-21 09:30:05 -04002062 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002063 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002064 Condition less_cond = kLess;
2065
Calin Juravleddb7df22014-11-25 20:56:51 +00002066 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002068 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002071 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002073 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002074 break;
2075 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002077 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002078 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002079 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002081 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2082 if (right.IsConstant()) {
2083 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2084 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2085 } else if (right.IsStackSlot()) {
2086 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2087 } else {
2088 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2089 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002090 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002091 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002092 break;
2093 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002095 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2096 if (right.IsConstant()) {
2097 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2098 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2099 } else if (right.IsDoubleStackSlot()) {
2100 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2101 } else {
2102 __ ucomisd(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 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002108 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002109 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002110 }
Aart Bika19616e2016-02-01 18:57:58 -08002111
Calin Juravleddb7df22014-11-25 20:56:51 +00002112 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002113 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002114 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002115
Calin Juravle91debbc2014-11-26 19:01:09 +00002116 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002117 __ movl(out, Immediate(1));
2118 __ jmp(&done);
2119
2120 __ Bind(&less);
2121 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002122
2123 __ Bind(&done);
2124}
2125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002126void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002127 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002128 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002129 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002130}
2131
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002132void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002133 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002134}
2135
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002136void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2137 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002138 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002139 locations->SetOut(Location::ConstantLocation(constant));
2140}
2141
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002142void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002143 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002144}
2145
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002146void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002148 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002149 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150}
2151
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002152void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002153 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154}
2155
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002156void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2157 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002158 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002159 locations->SetOut(Location::ConstantLocation(constant));
2160}
2161
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002162void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002163 // Will be generated at use site.
2164}
2165
2166void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2167 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002168 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002169 locations->SetOut(Location::ConstantLocation(constant));
2170}
2171
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002172void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2173 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002174 // Will be generated at use site.
2175}
2176
Igor Murashkind01745e2017-04-05 16:40:31 -07002177void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2178 constructor_fence->SetLocations(nullptr);
2179}
2180
2181void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2182 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2184}
2185
Calin Juravle27df7582015-04-17 19:12:31 +01002186void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2187 memory_barrier->SetLocations(nullptr);
2188}
2189
2190void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002191 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002192}
2193
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2195 ret->SetLocations(nullptr);
2196}
2197
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002198void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002200}
2201
2202void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002203 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002204 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002206 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002207 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002208 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 case DataType::Type::kInt8:
2210 case DataType::Type::kUint16:
2211 case DataType::Type::kInt16:
2212 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002214 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002215 break;
2216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002217 case DataType::Type::kFloat32:
2218 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002219 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002220 break;
2221
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002222 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002223 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002224 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225}
2226
2227void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2228 if (kIsDebugBuild) {
2229 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002230 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002232 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kInt8:
2234 case DataType::Type::kUint16:
2235 case DataType::Type::kInt16:
2236 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002238 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002239 break;
2240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002241 case DataType::Type::kFloat32:
2242 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002243 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002244 XMM0);
2245 break;
2246
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002247 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002248 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002249 }
2250 }
2251 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002252}
2253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002254Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002255 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002256 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002258 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 case DataType::Type::kInt8:
2260 case DataType::Type::kUint16:
2261 case DataType::Type::kInt16:
2262 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002264 return Location::RegisterLocation(RAX);
2265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002267 return Location::NoLocation();
2268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 case DataType::Type::kFloat64:
2270 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002271 return Location::FpuRegisterLocation(XMM0);
2272 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002273
2274 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002275}
2276
2277Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2278 return Location::RegisterLocation(kMethodRegisterArgument);
2279}
2280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002282 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002283 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002284 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002285 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 case DataType::Type::kInt8:
2287 case DataType::Type::kUint16:
2288 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002289 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002290 uint32_t index = gp_index_++;
2291 stack_index_++;
2292 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002293 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002294 } else {
2295 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2296 }
2297 }
2298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002300 uint32_t index = gp_index_;
2301 stack_index_ += 2;
2302 if (index < calling_convention.GetNumberOfRegisters()) {
2303 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002304 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002305 } else {
2306 gp_index_ += 2;
2307 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2308 }
2309 }
2310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002312 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 stack_index_++;
2314 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002315 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002316 } else {
2317 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2318 }
2319 }
2320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002322 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002323 stack_index_ += 2;
2324 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002325 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002326 } else {
2327 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2328 }
2329 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002332 LOG(FATAL) << "Unexpected parameter type " << type;
2333 break;
2334 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002335 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002336}
2337
Calin Juravle175dc732015-08-25 15:42:32 +01002338void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2339 // The trampoline uses the same calling convention as dex calling conventions,
2340 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2341 // the method_idx.
2342 HandleInvoke(invoke);
2343}
2344
2345void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2346 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2347}
2348
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002349void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002350 // Explicit clinit checks triggered by static invokes must have been pruned by
2351 // art::PrepareForRegisterAllocation.
2352 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002353
Mark Mendellfb8d2792015-03-31 22:16:59 -04002354 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002355 if (intrinsic.TryDispatch(invoke)) {
2356 return;
2357 }
2358
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002359 HandleInvoke(invoke);
2360}
2361
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002362static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2363 if (invoke->GetLocations()->Intrinsified()) {
2364 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2365 intrinsic.Dispatch(invoke);
2366 return true;
2367 }
2368 return false;
2369}
2370
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002371void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002372 // Explicit clinit checks triggered by static invokes must have been pruned by
2373 // art::PrepareForRegisterAllocation.
2374 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002375
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002376 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2377 return;
2378 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002379
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002380 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002381 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002382 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383}
2384
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002385void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002386 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002387 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002388}
2389
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002390void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002391 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002392 if (intrinsic.TryDispatch(invoke)) {
2393 return;
2394 }
2395
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002396 HandleInvoke(invoke);
2397}
2398
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002399void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002400 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2401 return;
2402 }
2403
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002404 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002405 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002406}
2407
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002408void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2409 HandleInvoke(invoke);
2410 // Add the hidden argument.
2411 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2412}
2413
2414void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2415 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002416 LocationSummary* locations = invoke->GetLocations();
2417 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2418 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002419 Location receiver = locations->InAt(0);
2420 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2421
Roland Levillain0d5a2812015-11-13 10:07:31 +00002422 // Set the hidden argument. This is safe to do this here, as RAX
2423 // won't be modified thereafter, before the `call` instruction.
2424 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002425 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002426
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002427 if (receiver.IsStackSlot()) {
2428 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002429 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002430 __ movl(temp, Address(temp, class_offset));
2431 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002432 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002433 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002435 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002436 // Instead of simply (possibly) unpoisoning `temp` here, we should
2437 // emit a read barrier for the previous class reference load.
2438 // However this is not required in practice, as this is an
2439 // intermediate/temporary reference and because the current
2440 // concurrent copying collector keeps the from-space memory
2441 // intact/accessible until the end of the marking phase (the
2442 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002443 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002444 // temp = temp->GetAddressOfIMT()
2445 __ movq(temp,
2446 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2447 // temp = temp->GetImtEntryAt(method_offset);
2448 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002449 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002451 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002452 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002453 __ call(Address(
2454 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002455
2456 DCHECK(!codegen_->IsLeafMethod());
2457 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2458}
2459
Orion Hodsonac141392017-01-13 11:53:47 +00002460void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2461 HandleInvoke(invoke);
2462}
2463
2464void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2465 codegen_->GenerateInvokePolymorphicCall(invoke);
2466}
2467
Roland Levillain88cb1752014-10-20 16:36:47 +01002468void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2469 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002470 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002471 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 case DataType::Type::kInt32:
2473 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002474 locations->SetInAt(0, Location::RequiresRegister());
2475 locations->SetOut(Location::SameAsFirstInput());
2476 break;
2477
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002478 case DataType::Type::kFloat32:
2479 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002480 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002481 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002482 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002483 break;
2484
2485 default:
2486 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2487 }
2488}
2489
2490void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2491 LocationSummary* locations = neg->GetLocations();
2492 Location out = locations->Out();
2493 Location in = locations->InAt(0);
2494 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002496 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002497 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002498 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002499 break;
2500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002502 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002503 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002504 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002505 break;
2506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002508 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002509 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002510 // Implement float negation with an exclusive or with value
2511 // 0x80000000 (mask for bit 31, representing the sign of a
2512 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002513 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002515 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002516 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002519 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002520 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002521 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002522 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002523 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002524 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002525 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002526 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002527 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002528
2529 default:
2530 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2531 }
2532}
2533
Roland Levillaindff1f282014-11-05 14:15:05 +00002534void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2535 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002536 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 DataType::Type result_type = conversion->GetResultType();
2538 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002539 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2540 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002541
Roland Levillaindff1f282014-11-05 14:15:05 +00002542 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002543 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002545 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002547 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2548 locations->SetInAt(0, Location::Any());
2549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002550 break;
2551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002553 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002555 locations->SetInAt(0, Location::Any());
2556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2557 break;
2558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002560 locations->SetInAt(0, Location::RequiresFpuRegister());
2561 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002562 break;
2563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002565 locations->SetInAt(0, Location::RequiresFpuRegister());
2566 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002567 break;
2568
2569 default:
2570 LOG(FATAL) << "Unexpected type conversion from " << input_type
2571 << " to " << result_type;
2572 }
2573 break;
2574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002576 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002578 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002580 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002581 case DataType::Type::kInt16:
2582 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002583 // TODO: We would benefit from a (to-be-implemented)
2584 // Location::RegisterOrStackSlot requirement for this input.
2585 locations->SetInAt(0, Location::RequiresRegister());
2586 locations->SetOut(Location::RequiresRegister());
2587 break;
2588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002592 break;
2593
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002594 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002595 locations->SetInAt(0, Location::RequiresFpuRegister());
2596 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected type conversion from " << input_type
2601 << " to " << result_type;
2602 }
2603 break;
2604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002606 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002608 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002610 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt16:
2612 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002613 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002614 locations->SetOut(Location::RequiresFpuRegister());
2615 break;
2616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002618 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002619 locations->SetOut(Location::RequiresFpuRegister());
2620 break;
2621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002623 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002624 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002630 }
Roland Levillaincff13742014-11-17 14:32:17 +00002631 break;
2632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002633 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002634 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002636 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002638 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kInt16:
2640 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002641 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002642 locations->SetOut(Location::RequiresFpuRegister());
2643 break;
2644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002646 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002647 locations->SetOut(Location::RequiresFpuRegister());
2648 break;
2649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002651 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002652 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002653 break;
2654
2655 default:
2656 LOG(FATAL) << "Unexpected type conversion from " << input_type
2657 << " to " << result_type;
2658 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected type conversion from " << input_type
2663 << " to " << result_type;
2664 }
2665}
2666
2667void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2668 LocationSummary* locations = conversion->GetLocations();
2669 Location out = locations->Out();
2670 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 DataType::Type result_type = conversion->GetResultType();
2672 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002673 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2674 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002675 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002676 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002677 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002678 case DataType::Type::kInt8:
2679 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kInt16:
2681 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002682 case DataType::Type::kInt64:
2683 if (in.IsRegister()) {
2684 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2685 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2686 __ movzxb(out.AsRegister<CpuRegister>(),
2687 Address(CpuRegister(RSP), in.GetStackIndex()));
2688 } else {
2689 __ movl(out.AsRegister<CpuRegister>(),
2690 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2691 }
2692 break;
2693
2694 default:
2695 LOG(FATAL) << "Unexpected type conversion from " << input_type
2696 << " to " << result_type;
2697 }
2698 break;
2699
2700 case DataType::Type::kInt8:
2701 switch (input_type) {
2702 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002704 case DataType::Type::kInt16:
2705 case DataType::Type::kInt32:
2706 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002707 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002709 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002710 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002711 Address(CpuRegister(RSP), in.GetStackIndex()));
2712 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002714 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002715 }
2716 break;
2717
2718 default:
2719 LOG(FATAL) << "Unexpected type conversion from " << input_type
2720 << " to " << result_type;
2721 }
2722 break;
2723
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002724 case DataType::Type::kUint16:
2725 switch (input_type) {
2726 case DataType::Type::kInt8:
2727 case DataType::Type::kInt16:
2728 case DataType::Type::kInt32:
2729 case DataType::Type::kInt64:
2730 if (in.IsRegister()) {
2731 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2732 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2733 __ movzxw(out.AsRegister<CpuRegister>(),
2734 Address(CpuRegister(RSP), in.GetStackIndex()));
2735 } else {
2736 __ movl(out.AsRegister<CpuRegister>(),
2737 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2738 }
2739 break;
2740
2741 default:
2742 LOG(FATAL) << "Unexpected type conversion from " << input_type
2743 << " to " << result_type;
2744 }
2745 break;
2746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002747 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002748 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002750 case DataType::Type::kInt32:
2751 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002752 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002753 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002754 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002756 Address(CpuRegister(RSP), in.GetStackIndex()));
2757 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002758 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002759 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002760 }
2761 break;
2762
2763 default:
2764 LOG(FATAL) << "Unexpected type conversion from " << input_type
2765 << " to " << result_type;
2766 }
2767 break;
2768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002770 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002772 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002774 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002776 Address(CpuRegister(RSP), in.GetStackIndex()));
2777 } else {
2778 DCHECK(in.IsConstant());
2779 DCHECK(in.GetConstant()->IsLongConstant());
2780 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002782 }
2783 break;
2784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002786 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2787 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002788 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002789
2790 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002791 // if input >= (float)INT_MAX goto done
2792 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002793 __ j(kAboveEqual, &done);
2794 // if input == NaN goto nan
2795 __ j(kUnordered, &nan);
2796 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002797 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002798 __ jmp(&done);
2799 __ Bind(&nan);
2800 // output = 0
2801 __ xorl(output, output);
2802 __ Bind(&done);
2803 break;
2804 }
2805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002807 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2808 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002809 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002810
2811 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002812 // if input >= (double)INT_MAX goto done
2813 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002814 __ j(kAboveEqual, &done);
2815 // if input == NaN goto nan
2816 __ j(kUnordered, &nan);
2817 // output = double-to-int-truncate(input)
2818 __ cvttsd2si(output, input);
2819 __ jmp(&done);
2820 __ Bind(&nan);
2821 // output = 0
2822 __ xorl(output, output);
2823 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002824 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002825 }
Roland Levillain946e1432014-11-11 17:35:19 +00002826
2827 default:
2828 LOG(FATAL) << "Unexpected type conversion from " << input_type
2829 << " to " << result_type;
2830 }
2831 break;
2832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002834 switch (input_type) {
2835 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002837 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002838 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002839 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kInt16:
2841 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002842 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002843 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002844 break;
2845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002847 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2848 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002849 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002850
Mark Mendell92e83bf2015-05-07 11:25:03 -04002851 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002852 // if input >= (float)LONG_MAX goto done
2853 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002854 __ j(kAboveEqual, &done);
2855 // if input == NaN goto nan
2856 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002857 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002858 __ cvttss2si(output, input, true);
2859 __ jmp(&done);
2860 __ Bind(&nan);
2861 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002862 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002863 __ Bind(&done);
2864 break;
2865 }
2866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002868 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2869 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002870 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002871
Mark Mendell92e83bf2015-05-07 11:25:03 -04002872 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002873 // if input >= (double)LONG_MAX goto done
2874 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002875 __ j(kAboveEqual, &done);
2876 // if input == NaN goto nan
2877 __ j(kUnordered, &nan);
2878 // output = double-to-long-truncate(input)
2879 __ cvttsd2si(output, input, true);
2880 __ jmp(&done);
2881 __ Bind(&nan);
2882 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002883 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002884 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002885 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002886 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002887
2888 default:
2889 LOG(FATAL) << "Unexpected type conversion from " << input_type
2890 << " to " << result_type;
2891 }
2892 break;
2893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002895 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002897 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002899 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002900 case DataType::Type::kInt16:
2901 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002902 if (in.IsRegister()) {
2903 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2904 } else if (in.IsConstant()) {
2905 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2906 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002907 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002908 } else {
2909 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2910 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2911 }
Roland Levillaincff13742014-11-17 14:32:17 +00002912 break;
2913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002915 if (in.IsRegister()) {
2916 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2917 } else if (in.IsConstant()) {
2918 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2919 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002920 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002921 } else {
2922 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2923 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2924 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002925 break;
2926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002928 if (in.IsFpuRegister()) {
2929 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2930 } else if (in.IsConstant()) {
2931 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2932 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002933 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002934 } else {
2935 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2936 Address(CpuRegister(RSP), in.GetStackIndex()));
2937 }
Roland Levillaincff13742014-11-17 14:32:17 +00002938 break;
2939
2940 default:
2941 LOG(FATAL) << "Unexpected type conversion from " << input_type
2942 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002943 }
Roland Levillaincff13742014-11-17 14:32:17 +00002944 break;
2945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002947 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002949 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002951 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kInt16:
2953 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002954 if (in.IsRegister()) {
2955 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2956 } else if (in.IsConstant()) {
2957 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2958 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002959 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002960 } else {
2961 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2962 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2963 }
Roland Levillaincff13742014-11-17 14:32:17 +00002964 break;
2965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002967 if (in.IsRegister()) {
2968 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2969 } else if (in.IsConstant()) {
2970 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2971 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002972 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002973 } else {
2974 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2975 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2976 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002977 break;
2978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002979 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002980 if (in.IsFpuRegister()) {
2981 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2982 } else if (in.IsConstant()) {
2983 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2984 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002985 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002986 } else {
2987 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2988 Address(CpuRegister(RSP), in.GetStackIndex()));
2989 }
Roland Levillaincff13742014-11-17 14:32:17 +00002990 break;
2991
2992 default:
2993 LOG(FATAL) << "Unexpected type conversion from " << input_type
2994 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002995 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002996 break;
2997
2998 default:
2999 LOG(FATAL) << "Unexpected type conversion from " << input_type
3000 << " to " << result_type;
3001 }
3002}
3003
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003004void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003005 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003006 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003007 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003008 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003009 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003010 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3011 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003012 break;
3013 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003016 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003017 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003018 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003020 break;
3021 }
3022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003023 case DataType::Type::kFloat64:
3024 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003025 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003026 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003028 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003029 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030
3031 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003032 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003033 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003034}
3035
3036void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3037 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003038 Location first = locations->InAt(0);
3039 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003040 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003041
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003042 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003044 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003045 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3046 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003047 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3048 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003049 } else {
3050 __ leal(out.AsRegister<CpuRegister>(), Address(
3051 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3052 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003053 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003054 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3055 __ addl(out.AsRegister<CpuRegister>(),
3056 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3057 } else {
3058 __ leal(out.AsRegister<CpuRegister>(), Address(
3059 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3060 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003061 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003062 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003064 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003065 break;
3066 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003069 if (second.IsRegister()) {
3070 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3071 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003072 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3073 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003074 } else {
3075 __ leaq(out.AsRegister<CpuRegister>(), Address(
3076 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3077 }
3078 } else {
3079 DCHECK(second.IsConstant());
3080 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3081 int32_t int32_value = Low32Bits(value);
3082 DCHECK_EQ(int32_value, value);
3083 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3084 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3085 } else {
3086 __ leaq(out.AsRegister<CpuRegister>(), Address(
3087 first.AsRegister<CpuRegister>(), int32_value));
3088 }
3089 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003090 break;
3091 }
3092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003093 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003094 if (second.IsFpuRegister()) {
3095 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3096 } else if (second.IsConstant()) {
3097 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003098 codegen_->LiteralFloatAddress(
3099 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003100 } else {
3101 DCHECK(second.IsStackSlot());
3102 __ addss(first.AsFpuRegister<XmmRegister>(),
3103 Address(CpuRegister(RSP), second.GetStackIndex()));
3104 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003105 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003106 }
3107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003108 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003109 if (second.IsFpuRegister()) {
3110 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3111 } else if (second.IsConstant()) {
3112 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003113 codegen_->LiteralDoubleAddress(
3114 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003115 } else {
3116 DCHECK(second.IsDoubleStackSlot());
3117 __ addsd(first.AsFpuRegister<XmmRegister>(),
3118 Address(CpuRegister(RSP), second.GetStackIndex()));
3119 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003120 break;
3121 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003122
3123 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003124 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003125 }
3126}
3127
3128void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003129 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003130 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003131 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003133 locations->SetInAt(0, Location::RequiresRegister());
3134 locations->SetInAt(1, Location::Any());
3135 locations->SetOut(Location::SameAsFirstInput());
3136 break;
3137 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003138 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003139 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003140 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003141 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003142 break;
3143 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003144 case DataType::Type::kFloat32:
3145 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003146 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003147 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003148 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003149 break;
Calin Juravle11351682014-10-23 15:38:15 +01003150 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003151 default:
Calin Juravle11351682014-10-23 15:38:15 +01003152 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003153 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003154}
3155
3156void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3157 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003158 Location first = locations->InAt(0);
3159 Location second = locations->InAt(1);
3160 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003161 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003162 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003163 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003164 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003165 } else if (second.IsConstant()) {
3166 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003167 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003168 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003169 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003170 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003171 break;
3172 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003174 if (second.IsConstant()) {
3175 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3176 DCHECK(IsInt<32>(value));
3177 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3178 } else {
3179 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3180 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003181 break;
3182 }
3183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003184 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003185 if (second.IsFpuRegister()) {
3186 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3187 } else if (second.IsConstant()) {
3188 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003189 codegen_->LiteralFloatAddress(
3190 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003191 } else {
3192 DCHECK(second.IsStackSlot());
3193 __ subss(first.AsFpuRegister<XmmRegister>(),
3194 Address(CpuRegister(RSP), second.GetStackIndex()));
3195 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003196 break;
Calin Juravle11351682014-10-23 15:38:15 +01003197 }
3198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003200 if (second.IsFpuRegister()) {
3201 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3202 } else if (second.IsConstant()) {
3203 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003204 codegen_->LiteralDoubleAddress(
3205 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003206 } else {
3207 DCHECK(second.IsDoubleStackSlot());
3208 __ subsd(first.AsFpuRegister<XmmRegister>(),
3209 Address(CpuRegister(RSP), second.GetStackIndex()));
3210 }
Calin Juravle11351682014-10-23 15:38:15 +01003211 break;
3212 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003213
3214 default:
Calin Juravle11351682014-10-23 15:38:15 +01003215 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003216 }
3217}
3218
Calin Juravle34bacdf2014-10-07 20:23:36 +01003219void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3220 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003221 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003222 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003223 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224 locations->SetInAt(0, Location::RequiresRegister());
3225 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003226 if (mul->InputAt(1)->IsIntConstant()) {
3227 // Can use 3 operand multiply.
3228 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3229 } else {
3230 locations->SetOut(Location::SameAsFirstInput());
3231 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003232 break;
3233 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003234 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003235 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003236 locations->SetInAt(1, Location::Any());
3237 if (mul->InputAt(1)->IsLongConstant() &&
3238 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003239 // Can use 3 operand multiply.
3240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3241 } else {
3242 locations->SetOut(Location::SameAsFirstInput());
3243 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003244 break;
3245 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003246 case DataType::Type::kFloat32:
3247 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003248 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003249 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003250 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003252 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253
3254 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003255 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003256 }
3257}
3258
3259void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3260 LocationSummary* locations = mul->GetLocations();
3261 Location first = locations->InAt(0);
3262 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003263 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003264 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003266 // The constant may have ended up in a register, so test explicitly to avoid
3267 // problems where the output may not be the same as the first operand.
3268 if (mul->InputAt(1)->IsIntConstant()) {
3269 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3270 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3271 } else if (second.IsRegister()) {
3272 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003273 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003274 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003275 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003276 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003277 __ imull(first.AsRegister<CpuRegister>(),
3278 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003279 }
3280 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003281 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003282 // The constant may have ended up in a register, so test explicitly to avoid
3283 // problems where the output may not be the same as the first operand.
3284 if (mul->InputAt(1)->IsLongConstant()) {
3285 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3286 if (IsInt<32>(value)) {
3287 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3288 Immediate(static_cast<int32_t>(value)));
3289 } else {
3290 // Have to use the constant area.
3291 DCHECK(first.Equals(out));
3292 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3293 }
3294 } else if (second.IsRegister()) {
3295 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003296 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003297 } else {
3298 DCHECK(second.IsDoubleStackSlot());
3299 DCHECK(first.Equals(out));
3300 __ imulq(first.AsRegister<CpuRegister>(),
3301 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003302 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 break;
3304 }
3305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003306 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003307 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003308 if (second.IsFpuRegister()) {
3309 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3310 } else if (second.IsConstant()) {
3311 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003312 codegen_->LiteralFloatAddress(
3313 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003314 } else {
3315 DCHECK(second.IsStackSlot());
3316 __ mulss(first.AsFpuRegister<XmmRegister>(),
3317 Address(CpuRegister(RSP), second.GetStackIndex()));
3318 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003320 }
3321
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003322 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003323 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003324 if (second.IsFpuRegister()) {
3325 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3326 } else if (second.IsConstant()) {
3327 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003328 codegen_->LiteralDoubleAddress(
3329 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003330 } else {
3331 DCHECK(second.IsDoubleStackSlot());
3332 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3333 Address(CpuRegister(RSP), second.GetStackIndex()));
3334 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003335 break;
3336 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003337
3338 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003339 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003340 }
3341}
3342
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003343void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3344 uint32_t stack_adjustment, bool is_float) {
3345 if (source.IsStackSlot()) {
3346 DCHECK(is_float);
3347 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3348 } else if (source.IsDoubleStackSlot()) {
3349 DCHECK(!is_float);
3350 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3351 } else {
3352 // Write the value to the temporary location on the stack and load to FP stack.
3353 if (is_float) {
3354 Location stack_temp = Location::StackSlot(temp_offset);
3355 codegen_->Move(stack_temp, source);
3356 __ flds(Address(CpuRegister(RSP), temp_offset));
3357 } else {
3358 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3359 codegen_->Move(stack_temp, source);
3360 __ fldl(Address(CpuRegister(RSP), temp_offset));
3361 }
3362 }
3363}
3364
3365void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003366 DataType::Type type = rem->GetResultType();
3367 bool is_float = type == DataType::Type::kFloat32;
3368 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003369 LocationSummary* locations = rem->GetLocations();
3370 Location first = locations->InAt(0);
3371 Location second = locations->InAt(1);
3372 Location out = locations->Out();
3373
3374 // Create stack space for 2 elements.
3375 // TODO: enhance register allocator to ask for stack temporaries.
3376 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3377
3378 // Load the values to the FP stack in reverse order, using temporaries if needed.
3379 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3380 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3381
3382 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003383 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 __ Bind(&retry);
3385 __ fprem();
3386
3387 // Move FP status to AX.
3388 __ fstsw();
3389
3390 // And see if the argument reduction is complete. This is signaled by the
3391 // C2 FPU flag bit set to 0.
3392 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3393 __ j(kNotEqual, &retry);
3394
3395 // We have settled on the final value. Retrieve it into an XMM register.
3396 // Store FP top of stack to real stack.
3397 if (is_float) {
3398 __ fsts(Address(CpuRegister(RSP), 0));
3399 } else {
3400 __ fstl(Address(CpuRegister(RSP), 0));
3401 }
3402
3403 // Pop the 2 items from the FP stack.
3404 __ fucompp();
3405
3406 // Load the value from the stack into an XMM register.
3407 DCHECK(out.IsFpuRegister()) << out;
3408 if (is_float) {
3409 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3410 } else {
3411 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3412 }
3413
3414 // And remove the temporary stack space we allocated.
3415 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3416}
3417
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003418void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3419 DCHECK(instruction->IsDiv() || instruction->IsRem());
3420
3421 LocationSummary* locations = instruction->GetLocations();
3422 Location second = locations->InAt(1);
3423 DCHECK(second.IsConstant());
3424
3425 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3426 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003427 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003428
3429 DCHECK(imm == 1 || imm == -1);
3430
3431 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003432 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003433 if (instruction->IsRem()) {
3434 __ xorl(output_register, output_register);
3435 } else {
3436 __ movl(output_register, input_register);
3437 if (imm == -1) {
3438 __ negl(output_register);
3439 }
3440 }
3441 break;
3442 }
3443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003444 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003446 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 } else {
3448 __ movq(output_register, input_register);
3449 if (imm == -1) {
3450 __ negq(output_register);
3451 }
3452 }
3453 break;
3454 }
3455
3456 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003457 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003458 }
3459}
3460
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003461void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003462 LocationSummary* locations = instruction->GetLocations();
3463 Location second = locations->InAt(1);
3464
3465 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3466 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3467
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003468 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003469 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3470 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471
3472 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003474 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003475 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476 __ testl(numerator, numerator);
3477 __ cmov(kGreaterEqual, tmp, numerator);
3478 int shift = CTZ(imm);
3479 __ sarl(tmp, Immediate(shift));
3480
3481 if (imm < 0) {
3482 __ negl(tmp);
3483 }
3484
3485 __ movl(output_register, tmp);
3486 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003487 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3489
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003490 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003491 __ addq(rdx, numerator);
3492 __ testq(numerator, numerator);
3493 __ cmov(kGreaterEqual, rdx, numerator);
3494 int shift = CTZ(imm);
3495 __ sarq(rdx, Immediate(shift));
3496
3497 if (imm < 0) {
3498 __ negq(rdx);
3499 }
3500
3501 __ movq(output_register, rdx);
3502 }
3503}
3504
3505void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3506 DCHECK(instruction->IsDiv() || instruction->IsRem());
3507
3508 LocationSummary* locations = instruction->GetLocations();
3509 Location second = locations->InAt(1);
3510
3511 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3512 : locations->GetTemp(0).AsRegister<CpuRegister>();
3513 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3514 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3515 : locations->Out().AsRegister<CpuRegister>();
3516 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3517
3518 DCHECK_EQ(RAX, eax.AsRegister());
3519 DCHECK_EQ(RDX, edx.AsRegister());
3520 if (instruction->IsDiv()) {
3521 DCHECK_EQ(RAX, out.AsRegister());
3522 } else {
3523 DCHECK_EQ(RDX, out.AsRegister());
3524 }
3525
3526 int64_t magic;
3527 int shift;
3528
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003529 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003530 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3532
3533 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3534
3535 __ movl(numerator, eax);
3536
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537 __ movl(eax, Immediate(magic));
3538 __ imull(numerator);
3539
3540 if (imm > 0 && magic < 0) {
3541 __ addl(edx, numerator);
3542 } else if (imm < 0 && magic > 0) {
3543 __ subl(edx, numerator);
3544 }
3545
3546 if (shift != 0) {
3547 __ sarl(edx, Immediate(shift));
3548 }
3549
3550 __ movl(eax, edx);
3551 __ shrl(edx, Immediate(31));
3552 __ addl(edx, eax);
3553
3554 if (instruction->IsRem()) {
3555 __ movl(eax, numerator);
3556 __ imull(edx, Immediate(imm));
3557 __ subl(eax, edx);
3558 __ movl(edx, eax);
3559 } else {
3560 __ movl(eax, edx);
3561 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003562 } else {
3563 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003565 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003566
3567 CpuRegister rax = eax;
3568 CpuRegister rdx = edx;
3569
3570 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3571
3572 // Save the numerator.
3573 __ movq(numerator, rax);
3574
3575 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003576 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003577
3578 // RDX:RAX = magic * numerator
3579 __ imulq(numerator);
3580
3581 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003582 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 __ addq(rdx, numerator);
3584 } else if (imm < 0 && magic > 0) {
3585 // RDX -= numerator
3586 __ subq(rdx, numerator);
3587 }
3588
3589 // Shift if needed.
3590 if (shift != 0) {
3591 __ sarq(rdx, Immediate(shift));
3592 }
3593
3594 // RDX += 1 if RDX < 0
3595 __ movq(rax, rdx);
3596 __ shrq(rdx, Immediate(63));
3597 __ addq(rdx, rax);
3598
3599 if (instruction->IsRem()) {
3600 __ movq(rax, numerator);
3601
3602 if (IsInt<32>(imm)) {
3603 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3604 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003605 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 }
3607
3608 __ subq(rax, rdx);
3609 __ movq(rdx, rax);
3610 } else {
3611 __ movq(rax, rdx);
3612 }
3613 }
3614}
3615
Calin Juravlebacfec32014-11-14 15:54:36 +00003616void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3617 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 DataType::Type type = instruction->GetResultType();
3619 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003620
3621 bool is_div = instruction->IsDiv();
3622 LocationSummary* locations = instruction->GetLocations();
3623
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3625 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003626
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003629
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003630 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003631 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003632
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003633 if (imm == 0) {
3634 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3635 } else if (imm == 1 || imm == -1) {
3636 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003637 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003638 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003639 } else {
3640 DCHECK(imm <= -2 || imm >= 2);
3641 GenerateDivRemWithAnyConstant(instruction);
3642 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003643 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003644 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003645 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003646 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003648
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003649 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3650 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3651 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3652 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003653 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003654 __ cmpl(second_reg, Immediate(-1));
3655 __ j(kEqual, slow_path->GetEntryLabel());
3656 // edx:eax <- sign-extended of eax
3657 __ cdq();
3658 // eax = quotient, edx = remainder
3659 __ idivl(second_reg);
3660 } else {
3661 __ cmpq(second_reg, Immediate(-1));
3662 __ j(kEqual, slow_path->GetEntryLabel());
3663 // rdx:rax <- sign-extended of rax
3664 __ cqo();
3665 // rax = quotient, rdx = remainder
3666 __ idivq(second_reg);
3667 }
3668 __ Bind(slow_path->GetExitLabel());
3669 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003670}
3671
Calin Juravle7c4954d2014-10-28 16:57:40 +00003672void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3673 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003674 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003675 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 case DataType::Type::kInt32:
3677 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003678 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003679 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003680 locations->SetOut(Location::SameAsFirstInput());
3681 // Intel uses edx:eax as the dividend.
3682 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003683 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3684 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3685 // output and request another temp.
3686 if (div->InputAt(1)->IsConstant()) {
3687 locations->AddTemp(Location::RequiresRegister());
3688 }
Calin Juravled0d48522014-11-04 16:40:20 +00003689 break;
3690 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 case DataType::Type::kFloat32:
3693 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003695 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003696 locations->SetOut(Location::SameAsFirstInput());
3697 break;
3698 }
3699
3700 default:
3701 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3702 }
3703}
3704
3705void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3706 LocationSummary* locations = div->GetLocations();
3707 Location first = locations->InAt(0);
3708 Location second = locations->InAt(1);
3709 DCHECK(first.Equals(locations->Out()));
3710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003712 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 case DataType::Type::kInt32:
3714 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003715 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003716 break;
3717 }
3718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003719 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003720 if (second.IsFpuRegister()) {
3721 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3722 } else if (second.IsConstant()) {
3723 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003724 codegen_->LiteralFloatAddress(
3725 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003726 } else {
3727 DCHECK(second.IsStackSlot());
3728 __ divss(first.AsFpuRegister<XmmRegister>(),
3729 Address(CpuRegister(RSP), second.GetStackIndex()));
3730 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003731 break;
3732 }
3733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003734 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003735 if (second.IsFpuRegister()) {
3736 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3737 } else if (second.IsConstant()) {
3738 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003739 codegen_->LiteralDoubleAddress(
3740 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003741 } else {
3742 DCHECK(second.IsDoubleStackSlot());
3743 __ divsd(first.AsFpuRegister<XmmRegister>(),
3744 Address(CpuRegister(RSP), second.GetStackIndex()));
3745 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003746 break;
3747 }
3748
3749 default:
3750 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3751 }
3752}
3753
Calin Juravlebacfec32014-11-14 15:54:36 +00003754void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003756 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003757 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003758
3759 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 case DataType::Type::kInt32:
3761 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003763 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3765 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003766 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3767 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3768 // output and request another temp.
3769 if (rem->InputAt(1)->IsConstant()) {
3770 locations->AddTemp(Location::RequiresRegister());
3771 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003772 break;
3773 }
3774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 case DataType::Type::kFloat32:
3776 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003777 locations->SetInAt(0, Location::Any());
3778 locations->SetInAt(1, Location::Any());
3779 locations->SetOut(Location::RequiresFpuRegister());
3780 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 break;
3782 }
3783
3784 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003785 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003786 }
3787}
3788
3789void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003791 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003792 case DataType::Type::kInt32:
3793 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003794 GenerateDivRemIntegral(rem);
3795 break;
3796 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003797 case DataType::Type::kFloat32:
3798 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003800 break;
3801 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003802 default:
3803 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3804 }
3805}
3806
Calin Juravled0d48522014-11-04 16:40:20 +00003807void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003808 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003809 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003810}
3811
3812void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003813 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003814 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003815 codegen_->AddSlowPath(slow_path);
3816
3817 LocationSummary* locations = instruction->GetLocations();
3818 Location value = locations->InAt(0);
3819
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003820 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003822 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003823 case DataType::Type::kInt8:
3824 case DataType::Type::kUint16:
3825 case DataType::Type::kInt16:
3826 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003827 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003829 __ j(kEqual, slow_path->GetEntryLabel());
3830 } else if (value.IsStackSlot()) {
3831 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3832 __ j(kEqual, slow_path->GetEntryLabel());
3833 } else {
3834 DCHECK(value.IsConstant()) << value;
3835 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003836 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003837 }
3838 }
3839 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003840 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003842 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003843 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003844 __ j(kEqual, slow_path->GetEntryLabel());
3845 } else if (value.IsDoubleStackSlot()) {
3846 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3847 __ j(kEqual, slow_path->GetEntryLabel());
3848 } else {
3849 DCHECK(value.IsConstant()) << value;
3850 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003851 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003852 }
3853 }
3854 break;
3855 }
3856 default:
3857 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003858 }
Calin Juravled0d48522014-11-04 16:40:20 +00003859}
3860
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3862 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3863
3864 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003865 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866
3867 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kInt32:
3869 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003870 locations->SetInAt(0, Location::RequiresRegister());
3871 // The shift count needs to be in CL.
3872 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3873 locations->SetOut(Location::SameAsFirstInput());
3874 break;
3875 }
3876 default:
3877 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3878 }
3879}
3880
3881void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3882 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3883
3884 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003886 Location second = locations->InAt(1);
3887
3888 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003890 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003891 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003892 if (op->IsShl()) {
3893 __ shll(first_reg, second_reg);
3894 } else if (op->IsShr()) {
3895 __ sarl(first_reg, second_reg);
3896 } else {
3897 __ shrl(first_reg, second_reg);
3898 }
3899 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003900 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003901 if (op->IsShl()) {
3902 __ shll(first_reg, imm);
3903 } else if (op->IsShr()) {
3904 __ sarl(first_reg, imm);
3905 } else {
3906 __ shrl(first_reg, imm);
3907 }
3908 }
3909 break;
3910 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00003912 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003914 if (op->IsShl()) {
3915 __ shlq(first_reg, second_reg);
3916 } else if (op->IsShr()) {
3917 __ sarq(first_reg, second_reg);
3918 } else {
3919 __ shrq(first_reg, second_reg);
3920 }
3921 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003922 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003923 if (op->IsShl()) {
3924 __ shlq(first_reg, imm);
3925 } else if (op->IsShr()) {
3926 __ sarq(first_reg, imm);
3927 } else {
3928 __ shrq(first_reg, imm);
3929 }
3930 }
3931 break;
3932 }
3933 default:
3934 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003935 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936 }
3937}
3938
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003939void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3940 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003941 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003942
3943 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003944 case DataType::Type::kInt32:
3945 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003946 locations->SetInAt(0, Location::RequiresRegister());
3947 // The shift count needs to be in CL (unless it is a constant).
3948 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3949 locations->SetOut(Location::SameAsFirstInput());
3950 break;
3951 }
3952 default:
3953 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3954 UNREACHABLE();
3955 }
3956}
3957
3958void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3959 LocationSummary* locations = ror->GetLocations();
3960 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3961 Location second = locations->InAt(1);
3962
3963 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003964 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003965 if (second.IsRegister()) {
3966 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3967 __ rorl(first_reg, second_reg);
3968 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003969 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003970 __ rorl(first_reg, imm);
3971 }
3972 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003973 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003974 if (second.IsRegister()) {
3975 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3976 __ rorq(first_reg, second_reg);
3977 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003978 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003979 __ rorq(first_reg, imm);
3980 }
3981 break;
3982 default:
3983 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3984 UNREACHABLE();
3985 }
3986}
3987
Calin Juravle9aec02f2014-11-18 23:06:35 +00003988void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3989 HandleShift(shl);
3990}
3991
3992void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3993 HandleShift(shl);
3994}
3995
3996void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3997 HandleShift(shr);
3998}
3999
4000void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4001 HandleShift(shr);
4002}
4003
4004void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4005 HandleShift(ushr);
4006}
4007
4008void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4009 HandleShift(ushr);
4010}
4011
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004012void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004013 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4014 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004015 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004016 if (instruction->IsStringAlloc()) {
4017 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4018 } else {
4019 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004020 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004021 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004022}
4023
4024void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004025 // Note: if heap poisoning is enabled, the entry point takes cares
4026 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004027 if (instruction->IsStringAlloc()) {
4028 // String is allocated through StringFactory. Call NewEmptyString entry point.
4029 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004030 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004031 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4032 __ call(Address(temp, code_offset.SizeValue()));
4033 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4034 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004035 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004036 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004037 DCHECK(!codegen_->IsLeafMethod());
4038 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004039}
4040
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004041void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004042 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4043 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004044 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004045 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004046 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4047 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004048}
4049
4050void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004051 // Note: if heap poisoning is enabled, the entry point takes cares
4052 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004053 QuickEntrypointEnum entrypoint =
4054 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4055 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004056 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004057 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004058}
4059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004060void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004061 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004062 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004063 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4064 if (location.IsStackSlot()) {
4065 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4066 } else if (location.IsDoubleStackSlot()) {
4067 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4068 }
4069 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004070}
4071
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004072void InstructionCodeGeneratorX86_64::VisitParameterValue(
4073 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004074 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004075}
4076
4077void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4078 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004079 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004080 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4081}
4082
4083void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4084 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4085 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004086}
4087
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004088void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4089 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004090 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetOut(Location::RequiresRegister());
4093}
4094
4095void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4096 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004097 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004098 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004099 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004100 __ movq(locations->Out().AsRegister<CpuRegister>(),
4101 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004102 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004103 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004104 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004105 __ movq(locations->Out().AsRegister<CpuRegister>(),
4106 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4107 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004108 __ movq(locations->Out().AsRegister<CpuRegister>(),
4109 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004110 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004111}
4112
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004113void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004114 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004115 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004116 locations->SetInAt(0, Location::RequiresRegister());
4117 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004118}
4119
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004120void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4121 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004122 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4123 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004124 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004125 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004126 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004127 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004128 break;
4129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004130 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004131 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004132 break;
4133
4134 default:
4135 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4136 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004137}
4138
David Brazdil66d126e2015-04-03 16:02:44 +01004139void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4140 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004141 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004142 locations->SetInAt(0, Location::RequiresRegister());
4143 locations->SetOut(Location::SameAsFirstInput());
4144}
4145
4146void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004147 LocationSummary* locations = bool_not->GetLocations();
4148 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4149 locations->Out().AsRegister<CpuRegister>().AsRegister());
4150 Location out = locations->Out();
4151 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4152}
4153
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004154void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004155 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004156 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004157 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004158 locations->SetInAt(i, Location::Any());
4159 }
4160 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004161}
4162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004163void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004164 LOG(FATAL) << "Unimplemented";
4165}
4166
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004167void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004168 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004169 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004170 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004171 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4172 */
4173 switch (kind) {
4174 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004175 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004176 break;
4177 }
4178 case MemBarrierKind::kAnyStore:
4179 case MemBarrierKind::kLoadAny:
4180 case MemBarrierKind::kStoreStore: {
4181 // nop
4182 break;
4183 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004184 case MemBarrierKind::kNTStoreStore:
4185 // Non-Temporal Store/Store needs an explicit fence.
4186 MemoryFence(/* non-temporal */ true);
4187 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004188 }
4189}
4190
4191void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4192 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4193
Roland Levillain0d5a2812015-11-13 10:07:31 +00004194 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004196 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004197 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4198 object_field_get_with_read_barrier
4199 ? LocationSummary::kCallOnSlowPath
4200 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004201 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004203 }
Calin Juravle52c48962014-12-16 17:02:57 +00004204 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004206 locations->SetOut(Location::RequiresFpuRegister());
4207 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004208 // The output overlaps for an object field get when read barriers
4209 // are enabled: we do not want the move to overwrite the object's
4210 // location, as we need it to emit the read barrier.
4211 locations->SetOut(
4212 Location::RequiresRegister(),
4213 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004214 }
Calin Juravle52c48962014-12-16 17:02:57 +00004215}
4216
4217void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4218 const FieldInfo& field_info) {
4219 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4220
4221 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004222 Location base_loc = locations->InAt(0);
4223 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004224 Location out = locations->Out();
4225 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004226 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4227 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004228 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4229
Vladimir Marko61b92282017-10-11 13:23:17 +01004230 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004231 case DataType::Type::kBool:
4232 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004233 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4234 break;
4235 }
4236
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004237 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004238 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4239 break;
4240 }
4241
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004242 case DataType::Type::kUint16: {
4243 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004244 break;
4245 }
4246
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004247 case DataType::Type::kInt16: {
4248 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004249 break;
4250 }
4251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004252 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004253 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4254 break;
4255 }
4256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004257 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004258 // /* HeapReference<Object> */ out = *(base + offset)
4259 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004260 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004261 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004262 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004263 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004264 if (is_volatile) {
4265 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4266 }
4267 } else {
4268 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4269 codegen_->MaybeRecordImplicitNullCheck(instruction);
4270 if (is_volatile) {
4271 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4272 }
4273 // If read barriers are enabled, emit read barriers other than
4274 // Baker's using a slow path (and also unpoison the loaded
4275 // reference, if heap poisoning is enabled).
4276 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4277 }
4278 break;
4279 }
4280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004281 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004282 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4283 break;
4284 }
4285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004286 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004287 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4288 break;
4289 }
4290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004292 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4293 break;
4294 }
4295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004296 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004297 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004298 UNREACHABLE();
4299 }
4300
Vladimir Marko61b92282017-10-11 13:23:17 +01004301 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004302 // Potential implicit null checks, in the case of reference
4303 // fields, are handled in the previous switch statement.
4304 } else {
4305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004306 }
Roland Levillain4d027112015-07-01 15:41:14 +01004307
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004308 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004309 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004310 // Memory barriers, in the case of references, are also handled
4311 // in the previous switch statement.
4312 } else {
4313 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4314 }
Roland Levillain4d027112015-07-01 15:41:14 +01004315 }
Calin Juravle52c48962014-12-16 17:02:57 +00004316}
4317
4318void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4319 const FieldInfo& field_info) {
4320 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4321
4322 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004323 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004324 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004325 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004326 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004327 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004328
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004329 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004331 if (is_volatile) {
4332 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4333 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4334 } else {
4335 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4336 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004337 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004338 if (is_volatile) {
4339 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4340 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4341 } else {
4342 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4343 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004344 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004345 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004346 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004347 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004348 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004349 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004350 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004351 locations->AddTemp(Location::RequiresRegister());
4352 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353}
4354
Calin Juravle52c48962014-12-16 17:02:57 +00004355void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004356 const FieldInfo& field_info,
4357 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004358 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004361 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4362 Location value = locations->InAt(1);
4363 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004364 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004365 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4366
4367 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004368 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004369 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370
Mark Mendellea5af682015-10-22 17:35:49 -04004371 bool maybe_record_implicit_null_check_done = false;
4372
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004374 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004375 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004376 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004377 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004378 __ movb(Address(base, offset),
4379 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004380 } else {
4381 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4382 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004383 break;
4384 }
4385
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004386 case DataType::Type::kUint16:
4387 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004388 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004389 __ movw(Address(base, offset),
4390 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004391 } else {
4392 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4393 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394 break;
4395 }
4396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004397 case DataType::Type::kInt32:
4398 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004399 if (value.IsConstant()) {
4400 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004401 // `field_type == DataType::Type::kReference` implies `v == 0`.
4402 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004403 // Note: if heap poisoning is enabled, no need to poison
4404 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004405 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004406 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004407 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004408 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4409 __ movl(temp, value.AsRegister<CpuRegister>());
4410 __ PoisonHeapReference(temp);
4411 __ movl(Address(base, offset), temp);
4412 } else {
4413 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4414 }
Mark Mendell40741f32015-04-20 22:10:34 -04004415 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416 break;
4417 }
4418
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004419 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004420 if (value.IsConstant()) {
4421 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004422 codegen_->MoveInt64ToAddress(Address(base, offset),
4423 Address(base, offset + sizeof(int32_t)),
4424 v,
4425 instruction);
4426 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004427 } else {
4428 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4429 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430 break;
4431 }
4432
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004433 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004434 if (value.IsConstant()) {
4435 int32_t v =
4436 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4437 __ movl(Address(base, offset), Immediate(v));
4438 } else {
4439 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4440 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004441 break;
4442 }
4443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004444 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004445 if (value.IsConstant()) {
4446 int64_t v =
4447 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4448 codegen_->MoveInt64ToAddress(Address(base, offset),
4449 Address(base, offset + sizeof(int32_t)),
4450 v,
4451 instruction);
4452 maybe_record_implicit_null_check_done = true;
4453 } else {
4454 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4455 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004456 break;
4457 }
4458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004461 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462 }
Calin Juravle52c48962014-12-16 17:02:57 +00004463
Mark Mendellea5af682015-10-22 17:35:49 -04004464 if (!maybe_record_implicit_null_check_done) {
4465 codegen_->MaybeRecordImplicitNullCheck(instruction);
4466 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004467
4468 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4469 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4470 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004471 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004472 }
4473
Calin Juravle52c48962014-12-16 17:02:57 +00004474 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004475 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004476 }
4477}
4478
4479void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4480 HandleFieldSet(instruction, instruction->GetFieldInfo());
4481}
4482
4483void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004484 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485}
4486
4487void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004488 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489}
4490
4491void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004492 HandleFieldGet(instruction, instruction->GetFieldInfo());
4493}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004494
Calin Juravle52c48962014-12-16 17:02:57 +00004495void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4496 HandleFieldGet(instruction);
4497}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004498
Calin Juravle52c48962014-12-16 17:02:57 +00004499void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4500 HandleFieldGet(instruction, instruction->GetFieldInfo());
4501}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502
Calin Juravle52c48962014-12-16 17:02:57 +00004503void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4504 HandleFieldSet(instruction, instruction->GetFieldInfo());
4505}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506
Calin Juravle52c48962014-12-16 17:02:57 +00004507void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004508 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509}
4510
Calin Juravlee460d1d2015-09-29 04:52:17 +01004511void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4512 HUnresolvedInstanceFieldGet* instruction) {
4513 FieldAccessCallingConventionX86_64 calling_convention;
4514 codegen_->CreateUnresolvedFieldLocationSummary(
4515 instruction, instruction->GetFieldType(), calling_convention);
4516}
4517
4518void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4519 HUnresolvedInstanceFieldGet* instruction) {
4520 FieldAccessCallingConventionX86_64 calling_convention;
4521 codegen_->GenerateUnresolvedFieldAccess(instruction,
4522 instruction->GetFieldType(),
4523 instruction->GetFieldIndex(),
4524 instruction->GetDexPc(),
4525 calling_convention);
4526}
4527
4528void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4529 HUnresolvedInstanceFieldSet* instruction) {
4530 FieldAccessCallingConventionX86_64 calling_convention;
4531 codegen_->CreateUnresolvedFieldLocationSummary(
4532 instruction, instruction->GetFieldType(), calling_convention);
4533}
4534
4535void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4536 HUnresolvedInstanceFieldSet* instruction) {
4537 FieldAccessCallingConventionX86_64 calling_convention;
4538 codegen_->GenerateUnresolvedFieldAccess(instruction,
4539 instruction->GetFieldType(),
4540 instruction->GetFieldIndex(),
4541 instruction->GetDexPc(),
4542 calling_convention);
4543}
4544
4545void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4546 HUnresolvedStaticFieldGet* instruction) {
4547 FieldAccessCallingConventionX86_64 calling_convention;
4548 codegen_->CreateUnresolvedFieldLocationSummary(
4549 instruction, instruction->GetFieldType(), calling_convention);
4550}
4551
4552void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4553 HUnresolvedStaticFieldGet* instruction) {
4554 FieldAccessCallingConventionX86_64 calling_convention;
4555 codegen_->GenerateUnresolvedFieldAccess(instruction,
4556 instruction->GetFieldType(),
4557 instruction->GetFieldIndex(),
4558 instruction->GetDexPc(),
4559 calling_convention);
4560}
4561
4562void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4563 HUnresolvedStaticFieldSet* instruction) {
4564 FieldAccessCallingConventionX86_64 calling_convention;
4565 codegen_->CreateUnresolvedFieldLocationSummary(
4566 instruction, instruction->GetFieldType(), calling_convention);
4567}
4568
4569void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4570 HUnresolvedStaticFieldSet* instruction) {
4571 FieldAccessCallingConventionX86_64 calling_convention;
4572 codegen_->GenerateUnresolvedFieldAccess(instruction,
4573 instruction->GetFieldType(),
4574 instruction->GetFieldIndex(),
4575 instruction->GetDexPc(),
4576 calling_convention);
4577}
4578
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004579void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004580 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4581 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4582 ? Location::RequiresRegister()
4583 : Location::Any();
4584 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585}
4586
Calin Juravle2ae48182016-03-16 14:05:09 +00004587void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4588 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004589 return;
4590 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004591 LocationSummary* locations = instruction->GetLocations();
4592 Location obj = locations->InAt(0);
4593
4594 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004595 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004596}
4597
Calin Juravle2ae48182016-03-16 14:05:09 +00004598void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004599 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004600 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601
4602 LocationSummary* locations = instruction->GetLocations();
4603 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004604
4605 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004606 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004607 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004608 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004609 } else {
4610 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004611 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004612 __ jmp(slow_path->GetEntryLabel());
4613 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614 }
4615 __ j(kEqual, slow_path->GetEntryLabel());
4616}
4617
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004618void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004619 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004620}
4621
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004623 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004624 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004625 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004626 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4627 object_array_get_with_read_barrier
4628 ? LocationSummary::kCallOnSlowPath
4629 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004630 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004631 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004632 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004633 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004634 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004635 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004636 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4637 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004638 // The output overlaps for an object array get when read barriers
4639 // are enabled: we do not want the move to overwrite the array's
4640 // location, as we need it to emit the read barrier.
4641 locations->SetOut(
4642 Location::RequiresRegister(),
4643 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004644 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004645}
4646
4647void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4648 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004649 Location obj_loc = locations->InAt(0);
4650 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004652 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004653 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004655 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004656 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004657 case DataType::Type::kBool:
4658 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004659 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004660 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 break;
4662 }
4663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004664 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004665 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004666 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 break;
4668 }
4669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004671 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004672 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4673 // Branch cases into compressed and uncompressed for each index's type.
4674 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4675 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004676 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004678 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4679 "Expecting 0=compressed, 1=uncompressed");
4680 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004681 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4682 __ jmp(&done);
4683 __ Bind(&not_compressed);
4684 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4685 __ Bind(&done);
4686 } else {
4687 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4688 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 break;
4690 }
4691
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004692 case DataType::Type::kInt16: {
4693 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4694 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4695 break;
4696 }
4697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004698 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004699 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004700 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701 break;
4702 }
4703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004704 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004705 static_assert(
4706 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4707 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004708 // /* HeapReference<Object> */ out =
4709 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4710 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004711 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004712 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004713 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004714 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004715 } else {
4716 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004717 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4718 codegen_->MaybeRecordImplicitNullCheck(instruction);
4719 // If read barriers are enabled, emit read barriers other than
4720 // Baker's using a slow path (and also unpoison the loaded
4721 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004722 if (index.IsConstant()) {
4723 uint32_t offset =
4724 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004725 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4726 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004727 codegen_->MaybeGenerateReadBarrierSlow(
4728 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4729 }
4730 }
4731 break;
4732 }
4733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004734 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004735 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004736 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 break;
4738 }
4739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004741 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004742 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004743 break;
4744 }
4745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004746 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004747 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004748 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004749 break;
4750 }
4751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004752 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004753 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004754 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 }
Roland Levillain4d027112015-07-01 15:41:14 +01004756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004758 // Potential implicit null checks, in the case of reference
4759 // arrays, are handled in the previous switch statement.
4760 } else {
4761 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004762 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004763}
4764
4765void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004766 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004767
4768 bool needs_write_barrier =
4769 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004770 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004771
Vladimir Markoca6fff82017-10-03 14:49:14 +01004772 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004774 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004775 LocationSummary::kCallOnSlowPath :
4776 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004777
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004778 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004779 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04004781 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004782 } else {
4783 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4784 }
4785
4786 if (needs_write_barrier) {
4787 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004788 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004789 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791}
4792
4793void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4794 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004795 Location array_loc = locations->InAt(0);
4796 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004798 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004799 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004800 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004801 bool needs_write_barrier =
4802 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004803 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4804 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4805 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004806
4807 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004808 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004809 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004810 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004811 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004812 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004813 if (value.IsRegister()) {
4814 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004816 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819 break;
4820 }
4821
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004822 case DataType::Type::kUint16:
4823 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004824 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004825 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004826 if (value.IsRegister()) {
4827 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004828 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004829 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01004830 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833 break;
4834 }
4835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004838 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004839
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004840 if (!value.IsRegister()) {
4841 // Just setting null.
4842 DCHECK(instruction->InputAt(2)->IsNullConstant());
4843 DCHECK(value.IsConstant()) << value;
4844 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004845 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004847 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004849 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004850
4851 DCHECK(needs_write_barrier);
4852 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004853 // We cannot use a NearLabel for `done`, as its range may be too
4854 // short when Baker read barriers are enabled.
4855 Label done;
4856 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004857 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004858 Location temp_loc = locations->GetTemp(0);
4859 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004860 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004861 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004862 codegen_->AddSlowPath(slow_path);
4863 if (instruction->GetValueCanBeNull()) {
4864 __ testl(register_value, register_value);
4865 __ j(kNotEqual, &not_null);
4866 __ movl(address, Immediate(0));
4867 codegen_->MaybeRecordImplicitNullCheck(instruction);
4868 __ jmp(&done);
4869 __ Bind(&not_null);
4870 }
4871
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004872 // Note that when Baker read barriers are enabled, the type
4873 // checks are performed without read barriers. This is fine,
4874 // even in the case where a class object is in the from-space
4875 // after the flip, as a comparison involving such a type would
4876 // not produce a false positive; it may of course produce a
4877 // false negative, in which case we would take the ArraySet
4878 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004879
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004880 // /* HeapReference<Class> */ temp = array->klass_
4881 __ movl(temp, Address(array, class_offset));
4882 codegen_->MaybeRecordImplicitNullCheck(instruction);
4883 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004884
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004885 // /* HeapReference<Class> */ temp = temp->component_type_
4886 __ movl(temp, Address(temp, component_offset));
4887 // If heap poisoning is enabled, no need to unpoison `temp`
4888 // nor the object reference in `register_value->klass`, as
4889 // we are comparing two poisoned references.
4890 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004891
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004892 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4893 __ j(kEqual, &do_put);
4894 // If heap poisoning is enabled, the `temp` reference has
4895 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004896 __ MaybeUnpoisonHeapReference(temp);
4897
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004898 // If heap poisoning is enabled, no need to unpoison the
4899 // heap reference loaded below, as it is only used for a
4900 // comparison with null.
4901 __ cmpl(Address(temp, super_offset), Immediate(0));
4902 __ j(kNotEqual, slow_path->GetEntryLabel());
4903 __ Bind(&do_put);
4904 } else {
4905 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004906 }
4907 }
4908
4909 if (kPoisonHeapReferences) {
4910 __ movl(temp, register_value);
4911 __ PoisonHeapReference(temp);
4912 __ movl(address, temp);
4913 } else {
4914 __ movl(address, register_value);
4915 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004916 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004917 codegen_->MaybeRecordImplicitNullCheck(instruction);
4918 }
4919
4920 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4921 codegen_->MarkGCCard(
4922 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4923 __ Bind(&done);
4924
4925 if (slow_path != nullptr) {
4926 __ Bind(slow_path->GetExitLabel());
4927 }
4928
4929 break;
4930 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004932 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004933 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004934 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004935 if (value.IsRegister()) {
4936 __ movl(address, value.AsRegister<CpuRegister>());
4937 } else {
4938 DCHECK(value.IsConstant()) << value;
4939 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4940 __ movl(address, Immediate(v));
4941 }
4942 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004943 break;
4944 }
4945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004946 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004947 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004948 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004949 if (value.IsRegister()) {
4950 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004951 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004952 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004954 Address address_high =
4955 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004956 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004957 }
4958 break;
4959 }
4960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004961 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004962 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004963 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004964 if (value.IsFpuRegister()) {
4965 __ movss(address, value.AsFpuRegister<XmmRegister>());
4966 } else {
4967 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004968 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004969 __ movl(address, Immediate(v));
4970 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004971 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004972 break;
4973 }
4974
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004975 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004976 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004977 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004978 if (value.IsFpuRegister()) {
4979 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4980 codegen_->MaybeRecordImplicitNullCheck(instruction);
4981 } else {
4982 int64_t v =
4983 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004984 Address address_high =
4985 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004986 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4987 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004988 break;
4989 }
4990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004991 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004993 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004994 }
4995}
4996
4997void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004998 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004999 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005000 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005001 if (!instruction->IsEmittedAtUseSite()) {
5002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5003 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004}
5005
5006void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005007 if (instruction->IsEmittedAtUseSite()) {
5008 return;
5009 }
5010
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005012 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005013 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5014 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005016 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005017 // Mask out most significant bit in case the array is String's array of char.
5018 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005019 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005020 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021}
5022
5023void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005024 RegisterSet caller_saves = RegisterSet::Empty();
5025 InvokeRuntimeCallingConvention calling_convention;
5026 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5027 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5028 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005029 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005030 HInstruction* length = instruction->InputAt(1);
5031 if (!length->IsEmittedAtUseSite()) {
5032 locations->SetInAt(1, Location::RegisterOrConstant(length));
5033 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034}
5035
5036void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5037 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005038 Location index_loc = locations->InAt(0);
5039 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005040 SlowPathCode* slow_path =
5041 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042
Mark Mendell99dbd682015-04-22 16:18:52 -04005043 if (length_loc.IsConstant()) {
5044 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5045 if (index_loc.IsConstant()) {
5046 // BCE will remove the bounds check if we are guarenteed to pass.
5047 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5048 if (index < 0 || index >= length) {
5049 codegen_->AddSlowPath(slow_path);
5050 __ jmp(slow_path->GetEntryLabel());
5051 } else {
5052 // Some optimization after BCE may have generated this, and we should not
5053 // generate a bounds check if it is a valid range.
5054 }
5055 return;
5056 }
5057
5058 // We have to reverse the jump condition because the length is the constant.
5059 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5060 __ cmpl(index_reg, Immediate(length));
5061 codegen_->AddSlowPath(slow_path);
5062 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005063 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005064 HInstruction* array_length = instruction->InputAt(1);
5065 if (array_length->IsEmittedAtUseSite()) {
5066 // Address the length field in the array.
5067 DCHECK(array_length->IsArrayLength());
5068 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5069 Location array_loc = array_length->GetLocations()->InAt(0);
5070 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005071 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005072 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5073 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005074 CpuRegister length_reg = CpuRegister(TMP);
5075 __ movl(length_reg, array_len);
5076 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005077 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005078 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005079 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005080 // Checking the bound for general case:
5081 // Array of char or String's array when the compression feature off.
5082 if (index_loc.IsConstant()) {
5083 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5084 __ cmpl(array_len, Immediate(value));
5085 } else {
5086 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5087 }
5088 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005089 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005090 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005091 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005092 }
5093 codegen_->AddSlowPath(slow_path);
5094 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005095 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096}
5097
5098void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5099 CpuRegister card,
5100 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005101 CpuRegister value,
5102 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005103 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005104 if (value_can_be_null) {
5105 __ testl(value, value);
5106 __ j(kEqual, &is_null);
5107 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005108 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005109 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 __ movq(temp, object);
5111 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005112 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005113 if (value_can_be_null) {
5114 __ Bind(&is_null);
5115 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116}
5117
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005118void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005119 LOG(FATAL) << "Unimplemented";
5120}
5121
5122void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005123 if (instruction->GetNext()->IsSuspendCheck() &&
5124 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5125 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5126 // The back edge will generate the suspend check.
5127 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5128 }
5129
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005130 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5131}
5132
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005133void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005134 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5135 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005136 // In suspend check slow path, usually there are no caller-save registers at all.
5137 // If SIMD instructions are present, however, we force spilling all live SIMD
5138 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005139 locations->SetCustomSlowPathCallerSaves(
5140 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005141}
5142
5143void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005144 HBasicBlock* block = instruction->GetBlock();
5145 if (block->GetLoopInformation() != nullptr) {
5146 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5147 // The back edge will generate the suspend check.
5148 return;
5149 }
5150 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5151 // The goto will generate the suspend check.
5152 return;
5153 }
5154 GenerateSuspendCheck(instruction, nullptr);
5155}
5156
5157void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5158 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005159 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005160 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5161 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005162 slow_path =
5163 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005164 instruction->SetSlowPath(slow_path);
5165 codegen_->AddSlowPath(slow_path);
5166 if (successor != nullptr) {
5167 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005168 }
5169 } else {
5170 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5171 }
5172
Andreas Gampe542451c2016-07-26 09:02:02 -07005173 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005174 /* no_rip */ true),
5175 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005176 if (successor == nullptr) {
5177 __ j(kNotEqual, slow_path->GetEntryLabel());
5178 __ Bind(slow_path->GetReturnLabel());
5179 } else {
5180 __ j(kEqual, codegen_->GetLabelOf(successor));
5181 __ jmp(slow_path->GetEntryLabel());
5182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005183}
5184
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005185X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5186 return codegen_->GetAssembler();
5187}
5188
5189void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005190 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005191 Location source = move->GetSource();
5192 Location destination = move->GetDestination();
5193
5194 if (source.IsRegister()) {
5195 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005196 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005197 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005198 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005199 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005200 } else {
5201 DCHECK(destination.IsDoubleStackSlot());
5202 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005203 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005204 }
5205 } else if (source.IsStackSlot()) {
5206 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005208 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005212 } else {
5213 DCHECK(destination.IsStackSlot());
5214 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5215 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5216 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005217 } else if (source.IsDoubleStackSlot()) {
5218 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005219 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005220 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005221 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005222 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5223 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005224 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005225 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005226 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5227 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5228 }
Aart Bik5576f372017-03-23 16:17:37 -07005229 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005230 if (destination.IsFpuRegister()) {
5231 __ movups(destination.AsFpuRegister<XmmRegister>(),
5232 Address(CpuRegister(RSP), source.GetStackIndex()));
5233 } else {
5234 DCHECK(destination.IsSIMDStackSlot());
5235 size_t high = kX86_64WordSize;
5236 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5237 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5238 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5239 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5240 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005241 } else if (source.IsConstant()) {
5242 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005243 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5244 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005245 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005246 if (value == 0) {
5247 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5248 } else {
5249 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5250 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005251 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005252 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005253 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005254 }
5255 } else if (constant->IsLongConstant()) {
5256 int64_t value = constant->AsLongConstant()->GetValue();
5257 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005258 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005259 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005261 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005262 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005263 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005264 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005265 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005266 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005267 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005268 } else {
5269 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005270 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005271 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5272 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005273 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005274 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005275 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005276 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005277 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005278 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005279 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005280 } else {
5281 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005282 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005283 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005284 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005285 } else if (source.IsFpuRegister()) {
5286 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005287 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 } else if (destination.IsStackSlot()) {
5289 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005290 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005291 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005294 } else {
5295 DCHECK(destination.IsSIMDStackSlot());
5296 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5297 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005299 }
5300}
5301
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005302void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005303 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005304 __ movl(Address(CpuRegister(RSP), mem), reg);
5305 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005306}
5307
Mark Mendell8a1c7282015-06-29 15:41:28 -04005308void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5309 __ movq(CpuRegister(TMP), reg1);
5310 __ movq(reg1, reg2);
5311 __ movq(reg2, CpuRegister(TMP));
5312}
5313
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005314void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5315 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5316 __ movq(Address(CpuRegister(RSP), mem), reg);
5317 __ movq(reg, CpuRegister(TMP));
5318}
5319
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005320void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5321 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5322 __ movss(Address(CpuRegister(RSP), mem), reg);
5323 __ movd(reg, CpuRegister(TMP));
5324}
5325
5326void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5327 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5328 __ movsd(Address(CpuRegister(RSP), mem), reg);
5329 __ movd(reg, CpuRegister(TMP));
5330}
5331
Aart Bikcfe50bb2017-12-12 14:54:12 -08005332void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5333 size_t extra_slot = 2 * kX86_64WordSize;
5334 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5335 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5336 ExchangeMemory64(0, mem + extra_slot, 2);
5337 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5338 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5339}
5340
5341void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5342 ScratchRegisterScope ensure_scratch(
5343 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5344
5345 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5346 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5347 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5348 Address(CpuRegister(RSP), mem2 + stack_offset));
5349 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5350 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5351 CpuRegister(ensure_scratch.GetRegister()));
5352}
5353
5354void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5355 ScratchRegisterScope ensure_scratch(
5356 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5357
5358 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5359
5360 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5361 for (int i = 0; i < num_of_qwords; i++) {
5362 __ movq(CpuRegister(TMP),
5363 Address(CpuRegister(RSP), mem1 + stack_offset));
5364 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5365 Address(CpuRegister(RSP), mem2 + stack_offset));
5366 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5367 CpuRegister(TMP));
5368 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5369 CpuRegister(ensure_scratch.GetRegister()));
5370 stack_offset += kX86_64WordSize;
5371 }
5372}
5373
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005374void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005375 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005376 Location source = move->GetSource();
5377 Location destination = move->GetDestination();
5378
5379 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005380 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005381 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005382 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005383 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005384 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005385 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005386 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005387 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005388 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005389 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005390 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005391 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005392 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005393 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005394 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5395 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5396 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005397 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005398 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005399 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005400 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005401 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005402 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005403 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005404 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005405 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5406 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5407 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5408 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5409 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5410 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005411 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005412 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005413 }
5414}
5415
5416
5417void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5418 __ pushq(CpuRegister(reg));
5419}
5420
5421
5422void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5423 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005424}
5425
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005426void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005427 SlowPathCode* slow_path, CpuRegister class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07005428 __ cmpb(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
Vladimir Marko2c64a832018-01-04 11:31:56 +00005429 Immediate(enum_cast<>(ClassStatus::kInitialized)));
5430 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005431 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005432 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005433}
5434
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005435HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5436 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005438 case HLoadClass::LoadKind::kInvalid:
5439 LOG(FATAL) << "UNREACHABLE";
5440 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005441 case HLoadClass::LoadKind::kReferrersClass:
5442 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005443 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005444 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005445 case HLoadClass::LoadKind::kBssEntry:
5446 DCHECK(!Runtime::Current()->UseJitCompilation());
5447 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005448 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005449 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005450 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005451 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005452 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005453 break;
5454 }
5455 return desired_class_load_kind;
5456}
5457
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005458void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005459 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005460 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005461 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005462 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005463 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005464 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005465 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005466 return;
5467 }
Vladimir Marko41559982017-01-06 14:04:23 +00005468 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005469
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005470 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5471 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005472 ? LocationSummary::kCallOnSlowPath
5473 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005474 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005475 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005476 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005477 }
5478
Vladimir Marko41559982017-01-06 14:04:23 +00005479 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 locations->SetInAt(0, Location::RequiresRegister());
5481 }
5482 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005483 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5484 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5485 // Rely on the type resolution and/or initialization to save everything.
5486 // Custom calling convention: RAX serves as both input and output.
5487 RegisterSet caller_saves = RegisterSet::Empty();
5488 caller_saves.Add(Location::RegisterLocation(RAX));
5489 locations->SetCustomSlowPathCallerSaves(caller_saves);
5490 } else {
5491 // For non-Baker read barrier we have a temp-clobbering call.
5492 }
5493 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494}
5495
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005496Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005497 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005498 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005499 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005500 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005501 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005502 PatchInfo<Label>* info = &jit_class_patches_.back();
5503 return &info->label;
5504}
5505
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005506// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5507// move.
5508void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005509 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005510 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005511 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005512 return;
5513 }
Vladimir Marko41559982017-01-06 14:04:23 +00005514 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005515
Vladimir Marko41559982017-01-06 14:04:23 +00005516 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005517 Location out_loc = locations->Out();
5518 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005519
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005520 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5521 ? kWithoutReadBarrier
5522 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005523 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005524 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005525 case HLoadClass::LoadKind::kReferrersClass: {
5526 DCHECK(!cls->CanCallRuntime());
5527 DCHECK(!cls->MustGenerateClinitCheck());
5528 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5529 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5530 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005531 cls,
5532 out_loc,
5533 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005534 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005535 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005536 break;
5537 }
5538 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005539 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005540 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005541 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005542 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005543 break;
5544 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005545 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005546 uint32_t address = dchecked_integral_cast<uint32_t>(
5547 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5548 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005549 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005550 break;
5551 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005552 case HLoadClass::LoadKind::kBootImageClassTable: {
5553 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5554 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5555 codegen_->RecordBootTypePatch(cls);
5556 // Extract the reference from the slot data, i.e. clear the hash bits.
5557 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
5558 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
5559 if (masked_hash != 0) {
5560 __ subl(out, Immediate(masked_hash));
5561 }
5562 break;
5563 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005564 case HLoadClass::LoadKind::kBssEntry: {
5565 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5566 /* no_rip */ false);
5567 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5568 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5569 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5570 generate_null_check = true;
5571 break;
5572 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005573 case HLoadClass::LoadKind::kJitTableAddress: {
5574 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5575 /* no_rip */ true);
5576 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005577 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005578 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005579 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005580 break;
5581 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005582 default:
5583 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5584 UNREACHABLE();
5585 }
5586
5587 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5588 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005589 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005590 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5591 codegen_->AddSlowPath(slow_path);
5592 if (generate_null_check) {
5593 __ testl(out, out);
5594 __ j(kEqual, slow_path->GetEntryLabel());
5595 }
5596 if (cls->MustGenerateClinitCheck()) {
5597 GenerateClassInitializationCheck(slow_path, out);
5598 } else {
5599 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005600 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005601 }
5602}
5603
5604void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5605 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005606 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607 locations->SetInAt(0, Location::RequiresRegister());
5608 if (check->HasUses()) {
5609 locations->SetOut(Location::SameAsFirstInput());
5610 }
5611}
5612
5613void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005614 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005615 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005616 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005617 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005618 GenerateClassInitializationCheck(slow_path,
5619 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005620}
5621
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005622HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5623 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005624 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005625 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005626 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005627 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005628 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005629 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005630 case HLoadString::LoadKind::kJitTableAddress:
5631 DCHECK(Runtime::Current()->UseJitCompilation());
5632 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005633 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005634 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005635 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005636 }
5637 return desired_string_load_kind;
5638}
5639
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005640void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005641 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005642 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005643 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005644 locations->SetOut(Location::RegisterLocation(RAX));
5645 } else {
5646 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005647 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5648 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005649 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005650 // Custom calling convention: RAX serves as both input and output.
5651 RegisterSet caller_saves = RegisterSet::Empty();
5652 caller_saves.Add(Location::RegisterLocation(RAX));
5653 locations->SetCustomSlowPathCallerSaves(caller_saves);
5654 } else {
5655 // For non-Baker read barrier we have a temp-clobbering call.
5656 }
5657 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005658 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005659}
5660
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005661Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005662 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005663 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005664 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005665 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005666 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005667 PatchInfo<Label>* info = &jit_string_patches_.back();
5668 return &info->label;
5669}
5670
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005671// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5672// move.
5673void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005674 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005675 Location out_loc = locations->Out();
5676 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005677
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 switch (load->GetLoadKind()) {
5679 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005680 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005681 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005682 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005683 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 }
5685 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005686 uint32_t address = dchecked_integral_cast<uint32_t>(
5687 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5688 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005689 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005690 return;
5691 }
5692 case HLoadString::LoadKind::kBootImageInternTable: {
5693 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5694 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5695 codegen_->RecordBootStringPatch(load);
5696 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005697 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005698 case HLoadString::LoadKind::kBssEntry: {
5699 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5700 /* no_rip */ false);
5701 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5702 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005703 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005704 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005705 codegen_->AddSlowPath(slow_path);
5706 __ testl(out, out);
5707 __ j(kEqual, slow_path->GetEntryLabel());
5708 __ Bind(slow_path->GetExitLabel());
5709 return;
5710 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005711 case HLoadString::LoadKind::kJitTableAddress: {
5712 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5713 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005714 Label* fixup_label = codegen_->NewJitRootStringPatch(
5715 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005716 // /* GcRoot<mirror::String> */ out = *address
5717 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5718 return;
5719 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005720 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005721 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005722 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005723
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005724 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005725 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005726 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005727 codegen_->InvokeRuntime(kQuickResolveString,
5728 load,
5729 load->GetDexPc());
5730 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005731}
5732
David Brazdilcb1c0552015-08-04 16:22:25 +01005733static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005734 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005735 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005736}
5737
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005738void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5739 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005740 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005741 locations->SetOut(Location::RequiresRegister());
5742}
5743
5744void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005745 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5746}
5747
5748void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005749 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005750}
5751
5752void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5753 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005754}
5755
5756void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005757 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5758 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005759 InvokeRuntimeCallingConvention calling_convention;
5760 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5761}
5762
5763void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005764 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005765 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005766}
5767
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005768static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00005769 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005770 // We need a temporary for holding the iftable length.
5771 return true;
5772 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005773 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005774 !kUseBakerReadBarrier &&
5775 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005776 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5777 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5778}
5779
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005780static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5781 return kEmitCompilerReadBarrier &&
5782 !kUseBakerReadBarrier &&
5783 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5784 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5785 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5786}
5787
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005788void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005790 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005791 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 case TypeCheckKind::kExactCheck:
5794 case TypeCheckKind::kAbstractClassCheck:
5795 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00005796 case TypeCheckKind::kArrayObjectCheck: {
5797 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5798 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5799 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005800 break;
Vladimir Marko87584542017-12-12 17:47:52 +00005801 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005802 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005803 case TypeCheckKind::kUnresolvedCheck:
5804 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 call_kind = LocationSummary::kCallOnSlowPath;
5806 break;
5807 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808
Vladimir Markoca6fff82017-10-03 14:49:14 +01005809 LocationSummary* locations =
5810 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005811 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005812 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005813 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005814 locations->SetInAt(0, Location::RequiresRegister());
5815 locations->SetInAt(1, Location::Any());
5816 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5817 locations->SetOut(Location::RequiresRegister());
5818 // When read barriers are enabled, we need a temporary register for
5819 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005820 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005823}
5824
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005825void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005826 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005827 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828 Location obj_loc = locations->InAt(0);
5829 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005830 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005831 Location out_loc = locations->Out();
5832 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005833 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005834 locations->GetTemp(0) :
5835 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005836 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5838 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5839 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005840 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005842
5843 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005844 // Avoid null check if we know obj is not null.
5845 if (instruction->MustDoNullCheck()) {
5846 __ testl(obj, obj);
5847 __ j(kEqual, &zero);
5848 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005850 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005852 ReadBarrierOption read_barrier_option =
5853 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005854 // /* HeapReference<Class> */ out = obj->klass_
5855 GenerateReferenceLoadTwoRegisters(instruction,
5856 out_loc,
5857 obj_loc,
5858 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005859 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 if (cls.IsRegister()) {
5861 __ cmpl(out, cls.AsRegister<CpuRegister>());
5862 } else {
5863 DCHECK(cls.IsStackSlot()) << cls;
5864 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5865 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005866 if (zero.IsLinked()) {
5867 // Classes must be equal for the instanceof to succeed.
5868 __ j(kNotEqual, &zero);
5869 __ movl(out, Immediate(1));
5870 __ jmp(&done);
5871 } else {
5872 __ setcc(kEqual, out);
5873 // setcc only sets the low byte.
5874 __ andl(out, Immediate(1));
5875 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 break;
5877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005880 ReadBarrierOption read_barrier_option =
5881 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005882 // /* HeapReference<Class> */ out = obj->klass_
5883 GenerateReferenceLoadTwoRegisters(instruction,
5884 out_loc,
5885 obj_loc,
5886 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005887 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005888 // If the class is abstract, we eagerly fetch the super class of the
5889 // object to avoid doing a comparison we know will fail.
5890 NearLabel loop, success;
5891 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005892 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005893 GenerateReferenceLoadOneRegister(instruction,
5894 out_loc,
5895 super_offset,
5896 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00005897 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005898 __ testl(out, out);
5899 // If `out` is null, we use it for the result, and jump to `done`.
5900 __ j(kEqual, &done);
5901 if (cls.IsRegister()) {
5902 __ cmpl(out, cls.AsRegister<CpuRegister>());
5903 } else {
5904 DCHECK(cls.IsStackSlot()) << cls;
5905 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5906 }
5907 __ j(kNotEqual, &loop);
5908 __ movl(out, Immediate(1));
5909 if (zero.IsLinked()) {
5910 __ jmp(&done);
5911 }
5912 break;
5913 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005914
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005916 ReadBarrierOption read_barrier_option =
5917 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005918 // /* HeapReference<Class> */ out = obj->klass_
5919 GenerateReferenceLoadTwoRegisters(instruction,
5920 out_loc,
5921 obj_loc,
5922 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005923 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005924 // Walk over the class hierarchy to find a match.
5925 NearLabel loop, success;
5926 __ Bind(&loop);
5927 if (cls.IsRegister()) {
5928 __ cmpl(out, cls.AsRegister<CpuRegister>());
5929 } else {
5930 DCHECK(cls.IsStackSlot()) << cls;
5931 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5932 }
5933 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005935 GenerateReferenceLoadOneRegister(instruction,
5936 out_loc,
5937 super_offset,
5938 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00005939 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 __ testl(out, out);
5941 __ j(kNotEqual, &loop);
5942 // If `out` is null, we use it for the result, and jump to `done`.
5943 __ jmp(&done);
5944 __ Bind(&success);
5945 __ movl(out, Immediate(1));
5946 if (zero.IsLinked()) {
5947 __ jmp(&done);
5948 }
5949 break;
5950 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005951
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005952 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00005953 ReadBarrierOption read_barrier_option =
5954 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005955 // /* HeapReference<Class> */ out = obj->klass_
5956 GenerateReferenceLoadTwoRegisters(instruction,
5957 out_loc,
5958 obj_loc,
5959 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00005960 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005961 // Do an exact check.
5962 NearLabel exact_check;
5963 if (cls.IsRegister()) {
5964 __ cmpl(out, cls.AsRegister<CpuRegister>());
5965 } else {
5966 DCHECK(cls.IsStackSlot()) << cls;
5967 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5968 }
5969 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005971 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005972 GenerateReferenceLoadOneRegister(instruction,
5973 out_loc,
5974 component_offset,
5975 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00005976 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 __ testl(out, out);
5978 // If `out` is null, we use it for the result, and jump to `done`.
5979 __ j(kEqual, &done);
5980 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5981 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005982 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 __ movl(out, Immediate(1));
5984 __ jmp(&done);
5985 break;
5986 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005987
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005989 // No read barrier since the slow path will retry upon failure.
5990 // /* HeapReference<Class> */ out = obj->klass_
5991 GenerateReferenceLoadTwoRegisters(instruction,
5992 out_loc,
5993 obj_loc,
5994 class_offset,
5995 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 if (cls.IsRegister()) {
5997 __ cmpl(out, cls.AsRegister<CpuRegister>());
5998 } else {
5999 DCHECK(cls.IsStackSlot()) << cls;
6000 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6001 }
6002 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006003 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6004 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 codegen_->AddSlowPath(slow_path);
6006 __ j(kNotEqual, slow_path->GetEntryLabel());
6007 __ movl(out, Immediate(1));
6008 if (zero.IsLinked()) {
6009 __ jmp(&done);
6010 }
6011 break;
6012 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013
Calin Juravle98893e12015-10-02 21:05:03 +01006014 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006015 case TypeCheckKind::kInterfaceCheck: {
6016 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006017 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018 // cases.
6019 //
6020 // We cannot directly call the InstanceofNonTrivial runtime
6021 // entry point without resorting to a type checking slow path
6022 // here (i.e. by calling InvokeRuntime directly), as it would
6023 // require to assign fixed registers for the inputs of this
6024 // HInstanceOf instruction (following the runtime calling
6025 // convention), which might be cluttered by the potential first
6026 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006027 //
6028 // TODO: Introduce a new runtime entry point taking the object
6029 // to test (instead of its class) as argument, and let it deal
6030 // with the read barrier issues. This will let us refactor this
6031 // case of the `switch` code as it was previously (with a direct
6032 // call to the runtime not using a type checking slow path).
6033 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006034 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006035 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6036 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037 codegen_->AddSlowPath(slow_path);
6038 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 if (zero.IsLinked()) {
6040 __ jmp(&done);
6041 }
6042 break;
6043 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006044 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006045
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006046 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006047 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 __ xorl(out, out);
6049 }
6050
6051 if (done.IsLinked()) {
6052 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006053 }
6054
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006055 if (slow_path != nullptr) {
6056 __ Bind(slow_path->GetExitLabel());
6057 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006058}
6059
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006060void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006061 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006062 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006063 LocationSummary* locations =
6064 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006065 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006066 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6067 // Require a register for the interface check since there is a loop that compares the class to
6068 // a memory address.
6069 locations->SetInAt(1, Location::RequiresRegister());
6070 } else {
6071 locations->SetInAt(1, Location::Any());
6072 }
6073
Roland Levillain0d5a2812015-11-13 10:07:31 +00006074 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6075 locations->AddTemp(Location::RequiresRegister());
6076 // When read barriers are enabled, we need an additional temporary
6077 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006078 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006079 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006081}
6082
6083void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006085 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 Location obj_loc = locations->InAt(0);
6087 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006088 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 Location temp_loc = locations->GetTemp(0);
6090 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006091 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006092 locations->GetTemp(1) :
6093 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006094 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6095 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6096 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6097 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6098 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6099 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006100 const uint32_t object_array_data_offset =
6101 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102
Vladimir Marko87584542017-12-12 17:47:52 +00006103 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006105 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6106 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006107 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006108
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006109
6110 NearLabel done;
6111 // Avoid null check if we know obj is not null.
6112 if (instruction->MustDoNullCheck()) {
6113 __ testl(obj, obj);
6114 __ j(kEqual, &done);
6115 }
6116
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006118 case TypeCheckKind::kExactCheck:
6119 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006120 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006121 GenerateReferenceLoadTwoRegisters(instruction,
6122 temp_loc,
6123 obj_loc,
6124 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006125 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 if (cls.IsRegister()) {
6127 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6128 } else {
6129 DCHECK(cls.IsStackSlot()) << cls;
6130 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6131 }
6132 // Jump to slow path for throwing the exception or doing a
6133 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135 break;
6136 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006139 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006140 GenerateReferenceLoadTwoRegisters(instruction,
6141 temp_loc,
6142 obj_loc,
6143 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006144 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 // If the class is abstract, we eagerly fetch the super class of the
6146 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006147 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006150 GenerateReferenceLoadOneRegister(instruction,
6151 temp_loc,
6152 super_offset,
6153 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006154 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006156 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6157 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006159 // Otherwise, compare the classes.
6160 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161 if (cls.IsRegister()) {
6162 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6163 } else {
6164 DCHECK(cls.IsStackSlot()) << cls;
6165 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6166 }
6167 __ j(kNotEqual, &loop);
6168 break;
6169 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006172 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006173 GenerateReferenceLoadTwoRegisters(instruction,
6174 temp_loc,
6175 obj_loc,
6176 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006177 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006179 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180 __ Bind(&loop);
6181 if (cls.IsRegister()) {
6182 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6183 } else {
6184 DCHECK(cls.IsStackSlot()) << cls;
6185 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6186 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006187 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006190 GenerateReferenceLoadOneRegister(instruction,
6191 temp_loc,
6192 super_offset,
6193 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006194 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195
6196 // If the class reference currently in `temp` is not null, jump
6197 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006198 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006199 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006200 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 break;
6203 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006206 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006207 GenerateReferenceLoadTwoRegisters(instruction,
6208 temp_loc,
6209 obj_loc,
6210 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006211 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006212 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006214 if (cls.IsRegister()) {
6215 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6216 } else {
6217 DCHECK(cls.IsStackSlot()) << cls;
6218 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6219 }
6220 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006221
6222 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006224 GenerateReferenceLoadOneRegister(instruction,
6225 temp_loc,
6226 component_offset,
6227 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006228 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229
6230 // If the component type is not null (i.e. the object is indeed
6231 // an array), jump to label `check_non_primitive_component_type`
6232 // to further check that this component type is not a primitive
6233 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006235 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006236 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006237 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006238 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 break;
6240 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006242 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006243 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244 //
6245 // We cannot directly call the CheckCast runtime entry point
6246 // without resorting to a type checking slow path here (i.e. by
6247 // calling InvokeRuntime directly), as it would require to
6248 // assign fixed registers for the inputs of this HInstanceOf
6249 // instruction (following the runtime calling convention), which
6250 // might be cluttered by the potential first read barrier
6251 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006252 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006253 break;
6254 }
6255
6256 case TypeCheckKind::kInterfaceCheck:
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006257 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6258 // We can not get false positives by doing this.
6259 // /* HeapReference<Class> */ temp = obj->klass_
6260 GenerateReferenceLoadTwoRegisters(instruction,
6261 temp_loc,
6262 obj_loc,
6263 class_offset,
6264 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006265
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006266 // /* HeapReference<Class> */ temp = temp->iftable_
6267 GenerateReferenceLoadTwoRegisters(instruction,
6268 temp_loc,
6269 temp_loc,
6270 iftable_offset,
6271 kWithoutReadBarrier);
6272 // Iftable is never null.
6273 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6274 // Maybe poison the `cls` for direct comparison with memory.
6275 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6276 // Loop through the iftable and check if any class matches.
6277 NearLabel start_loop;
6278 __ Bind(&start_loop);
6279 // Need to subtract first to handle the empty array case.
6280 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6281 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6282 // Go to next interface if the classes do not match.
6283 __ cmpl(cls.AsRegister<CpuRegister>(),
6284 CodeGeneratorX86_64::ArrayAddress(temp,
6285 maybe_temp2_loc,
6286 TIMES_4,
6287 object_array_data_offset));
6288 __ j(kNotEqual, &start_loop); // Return if same class.
6289 // If `cls` was poisoned above, unpoison it.
6290 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 break;
6292 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006293
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006294 if (done.IsLinked()) {
6295 __ Bind(&done);
6296 }
6297
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006299}
6300
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006301void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006302 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6303 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006304 InvokeRuntimeCallingConvention calling_convention;
6305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6306}
6307
6308void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006309 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006310 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006311 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006312 if (instruction->IsEnter()) {
6313 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6314 } else {
6315 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6316 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006317}
6318
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006319void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6320void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6321void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6322
6323void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6324 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006325 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006326 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6327 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006328 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006329 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006330 locations->SetOut(Location::SameAsFirstInput());
6331}
6332
6333void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6334 HandleBitwiseOperation(instruction);
6335}
6336
6337void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6338 HandleBitwiseOperation(instruction);
6339}
6340
6341void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6342 HandleBitwiseOperation(instruction);
6343}
6344
6345void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6346 LocationSummary* locations = instruction->GetLocations();
6347 Location first = locations->InAt(0);
6348 Location second = locations->InAt(1);
6349 DCHECK(first.Equals(locations->Out()));
6350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006351 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006352 if (second.IsRegister()) {
6353 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006354 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006355 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006356 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006357 } else {
6358 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006359 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006360 }
6361 } else if (second.IsConstant()) {
6362 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6363 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006364 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006365 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006366 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006367 } else {
6368 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006369 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 }
6371 } else {
6372 Address address(CpuRegister(RSP), second.GetStackIndex());
6373 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006374 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006375 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006376 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006377 } else {
6378 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006379 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006380 }
6381 }
6382 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006384 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6385 bool second_is_constant = false;
6386 int64_t value = 0;
6387 if (second.IsConstant()) {
6388 second_is_constant = true;
6389 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006390 }
Mark Mendell40741f32015-04-20 22:10:34 -04006391 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006392
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006393 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006394 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006395 if (is_int32_value) {
6396 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6397 } else {
6398 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6399 }
6400 } else if (second.IsDoubleStackSlot()) {
6401 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006402 } else {
6403 __ andq(first_reg, second.AsRegister<CpuRegister>());
6404 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006405 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006406 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006407 if (is_int32_value) {
6408 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6409 } else {
6410 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6411 }
6412 } else if (second.IsDoubleStackSlot()) {
6413 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006414 } else {
6415 __ orq(first_reg, second.AsRegister<CpuRegister>());
6416 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006417 } else {
6418 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006419 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006420 if (is_int32_value) {
6421 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6422 } else {
6423 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6424 }
6425 } else if (second.IsDoubleStackSlot()) {
6426 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006427 } else {
6428 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6429 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006430 }
6431 }
6432}
6433
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006434void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6435 HInstruction* instruction,
6436 Location out,
6437 uint32_t offset,
6438 Location maybe_temp,
6439 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006440 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006441 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006442 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006443 if (kUseBakerReadBarrier) {
6444 // Load with fast path based Baker's read barrier.
6445 // /* HeapReference<Object> */ out = *(out + offset)
6446 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006447 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006448 } else {
6449 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006450 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006451 // in the following move operation, as we will need it for the
6452 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006453 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006454 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006455 // /* HeapReference<Object> */ out = *(out + offset)
6456 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006457 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006458 }
6459 } else {
6460 // Plain load with no read barrier.
6461 // /* HeapReference<Object> */ out = *(out + offset)
6462 __ movl(out_reg, Address(out_reg, offset));
6463 __ MaybeUnpoisonHeapReference(out_reg);
6464 }
6465}
6466
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006467void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6468 HInstruction* instruction,
6469 Location out,
6470 Location obj,
6471 uint32_t offset,
6472 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006473 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6474 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006475 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006476 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006477 if (kUseBakerReadBarrier) {
6478 // Load with fast path based Baker's read barrier.
6479 // /* HeapReference<Object> */ out = *(obj + offset)
6480 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006481 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006482 } else {
6483 // Load with slow path based read barrier.
6484 // /* HeapReference<Object> */ out = *(obj + offset)
6485 __ movl(out_reg, Address(obj_reg, offset));
6486 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6487 }
6488 } else {
6489 // Plain load with no read barrier.
6490 // /* HeapReference<Object> */ out = *(obj + offset)
6491 __ movl(out_reg, Address(obj_reg, offset));
6492 __ MaybeUnpoisonHeapReference(out_reg);
6493 }
6494}
6495
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006496void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6497 HInstruction* instruction,
6498 Location root,
6499 const Address& address,
6500 Label* fixup_label,
6501 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006502 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006503 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006504 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006505 if (kUseBakerReadBarrier) {
6506 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6507 // Baker's read barrier are used:
6508 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006509 // root = obj.field;
6510 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6511 // if (temp != null) {
6512 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006513 // }
6514
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006515 // /* GcRoot<mirror::Object> */ root = *address
6516 __ movl(root_reg, address);
6517 if (fixup_label != nullptr) {
6518 __ Bind(fixup_label);
6519 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006520 static_assert(
6521 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6522 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6523 "have different sizes.");
6524 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6525 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6526 "have different sizes.");
6527
Vladimir Marko953437b2016-08-24 08:30:46 +00006528 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006529 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006530 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006531 codegen_->AddSlowPath(slow_path);
6532
Roland Levillaind966ce72017-02-09 16:20:14 +00006533 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6534 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006535 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006536 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6537 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006538 __ j(kNotEqual, slow_path->GetEntryLabel());
6539 __ Bind(slow_path->GetExitLabel());
6540 } else {
6541 // GC root loaded through a slow path for read barriers other
6542 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006543 // /* GcRoot<mirror::Object>* */ root = address
6544 __ leaq(root_reg, address);
6545 if (fixup_label != nullptr) {
6546 __ Bind(fixup_label);
6547 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006548 // /* mirror::Object* */ root = root->Read()
6549 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6550 }
6551 } else {
6552 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006553 // /* GcRoot<mirror::Object> */ root = *address
6554 __ movl(root_reg, address);
6555 if (fixup_label != nullptr) {
6556 __ Bind(fixup_label);
6557 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006558 // Note that GC roots are not affected by heap poisoning, thus we
6559 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006560 }
6561}
6562
6563void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6564 Location ref,
6565 CpuRegister obj,
6566 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006567 bool needs_null_check) {
6568 DCHECK(kEmitCompilerReadBarrier);
6569 DCHECK(kUseBakerReadBarrier);
6570
6571 // /* HeapReference<Object> */ ref = *(obj + offset)
6572 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006573 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006574}
6575
6576void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6577 Location ref,
6578 CpuRegister obj,
6579 uint32_t data_offset,
6580 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006581 bool needs_null_check) {
6582 DCHECK(kEmitCompilerReadBarrier);
6583 DCHECK(kUseBakerReadBarrier);
6584
Roland Levillain3d312422016-06-23 13:53:42 +01006585 static_assert(
6586 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6587 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006588 // /* HeapReference<Object> */ ref =
6589 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006590 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006591 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006592}
6593
6594void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6595 Location ref,
6596 CpuRegister obj,
6597 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006598 bool needs_null_check,
6599 bool always_update_field,
6600 CpuRegister* temp1,
6601 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006602 DCHECK(kEmitCompilerReadBarrier);
6603 DCHECK(kUseBakerReadBarrier);
6604
6605 // In slow path based read barriers, the read barrier call is
6606 // inserted after the original load. However, in fast path based
6607 // Baker's read barriers, we need to perform the load of
6608 // mirror::Object::monitor_ *before* the original reference load.
6609 // This load-load ordering is required by the read barrier.
6610 // The fast path/slow path (for Baker's algorithm) should look like:
6611 //
6612 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6613 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6614 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006615 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006616 // if (is_gray) {
6617 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6618 // }
6619 //
6620 // Note: the original implementation in ReadBarrier::Barrier is
6621 // slightly more complex as:
6622 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006623 // the high-bits of rb_state, which are expected to be all zeroes
6624 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6625 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006626 // - it performs additional checks that we do not do here for
6627 // performance reasons.
6628
6629 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006630 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6631
Vladimir Marko953437b2016-08-24 08:30:46 +00006632 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006633 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6634 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006635 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6636 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6637 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6638
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006639 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006640 // ref = ReadBarrier::Mark(ref);
6641 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6642 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006643 if (needs_null_check) {
6644 MaybeRecordImplicitNullCheck(instruction);
6645 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006646
6647 // Load fence to prevent load-load reordering.
6648 // Note that this is a no-op, thanks to the x86-64 memory model.
6649 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6650
6651 // The actual reference load.
6652 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006653 __ movl(ref_reg, src); // Flags are unaffected.
6654
6655 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6656 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006657 SlowPathCode* slow_path;
6658 if (always_update_field) {
6659 DCHECK(temp1 != nullptr);
6660 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006661 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006662 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6663 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006664 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006665 instruction, ref, /* unpoison_ref_before_marking */ true);
6666 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006667 AddSlowPath(slow_path);
6668
6669 // We have done the "if" of the gray bit check above, now branch based on the flags.
6670 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006671
6672 // Object* ref = ref_addr->AsMirrorPtr()
6673 __ MaybeUnpoisonHeapReference(ref_reg);
6674
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006675 __ Bind(slow_path->GetExitLabel());
6676}
6677
6678void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6679 Location out,
6680 Location ref,
6681 Location obj,
6682 uint32_t offset,
6683 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 DCHECK(kEmitCompilerReadBarrier);
6685
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006686 // Insert a slow path based read barrier *after* the reference load.
6687 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006688 // If heap poisoning is enabled, the unpoisoning of the loaded
6689 // reference will be carried out by the runtime within the slow
6690 // path.
6691 //
6692 // Note that `ref` currently does not get unpoisoned (when heap
6693 // poisoning is enabled), which is alright as the `ref` argument is
6694 // not used by the artReadBarrierSlow entry point.
6695 //
6696 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006697 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6699 AddSlowPath(slow_path);
6700
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 __ jmp(slow_path->GetEntryLabel());
6702 __ Bind(slow_path->GetExitLabel());
6703}
6704
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006705void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6706 Location out,
6707 Location ref,
6708 Location obj,
6709 uint32_t offset,
6710 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006712 // Baker's read barriers shall be handled by the fast path
6713 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6714 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 // If heap poisoning is enabled, unpoisoning will be taken care of
6716 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006717 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006718 } else if (kPoisonHeapReferences) {
6719 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6720 }
6721}
6722
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006723void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6724 Location out,
6725 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006726 DCHECK(kEmitCompilerReadBarrier);
6727
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006728 // Insert a slow path based read barrier *after* the GC root load.
6729 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730 // Note that GC roots are not affected by heap poisoning, so we do
6731 // not need to do anything special for this here.
6732 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006733 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006734 AddSlowPath(slow_path);
6735
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736 __ jmp(slow_path->GetEntryLabel());
6737 __ Bind(slow_path->GetExitLabel());
6738}
6739
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006740void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006741 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006742 LOG(FATAL) << "Unreachable";
6743}
6744
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006745void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006746 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006747 LOG(FATAL) << "Unreachable";
6748}
6749
Mark Mendellfe57faa2015-09-18 09:26:15 -04006750// Simple implementation of packed switch - generate cascaded compare/jumps.
6751void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6752 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006753 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006754 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006755 locations->AddTemp(Location::RequiresRegister());
6756 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006757}
6758
6759void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6760 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006761 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006762 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006763 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6764 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6765 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006766 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6767
6768 // Should we generate smaller inline compare/jumps?
6769 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6770 // Figure out the correct compare values and jump conditions.
6771 // Handle the first compare/branch as a special case because it might
6772 // jump to the default case.
6773 DCHECK_GT(num_entries, 2u);
6774 Condition first_condition;
6775 uint32_t index;
6776 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6777 if (lower_bound != 0) {
6778 first_condition = kLess;
6779 __ cmpl(value_reg_in, Immediate(lower_bound));
6780 __ j(first_condition, codegen_->GetLabelOf(default_block));
6781 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6782
6783 index = 1;
6784 } else {
6785 // Handle all the compare/jumps below.
6786 first_condition = kBelow;
6787 index = 0;
6788 }
6789
6790 // Handle the rest of the compare/jumps.
6791 for (; index + 1 < num_entries; index += 2) {
6792 int32_t compare_to_value = lower_bound + index + 1;
6793 __ cmpl(value_reg_in, Immediate(compare_to_value));
6794 // Jump to successors[index] if value < case_value[index].
6795 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6796 // Jump to successors[index + 1] if value == case_value[index + 1].
6797 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6798 }
6799
6800 if (index != num_entries) {
6801 // There are an odd number of entries. Handle the last one.
6802 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006803 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006804 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6805 }
6806
6807 // And the default for any other value.
6808 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6809 __ jmp(codegen_->GetLabelOf(default_block));
6810 }
6811 return;
6812 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006813
6814 // Remove the bias, if needed.
6815 Register value_reg_out = value_reg_in.AsRegister();
6816 if (lower_bound != 0) {
6817 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6818 value_reg_out = temp_reg.AsRegister();
6819 }
6820 CpuRegister value_reg(value_reg_out);
6821
6822 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006823 __ cmpl(value_reg, Immediate(num_entries - 1));
6824 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006825
Mark Mendell9c86b482015-09-18 13:36:07 -04006826 // We are in the range of the table.
6827 // Load the address of the jump table in the constant area.
6828 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006829
Mark Mendell9c86b482015-09-18 13:36:07 -04006830 // Load the (signed) offset from the jump table.
6831 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6832
6833 // Add the offset to the address of the table base.
6834 __ addq(temp_reg, base_reg);
6835
6836 // And jump.
6837 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006838}
6839
xueliang.zhonge0eb4832017-10-30 13:43:14 +00006840void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
6841 ATTRIBUTE_UNUSED) {
6842 LOG(FATAL) << "Unreachable";
6843}
6844
6845void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
6846 ATTRIBUTE_UNUSED) {
6847 LOG(FATAL) << "Unreachable";
6848}
6849
Aart Bikc5d47542016-01-27 17:00:35 -08006850void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6851 if (value == 0) {
6852 __ xorl(dest, dest);
6853 } else {
6854 __ movl(dest, Immediate(value));
6855 }
6856}
6857
Mark Mendell92e83bf2015-05-07 11:25:03 -04006858void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6859 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006860 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006861 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006862 } else if (IsUint<32>(value)) {
6863 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006864 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6865 } else {
6866 __ movq(dest, Immediate(value));
6867 }
6868}
6869
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006870void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6871 if (value == 0) {
6872 __ xorps(dest, dest);
6873 } else {
6874 __ movss(dest, LiteralInt32Address(value));
6875 }
6876}
6877
6878void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6879 if (value == 0) {
6880 __ xorpd(dest, dest);
6881 } else {
6882 __ movsd(dest, LiteralInt64Address(value));
6883 }
6884}
6885
6886void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6887 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6888}
6889
6890void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6891 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6892}
6893
Aart Bika19616e2016-02-01 18:57:58 -08006894void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6895 if (value == 0) {
6896 __ testl(dest, dest);
6897 } else {
6898 __ cmpl(dest, Immediate(value));
6899 }
6900}
6901
6902void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6903 if (IsInt<32>(value)) {
6904 if (value == 0) {
6905 __ testq(dest, dest);
6906 } else {
6907 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6908 }
6909 } else {
6910 // Value won't fit in an int.
6911 __ cmpq(dest, LiteralInt64Address(value));
6912 }
6913}
6914
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006915void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6916 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006917 GenerateIntCompare(lhs_reg, rhs);
6918}
6919
6920void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006921 if (rhs.IsConstant()) {
6922 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006923 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006924 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006925 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006926 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006927 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006928 }
6929}
6930
6931void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6932 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6933 if (rhs.IsConstant()) {
6934 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6935 Compare64BitValue(lhs_reg, value);
6936 } else if (rhs.IsDoubleStackSlot()) {
6937 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6938 } else {
6939 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6940 }
6941}
6942
6943Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6944 Location index,
6945 ScaleFactor scale,
6946 uint32_t data_offset) {
6947 return index.IsConstant() ?
6948 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6949 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6950}
6951
Mark Mendellcfa410b2015-05-25 16:02:44 -04006952void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6953 DCHECK(dest.IsDoubleStackSlot());
6954 if (IsInt<32>(value)) {
6955 // Can move directly as an int32 constant.
6956 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6957 Immediate(static_cast<int32_t>(value)));
6958 } else {
6959 Load64BitValue(CpuRegister(TMP), value);
6960 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6961 }
6962}
6963
Mark Mendell9c86b482015-09-18 13:36:07 -04006964/**
6965 * Class to handle late fixup of offsets into constant area.
6966 */
6967class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6968 public:
6969 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6970 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6971
6972 protected:
6973 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6974
6975 CodeGeneratorX86_64* codegen_;
6976
6977 private:
6978 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6979 // Patch the correct offset for the instruction. We use the address of the
6980 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6981 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6982 int32_t relative_position = constant_offset - pos;
6983
6984 // Patch in the right value.
6985 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6986 }
6987
6988 // Location in constant area that the fixup refers to.
6989 size_t offset_into_constant_area_;
6990};
6991
6992/**
6993 t * Class to handle late fixup of offsets to a jump table that will be created in the
6994 * constant area.
6995 */
6996class JumpTableRIPFixup : public RIPFixup {
6997 public:
6998 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6999 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7000
7001 void CreateJumpTable() {
7002 X86_64Assembler* assembler = codegen_->GetAssembler();
7003
7004 // Ensure that the reference to the jump table has the correct offset.
7005 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7006 SetOffset(offset_in_constant_table);
7007
7008 // Compute the offset from the start of the function to this jump table.
7009 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7010
7011 // Populate the jump table with the correct values for the jump table.
7012 int32_t num_entries = switch_instr_->GetNumEntries();
7013 HBasicBlock* block = switch_instr_->GetBlock();
7014 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7015 // The value that we want is the target offset - the position of the table.
7016 for (int32_t i = 0; i < num_entries; i++) {
7017 HBasicBlock* b = successors[i];
7018 Label* l = codegen_->GetLabelOf(b);
7019 DCHECK(l->IsBound());
7020 int32_t offset_to_block = l->Position() - current_table_offset;
7021 assembler->AppendInt32(offset_to_block);
7022 }
7023 }
7024
7025 private:
7026 const HPackedSwitch* switch_instr_;
7027};
7028
Mark Mendellf55c3e02015-03-26 21:07:46 -04007029void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7030 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007031 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007032 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7033 // 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 -04007034 assembler->Align(4, 0);
7035 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007036
7037 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007038 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007039 jump_table->CreateJumpTable();
7040 }
7041
7042 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007043 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007044 }
7045
7046 // And finish up.
7047 CodeGenerator::Finalize(allocator);
7048}
7049
Mark Mendellf55c3e02015-03-26 21:07:46 -04007050Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007051 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007052 return Address::RIP(fixup);
7053}
7054
7055Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007056 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007057 return Address::RIP(fixup);
7058}
7059
7060Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007061 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007062 return Address::RIP(fixup);
7063}
7064
7065Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007066 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007067 return Address::RIP(fixup);
7068}
7069
Andreas Gampe85b62f22015-09-09 13:15:38 -07007070// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007071void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007072 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007073 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007074 return;
7075 }
7076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007077 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007078
7079 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7080 if (trg.Equals(return_loc)) {
7081 return;
7082 }
7083
7084 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007085 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007086 parallel_move.AddMove(return_loc, trg, type, nullptr);
7087 GetMoveResolver()->EmitNativeCode(&parallel_move);
7088}
7089
Mark Mendell9c86b482015-09-18 13:36:07 -04007090Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7091 // Create a fixup to be used to create and address the jump table.
7092 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007093 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007094
7095 // We have to populate the jump tables.
7096 fixups_to_jump_tables_.push_back(table_fixup);
7097 return Address::RIP(table_fixup);
7098}
7099
Mark Mendellea5af682015-10-22 17:35:49 -04007100void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7101 const Address& addr_high,
7102 int64_t v,
7103 HInstruction* instruction) {
7104 if (IsInt<32>(v)) {
7105 int32_t v_32 = v;
7106 __ movq(addr_low, Immediate(v_32));
7107 MaybeRecordImplicitNullCheck(instruction);
7108 } else {
7109 // Didn't fit in a register. Do it in pieces.
7110 int32_t low_v = Low32Bits(v);
7111 int32_t high_v = High32Bits(v);
7112 __ movl(addr_low, Immediate(low_v));
7113 MaybeRecordImplicitNullCheck(instruction);
7114 __ movl(addr_high, Immediate(high_v));
7115 }
7116}
7117
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007118void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7119 const uint8_t* roots_data,
7120 const PatchInfo<Label>& info,
7121 uint64_t index_in_table) const {
7122 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7123 uintptr_t address =
7124 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7125 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7126 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7127 dchecked_integral_cast<uint32_t>(address);
7128}
7129
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007130void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7131 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007132 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7133 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007134 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007135 }
7136
7137 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007138 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7139 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007140 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007141 }
7142}
7143
Roland Levillain4d027112015-07-01 15:41:14 +01007144#undef __
7145
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007146} // namespace x86_64
7147} // namespace art