blob: aabf2e0be435a37aded39aeccf009b71ed3e9f4b [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"
Vladimir Markoeebb8212018-06-05 14:57:24 +010025#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027#include "intrinsics.h"
28#include "intrinsics_x86_64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010029#include "linker/linker_patch.h"
Andreas Gamped4901292017-05-30 18:41:34 -070030#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070031#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033#include "mirror/object_reference.h"
34#include "thread.h"
35#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010037#include "utils/x86_64/assembler_x86_64.h"
38#include "utils/x86_64/managed_register_x86_64.h"
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace art {
41
Roland Levillain0d5a2812015-11-13 10:07:31 +000042template<class MirrorType>
43class GcRoot;
44
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010045namespace x86_64 {
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000049// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
50// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
51// generates less code/data with a small num_entries.
52static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010053
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000054static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000055static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Mark Mendell24f2dfa2015-01-14 19:51:45 -050057static constexpr int kC2ConditionMask = 0x400;
58
Roland Levillain7cbd27f2016-08-11 23:53:33 +010059// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
60#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070061#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062
Andreas Gampe85b62f22015-09-09 13:15:38 -070063class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000065 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000068 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000070 if (instruction_->CanThrowIntoCatchBlock()) {
71 // Live registers will be restored in the catch block if caught.
72 SaveLiveRegisters(codegen, instruction_->GetLocations());
73 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010074 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000075 instruction_,
76 instruction_->GetDexPc(),
77 this);
Roland Levillain888d0672015-11-23 18:53:50 +000078 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 }
80
Alexandre Rames8158f282015-08-07 10:26:17 +010081 bool IsFatal() const OVERRIDE { return true; }
82
Alexandre Rames9931f312015-06-19 14:47:01 +010083 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
84
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
87};
88
Andreas Gampe85b62f22015-09-09 13:15:38 -070089class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000090 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000091 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000092
Alexandre Rames2ed20af2015-03-06 13:55:35 +000093 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000094 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000095 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010096 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000097 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000098 }
99
Alexandre Rames8158f282015-08-07 10:26:17 +0100100 bool IsFatal() const OVERRIDE { return true; }
101
Alexandre Rames9931f312015-06-19 14:47:01 +0100102 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
103
Calin Juravled0d48522014-11-04 16:40:20 +0000104 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
106};
107
Andreas Gampe85b62f22015-09-09 13:15:38 -0700108class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100110 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, DataType::Type type, bool is_div)
David Srbecky9cd6d372016-02-09 15:24:47 +0000111 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000112
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000113 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000114 __ Bind(GetEntryLabel());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100115 if (type_ == DataType::Type::kInt32) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000116 if (is_div_) {
117 __ negl(cpu_reg_);
118 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400119 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000120 }
121
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000122 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100123 DCHECK_EQ(DataType::Type::kInt64, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 if (is_div_) {
125 __ negq(cpu_reg_);
126 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400127 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000128 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000129 }
Calin Juravled0d48522014-11-04 16:40:20 +0000130 __ jmp(GetExitLabel());
131 }
132
Alexandre Rames9931f312015-06-19 14:47:01 +0100133 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
134
Calin Juravled0d48522014-11-04 16:40:20 +0000135 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000136 const CpuRegister cpu_reg_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 const DataType::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const bool is_div_;
139 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000140};
141
Andreas Gampe85b62f22015-09-09 13:15:38 -0700142class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100144 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000145 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700148 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000149 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700151 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100152 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000153 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700154 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100155 if (successor_ == nullptr) {
156 __ jmp(GetReturnLabel());
157 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000158 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 }
161
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 Label* GetReturnLabel() {
163 DCHECK(successor_ == nullptr);
164 return &return_label_;
165 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100167 HBasicBlock* GetSuccessor() const {
168 return successor_;
169 }
170
Alexandre Rames9931f312015-06-19 14:47:01 +0100171 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
172
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 Label return_label_;
176
177 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
178};
179
Andreas Gampe85b62f22015-09-09 13:15:38 -0700180class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100181 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100182 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000183 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100186 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000187 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000189 if (instruction_->CanThrowIntoCatchBlock()) {
190 // Live registers will be restored in the catch block if caught.
191 SaveLiveRegisters(codegen, instruction_->GetLocations());
192 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400193 // Are we using an array length from memory?
194 HInstruction* array_length = instruction_->InputAt(1);
195 Location length_loc = locations->InAt(1);
196 InvokeRuntimeCallingConvention calling_convention;
197 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
198 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100199 HArrayLength* length = array_length->AsArrayLength();
Nicolas Geoffray003444a2017-10-17 10:58:42 +0100200 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400201 Location array_loc = array_length->GetLocations()->InAt(0);
202 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
203 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
204 // Check for conflicts with index.
205 if (length_loc.Equals(locations->InAt(0))) {
206 // We know we aren't using parameter 2.
207 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
208 }
209 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100210 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100211 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700212 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400213 }
214
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000215 // We're moving two locations to locations that could overlap, so we need a parallel
216 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000217 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100218 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000219 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100220 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400221 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100222 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100223 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100224 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
225 ? kQuickThrowStringBounds
226 : kQuickThrowArrayBounds;
227 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100228 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000229 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100230 }
231
Alexandre Rames8158f282015-08-07 10:26:17 +0100232 bool IsFatal() const OVERRIDE { return true; }
233
Alexandre Rames9931f312015-06-19 14:47:01 +0100234 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
235
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100236 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100237 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
238};
239
Andreas Gampe85b62f22015-09-09 13:15:38 -0700240class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100241 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100242 LoadClassSlowPathX86_64(HLoadClass* cls, HInstruction* at)
243 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100245 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000246 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100250 Location out = locations->Out();
251 const uint32_t dex_pc = instruction_->GetDexPc();
252 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
253 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
254
Roland Levillain0d5a2812015-11-13 10:07:31 +0000255 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258
Vladimir Markoea4c1262017-02-06 19:59:33 +0000259 // Custom calling convention: RAX serves as both input and output.
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100260 if (must_resolve_type) {
261 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_64_codegen->GetGraph()->GetDexFile()));
262 dex::TypeIndex type_index = cls_->GetTypeIndex();
263 __ movl(CpuRegister(RAX), Immediate(type_index.index_));
264 x86_64_codegen->InvokeRuntime(kQuickInitializeType, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000265 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100266 // If we also must_do_clinit, the resolved type is now in the correct register.
267 } else {
268 DCHECK(must_do_clinit);
269 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
270 x86_64_codegen->Move(Location::RegisterLocation(RAX), source);
271 }
272 if (must_do_clinit) {
273 x86_64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
274 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000275 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100276
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 if (out.IsValid()) {
279 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000280 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000281 }
282
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000283 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100284 __ jmp(GetExitLabel());
285 }
286
Alexandre Rames9931f312015-06-19 14:47:01 +0100287 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
288
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100289 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290 // The class this slow path will load.
291 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100292
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000293 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100294};
295
Vladimir Markoaad75c62016-10-03 08:46:48 +0000296class LoadStringSlowPathX86_64 : public SlowPathCode {
297 public:
298 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
299
300 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
301 LocationSummary* locations = instruction_->GetLocations();
302 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
303
304 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
305 __ Bind(GetEntryLabel());
306 SaveLiveRegisters(codegen, locations);
307
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000308 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100309 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000310 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000311 x86_64_codegen->InvokeRuntime(kQuickResolveString,
312 instruction_,
313 instruction_->GetDexPc(),
314 this);
315 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
316 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
317 RestoreLiveRegisters(codegen, locations);
318
Vladimir Markoaad75c62016-10-03 08:46:48 +0000319 __ jmp(GetExitLabel());
320 }
321
322 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
323
324 private:
325 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
326};
327
Andreas Gampe85b62f22015-09-09 13:15:38 -0700328class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000331 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 DCHECK(instruction_->IsCheckCast()
337 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
Roland Levillain0d5a2812015-11-13 10:07:31 +0000339 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000342 if (kPoisonHeapReferences &&
343 instruction_->IsCheckCast() &&
344 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
345 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
346 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<CpuRegister>());
347 }
348
Vladimir Marko87584542017-12-12 17:47:52 +0000349 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000350 SaveLiveRegisters(codegen, locations);
351 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352
353 // We're moving two locations to locations that could overlap, so we need a parallel
354 // move resolver.
355 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800356 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800357 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100358 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800359 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800360 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100361 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100363 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800364 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000365 } else {
366 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800367 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
368 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 if (!is_fatal_) {
372 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000373 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 RestoreLiveRegisters(codegen, locations);
377 __ jmp(GetExitLabel());
378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
382
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000383 bool IsFatal() const OVERRIDE { return is_fatal_; }
384
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000385 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000386 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000387
388 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
389};
390
Andreas Gampe85b62f22015-09-09 13:15:38 -0700391class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700392 public:
Aart Bik42249c32016-01-07 15:33:50 -0800393 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100399 LocationSummary* locations = instruction_->GetLocations();
400 SaveLiveRegisters(codegen, locations);
401 InvokeRuntimeCallingConvention calling_convention;
402 x86_64_codegen->Load32BitValue(
403 CpuRegister(calling_convention.GetRegisterAt(0)),
404 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100405 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100406 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700407 }
408
Alexandre Rames9931f312015-06-19 14:47:01 +0100409 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
410
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700411 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700412 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
413};
414
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100415class ArraySetSlowPathX86_64 : public SlowPathCode {
416 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000417 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100418
419 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
420 LocationSummary* locations = instruction_->GetLocations();
421 __ Bind(GetEntryLabel());
422 SaveLiveRegisters(codegen, locations);
423
424 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100425 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100426 parallel_move.AddMove(
427 locations->InAt(0),
428 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100429 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100430 nullptr);
431 parallel_move.AddMove(
432 locations->InAt(1),
433 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100434 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435 nullptr);
436 parallel_move.AddMove(
437 locations->InAt(2),
438 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100439 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100440 nullptr);
441 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
442
Roland Levillain0d5a2812015-11-13 10:07:31 +0000443 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100444 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000445 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100446 RestoreLiveRegisters(codegen, locations);
447 __ jmp(GetExitLabel());
448 }
449
450 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
451
452 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100453 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
454};
455
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100456// Slow path marking an object reference `ref` during a read
457// barrier. The field `obj.field` in the object `obj` holding this
458// reference does not get updated by this slow path after marking (see
459// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
460//
461// This means that after the execution of this slow path, `ref` will
462// always be up-to-date, but `obj.field` may not; i.e., after the
463// flip, `ref` will be a to-space reference, but `obj.field` will
464// probably still be a from-space reference (unless it gets updated by
465// another thread, or if another thread installed another object
466// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000467class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
468 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100469 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
470 Location ref,
471 bool unpoison_ref_before_marking)
472 : SlowPathCode(instruction),
473 ref_(ref),
474 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000475 DCHECK(kEmitCompilerReadBarrier);
476 }
477
478 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
479
480 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
481 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100482 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
483 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000484 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100485 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000486 DCHECK(instruction_->IsInstanceFieldGet() ||
487 instruction_->IsStaticFieldGet() ||
488 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100489 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000490 instruction_->IsLoadClass() ||
491 instruction_->IsLoadString() ||
492 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100493 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100494 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
495 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000496 << "Unexpected instruction in read barrier marking slow path: "
497 << instruction_->DebugName();
498
499 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000501 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000503 }
Roland Levillain4359e612016-07-20 11:32:19 +0100504 // No need to save live registers; it's taken care of by the
505 // entrypoint. Also, there is no need to update the stack mask,
506 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000507 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100508 DCHECK_NE(ref_reg, RSP);
509 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100510 // "Compact" slow path, saving two moves.
511 //
512 // Instead of using the standard runtime calling convention (input
513 // and output in R0):
514 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100516 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100518 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100519 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100520 // of a dedicated entrypoint:
521 //
522 // rX <- ReadBarrierMarkRegX(rX)
523 //
524 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100525 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100526 // This runtime call does not require a stack map.
527 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000528 __ jmp(GetExitLabel());
529 }
530
531 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100532 // The location (register) of the marked object reference.
533 const Location ref_;
534 // Should the reference in `ref_` be unpoisoned prior to marking it?
535 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000536
537 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
538};
539
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100540// Slow path marking an object reference `ref` during a read barrier,
541// and if needed, atomically updating the field `obj.field` in the
542// object `obj` holding this reference after marking (contrary to
543// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
544// `obj.field`).
545//
546// This means that after the execution of this slow path, both `ref`
547// and `obj.field` will be up-to-date; i.e., after the flip, both will
548// hold the same to-space reference (unless another thread installed
549// another object reference (different from `ref`) in `obj.field`).
550class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
551 public:
552 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
553 Location ref,
554 CpuRegister obj,
555 const Address& field_addr,
556 bool unpoison_ref_before_marking,
557 CpuRegister temp1,
558 CpuRegister temp2)
559 : SlowPathCode(instruction),
560 ref_(ref),
561 obj_(obj),
562 field_addr_(field_addr),
563 unpoison_ref_before_marking_(unpoison_ref_before_marking),
564 temp1_(temp1),
565 temp2_(temp2) {
566 DCHECK(kEmitCompilerReadBarrier);
567 }
568
569 const char* GetDescription() const OVERRIDE {
570 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
571 }
572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
576 Register ref_reg = ref_cpu_reg.AsRegister();
577 DCHECK(locations->CanCall());
578 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
579 // This slow path is only used by the UnsafeCASObject intrinsic.
580 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
581 << "Unexpected instruction in read barrier marking and field updating slow path: "
582 << instruction_->DebugName();
583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
585
586 __ Bind(GetEntryLabel());
587 if (unpoison_ref_before_marking_) {
588 // Object* ref = ref_addr->AsMirrorPtr()
589 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
590 }
591
592 // Save the old (unpoisoned) reference.
593 __ movl(temp1_, ref_cpu_reg);
594
595 // No need to save live registers; it's taken care of by the
596 // entrypoint. Also, there is no need to update the stack mask,
597 // as this runtime call will not trigger a garbage collection.
598 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
599 DCHECK_NE(ref_reg, RSP);
600 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
601 // "Compact" slow path, saving two moves.
602 //
603 // Instead of using the standard runtime calling convention (input
604 // and output in R0):
605 //
606 // RDI <- ref
607 // RAX <- ReadBarrierMark(RDI)
608 // ref <- RAX
609 //
610 // we just use rX (the register containing `ref`) as input and output
611 // of a dedicated entrypoint:
612 //
613 // rX <- ReadBarrierMarkRegX(rX)
614 //
615 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100616 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100617 // This runtime call does not require a stack map.
618 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
619
620 // If the new reference is different from the old reference,
621 // update the field in the holder (`*field_addr`).
622 //
623 // Note that this field could also hold a different object, if
624 // another thread had concurrently changed it. In that case, the
625 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
626 // operation below would abort the CAS, leaving the field as-is.
627 NearLabel done;
628 __ cmpl(temp1_, ref_cpu_reg);
629 __ j(kEqual, &done);
630
631 // Update the the holder's field atomically. This may fail if
632 // mutator updates before us, but it's OK. This is achived
633 // using a strong compare-and-set (CAS) operation with relaxed
634 // memory synchronization ordering, where the expected value is
635 // the old reference and the desired value is the new reference.
636 // This operation is implemented with a 32-bit LOCK CMPXLCHG
637 // instruction, which requires the expected value (the old
638 // reference) to be in EAX. Save RAX beforehand, and move the
639 // expected value (stored in `temp1_`) into EAX.
640 __ movq(temp2_, CpuRegister(RAX));
641 __ movl(CpuRegister(RAX), temp1_);
642
643 // Convenience aliases.
644 CpuRegister base = obj_;
645 CpuRegister expected = CpuRegister(RAX);
646 CpuRegister value = ref_cpu_reg;
647
648 bool base_equals_value = (base.AsRegister() == value.AsRegister());
649 Register value_reg = ref_reg;
650 if (kPoisonHeapReferences) {
651 if (base_equals_value) {
652 // If `base` and `value` are the same register location, move
653 // `value_reg` to a temporary register. This way, poisoning
654 // `value_reg` won't invalidate `base`.
655 value_reg = temp1_.AsRegister();
656 __ movl(CpuRegister(value_reg), base);
657 }
658
659 // Check that the register allocator did not assign the location
660 // of `expected` (RAX) to `value` nor to `base`, so that heap
661 // poisoning (when enabled) works as intended below.
662 // - If `value` were equal to `expected`, both references would
663 // be poisoned twice, meaning they would not be poisoned at
664 // all, as heap poisoning uses address negation.
665 // - If `base` were equal to `expected`, poisoning `expected`
666 // would invalidate `base`.
667 DCHECK_NE(value_reg, expected.AsRegister());
668 DCHECK_NE(base.AsRegister(), expected.AsRegister());
669
670 __ PoisonHeapReference(expected);
671 __ PoisonHeapReference(CpuRegister(value_reg));
672 }
673
674 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
675
676 // If heap poisoning is enabled, we need to unpoison the values
677 // that were poisoned earlier.
678 if (kPoisonHeapReferences) {
679 if (base_equals_value) {
680 // `value_reg` has been moved to a temporary register, no need
681 // to unpoison it.
682 } else {
683 __ UnpoisonHeapReference(CpuRegister(value_reg));
684 }
685 // No need to unpoison `expected` (RAX), as it is be overwritten below.
686 }
687
688 // Restore RAX.
689 __ movq(CpuRegister(RAX), temp2_);
690
691 __ Bind(&done);
692 __ jmp(GetExitLabel());
693 }
694
695 private:
696 // The location (register) of the marked object reference.
697 const Location ref_;
698 // The register containing the object holding the marked object reference field.
699 const CpuRegister obj_;
700 // The address of the marked reference field. The base of this address must be `obj_`.
701 const Address field_addr_;
702
703 // Should the reference in `ref_` be unpoisoned prior to marking it?
704 const bool unpoison_ref_before_marking_;
705
706 const CpuRegister temp1_;
707 const CpuRegister temp2_;
708
709 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
710};
711
Roland Levillain0d5a2812015-11-13 10:07:31 +0000712// Slow path generating a read barrier for a heap reference.
713class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
714 public:
715 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
716 Location out,
717 Location ref,
718 Location obj,
719 uint32_t offset,
720 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000721 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722 out_(out),
723 ref_(ref),
724 obj_(obj),
725 offset_(offset),
726 index_(index) {
727 DCHECK(kEmitCompilerReadBarrier);
728 // If `obj` is equal to `out` or `ref`, it means the initial
729 // object has been overwritten by (or after) the heap object
730 // reference load to be instrumented, e.g.:
731 //
732 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000733 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000734 //
735 // In that case, we have lost the information about the original
736 // object, and the emitted read barrier cannot work properly.
737 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
738 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
739}
740
741 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
742 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
743 LocationSummary* locations = instruction_->GetLocations();
744 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
745 DCHECK(locations->CanCall());
746 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100747 DCHECK(instruction_->IsInstanceFieldGet() ||
748 instruction_->IsStaticFieldGet() ||
749 instruction_->IsArrayGet() ||
750 instruction_->IsInstanceOf() ||
751 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700752 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000753 << "Unexpected instruction in read barrier for heap reference slow path: "
754 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000755
756 __ Bind(GetEntryLabel());
757 SaveLiveRegisters(codegen, locations);
758
759 // We may have to change the index's value, but as `index_` is a
760 // constant member (like other "inputs" of this slow path),
761 // introduce a copy of it, `index`.
762 Location index = index_;
763 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100764 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000765 if (instruction_->IsArrayGet()) {
766 // Compute real offset and store it in index_.
767 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
768 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
769 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
770 // We are about to change the value of `index_reg` (see the
771 // calls to art::x86_64::X86_64Assembler::shll and
772 // art::x86_64::X86_64Assembler::AddImmediate below), but it
773 // has not been saved by the previous call to
774 // art::SlowPathCode::SaveLiveRegisters, as it is a
775 // callee-save register --
776 // art::SlowPathCode::SaveLiveRegisters does not consider
777 // callee-save registers, as it has been designed with the
778 // assumption that callee-save registers are supposed to be
779 // handled by the called function. So, as a callee-save
780 // register, `index_reg` _would_ eventually be saved onto
781 // the stack, but it would be too late: we would have
782 // changed its value earlier. Therefore, we manually save
783 // it here into another freely available register,
784 // `free_reg`, chosen of course among the caller-save
785 // registers (as a callee-save `free_reg` register would
786 // exhibit the same problem).
787 //
788 // Note we could have requested a temporary register from
789 // the register allocator instead; but we prefer not to, as
790 // this is a slow path, and we know we can find a
791 // caller-save register that is available.
792 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
793 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
794 index_reg = free_reg;
795 index = Location::RegisterLocation(index_reg);
796 } else {
797 // The initial register stored in `index_` has already been
798 // saved in the call to art::SlowPathCode::SaveLiveRegisters
799 // (as it is not a callee-save register), so we can freely
800 // use it.
801 }
802 // Shifting the index value contained in `index_reg` by the
803 // scale factor (2) cannot overflow in practice, as the
804 // runtime is unable to allocate object arrays with a size
805 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
806 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
807 static_assert(
808 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
809 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
810 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
811 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100812 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
813 // intrinsics, `index_` is not shifted by a scale factor of 2
814 // (as in the case of ArrayGet), as it is actually an offset
815 // to an object field within an object.
816 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000817 DCHECK(instruction_->GetLocations()->Intrinsified());
818 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
819 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
820 << instruction_->AsInvoke()->GetIntrinsic();
821 DCHECK_EQ(offset_, 0U);
822 DCHECK(index_.IsRegister());
823 }
824 }
825
826 // We're moving two or three locations to locations that could
827 // overlap, so we need a parallel move resolver.
828 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100829 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000830 parallel_move.AddMove(ref_,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 nullptr);
834 parallel_move.AddMove(obj_,
835 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100836 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000837 nullptr);
838 if (index.IsValid()) {
839 parallel_move.AddMove(index,
840 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100841 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000842 nullptr);
843 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
844 } else {
845 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
846 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
847 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100848 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849 instruction_,
850 instruction_->GetDexPc(),
851 this);
852 CheckEntrypointTypes<
853 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
854 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
855
856 RestoreLiveRegisters(codegen, locations);
857 __ jmp(GetExitLabel());
858 }
859
860 const char* GetDescription() const OVERRIDE {
861 return "ReadBarrierForHeapReferenceSlowPathX86_64";
862 }
863
864 private:
865 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
866 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
867 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
868 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
869 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
870 return static_cast<CpuRegister>(i);
871 }
872 }
873 // We shall never fail to find a free caller-save register, as
874 // there are more than two core caller-save registers on x86-64
875 // (meaning it is possible to find one which is different from
876 // `ref` and `obj`).
877 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
878 LOG(FATAL) << "Could not find a free caller-save register";
879 UNREACHABLE();
880 }
881
Roland Levillain0d5a2812015-11-13 10:07:31 +0000882 const Location out_;
883 const Location ref_;
884 const Location obj_;
885 const uint32_t offset_;
886 // An additional location containing an index to an array.
887 // Only used for HArrayGet and the UnsafeGetObject &
888 // UnsafeGetObjectVolatile intrinsics.
889 const Location index_;
890
891 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
892};
893
894// Slow path generating a read barrier for a GC root.
895class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
896 public:
897 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000898 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000899 DCHECK(kEmitCompilerReadBarrier);
900 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000901
902 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
903 LocationSummary* locations = instruction_->GetLocations();
904 DCHECK(locations->CanCall());
905 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000906 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
907 << "Unexpected instruction in read barrier for GC root slow path: "
908 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000909
910 __ Bind(GetEntryLabel());
911 SaveLiveRegisters(codegen, locations);
912
913 InvokeRuntimeCallingConvention calling_convention;
914 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
915 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100916 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000917 instruction_,
918 instruction_->GetDexPc(),
919 this);
920 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
921 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
922
923 RestoreLiveRegisters(codegen, locations);
924 __ jmp(GetExitLabel());
925 }
926
927 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
928
929 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000930 const Location out_;
931 const Location root_;
932
933 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
934};
935
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100936#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100937// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
938#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100939
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700941 switch (cond) {
942 case kCondEQ: return kEqual;
943 case kCondNE: return kNotEqual;
944 case kCondLT: return kLess;
945 case kCondLE: return kLessEqual;
946 case kCondGT: return kGreater;
947 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700948 case kCondB: return kBelow;
949 case kCondBE: return kBelowEqual;
950 case kCondA: return kAbove;
951 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700952 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100953 LOG(FATAL) << "Unreachable";
954 UNREACHABLE();
955}
956
Aart Bike9f37602015-10-09 11:15:55 -0700957// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100958inline Condition X86_64FPCondition(IfCondition cond) {
959 switch (cond) {
960 case kCondEQ: return kEqual;
961 case kCondNE: return kNotEqual;
962 case kCondLT: return kBelow;
963 case kCondLE: return kBelowEqual;
964 case kCondGT: return kAbove;
965 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700966 default: break; // should not happen
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800967 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100968 LOG(FATAL) << "Unreachable";
969 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700970}
971
Vladimir Markodc151b22015-10-15 18:02:30 +0100972HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
973 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100974 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000975 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100976}
977
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100978void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
979 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800980 // All registers are assumed to be correctly set up.
Vladimir Marko4ee8e292017-06-02 15:39:30 +0000981
Vladimir Marko58155012015-08-19 12:49:41 +0000982 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
983 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100984 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000985 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100986 uint32_t offset =
987 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
988 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000989 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100990 }
Vladimir Marko58155012015-08-19 12:49:41 +0000991 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000992 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000993 break;
Vladimir Marko65979462017-05-19 17:25:12 +0100994 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
995 DCHECK(GetCompilerOptions().IsBootImage());
996 __ leal(temp.AsRegister<CpuRegister>(),
997 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000998 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +0100999 break;
Vladimir Markob066d432018-01-03 13:14:37 +00001000 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
1001 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1002 __ movl(temp.AsRegister<CpuRegister>(),
1003 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001004 RecordBootImageRelRoPatch(GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00001005 break;
1006 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001007 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001008 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001009 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001010 RecordMethodBssEntryPatch(invoke);
Vladimir Marko58155012015-08-19 12:49:41 +00001011 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001012 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001013 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
1014 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
1015 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001016 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1017 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1018 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001019 }
Vladimir Marko58155012015-08-19 12:49:41 +00001020 }
1021
1022 switch (invoke->GetCodePtrLocation()) {
1023 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1024 __ call(&frame_entry_label_);
1025 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001026 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1027 // (callee_method + offset_of_quick_compiled_code)()
1028 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1029 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001030 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001031 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001032 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001033 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001034
1035 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001036}
1037
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001038void CodeGeneratorX86_64::GenerateVirtualCall(
1039 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001040 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1041 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1042 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001043
1044 // Use the calling convention instead of the location of the receiver, as
1045 // intrinsics may have put the receiver in a different register. In the intrinsics
1046 // slow path, the arguments have been moved to the right place, so here we are
1047 // guaranteed that the receiver is the first register of the calling convention.
1048 InvokeDexCallingConvention calling_convention;
1049 Register receiver = calling_convention.GetRegisterAt(0);
1050
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001051 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001052 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001053 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001054 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001055 // Instead of simply (possibly) unpoisoning `temp` here, we should
1056 // emit a read barrier for the previous class reference load.
1057 // However this is not required in practice, as this is an
1058 // intermediate/temporary reference and because the current
1059 // concurrent copying collector keeps the from-space memory
1060 // intact/accessible until the end of the marking phase (the
1061 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001062 __ MaybeUnpoisonHeapReference(temp);
1063 // temp = temp->GetMethodAt(method_offset);
1064 __ movq(temp, Address(temp, method_offset));
1065 // call temp->GetEntryPoint();
1066 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001067 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001068 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001069}
1070
Vladimir Marko6fd16062018-06-26 11:02:04 +01001071void CodeGeneratorX86_64::RecordBootImageIntrinsicPatch(uint32_t intrinsic_data) {
1072 boot_image_intrinsic_patches_.emplace_back(/* target_dex_file */ nullptr, intrinsic_data);
1073 __ Bind(&boot_image_intrinsic_patches_.back().label);
1074}
1075
Vladimir Markob066d432018-01-03 13:14:37 +00001076void CodeGeneratorX86_64::RecordBootImageRelRoPatch(uint32_t boot_image_offset) {
1077 boot_image_method_patches_.emplace_back(/* target_dex_file */ nullptr, boot_image_offset);
1078 __ Bind(&boot_image_method_patches_.back().label);
1079}
1080
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001081void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
1082 boot_image_method_patches_.emplace_back(
1083 invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001084 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001085}
1086
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001087void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
1088 method_bss_entry_patches_.emplace_back(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
1089 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001090}
1091
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001092void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1093 boot_image_type_patches_.emplace_back(
1094 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001095 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001096}
1097
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001098Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001099 type_bss_entry_patches_.emplace_back(
1100 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001101 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001102}
1103
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001104void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1105 boot_image_string_patches_.emplace_back(
1106 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1107 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001108}
1109
Vladimir Markoaad75c62016-10-03 08:46:48 +00001110Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1111 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001112 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001113 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001114 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001115}
1116
Vladimir Marko6fd16062018-06-26 11:02:04 +01001117void CodeGeneratorX86_64::LoadBootImageAddress(CpuRegister reg, uint32_t boot_image_reference) {
1118 if (GetCompilerOptions().IsBootImage()) {
1119 __ leal(reg, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
1120 RecordBootImageIntrinsicPatch(boot_image_reference);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001121 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01001122 __ movl(reg, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko6fd16062018-06-26 11:02:04 +01001123 RecordBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001124 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001125 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001126 gc::Heap* heap = Runtime::Current()->GetHeap();
1127 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001128 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001129 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
1130 }
1131}
1132
Vladimir Marko6fd16062018-06-26 11:02:04 +01001133void CodeGeneratorX86_64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1134 uint32_t boot_image_offset) {
1135 DCHECK(invoke->IsStatic());
1136 InvokeRuntimeCallingConvention calling_convention;
1137 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
1138 if (GetCompilerOptions().IsBootImage()) {
1139 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1140 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1141 __ leal(argument,
1142 Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
1143 MethodReference target_method = invoke->GetTargetMethod();
1144 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1145 boot_image_type_patches_.emplace_back(target_method.dex_file, type_idx.index_);
1146 __ Bind(&boot_image_type_patches_.back().label);
1147 } else {
1148 LoadBootImageAddress(argument, boot_image_offset);
1149 }
1150 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1151 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1152}
1153
Vladimir Markoaad75c62016-10-03 08:46:48 +00001154// The label points to the end of the "movl" or another instruction but the literal offset
1155// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1156constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1157
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001158template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001159inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1160 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001161 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001162 for (const PatchInfo<Label>& info : infos) {
1163 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1164 linker_patches->push_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001165 Factory(literal_offset, info.target_dex_file, info.label.Position(), info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001166 }
1167}
1168
Vladimir Marko6fd16062018-06-26 11:02:04 +01001169template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1170linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1171 const DexFile* target_dex_file,
1172 uint32_t pc_insn_offset,
1173 uint32_t boot_image_offset) {
1174 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1175 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001176}
1177
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001178void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001179 DCHECK(linker_patches->empty());
1180 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001181 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001182 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001183 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001184 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001185 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001186 string_bss_entry_patches_.size() +
1187 boot_image_intrinsic_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001188 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001189 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001190 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1191 boot_image_method_patches_, linker_patches);
1192 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1193 boot_image_type_patches_, linker_patches);
1194 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001195 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001196 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1197 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001198 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001199 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001200 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001201 DCHECK(boot_image_type_patches_.empty());
1202 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001203 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001204 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001205 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1206 method_bss_entry_patches_, linker_patches);
1207 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1208 type_bss_entry_patches_, linker_patches);
1209 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1210 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001211 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001212}
1213
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001214void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001215 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216}
1217
1218void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001219 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001220}
1221
Vladimir Markoa0431112018-06-25 09:32:54 +01001222const X86_64InstructionSetFeatures& CodeGeneratorX86_64::GetInstructionSetFeatures() const {
1223 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86_64InstructionSetFeatures();
1224}
1225
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001226size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1227 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1228 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001229}
1230
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001231size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1232 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1233 return kX86_64WordSize;
1234}
1235
1236size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001237 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001238 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001239 } else {
1240 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1241 }
1242 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001243}
1244
1245size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001246 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001247 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001248 } else {
1249 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1250 }
1251 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001252}
1253
Calin Juravle175dc732015-08-25 15:42:32 +01001254void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1255 HInstruction* instruction,
1256 uint32_t dex_pc,
1257 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001258 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001259 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1260 if (EntrypointRequiresStackMap(entrypoint)) {
1261 RecordPcInfo(instruction, dex_pc, slow_path);
1262 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001263}
1264
Roland Levillaindec8f632016-07-22 17:10:06 +01001265void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1266 HInstruction* instruction,
1267 SlowPathCode* slow_path) {
1268 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001269 GenerateInvokeRuntime(entry_point_offset);
1270}
1271
1272void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001273 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1274}
1275
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001276static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001277// Use a fake return address register to mimic Quick.
1278static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001279CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001280 const CompilerOptions& compiler_options,
1281 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001282 : CodeGenerator(graph,
1283 kNumberOfCpuRegisters,
1284 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001285 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001286 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1287 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001288 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001289 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1290 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001291 compiler_options,
1292 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001293 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001294 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001295 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001296 move_resolver_(graph->GetAllocator(), this),
1297 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001298 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001299 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1300 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1301 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1302 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001303 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001304 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001305 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001306 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1307 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1308 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001309 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1310}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001311
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001312InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1313 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001314 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001315 assembler_(codegen->GetAssembler()),
1316 codegen_(codegen) {}
1317
David Brazdil58282f42016-01-14 12:45:10 +00001318void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001320 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001321
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001322 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001323 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324}
1325
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001326static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001327 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001328}
David Srbecky9d8606d2015-04-12 09:35:32 +01001329
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001330static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001331 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001332}
1333
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001334void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001335 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001336 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001337 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001338 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001339 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001340
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001341 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1342 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1343 ArtMethod::HotnessCountOffset().Int32Value()),
1344 Immediate(1));
1345 }
1346
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001347 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001348 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1349 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001350 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001351 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001352
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001353 if (HasEmptyFrame()) {
1354 return;
1355 }
1356
Nicolas Geoffray98893962015-01-21 12:32:32 +00001357 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001358 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001359 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001360 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001361 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1362 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001363 }
1364 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001365
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001366 int adjust = GetFrameSize() - GetCoreSpillSize();
1367 __ subq(CpuRegister(RSP), Immediate(adjust));
1368 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001369 uint32_t xmm_spill_location = GetFpuSpillStart();
1370 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001371
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001372 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1373 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001374 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1375 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1376 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001377 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001378 }
1379
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001380 // Save the current method if we need it. Note that we do not
1381 // do this in HCurrentMethod, as the instruction might have been removed
1382 // in the SSA graph.
1383 if (RequiresCurrentMethod()) {
1384 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1385 CpuRegister(kMethodRegisterArgument));
1386 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001387
1388 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1389 // Initialize should_deoptimize flag to 0.
1390 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1391 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001392}
1393
1394void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001395 __ cfi().RememberState();
1396 if (!HasEmptyFrame()) {
1397 uint32_t xmm_spill_location = GetFpuSpillStart();
1398 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1399 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1400 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1401 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1402 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1403 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1404 }
1405 }
1406
1407 int adjust = GetFrameSize() - GetCoreSpillSize();
1408 __ addq(CpuRegister(RSP), Immediate(adjust));
1409 __ cfi().AdjustCFAOffset(-adjust);
1410
1411 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1412 Register reg = kCoreCalleeSaves[i];
1413 if (allocated_registers_.ContainsCoreRegister(reg)) {
1414 __ popq(CpuRegister(reg));
1415 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1416 __ cfi().Restore(DWARFReg(reg));
1417 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001418 }
1419 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001420 __ ret();
1421 __ cfi().RestoreState();
1422 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001423}
1424
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001425void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1426 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001427}
1428
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001429void CodeGeneratorX86_64::Move(Location destination, Location source) {
1430 if (source.Equals(destination)) {
1431 return;
1432 }
1433 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001434 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001435 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001436 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001437 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001438 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001439 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001440 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1441 } else if (source.IsConstant()) {
1442 HConstant* constant = source.GetConstant();
1443 if (constant->IsLongConstant()) {
1444 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1445 } else {
1446 Load32BitValue(dest, GetInt32ValueOf(constant));
1447 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001448 } else {
1449 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001450 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001451 }
1452 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001453 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001454 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001455 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001456 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001457 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1458 } else if (source.IsConstant()) {
1459 HConstant* constant = source.GetConstant();
1460 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1461 if (constant->IsFloatConstant()) {
1462 Load32BitValue(dest, static_cast<int32_t>(value));
1463 } else {
1464 Load64BitValue(dest, value);
1465 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001467 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001468 } else {
1469 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001470 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001471 }
1472 } else if (destination.IsStackSlot()) {
1473 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001475 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001476 } else if (source.IsFpuRegister()) {
1477 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001478 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001479 } else if (source.IsConstant()) {
1480 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001481 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001482 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001483 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001484 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001485 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1486 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001487 }
1488 } else {
1489 DCHECK(destination.IsDoubleStackSlot());
1490 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001491 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001493 } else if (source.IsFpuRegister()) {
1494 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001495 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001496 } else if (source.IsConstant()) {
1497 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001498 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1499 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001500 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001501 } else {
1502 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001503 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1504 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001505 }
1506 }
1507}
1508
Calin Juravle175dc732015-08-25 15:42:32 +01001509void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1510 DCHECK(location.IsRegister());
1511 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1512}
1513
Calin Juravlee460d1d2015-09-29 04:52:17 +01001514void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001515 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001516 Move(dst, src);
1517}
1518
1519void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1520 if (location.IsRegister()) {
1521 locations->AddTemp(location);
1522 } else {
1523 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1524 }
1525}
1526
David Brazdilfc6a86a2015-06-26 10:33:45 +00001527void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001528 if (successor->IsExitBlock()) {
1529 DCHECK(got->GetPrevious()->AlwaysThrows());
1530 return; // no code needed
1531 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001532
1533 HBasicBlock* block = got->GetBlock();
1534 HInstruction* previous = got->GetPrevious();
1535
1536 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001537 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001538 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1539 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1540 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1541 Immediate(1));
1542 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001543 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1544 return;
1545 }
1546
1547 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1548 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1549 }
1550 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001551 __ jmp(codegen_->GetLabelOf(successor));
1552 }
1553}
1554
David Brazdilfc6a86a2015-06-26 10:33:45 +00001555void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1556 got->SetLocations(nullptr);
1557}
1558
1559void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1560 HandleGoto(got, got->GetSuccessor());
1561}
1562
1563void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1564 try_boundary->SetLocations(nullptr);
1565}
1566
1567void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1568 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1569 if (!successor->IsExitBlock()) {
1570 HandleGoto(try_boundary, successor);
1571 }
1572}
1573
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001574void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1575 exit->SetLocations(nullptr);
1576}
1577
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001578void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579}
1580
Mark Mendell152408f2015-12-31 12:28:50 -05001581template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001582void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001583 LabelType* true_label,
1584 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001585 if (cond->IsFPConditionTrueIfNaN()) {
1586 __ j(kUnordered, true_label);
1587 } else if (cond->IsFPConditionFalseIfNaN()) {
1588 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001589 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001590 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001591}
1592
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001593void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001594 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001595
Mark Mendellc4701932015-04-10 13:18:51 -04001596 Location left = locations->InAt(0);
1597 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001598 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001599 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001600 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001601 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001602 case DataType::Type::kInt8:
1603 case DataType::Type::kUint16:
1604 case DataType::Type::kInt16:
1605 case DataType::Type::kInt32:
1606 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001607 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001608 break;
1609 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001611 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001612 break;
1613 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001614 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001615 if (right.IsFpuRegister()) {
1616 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1617 } else if (right.IsConstant()) {
1618 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1619 codegen_->LiteralFloatAddress(
1620 right.GetConstant()->AsFloatConstant()->GetValue()));
1621 } else {
1622 DCHECK(right.IsStackSlot());
1623 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1624 Address(CpuRegister(RSP), right.GetStackIndex()));
1625 }
Mark Mendellc4701932015-04-10 13:18:51 -04001626 break;
1627 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001629 if (right.IsFpuRegister()) {
1630 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1631 } else if (right.IsConstant()) {
1632 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1633 codegen_->LiteralDoubleAddress(
1634 right.GetConstant()->AsDoubleConstant()->GetValue()));
1635 } else {
1636 DCHECK(right.IsDoubleStackSlot());
1637 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1638 Address(CpuRegister(RSP), right.GetStackIndex()));
1639 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001640 break;
1641 }
1642 default:
1643 LOG(FATAL) << "Unexpected condition type " << type;
1644 }
1645}
1646
1647template<class LabelType>
1648void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1649 LabelType* true_target_in,
1650 LabelType* false_target_in) {
1651 // Generated branching requires both targets to be explicit. If either of the
1652 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1653 LabelType fallthrough_target;
1654 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1655 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1656
1657 // Generate the comparison to set the CC.
1658 GenerateCompareTest(condition);
1659
1660 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001661 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001662 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001663 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001664 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1665 break;
1666 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001667 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001668 GenerateFPJumps(condition, true_target, false_target);
1669 break;
1670 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001671 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001672 GenerateFPJumps(condition, true_target, false_target);
1673 break;
1674 }
1675 default:
1676 LOG(FATAL) << "Unexpected condition type " << type;
1677 }
1678
David Brazdil0debae72015-11-12 18:37:00 +00001679 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001680 __ jmp(false_target);
1681 }
David Brazdil0debae72015-11-12 18:37:00 +00001682
1683 if (fallthrough_target.IsLinked()) {
1684 __ Bind(&fallthrough_target);
1685 }
Mark Mendellc4701932015-04-10 13:18:51 -04001686}
1687
David Brazdil0debae72015-11-12 18:37:00 +00001688static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1689 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1690 // are set only strictly before `branch`. We can't use the eflags on long
1691 // conditions if they are materialized due to the complex branching.
1692 return cond->IsCondition() &&
1693 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001695}
1696
Mark Mendell152408f2015-12-31 12:28:50 -05001697template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001698void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001699 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001700 LabelType* true_target,
1701 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001702 HInstruction* cond = instruction->InputAt(condition_input_index);
1703
1704 if (true_target == nullptr && false_target == nullptr) {
1705 // Nothing to do. The code always falls through.
1706 return;
1707 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001708 // Constant condition, statically compared against "true" (integer value 1).
1709 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001710 if (true_target != nullptr) {
1711 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001712 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001713 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001714 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001715 if (false_target != nullptr) {
1716 __ jmp(false_target);
1717 }
1718 }
1719 return;
1720 }
1721
1722 // The following code generates these patterns:
1723 // (1) true_target == nullptr && false_target != nullptr
1724 // - opposite condition true => branch to false_target
1725 // (2) true_target != nullptr && false_target == nullptr
1726 // - condition true => branch to true_target
1727 // (3) true_target != nullptr && false_target != nullptr
1728 // - condition true => branch to true_target
1729 // - branch to false_target
1730 if (IsBooleanValueOrMaterializedCondition(cond)) {
1731 if (AreEflagsSetFrom(cond, instruction)) {
1732 if (true_target == nullptr) {
1733 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1734 } else {
1735 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1736 }
1737 } else {
1738 // Materialized condition, compare against 0.
1739 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1740 if (lhs.IsRegister()) {
1741 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1742 } else {
1743 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1744 }
1745 if (true_target == nullptr) {
1746 __ j(kEqual, false_target);
1747 } else {
1748 __ j(kNotEqual, true_target);
1749 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001750 }
1751 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001752 // Condition has not been materialized, use its inputs as the
1753 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001754 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001755
David Brazdil0debae72015-11-12 18:37:00 +00001756 // If this is a long or FP comparison that has been folded into
1757 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 DataType::Type type = condition->InputAt(0)->GetType();
1759 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001760 GenerateCompareTestAndBranch(condition, true_target, false_target);
1761 return;
1762 }
1763
1764 Location lhs = condition->GetLocations()->InAt(0);
1765 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001766 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001767 if (true_target == nullptr) {
1768 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1769 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001770 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001771 }
Dave Allison20dfc792014-06-16 20:44:29 -07001772 }
David Brazdil0debae72015-11-12 18:37:00 +00001773
1774 // If neither branch falls through (case 3), the conditional branch to `true_target`
1775 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1776 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001777 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001778 }
1779}
1780
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001781void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001782 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001783 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001784 locations->SetInAt(0, Location::Any());
1785 }
1786}
1787
1788void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001789 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1790 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1791 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1792 nullptr : codegen_->GetLabelOf(true_successor);
1793 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1794 nullptr : codegen_->GetLabelOf(false_successor);
1795 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001796}
1797
1798void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001799 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001800 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001801 InvokeRuntimeCallingConvention calling_convention;
1802 RegisterSet caller_saves = RegisterSet::Empty();
1803 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1804 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001805 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001806 locations->SetInAt(0, Location::Any());
1807 }
1808}
1809
1810void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001811 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001812 GenerateTestAndBranch<Label>(deoptimize,
1813 /* condition_input_index */ 0,
1814 slow_path->GetEntryLabel(),
1815 /* false_target */ nullptr);
1816}
1817
Mingyao Yang063fc772016-08-02 11:02:54 -07001818void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001819 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001820 LocationSummary(flag, LocationSummary::kNoCall);
1821 locations->SetOut(Location::RequiresRegister());
1822}
1823
1824void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1825 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1826 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1827}
1828
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001829static bool SelectCanUseCMOV(HSelect* select) {
1830 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001832 return false;
1833 }
1834
1835 // A FP condition doesn't generate the single CC that we need.
1836 HInstruction* condition = select->GetCondition();
1837 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001839 return false;
1840 }
1841
1842 // We can generate a CMOV for this Select.
1843 return true;
1844}
1845
David Brazdil74eb1b22015-12-14 11:44:01 +00001846void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001847 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001849 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001850 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001851 } else {
1852 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001853 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001854 if (select->InputAt(1)->IsConstant()) {
1855 locations->SetInAt(1, Location::RequiresRegister());
1856 } else {
1857 locations->SetInAt(1, Location::Any());
1858 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001859 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001860 locations->SetInAt(1, Location::Any());
1861 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001862 }
1863 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1864 locations->SetInAt(2, Location::RequiresRegister());
1865 }
1866 locations->SetOut(Location::SameAsFirstInput());
1867}
1868
1869void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1870 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001871 if (SelectCanUseCMOV(select)) {
1872 // If both the condition and the source types are integer, we can generate
1873 // a CMOV to implement Select.
1874 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001875 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001876 DCHECK(locations->InAt(0).Equals(locations->Out()));
1877
1878 HInstruction* select_condition = select->GetCondition();
1879 Condition cond = kNotEqual;
1880
1881 // Figure out how to test the 'condition'.
1882 if (select_condition->IsCondition()) {
1883 HCondition* condition = select_condition->AsCondition();
1884 if (!condition->IsEmittedAtUseSite()) {
1885 // This was a previously materialized condition.
1886 // Can we use the existing condition code?
1887 if (AreEflagsSetFrom(condition, select)) {
1888 // Materialization was the previous instruction. Condition codes are right.
1889 cond = X86_64IntegerCondition(condition->GetCondition());
1890 } else {
1891 // No, we have to recreate the condition code.
1892 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1893 __ testl(cond_reg, cond_reg);
1894 }
1895 } else {
1896 GenerateCompareTest(condition);
1897 cond = X86_64IntegerCondition(condition->GetCondition());
1898 }
1899 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001900 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001901 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1902 __ testl(cond_reg, cond_reg);
1903 }
1904
1905 // If the condition is true, overwrite the output, which already contains false.
1906 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001907 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001908 if (value_true_loc.IsRegister()) {
1909 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1910 } else {
1911 __ cmov(cond,
1912 value_false,
1913 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1914 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001915 } else {
1916 NearLabel false_target;
1917 GenerateTestAndBranch<NearLabel>(select,
1918 /* condition_input_index */ 2,
1919 /* true_target */ nullptr,
1920 &false_target);
1921 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1922 __ Bind(&false_target);
1923 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001924}
1925
David Srbecky0cf44932015-12-09 14:09:59 +00001926void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001927 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001928}
1929
David Srbeckyd28f4a02016-03-14 17:14:24 +00001930void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1931 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001932}
1933
1934void CodeGeneratorX86_64::GenerateNop() {
1935 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001936}
1937
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001938void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001939 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001940 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001941 // Handle the long/FP comparisons made in instruction simplification.
1942 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001943 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001944 locations->SetInAt(0, Location::RequiresRegister());
1945 locations->SetInAt(1, Location::Any());
1946 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 case DataType::Type::kFloat32:
1948 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001949 locations->SetInAt(0, Location::RequiresFpuRegister());
1950 locations->SetInAt(1, Location::Any());
1951 break;
1952 default:
1953 locations->SetInAt(0, Location::RequiresRegister());
1954 locations->SetInAt(1, Location::Any());
1955 break;
1956 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001957 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001958 locations->SetOut(Location::RequiresRegister());
1959 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001960}
1961
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001963 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001964 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001965 }
Mark Mendellc4701932015-04-10 13:18:51 -04001966
1967 LocationSummary* locations = cond->GetLocations();
1968 Location lhs = locations->InAt(0);
1969 Location rhs = locations->InAt(1);
1970 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001971 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001972
1973 switch (cond->InputAt(0)->GetType()) {
1974 default:
1975 // Integer case.
1976
1977 // Clear output register: setcc only sets the low byte.
1978 __ xorl(reg, reg);
1979
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001980 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001981 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001982 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001984 // Clear output register: setcc only sets the low byte.
1985 __ xorl(reg, reg);
1986
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001987 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001988 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001989 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001991 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1992 if (rhs.IsConstant()) {
1993 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1994 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1995 } else if (rhs.IsStackSlot()) {
1996 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1997 } else {
1998 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1999 }
2000 GenerateFPJumps(cond, &true_label, &false_label);
2001 break;
2002 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002003 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002004 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2005 if (rhs.IsConstant()) {
2006 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
2007 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
2008 } else if (rhs.IsDoubleStackSlot()) {
2009 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2010 } else {
2011 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2012 }
2013 GenerateFPJumps(cond, &true_label, &false_label);
2014 break;
2015 }
2016 }
2017
2018 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002019 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002020
Roland Levillain4fa13f62015-07-06 18:11:54 +01002021 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002022 __ Bind(&false_label);
2023 __ xorl(reg, reg);
2024 __ jmp(&done_label);
2025
Roland Levillain4fa13f62015-07-06 18:11:54 +01002026 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002027 __ Bind(&true_label);
2028 __ movl(reg, Immediate(1));
2029 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002030}
2031
2032void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002033 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002034}
2035
2036void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002037 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002038}
2039
2040void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002041 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002042}
2043
2044void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002045 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002046}
2047
2048void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002049 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002050}
2051
2052void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002053 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002054}
2055
2056void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002058}
2059
2060void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002061 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002062}
2063
2064void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002066}
2067
2068void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002069 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002070}
2071
2072void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002073 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002074}
2075
2076void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002077 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002078}
2079
Aart Bike9f37602015-10-09 11:15:55 -07002080void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002081 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002082}
2083
2084void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002085 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002086}
2087
2088void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002089 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002090}
2091
2092void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002094}
2095
2096void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002097 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002098}
2099
2100void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002101 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002102}
2103
2104void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002105 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002106}
2107
2108void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002109 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002110}
2111
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002112void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002113 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002114 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002115 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002117 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002120 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt32:
2122 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002123 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002124 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2126 break;
2127 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kFloat32:
2129 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002130 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002131 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002132 locations->SetOut(Location::RequiresRegister());
2133 break;
2134 }
2135 default:
2136 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2137 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002138}
2139
2140void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002141 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002143 Location left = locations->InAt(0);
2144 Location right = locations->InAt(1);
2145
Mark Mendell0c9497d2015-08-21 09:30:05 -04002146 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002148 Condition less_cond = kLess;
2149
Calin Juravleddb7df22014-11-25 20:56:51 +00002150 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002151 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002152 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002155 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002156 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002157 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002158 break;
2159 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002161 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002162 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002163 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002165 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2166 if (right.IsConstant()) {
2167 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2168 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2169 } else if (right.IsStackSlot()) {
2170 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2171 } else {
2172 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2173 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002174 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002175 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002176 break;
2177 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002179 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2180 if (right.IsConstant()) {
2181 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2182 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2183 } else if (right.IsDoubleStackSlot()) {
2184 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2185 } else {
2186 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2187 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002188 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002189 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002190 break;
2191 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002192 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002193 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002194 }
Aart Bika19616e2016-02-01 18:57:58 -08002195
Calin Juravleddb7df22014-11-25 20:56:51 +00002196 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002197 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002198 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002199
Calin Juravle91debbc2014-11-26 19:01:09 +00002200 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002201 __ movl(out, Immediate(1));
2202 __ jmp(&done);
2203
2204 __ Bind(&less);
2205 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002206
2207 __ Bind(&done);
2208}
2209
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002210void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002212 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002213 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002214}
2215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002216void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002217 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002218}
2219
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002220void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2221 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002222 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002223 locations->SetOut(Location::ConstantLocation(constant));
2224}
2225
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002226void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002227 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002228}
2229
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002230void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002231 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002232 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002233 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002234}
2235
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002236void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002237 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002238}
2239
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002240void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2241 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002242 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002243 locations->SetOut(Location::ConstantLocation(constant));
2244}
2245
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002246void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002247 // Will be generated at use site.
2248}
2249
2250void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002252 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002253 locations->SetOut(Location::ConstantLocation(constant));
2254}
2255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002256void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2257 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002258 // Will be generated at use site.
2259}
2260
Igor Murashkind01745e2017-04-05 16:40:31 -07002261void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2262 constructor_fence->SetLocations(nullptr);
2263}
2264
2265void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2266 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2267 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2268}
2269
Calin Juravle27df7582015-04-17 19:12:31 +01002270void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2271 memory_barrier->SetLocations(nullptr);
2272}
2273
2274void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002275 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002276}
2277
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002278void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2279 ret->SetLocations(nullptr);
2280}
2281
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002282void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002283 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002284}
2285
2286void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002287 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002288 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002289 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002290 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002292 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 case DataType::Type::kInt8:
2294 case DataType::Type::kUint16:
2295 case DataType::Type::kInt16:
2296 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002298 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002299 break;
2300
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 case DataType::Type::kFloat32:
2302 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002303 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 break;
2305
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002306 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002308 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002309}
2310
2311void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2312 if (kIsDebugBuild) {
2313 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002314 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002316 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 case DataType::Type::kInt8:
2318 case DataType::Type::kUint16:
2319 case DataType::Type::kInt16:
2320 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002322 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002323 break;
2324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325 case DataType::Type::kFloat32:
2326 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002328 XMM0);
2329 break;
2330
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002331 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002332 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002333 }
2334 }
2335 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002336}
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002339 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002340 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002342 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 case DataType::Type::kInt8:
2344 case DataType::Type::kUint16:
2345 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002346 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002348 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002350 return Location::RegisterLocation(RAX);
2351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002353 return Location::NoLocation();
2354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 case DataType::Type::kFloat64:
2356 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002357 return Location::FpuRegisterLocation(XMM0);
2358 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002359
2360 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002361}
2362
2363Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2364 return Location::RegisterLocation(kMethodRegisterArgument);
2365}
2366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002368 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002369 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002371 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kInt8:
2373 case DataType::Type::kUint16:
2374 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002375 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002376 uint32_t index = gp_index_++;
2377 stack_index_++;
2378 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002379 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002380 } else {
2381 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2382 }
2383 }
2384
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002386 uint32_t index = gp_index_;
2387 stack_index_ += 2;
2388 if (index < calling_convention.GetNumberOfRegisters()) {
2389 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002390 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002391 } else {
2392 gp_index_ += 2;
2393 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2394 }
2395 }
2396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002398 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002399 stack_index_++;
2400 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002401 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002402 } else {
2403 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2404 }
2405 }
2406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002408 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002409 stack_index_ += 2;
2410 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002411 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412 } else {
2413 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2414 }
2415 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002416
Aart Bik66c158e2018-01-31 12:55:04 -08002417 case DataType::Type::kUint32:
2418 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002419 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420 LOG(FATAL) << "Unexpected parameter type " << type;
2421 break;
2422 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002423 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002424}
2425
Calin Juravle175dc732015-08-25 15:42:32 +01002426void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2427 // The trampoline uses the same calling convention as dex calling conventions,
2428 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2429 // the method_idx.
2430 HandleInvoke(invoke);
2431}
2432
2433void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2434 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2435}
2436
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002437void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002438 // Explicit clinit checks triggered by static invokes must have been pruned by
2439 // art::PrepareForRegisterAllocation.
2440 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002441
Mark Mendellfb8d2792015-03-31 22:16:59 -04002442 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002443 if (intrinsic.TryDispatch(invoke)) {
2444 return;
2445 }
2446
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002447 HandleInvoke(invoke);
2448}
2449
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002450static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2451 if (invoke->GetLocations()->Intrinsified()) {
2452 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2453 intrinsic.Dispatch(invoke);
2454 return true;
2455 }
2456 return false;
2457}
2458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002459void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002460 // Explicit clinit checks triggered by static invokes must have been pruned by
2461 // art::PrepareForRegisterAllocation.
2462 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002463
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002464 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2465 return;
2466 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002467
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002468 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002469 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002470 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002471}
2472
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002473void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002474 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002475 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002476}
2477
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002478void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002479 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002480 if (intrinsic.TryDispatch(invoke)) {
2481 return;
2482 }
2483
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002484 HandleInvoke(invoke);
2485}
2486
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002487void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002488 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2489 return;
2490 }
2491
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002492 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002493 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002494}
2495
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002496void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2497 HandleInvoke(invoke);
2498 // Add the hidden argument.
2499 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2500}
2501
2502void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2503 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002504 LocationSummary* locations = invoke->GetLocations();
2505 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2506 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002507 Location receiver = locations->InAt(0);
2508 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2509
Roland Levillain0d5a2812015-11-13 10:07:31 +00002510 // Set the hidden argument. This is safe to do this here, as RAX
2511 // won't be modified thereafter, before the `call` instruction.
2512 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002513 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002514
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002515 if (receiver.IsStackSlot()) {
2516 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002517 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002518 __ movl(temp, Address(temp, class_offset));
2519 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002520 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002522 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002523 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002524 // Instead of simply (possibly) unpoisoning `temp` here, we should
2525 // emit a read barrier for the previous class reference load.
2526 // However this is not required in practice, as this is an
2527 // intermediate/temporary reference and because the current
2528 // concurrent copying collector keeps the from-space memory
2529 // intact/accessible until the end of the marking phase (the
2530 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002531 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002532 // temp = temp->GetAddressOfIMT()
2533 __ movq(temp,
2534 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2535 // temp = temp->GetImtEntryAt(method_offset);
2536 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002537 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002538 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002539 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002540 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002541 __ call(Address(
2542 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002543
2544 DCHECK(!codegen_->IsLeafMethod());
2545 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2546}
2547
Orion Hodsonac141392017-01-13 11:53:47 +00002548void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2549 HandleInvoke(invoke);
2550}
2551
2552void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2553 codegen_->GenerateInvokePolymorphicCall(invoke);
2554}
2555
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002556void LocationsBuilderX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2557 HandleInvoke(invoke);
2558}
2559
2560void InstructionCodeGeneratorX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2561 codegen_->GenerateInvokeCustomCall(invoke);
2562}
2563
Roland Levillain88cb1752014-10-20 16:36:47 +01002564void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2565 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002566 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002567 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002568 case DataType::Type::kInt32:
2569 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002570 locations->SetInAt(0, Location::RequiresRegister());
2571 locations->SetOut(Location::SameAsFirstInput());
2572 break;
2573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002574 case DataType::Type::kFloat32:
2575 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002576 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002577 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002578 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2583 }
2584}
2585
2586void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2587 LocationSummary* locations = neg->GetLocations();
2588 Location out = locations->Out();
2589 Location in = locations->InAt(0);
2590 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002592 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002593 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002595 break;
2596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002598 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002599 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002601 break;
2602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002604 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002605 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002606 // Implement float negation with an exclusive or with value
2607 // 0x80000000 (mask for bit 31, representing the sign of a
2608 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002609 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002611 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002612 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002615 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002616 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002617 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002618 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002619 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002620 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002622 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002623 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002624
2625 default:
2626 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2627 }
2628}
2629
Roland Levillaindff1f282014-11-05 14:15:05 +00002630void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2631 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002632 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002633 DataType::Type result_type = conversion->GetResultType();
2634 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002635 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2636 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002637
Roland Levillaindff1f282014-11-05 14:15:05 +00002638 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002639 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002641 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002643 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2644 locations->SetInAt(0, Location::Any());
2645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002646 break;
2647
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002649 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002651 locations->SetInAt(0, Location::Any());
2652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2653 break;
2654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002656 locations->SetInAt(0, Location::RequiresFpuRegister());
2657 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002658 break;
2659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002661 locations->SetInAt(0, Location::RequiresFpuRegister());
2662 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002663 break;
2664
2665 default:
2666 LOG(FATAL) << "Unexpected type conversion from " << input_type
2667 << " to " << result_type;
2668 }
2669 break;
2670
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002672 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002674 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002675 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002676 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 case DataType::Type::kInt16:
2678 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002679 // TODO: We would benefit from a (to-be-implemented)
2680 // Location::RegisterOrStackSlot requirement for this input.
2681 locations->SetInAt(0, Location::RequiresRegister());
2682 locations->SetOut(Location::RequiresRegister());
2683 break;
2684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002686 locations->SetInAt(0, Location::RequiresFpuRegister());
2687 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002688 break;
2689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002691 locations->SetInAt(0, Location::RequiresFpuRegister());
2692 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 break;
2694
2695 default:
2696 LOG(FATAL) << "Unexpected type conversion from " << input_type
2697 << " to " << result_type;
2698 }
2699 break;
2700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002702 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002704 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002706 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 case DataType::Type::kInt16:
2708 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002709 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002710 locations->SetOut(Location::RequiresFpuRegister());
2711 break;
2712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002713 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002714 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002715 locations->SetOut(Location::RequiresFpuRegister());
2716 break;
2717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002719 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002720 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002721 break;
2722
2723 default:
2724 LOG(FATAL) << "Unexpected type conversion from " << input_type
2725 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002726 }
Roland Levillaincff13742014-11-17 14:32:17 +00002727 break;
2728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002730 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002732 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002734 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 case DataType::Type::kInt16:
2736 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002737 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002738 locations->SetOut(Location::RequiresFpuRegister());
2739 break;
2740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002742 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002743 locations->SetOut(Location::RequiresFpuRegister());
2744 break;
2745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002747 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002748 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected type conversion from " << input_type
2753 << " to " << result_type;
2754 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002755 break;
2756
2757 default:
2758 LOG(FATAL) << "Unexpected type conversion from " << input_type
2759 << " to " << result_type;
2760 }
2761}
2762
2763void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2764 LocationSummary* locations = conversion->GetLocations();
2765 Location out = locations->Out();
2766 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002767 DataType::Type result_type = conversion->GetResultType();
2768 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002769 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2770 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002771 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002772 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002773 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002774 case DataType::Type::kInt8:
2775 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 case DataType::Type::kInt16:
2777 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002778 case DataType::Type::kInt64:
2779 if (in.IsRegister()) {
2780 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2781 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2782 __ movzxb(out.AsRegister<CpuRegister>(),
2783 Address(CpuRegister(RSP), in.GetStackIndex()));
2784 } else {
2785 __ movl(out.AsRegister<CpuRegister>(),
2786 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2787 }
2788 break;
2789
2790 default:
2791 LOG(FATAL) << "Unexpected type conversion from " << input_type
2792 << " to " << result_type;
2793 }
2794 break;
2795
2796 case DataType::Type::kInt8:
2797 switch (input_type) {
2798 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002800 case DataType::Type::kInt16:
2801 case DataType::Type::kInt32:
2802 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002803 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002804 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002805 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002807 Address(CpuRegister(RSP), in.GetStackIndex()));
2808 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002810 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002811 }
2812 break;
2813
2814 default:
2815 LOG(FATAL) << "Unexpected type conversion from " << input_type
2816 << " to " << result_type;
2817 }
2818 break;
2819
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002820 case DataType::Type::kUint16:
2821 switch (input_type) {
2822 case DataType::Type::kInt8:
2823 case DataType::Type::kInt16:
2824 case DataType::Type::kInt32:
2825 case DataType::Type::kInt64:
2826 if (in.IsRegister()) {
2827 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2828 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2829 __ movzxw(out.AsRegister<CpuRegister>(),
2830 Address(CpuRegister(RSP), in.GetStackIndex()));
2831 } else {
2832 __ movl(out.AsRegister<CpuRegister>(),
2833 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2834 }
2835 break;
2836
2837 default:
2838 LOG(FATAL) << "Unexpected type conversion from " << input_type
2839 << " to " << result_type;
2840 }
2841 break;
2842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002844 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002846 case DataType::Type::kInt32:
2847 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002848 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002850 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002851 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002852 Address(CpuRegister(RSP), in.GetStackIndex()));
2853 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002855 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002856 }
2857 break;
2858
2859 default:
2860 LOG(FATAL) << "Unexpected type conversion from " << input_type
2861 << " to " << result_type;
2862 }
2863 break;
2864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002866 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002868 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002869 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002870 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002872 Address(CpuRegister(RSP), in.GetStackIndex()));
2873 } else {
2874 DCHECK(in.IsConstant());
2875 DCHECK(in.GetConstant()->IsLongConstant());
2876 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002878 }
2879 break;
2880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002881 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002882 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2883 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002884 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002885
2886 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002887 // if input >= (float)INT_MAX goto done
2888 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002889 __ j(kAboveEqual, &done);
2890 // if input == NaN goto nan
2891 __ j(kUnordered, &nan);
2892 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002893 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002894 __ jmp(&done);
2895 __ Bind(&nan);
2896 // output = 0
2897 __ xorl(output, output);
2898 __ Bind(&done);
2899 break;
2900 }
2901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002903 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2904 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002905 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002906
2907 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002908 // if input >= (double)INT_MAX goto done
2909 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002910 __ j(kAboveEqual, &done);
2911 // if input == NaN goto nan
2912 __ j(kUnordered, &nan);
2913 // output = double-to-int-truncate(input)
2914 __ cvttsd2si(output, input);
2915 __ jmp(&done);
2916 __ Bind(&nan);
2917 // output = 0
2918 __ xorl(output, output);
2919 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002920 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002921 }
Roland Levillain946e1432014-11-11 17:35:19 +00002922
2923 default:
2924 LOG(FATAL) << "Unexpected type conversion from " << input_type
2925 << " to " << result_type;
2926 }
2927 break;
2928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002930 switch (input_type) {
2931 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002933 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002935 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt16:
2937 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002938 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002940 break;
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002943 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2944 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002945 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002946
Mark Mendell92e83bf2015-05-07 11:25:03 -04002947 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002948 // if input >= (float)LONG_MAX goto done
2949 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002950 __ j(kAboveEqual, &done);
2951 // if input == NaN goto nan
2952 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002953 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002954 __ cvttss2si(output, input, true);
2955 __ jmp(&done);
2956 __ Bind(&nan);
2957 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002958 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002959 __ Bind(&done);
2960 break;
2961 }
2962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002964 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2965 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002966 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002967
Mark Mendell92e83bf2015-05-07 11:25:03 -04002968 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002969 // if input >= (double)LONG_MAX goto done
2970 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002971 __ j(kAboveEqual, &done);
2972 // if input == NaN goto nan
2973 __ j(kUnordered, &nan);
2974 // output = double-to-long-truncate(input)
2975 __ cvttsd2si(output, input, true);
2976 __ jmp(&done);
2977 __ Bind(&nan);
2978 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002979 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002980 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002981 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002982 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002983
2984 default:
2985 LOG(FATAL) << "Unexpected type conversion from " << input_type
2986 << " to " << result_type;
2987 }
2988 break;
2989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002991 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002993 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002995 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002996 case DataType::Type::kInt16:
2997 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002998 if (in.IsRegister()) {
2999 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3000 } else if (in.IsConstant()) {
3001 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3002 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003003 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003004 } else {
3005 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3006 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3007 }
Roland Levillaincff13742014-11-17 14:32:17 +00003008 break;
3009
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003011 if (in.IsRegister()) {
3012 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3013 } else if (in.IsConstant()) {
3014 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3015 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06003016 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003017 } else {
3018 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3019 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3020 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003021 break;
3022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003023 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04003024 if (in.IsFpuRegister()) {
3025 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3026 } else if (in.IsConstant()) {
3027 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
3028 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003029 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003030 } else {
3031 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
3032 Address(CpuRegister(RSP), in.GetStackIndex()));
3033 }
Roland Levillaincff13742014-11-17 14:32:17 +00003034 break;
3035
3036 default:
3037 LOG(FATAL) << "Unexpected type conversion from " << input_type
3038 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003039 }
Roland Levillaincff13742014-11-17 14:32:17 +00003040 break;
3041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003042 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003043 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003045 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003047 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003048 case DataType::Type::kInt16:
3049 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003050 if (in.IsRegister()) {
3051 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3052 } else if (in.IsConstant()) {
3053 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3054 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003055 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003056 } else {
3057 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3058 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3059 }
Roland Levillaincff13742014-11-17 14:32:17 +00003060 break;
3061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003063 if (in.IsRegister()) {
3064 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3065 } else if (in.IsConstant()) {
3066 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3067 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003068 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003069 } else {
3070 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3071 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3072 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003073 break;
3074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003076 if (in.IsFpuRegister()) {
3077 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3078 } else if (in.IsConstant()) {
3079 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3080 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003081 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003082 } else {
3083 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3084 Address(CpuRegister(RSP), in.GetStackIndex()));
3085 }
Roland Levillaincff13742014-11-17 14:32:17 +00003086 break;
3087
3088 default:
3089 LOG(FATAL) << "Unexpected type conversion from " << input_type
3090 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003091 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003092 break;
3093
3094 default:
3095 LOG(FATAL) << "Unexpected type conversion from " << input_type
3096 << " to " << result_type;
3097 }
3098}
3099
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003100void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003101 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003102 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003103 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003104 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003105 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003106 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3107 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003108 break;
3109 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003112 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003113 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003114 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003116 break;
3117 }
3118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003119 case DataType::Type::kFloat64:
3120 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003121 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003122 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003123 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003124 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003125 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003126
3127 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003128 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003130}
3131
3132void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3133 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003134 Location first = locations->InAt(0);
3135 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003136 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003137
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003138 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003140 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003141 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3142 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003143 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3144 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003145 } else {
3146 __ leal(out.AsRegister<CpuRegister>(), Address(
3147 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3148 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003149 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003150 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3151 __ addl(out.AsRegister<CpuRegister>(),
3152 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3153 } else {
3154 __ leal(out.AsRegister<CpuRegister>(), Address(
3155 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3156 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003157 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003158 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003159 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003160 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003161 break;
3162 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003165 if (second.IsRegister()) {
3166 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3167 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003168 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3169 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003170 } else {
3171 __ leaq(out.AsRegister<CpuRegister>(), Address(
3172 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3173 }
3174 } else {
3175 DCHECK(second.IsConstant());
3176 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3177 int32_t int32_value = Low32Bits(value);
3178 DCHECK_EQ(int32_value, value);
3179 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3180 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3181 } else {
3182 __ leaq(out.AsRegister<CpuRegister>(), Address(
3183 first.AsRegister<CpuRegister>(), int32_value));
3184 }
3185 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003186 break;
3187 }
3188
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003189 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003190 if (second.IsFpuRegister()) {
3191 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3192 } else if (second.IsConstant()) {
3193 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003194 codegen_->LiteralFloatAddress(
3195 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003196 } else {
3197 DCHECK(second.IsStackSlot());
3198 __ addss(first.AsFpuRegister<XmmRegister>(),
3199 Address(CpuRegister(RSP), second.GetStackIndex()));
3200 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003201 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003202 }
3203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003205 if (second.IsFpuRegister()) {
3206 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3207 } else if (second.IsConstant()) {
3208 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003209 codegen_->LiteralDoubleAddress(
3210 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003211 } else {
3212 DCHECK(second.IsDoubleStackSlot());
3213 __ addsd(first.AsFpuRegister<XmmRegister>(),
3214 Address(CpuRegister(RSP), second.GetStackIndex()));
3215 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003216 break;
3217 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003218
3219 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003220 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003221 }
3222}
3223
3224void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003225 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003226 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003227 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003229 locations->SetInAt(0, Location::RequiresRegister());
3230 locations->SetInAt(1, Location::Any());
3231 locations->SetOut(Location::SameAsFirstInput());
3232 break;
3233 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003234 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003235 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003236 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003237 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003238 break;
3239 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 case DataType::Type::kFloat32:
3241 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003242 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003243 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003244 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003245 break;
Calin Juravle11351682014-10-23 15:38:15 +01003246 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003247 default:
Calin Juravle11351682014-10-23 15:38:15 +01003248 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003249 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003250}
3251
3252void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3253 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003254 Location first = locations->InAt(0);
3255 Location second = locations->InAt(1);
3256 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003257 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003258 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003259 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003260 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003261 } else if (second.IsConstant()) {
3262 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003264 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003266 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003267 break;
3268 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003269 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003270 if (second.IsConstant()) {
3271 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3272 DCHECK(IsInt<32>(value));
3273 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3274 } else {
3275 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3276 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003277 break;
3278 }
3279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003281 if (second.IsFpuRegister()) {
3282 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3283 } else if (second.IsConstant()) {
3284 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003285 codegen_->LiteralFloatAddress(
3286 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003287 } else {
3288 DCHECK(second.IsStackSlot());
3289 __ subss(first.AsFpuRegister<XmmRegister>(),
3290 Address(CpuRegister(RSP), second.GetStackIndex()));
3291 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003292 break;
Calin Juravle11351682014-10-23 15:38:15 +01003293 }
3294
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003295 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003296 if (second.IsFpuRegister()) {
3297 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3298 } else if (second.IsConstant()) {
3299 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003300 codegen_->LiteralDoubleAddress(
3301 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003302 } else {
3303 DCHECK(second.IsDoubleStackSlot());
3304 __ subsd(first.AsFpuRegister<XmmRegister>(),
3305 Address(CpuRegister(RSP), second.GetStackIndex()));
3306 }
Calin Juravle11351682014-10-23 15:38:15 +01003307 break;
3308 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003309
3310 default:
Calin Juravle11351682014-10-23 15:38:15 +01003311 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003312 }
3313}
3314
Calin Juravle34bacdf2014-10-07 20:23:36 +01003315void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003317 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003318 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003319 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003322 if (mul->InputAt(1)->IsIntConstant()) {
3323 // Can use 3 operand multiply.
3324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3325 } else {
3326 locations->SetOut(Location::SameAsFirstInput());
3327 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003328 break;
3329 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003330 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003331 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003332 locations->SetInAt(1, Location::Any());
3333 if (mul->InputAt(1)->IsLongConstant() &&
3334 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003335 // Can use 3 operand multiply.
3336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3337 } else {
3338 locations->SetOut(Location::SameAsFirstInput());
3339 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003340 break;
3341 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 case DataType::Type::kFloat32:
3343 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003344 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003345 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003346 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003347 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003348 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003349
3350 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003351 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352 }
3353}
3354
3355void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3356 LocationSummary* locations = mul->GetLocations();
3357 Location first = locations->InAt(0);
3358 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003359 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003360 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003361 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003362 // The constant may have ended up in a register, so test explicitly to avoid
3363 // problems where the output may not be the same as the first operand.
3364 if (mul->InputAt(1)->IsIntConstant()) {
3365 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3366 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3367 } else if (second.IsRegister()) {
3368 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003369 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003370 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003371 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003372 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003373 __ imull(first.AsRegister<CpuRegister>(),
3374 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375 }
3376 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003377 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003378 // The constant may have ended up in a register, so test explicitly to avoid
3379 // problems where the output may not be the same as the first operand.
3380 if (mul->InputAt(1)->IsLongConstant()) {
3381 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3382 if (IsInt<32>(value)) {
3383 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3384 Immediate(static_cast<int32_t>(value)));
3385 } else {
3386 // Have to use the constant area.
3387 DCHECK(first.Equals(out));
3388 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3389 }
3390 } else if (second.IsRegister()) {
3391 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003392 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003393 } else {
3394 DCHECK(second.IsDoubleStackSlot());
3395 DCHECK(first.Equals(out));
3396 __ imulq(first.AsRegister<CpuRegister>(),
3397 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003398 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003399 break;
3400 }
3401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003403 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003404 if (second.IsFpuRegister()) {
3405 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3406 } else if (second.IsConstant()) {
3407 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003408 codegen_->LiteralFloatAddress(
3409 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003410 } else {
3411 DCHECK(second.IsStackSlot());
3412 __ mulss(first.AsFpuRegister<XmmRegister>(),
3413 Address(CpuRegister(RSP), second.GetStackIndex()));
3414 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003415 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003416 }
3417
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003418 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003419 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003420 if (second.IsFpuRegister()) {
3421 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3422 } else if (second.IsConstant()) {
3423 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003424 codegen_->LiteralDoubleAddress(
3425 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003426 } else {
3427 DCHECK(second.IsDoubleStackSlot());
3428 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3429 Address(CpuRegister(RSP), second.GetStackIndex()));
3430 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003431 break;
3432 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003433
3434 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003435 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003436 }
3437}
3438
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003439void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3440 uint32_t stack_adjustment, bool is_float) {
3441 if (source.IsStackSlot()) {
3442 DCHECK(is_float);
3443 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3444 } else if (source.IsDoubleStackSlot()) {
3445 DCHECK(!is_float);
3446 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3447 } else {
3448 // Write the value to the temporary location on the stack and load to FP stack.
3449 if (is_float) {
3450 Location stack_temp = Location::StackSlot(temp_offset);
3451 codegen_->Move(stack_temp, source);
3452 __ flds(Address(CpuRegister(RSP), temp_offset));
3453 } else {
3454 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3455 codegen_->Move(stack_temp, source);
3456 __ fldl(Address(CpuRegister(RSP), temp_offset));
3457 }
3458 }
3459}
3460
3461void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 DataType::Type type = rem->GetResultType();
3463 bool is_float = type == DataType::Type::kFloat32;
3464 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003465 LocationSummary* locations = rem->GetLocations();
3466 Location first = locations->InAt(0);
3467 Location second = locations->InAt(1);
3468 Location out = locations->Out();
3469
3470 // Create stack space for 2 elements.
3471 // TODO: enhance register allocator to ask for stack temporaries.
3472 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3473
3474 // Load the values to the FP stack in reverse order, using temporaries if needed.
3475 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3476 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3477
3478 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003479 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003480 __ Bind(&retry);
3481 __ fprem();
3482
3483 // Move FP status to AX.
3484 __ fstsw();
3485
3486 // And see if the argument reduction is complete. This is signaled by the
3487 // C2 FPU flag bit set to 0.
3488 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3489 __ j(kNotEqual, &retry);
3490
3491 // We have settled on the final value. Retrieve it into an XMM register.
3492 // Store FP top of stack to real stack.
3493 if (is_float) {
3494 __ fsts(Address(CpuRegister(RSP), 0));
3495 } else {
3496 __ fstl(Address(CpuRegister(RSP), 0));
3497 }
3498
3499 // Pop the 2 items from the FP stack.
3500 __ fucompp();
3501
3502 // Load the value from the stack into an XMM register.
3503 DCHECK(out.IsFpuRegister()) << out;
3504 if (is_float) {
3505 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3506 } else {
3507 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3508 }
3509
3510 // And remove the temporary stack space we allocated.
3511 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3512}
3513
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003514void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3515 DCHECK(instruction->IsDiv() || instruction->IsRem());
3516
3517 LocationSummary* locations = instruction->GetLocations();
3518 Location second = locations->InAt(1);
3519 DCHECK(second.IsConstant());
3520
3521 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3522 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003523 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003524
3525 DCHECK(imm == 1 || imm == -1);
3526
3527 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003529 if (instruction->IsRem()) {
3530 __ xorl(output_register, output_register);
3531 } else {
3532 __ movl(output_register, input_register);
3533 if (imm == -1) {
3534 __ negl(output_register);
3535 }
3536 }
3537 break;
3538 }
3539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003542 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003543 } else {
3544 __ movq(output_register, input_register);
3545 if (imm == -1) {
3546 __ negq(output_register);
3547 }
3548 }
3549 break;
3550 }
3551
3552 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003553 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 }
3555}
3556
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003557void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003558 LocationSummary* locations = instruction->GetLocations();
3559 Location second = locations->InAt(1);
3560
3561 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3562 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3563
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003564 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003565 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3566 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003567
3568 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003570 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003571 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572 __ testl(numerator, numerator);
3573 __ cmov(kGreaterEqual, tmp, numerator);
3574 int shift = CTZ(imm);
3575 __ sarl(tmp, Immediate(shift));
3576
3577 if (imm < 0) {
3578 __ negl(tmp);
3579 }
3580
3581 __ movl(output_register, tmp);
3582 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003583 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3585
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003586 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003587 __ addq(rdx, numerator);
3588 __ testq(numerator, numerator);
3589 __ cmov(kGreaterEqual, rdx, numerator);
3590 int shift = CTZ(imm);
3591 __ sarq(rdx, Immediate(shift));
3592
3593 if (imm < 0) {
3594 __ negq(rdx);
3595 }
3596
3597 __ movq(output_register, rdx);
3598 }
3599}
3600
3601void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3602 DCHECK(instruction->IsDiv() || instruction->IsRem());
3603
3604 LocationSummary* locations = instruction->GetLocations();
3605 Location second = locations->InAt(1);
3606
3607 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3608 : locations->GetTemp(0).AsRegister<CpuRegister>();
3609 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3610 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3611 : locations->Out().AsRegister<CpuRegister>();
3612 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3613
3614 DCHECK_EQ(RAX, eax.AsRegister());
3615 DCHECK_EQ(RDX, edx.AsRegister());
3616 if (instruction->IsDiv()) {
3617 DCHECK_EQ(RAX, out.AsRegister());
3618 } else {
3619 DCHECK_EQ(RDX, out.AsRegister());
3620 }
3621
3622 int64_t magic;
3623 int shift;
3624
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003625 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003626 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003627 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3628
3629 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3630
3631 __ movl(numerator, eax);
3632
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003633 __ movl(eax, Immediate(magic));
3634 __ imull(numerator);
3635
3636 if (imm > 0 && magic < 0) {
3637 __ addl(edx, numerator);
3638 } else if (imm < 0 && magic > 0) {
3639 __ subl(edx, numerator);
3640 }
3641
3642 if (shift != 0) {
3643 __ sarl(edx, Immediate(shift));
3644 }
3645
3646 __ movl(eax, edx);
3647 __ shrl(edx, Immediate(31));
3648 __ addl(edx, eax);
3649
3650 if (instruction->IsRem()) {
3651 __ movl(eax, numerator);
3652 __ imull(edx, Immediate(imm));
3653 __ subl(eax, edx);
3654 __ movl(edx, eax);
3655 } else {
3656 __ movl(eax, edx);
3657 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003658 } else {
3659 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003662
3663 CpuRegister rax = eax;
3664 CpuRegister rdx = edx;
3665
3666 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3667
3668 // Save the numerator.
3669 __ movq(numerator, rax);
3670
3671 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003672 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003673
3674 // RDX:RAX = magic * numerator
3675 __ imulq(numerator);
3676
3677 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003678 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003679 __ addq(rdx, numerator);
3680 } else if (imm < 0 && magic > 0) {
3681 // RDX -= numerator
3682 __ subq(rdx, numerator);
3683 }
3684
3685 // Shift if needed.
3686 if (shift != 0) {
3687 __ sarq(rdx, Immediate(shift));
3688 }
3689
3690 // RDX += 1 if RDX < 0
3691 __ movq(rax, rdx);
3692 __ shrq(rdx, Immediate(63));
3693 __ addq(rdx, rax);
3694
3695 if (instruction->IsRem()) {
3696 __ movq(rax, numerator);
3697
3698 if (IsInt<32>(imm)) {
3699 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3700 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003701 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003702 }
3703
3704 __ subq(rax, rdx);
3705 __ movq(rdx, rax);
3706 } else {
3707 __ movq(rax, rdx);
3708 }
3709 }
3710}
3711
Calin Juravlebacfec32014-11-14 15:54:36 +00003712void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3713 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 DataType::Type type = instruction->GetResultType();
3715 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003716
3717 bool is_div = instruction->IsDiv();
3718 LocationSummary* locations = instruction->GetLocations();
3719
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003720 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3721 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003722
Roland Levillain271ab9c2014-11-27 15:23:57 +00003723 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003724 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003725
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003726 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003727 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003728
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003729 if (imm == 0) {
3730 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3731 } else if (imm == 1 || imm == -1) {
3732 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003733 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003734 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003735 } else {
3736 DCHECK(imm <= -2 || imm >= 2);
3737 GenerateDivRemWithAnyConstant(instruction);
3738 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003739 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003740 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003741 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003742 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003743 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003744
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003745 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3746 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3747 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3748 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003749 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003750 __ cmpl(second_reg, Immediate(-1));
3751 __ j(kEqual, slow_path->GetEntryLabel());
3752 // edx:eax <- sign-extended of eax
3753 __ cdq();
3754 // eax = quotient, edx = remainder
3755 __ idivl(second_reg);
3756 } else {
3757 __ cmpq(second_reg, Immediate(-1));
3758 __ j(kEqual, slow_path->GetEntryLabel());
3759 // rdx:rax <- sign-extended of rax
3760 __ cqo();
3761 // rax = quotient, rdx = remainder
3762 __ idivq(second_reg);
3763 }
3764 __ Bind(slow_path->GetExitLabel());
3765 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003766}
3767
Calin Juravle7c4954d2014-10-28 16:57:40 +00003768void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3769 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003770 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003771 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003772 case DataType::Type::kInt32:
3773 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003774 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003775 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003776 locations->SetOut(Location::SameAsFirstInput());
3777 // Intel uses edx:eax as the dividend.
3778 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3780 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3781 // output and request another temp.
3782 if (div->InputAt(1)->IsConstant()) {
3783 locations->AddTemp(Location::RequiresRegister());
3784 }
Calin Juravled0d48522014-11-04 16:40:20 +00003785 break;
3786 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003788 case DataType::Type::kFloat32:
3789 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003790 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003791 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003792 locations->SetOut(Location::SameAsFirstInput());
3793 break;
3794 }
3795
3796 default:
3797 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3798 }
3799}
3800
3801void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3802 LocationSummary* locations = div->GetLocations();
3803 Location first = locations->InAt(0);
3804 Location second = locations->InAt(1);
3805 DCHECK(first.Equals(locations->Out()));
3806
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003808 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt32:
3810 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003811 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003812 break;
3813 }
3814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003816 if (second.IsFpuRegister()) {
3817 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3818 } else if (second.IsConstant()) {
3819 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003820 codegen_->LiteralFloatAddress(
3821 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003822 } else {
3823 DCHECK(second.IsStackSlot());
3824 __ divss(first.AsFpuRegister<XmmRegister>(),
3825 Address(CpuRegister(RSP), second.GetStackIndex()));
3826 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003827 break;
3828 }
3829
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003831 if (second.IsFpuRegister()) {
3832 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3833 } else if (second.IsConstant()) {
3834 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003835 codegen_->LiteralDoubleAddress(
3836 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003837 } else {
3838 DCHECK(second.IsDoubleStackSlot());
3839 __ divsd(first.AsFpuRegister<XmmRegister>(),
3840 Address(CpuRegister(RSP), second.GetStackIndex()));
3841 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003842 break;
3843 }
3844
3845 default:
3846 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3847 }
3848}
3849
Calin Juravlebacfec32014-11-14 15:54:36 +00003850void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003852 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003853 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003854
3855 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003856 case DataType::Type::kInt32:
3857 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003858 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003859 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003860 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3861 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003862 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3863 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3864 // output and request another temp.
3865 if (rem->InputAt(1)->IsConstant()) {
3866 locations->AddTemp(Location::RequiresRegister());
3867 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003868 break;
3869 }
3870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003871 case DataType::Type::kFloat32:
3872 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003873 locations->SetInAt(0, Location::Any());
3874 locations->SetInAt(1, Location::Any());
3875 locations->SetOut(Location::RequiresFpuRegister());
3876 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003877 break;
3878 }
3879
3880 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003881 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003882 }
3883}
3884
3885void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003887 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003888 case DataType::Type::kInt32:
3889 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003890 GenerateDivRemIntegral(rem);
3891 break;
3892 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003893 case DataType::Type::kFloat32:
3894 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003895 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003896 break;
3897 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003898 default:
3899 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3900 }
3901}
3902
Aart Bik1f8d51b2018-02-15 10:42:37 -08003903static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3904 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3905 switch (minmax->GetResultType()) {
3906 case DataType::Type::kInt32:
3907 case DataType::Type::kInt64:
3908 locations->SetInAt(0, Location::RequiresRegister());
3909 locations->SetInAt(1, Location::RequiresRegister());
3910 locations->SetOut(Location::SameAsFirstInput());
3911 break;
3912 case DataType::Type::kFloat32:
3913 case DataType::Type::kFloat64:
3914 locations->SetInAt(0, Location::RequiresFpuRegister());
3915 locations->SetInAt(1, Location::RequiresFpuRegister());
3916 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
3917 // the second input to be the output (we can simply swap inputs).
3918 locations->SetOut(Location::SameAsFirstInput());
3919 break;
3920 default:
3921 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3922 }
3923}
3924
Aart Bik351df3e2018-03-07 11:54:57 -08003925void InstructionCodeGeneratorX86_64::GenerateMinMaxInt(LocationSummary* locations,
3926 bool is_min,
3927 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003928 Location op1_loc = locations->InAt(0);
3929 Location op2_loc = locations->InAt(1);
3930
3931 // Shortcut for same input locations.
3932 if (op1_loc.Equals(op2_loc)) {
3933 // Can return immediately, as op1_loc == out_loc.
3934 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3935 // a copy here.
3936 DCHECK(locations->Out().Equals(op1_loc));
3937 return;
3938 }
3939
3940 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3941 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
3942
3943 // (out := op1)
3944 // out <=? op2
3945 // if out is min jmp done
3946 // out := op2
3947 // done:
3948
3949 if (type == DataType::Type::kInt64) {
3950 __ cmpq(out, op2);
3951 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ true);
3952 } else {
3953 DCHECK_EQ(type, DataType::Type::kInt32);
3954 __ cmpl(out, op2);
3955 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ false);
3956 }
3957}
3958
3959void InstructionCodeGeneratorX86_64::GenerateMinMaxFP(LocationSummary* locations,
3960 bool is_min,
3961 DataType::Type type) {
3962 Location op1_loc = locations->InAt(0);
3963 Location op2_loc = locations->InAt(1);
3964 Location out_loc = locations->Out();
3965 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3966
3967 // Shortcut for same input locations.
3968 if (op1_loc.Equals(op2_loc)) {
3969 DCHECK(out_loc.Equals(op1_loc));
3970 return;
3971 }
3972
3973 // (out := op1)
3974 // out <=? op2
3975 // if Nan jmp Nan_label
3976 // if out is min jmp done
3977 // if op2 is min jmp op2_label
3978 // handle -0/+0
3979 // jmp done
3980 // Nan_label:
3981 // out := NaN
3982 // op2_label:
3983 // out := op2
3984 // done:
3985 //
3986 // This removes one jmp, but needs to copy one input (op1) to out.
3987 //
3988 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
3989
3990 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3991
3992 NearLabel nan, done, op2_label;
3993 if (type == DataType::Type::kFloat64) {
3994 __ ucomisd(out, op2);
3995 } else {
3996 DCHECK_EQ(type, DataType::Type::kFloat32);
3997 __ ucomiss(out, op2);
3998 }
3999
4000 __ j(Condition::kParityEven, &nan);
4001
4002 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4003 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4004
4005 // Handle 0.0/-0.0.
4006 if (is_min) {
4007 if (type == DataType::Type::kFloat64) {
4008 __ orpd(out, op2);
4009 } else {
4010 __ orps(out, op2);
4011 }
4012 } else {
4013 if (type == DataType::Type::kFloat64) {
4014 __ andpd(out, op2);
4015 } else {
4016 __ andps(out, op2);
4017 }
4018 }
4019 __ jmp(&done);
4020
4021 // NaN handling.
4022 __ Bind(&nan);
4023 if (type == DataType::Type::kFloat64) {
4024 __ movsd(out, codegen_->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
4025 } else {
4026 __ movss(out, codegen_->LiteralInt32Address(INT32_C(0x7FC00000)));
4027 }
4028 __ jmp(&done);
4029
4030 // out := op2;
4031 __ Bind(&op2_label);
4032 if (type == DataType::Type::kFloat64) {
4033 __ movsd(out, op2);
4034 } else {
4035 __ movss(out, op2);
4036 }
4037
4038 // Done.
4039 __ Bind(&done);
4040}
4041
Aart Bik351df3e2018-03-07 11:54:57 -08004042void InstructionCodeGeneratorX86_64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4043 DataType::Type type = minmax->GetResultType();
4044 switch (type) {
4045 case DataType::Type::kInt32:
4046 case DataType::Type::kInt64:
4047 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4048 break;
4049 case DataType::Type::kFloat32:
4050 case DataType::Type::kFloat64:
4051 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4052 break;
4053 default:
4054 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4055 }
4056}
4057
Aart Bik1f8d51b2018-02-15 10:42:37 -08004058void LocationsBuilderX86_64::VisitMin(HMin* min) {
4059 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4060}
4061
4062void InstructionCodeGeneratorX86_64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004063 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004064}
4065
4066void LocationsBuilderX86_64::VisitMax(HMax* max) {
4067 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4068}
4069
4070void InstructionCodeGeneratorX86_64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004071 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004072}
4073
Aart Bik3dad3412018-02-28 12:01:46 -08004074void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
4075 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4076 switch (abs->GetResultType()) {
4077 case DataType::Type::kInt32:
4078 case DataType::Type::kInt64:
4079 locations->SetInAt(0, Location::RequiresRegister());
4080 locations->SetOut(Location::SameAsFirstInput());
4081 locations->AddTemp(Location::RequiresRegister());
4082 break;
4083 case DataType::Type::kFloat32:
4084 case DataType::Type::kFloat64:
4085 locations->SetInAt(0, Location::RequiresFpuRegister());
4086 locations->SetOut(Location::SameAsFirstInput());
4087 locations->AddTemp(Location::RequiresFpuRegister());
4088 break;
4089 default:
4090 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4091 }
4092}
4093
4094void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
4095 LocationSummary* locations = abs->GetLocations();
4096 switch (abs->GetResultType()) {
4097 case DataType::Type::kInt32: {
4098 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4099 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4100 // Create mask.
4101 __ movl(mask, out);
4102 __ sarl(mask, Immediate(31));
4103 // Add mask.
4104 __ addl(out, mask);
4105 __ xorl(out, mask);
4106 break;
4107 }
4108 case DataType::Type::kInt64: {
4109 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4110 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4111 // Create mask.
4112 __ movq(mask, out);
4113 __ sarq(mask, Immediate(63));
4114 // Add mask.
4115 __ addq(out, mask);
4116 __ xorq(out, mask);
4117 break;
4118 }
4119 case DataType::Type::kFloat32: {
4120 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4121 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4122 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
4123 __ andps(out, mask);
4124 break;
4125 }
4126 case DataType::Type::kFloat64: {
4127 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4128 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4129 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
4130 __ andpd(out, mask);
4131 break;
4132 }
4133 default:
4134 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4135 }
4136}
4137
Calin Juravled0d48522014-11-04 16:40:20 +00004138void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004139 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004140 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00004141}
4142
4143void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004144 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004145 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004146 codegen_->AddSlowPath(slow_path);
4147
4148 LocationSummary* locations = instruction->GetLocations();
4149 Location value = locations->InAt(0);
4150
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004151 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004153 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004154 case DataType::Type::kInt8:
4155 case DataType::Type::kUint16:
4156 case DataType::Type::kInt16:
4157 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004158 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004159 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004160 __ j(kEqual, slow_path->GetEntryLabel());
4161 } else if (value.IsStackSlot()) {
4162 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4163 __ j(kEqual, slow_path->GetEntryLabel());
4164 } else {
4165 DCHECK(value.IsConstant()) << value;
4166 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004167 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004168 }
4169 }
4170 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004171 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004172 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004173 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004175 __ j(kEqual, slow_path->GetEntryLabel());
4176 } else if (value.IsDoubleStackSlot()) {
4177 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4178 __ j(kEqual, slow_path->GetEntryLabel());
4179 } else {
4180 DCHECK(value.IsConstant()) << value;
4181 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004182 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004183 }
4184 }
4185 break;
4186 }
4187 default:
4188 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004189 }
Calin Juravled0d48522014-11-04 16:40:20 +00004190}
4191
Calin Juravle9aec02f2014-11-18 23:06:35 +00004192void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
4193 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4194
4195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004196 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004197
4198 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kInt32:
4200 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004201 locations->SetInAt(0, Location::RequiresRegister());
4202 // The shift count needs to be in CL.
4203 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
4204 locations->SetOut(Location::SameAsFirstInput());
4205 break;
4206 }
4207 default:
4208 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4209 }
4210}
4211
4212void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
4213 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4214
4215 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004216 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004217 Location second = locations->InAt(1);
4218
4219 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004220 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004221 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004222 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004223 if (op->IsShl()) {
4224 __ shll(first_reg, second_reg);
4225 } else if (op->IsShr()) {
4226 __ sarl(first_reg, second_reg);
4227 } else {
4228 __ shrl(first_reg, second_reg);
4229 }
4230 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004231 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004232 if (op->IsShl()) {
4233 __ shll(first_reg, imm);
4234 } else if (op->IsShr()) {
4235 __ sarl(first_reg, imm);
4236 } else {
4237 __ shrl(first_reg, imm);
4238 }
4239 }
4240 break;
4241 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004242 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004243 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004244 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004245 if (op->IsShl()) {
4246 __ shlq(first_reg, second_reg);
4247 } else if (op->IsShr()) {
4248 __ sarq(first_reg, second_reg);
4249 } else {
4250 __ shrq(first_reg, second_reg);
4251 }
4252 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004253 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004254 if (op->IsShl()) {
4255 __ shlq(first_reg, imm);
4256 } else if (op->IsShr()) {
4257 __ sarq(first_reg, imm);
4258 } else {
4259 __ shrq(first_reg, imm);
4260 }
4261 }
4262 break;
4263 }
4264 default:
4265 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004266 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004267 }
4268}
4269
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004270void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4271 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004272 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004273
4274 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004275 case DataType::Type::kInt32:
4276 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004277 locations->SetInAt(0, Location::RequiresRegister());
4278 // The shift count needs to be in CL (unless it is a constant).
4279 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4280 locations->SetOut(Location::SameAsFirstInput());
4281 break;
4282 }
4283 default:
4284 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4285 UNREACHABLE();
4286 }
4287}
4288
4289void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4290 LocationSummary* locations = ror->GetLocations();
4291 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4292 Location second = locations->InAt(1);
4293
4294 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004296 if (second.IsRegister()) {
4297 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4298 __ rorl(first_reg, second_reg);
4299 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004300 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004301 __ rorl(first_reg, imm);
4302 }
4303 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004304 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004305 if (second.IsRegister()) {
4306 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4307 __ rorq(first_reg, second_reg);
4308 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004309 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004310 __ rorq(first_reg, imm);
4311 }
4312 break;
4313 default:
4314 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4315 UNREACHABLE();
4316 }
4317}
4318
Calin Juravle9aec02f2014-11-18 23:06:35 +00004319void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4320 HandleShift(shl);
4321}
4322
4323void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4324 HandleShift(shl);
4325}
4326
4327void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4328 HandleShift(shr);
4329}
4330
4331void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4332 HandleShift(shr);
4333}
4334
4335void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4336 HandleShift(ushr);
4337}
4338
4339void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4340 HandleShift(ushr);
4341}
4342
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004343void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004344 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4345 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004346 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07004347 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004348 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004349}
4350
4351void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004352 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4353 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4354 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004355}
4356
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004357void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004358 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4359 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004360 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004361 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004362 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4363 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004364}
4365
4366void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004367 // Note: if heap poisoning is enabled, the entry point takes cares
4368 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004369 QuickEntrypointEnum entrypoint =
4370 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4371 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004372 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004373 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004374}
4375
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004376void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004377 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004378 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004379 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4380 if (location.IsStackSlot()) {
4381 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4382 } else if (location.IsDoubleStackSlot()) {
4383 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4384 }
4385 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004386}
4387
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004388void InstructionCodeGeneratorX86_64::VisitParameterValue(
4389 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004390 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004391}
4392
4393void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4394 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004395 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004396 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4397}
4398
4399void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4400 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4401 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004402}
4403
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004404void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4405 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004406 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004407 locations->SetInAt(0, Location::RequiresRegister());
4408 locations->SetOut(Location::RequiresRegister());
4409}
4410
4411void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4412 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004413 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004414 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004415 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004416 __ movq(locations->Out().AsRegister<CpuRegister>(),
4417 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004418 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004419 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004420 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004421 __ movq(locations->Out().AsRegister<CpuRegister>(),
4422 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4423 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004424 __ movq(locations->Out().AsRegister<CpuRegister>(),
4425 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004426 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004427}
4428
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004429void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004430 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004431 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004432 locations->SetInAt(0, Location::RequiresRegister());
4433 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004434}
4435
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004436void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4437 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004438 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4439 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004440 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004441 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004443 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004444 break;
4445
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004446 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004448 break;
4449
4450 default:
4451 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4452 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004453}
4454
David Brazdil66d126e2015-04-03 16:02:44 +01004455void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4456 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004457 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004458 locations->SetInAt(0, Location::RequiresRegister());
4459 locations->SetOut(Location::SameAsFirstInput());
4460}
4461
4462void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004463 LocationSummary* locations = bool_not->GetLocations();
4464 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4465 locations->Out().AsRegister<CpuRegister>().AsRegister());
4466 Location out = locations->Out();
4467 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4468}
4469
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004470void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004472 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004473 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004474 locations->SetInAt(i, Location::Any());
4475 }
4476 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004477}
4478
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004479void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004480 LOG(FATAL) << "Unimplemented";
4481}
4482
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004483void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004484 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004485 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004486 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004487 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4488 */
4489 switch (kind) {
4490 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004491 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004492 break;
4493 }
4494 case MemBarrierKind::kAnyStore:
4495 case MemBarrierKind::kLoadAny:
4496 case MemBarrierKind::kStoreStore: {
4497 // nop
4498 break;
4499 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004500 case MemBarrierKind::kNTStoreStore:
4501 // Non-Temporal Store/Store needs an explicit fence.
4502 MemoryFence(/* non-temporal */ true);
4503 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004504 }
4505}
4506
4507void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4508 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4509
Roland Levillain0d5a2812015-11-13 10:07:31 +00004510 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004511 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004512 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004513 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4514 object_field_get_with_read_barrier
4515 ? LocationSummary::kCallOnSlowPath
4516 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004517 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004518 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004519 }
Calin Juravle52c48962014-12-16 17:02:57 +00004520 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004521 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004522 locations->SetOut(Location::RequiresFpuRegister());
4523 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004524 // The output overlaps for an object field get when read barriers
4525 // are enabled: we do not want the move to overwrite the object's
4526 // location, as we need it to emit the read barrier.
4527 locations->SetOut(
4528 Location::RequiresRegister(),
4529 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004530 }
Calin Juravle52c48962014-12-16 17:02:57 +00004531}
4532
4533void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4534 const FieldInfo& field_info) {
4535 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4536
4537 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004538 Location base_loc = locations->InAt(0);
4539 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004540 Location out = locations->Out();
4541 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004542 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4543 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004544 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4545
Vladimir Marko61b92282017-10-11 13:23:17 +01004546 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004547 case DataType::Type::kBool:
4548 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004549 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4550 break;
4551 }
4552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004553 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004554 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4555 break;
4556 }
4557
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004558 case DataType::Type::kUint16: {
4559 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004560 break;
4561 }
4562
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004563 case DataType::Type::kInt16: {
4564 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004565 break;
4566 }
4567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004568 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004569 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4570 break;
4571 }
4572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004573 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004574 // /* HeapReference<Object> */ out = *(base + offset)
4575 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004576 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004577 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004578 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004579 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004580 if (is_volatile) {
4581 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4582 }
4583 } else {
4584 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4585 codegen_->MaybeRecordImplicitNullCheck(instruction);
4586 if (is_volatile) {
4587 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4588 }
4589 // If read barriers are enabled, emit read barriers other than
4590 // Baker's using a slow path (and also unpoison the loaded
4591 // reference, if heap poisoning is enabled).
4592 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4593 }
4594 break;
4595 }
4596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004597 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004598 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4599 break;
4600 }
4601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004602 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004603 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4604 break;
4605 }
4606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004607 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004608 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4609 break;
4610 }
4611
Aart Bik66c158e2018-01-31 12:55:04 -08004612 case DataType::Type::kUint32:
4613 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004614 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004615 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004616 UNREACHABLE();
4617 }
4618
Vladimir Marko61b92282017-10-11 13:23:17 +01004619 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004620 // Potential implicit null checks, in the case of reference
4621 // fields, are handled in the previous switch statement.
4622 } else {
4623 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004624 }
Roland Levillain4d027112015-07-01 15:41:14 +01004625
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004626 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004627 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004628 // Memory barriers, in the case of references, are also handled
4629 // in the previous switch statement.
4630 } else {
4631 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4632 }
Roland Levillain4d027112015-07-01 15:41:14 +01004633 }
Calin Juravle52c48962014-12-16 17:02:57 +00004634}
4635
4636void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4637 const FieldInfo& field_info) {
4638 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4639
4640 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004641 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004643 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004644 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004645 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004646
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004647 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004648 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004649 if (is_volatile) {
4650 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4651 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4652 } else {
4653 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4654 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004655 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004656 if (is_volatile) {
4657 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4658 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4659 } else {
4660 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4661 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004662 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004663 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004664 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004665 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004666 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004667 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004668 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004669 locations->AddTemp(Location::RequiresRegister());
4670 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671}
4672
Calin Juravle52c48962014-12-16 17:02:57 +00004673void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004674 const FieldInfo& field_info,
4675 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004676 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4677
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004678 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004679 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4680 Location value = locations->InAt(1);
4681 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004683 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4684
4685 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004686 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004687 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688
Mark Mendellea5af682015-10-22 17:35:49 -04004689 bool maybe_record_implicit_null_check_done = false;
4690
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004691 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004693 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004694 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004695 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004696 __ movb(Address(base, offset),
4697 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004698 } else {
4699 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4700 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004701 break;
4702 }
4703
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004704 case DataType::Type::kUint16:
4705 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004706 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004707 __ movw(Address(base, offset),
4708 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004709 } else {
4710 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4711 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712 break;
4713 }
4714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004715 case DataType::Type::kInt32:
4716 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004717 if (value.IsConstant()) {
4718 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 // `field_type == DataType::Type::kReference` implies `v == 0`.
4720 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004721 // Note: if heap poisoning is enabled, no need to poison
4722 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004723 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004724 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004726 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4727 __ movl(temp, value.AsRegister<CpuRegister>());
4728 __ PoisonHeapReference(temp);
4729 __ movl(Address(base, offset), temp);
4730 } else {
4731 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4732 }
Mark Mendell40741f32015-04-20 22:10:34 -04004733 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734 break;
4735 }
4736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004738 if (value.IsConstant()) {
4739 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004740 codegen_->MoveInt64ToAddress(Address(base, offset),
4741 Address(base, offset + sizeof(int32_t)),
4742 v,
4743 instruction);
4744 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004745 } else {
4746 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4747 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004748 break;
4749 }
4750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004751 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004752 if (value.IsConstant()) {
4753 int32_t v =
4754 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4755 __ movl(Address(base, offset), Immediate(v));
4756 } else {
4757 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4758 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004759 break;
4760 }
4761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004762 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004763 if (value.IsConstant()) {
4764 int64_t v =
4765 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4766 codegen_->MoveInt64ToAddress(Address(base, offset),
4767 Address(base, offset + sizeof(int32_t)),
4768 v,
4769 instruction);
4770 maybe_record_implicit_null_check_done = true;
4771 } else {
4772 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4773 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004774 break;
4775 }
4776
Aart Bik66c158e2018-01-31 12:55:04 -08004777 case DataType::Type::kUint32:
4778 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004779 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004781 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782 }
Calin Juravle52c48962014-12-16 17:02:57 +00004783
Mark Mendellea5af682015-10-22 17:35:49 -04004784 if (!maybe_record_implicit_null_check_done) {
4785 codegen_->MaybeRecordImplicitNullCheck(instruction);
4786 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004787
4788 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4789 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4790 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004791 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004792 }
4793
Calin Juravle52c48962014-12-16 17:02:57 +00004794 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004795 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004796 }
4797}
4798
4799void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4800 HandleFieldSet(instruction, instruction->GetFieldInfo());
4801}
4802
4803void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004804 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004805}
4806
4807void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004808 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004809}
4810
4811void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004812 HandleFieldGet(instruction, instruction->GetFieldInfo());
4813}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004814
Calin Juravle52c48962014-12-16 17:02:57 +00004815void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4816 HandleFieldGet(instruction);
4817}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004818
Calin Juravle52c48962014-12-16 17:02:57 +00004819void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4820 HandleFieldGet(instruction, instruction->GetFieldInfo());
4821}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004822
Calin Juravle52c48962014-12-16 17:02:57 +00004823void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4824 HandleFieldSet(instruction, instruction->GetFieldInfo());
4825}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826
Calin Juravle52c48962014-12-16 17:02:57 +00004827void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004828 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004829}
4830
Calin Juravlee460d1d2015-09-29 04:52:17 +01004831void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4832 HUnresolvedInstanceFieldGet* instruction) {
4833 FieldAccessCallingConventionX86_64 calling_convention;
4834 codegen_->CreateUnresolvedFieldLocationSummary(
4835 instruction, instruction->GetFieldType(), calling_convention);
4836}
4837
4838void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4839 HUnresolvedInstanceFieldGet* instruction) {
4840 FieldAccessCallingConventionX86_64 calling_convention;
4841 codegen_->GenerateUnresolvedFieldAccess(instruction,
4842 instruction->GetFieldType(),
4843 instruction->GetFieldIndex(),
4844 instruction->GetDexPc(),
4845 calling_convention);
4846}
4847
4848void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4849 HUnresolvedInstanceFieldSet* instruction) {
4850 FieldAccessCallingConventionX86_64 calling_convention;
4851 codegen_->CreateUnresolvedFieldLocationSummary(
4852 instruction, instruction->GetFieldType(), calling_convention);
4853}
4854
4855void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4856 HUnresolvedInstanceFieldSet* instruction) {
4857 FieldAccessCallingConventionX86_64 calling_convention;
4858 codegen_->GenerateUnresolvedFieldAccess(instruction,
4859 instruction->GetFieldType(),
4860 instruction->GetFieldIndex(),
4861 instruction->GetDexPc(),
4862 calling_convention);
4863}
4864
4865void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4866 HUnresolvedStaticFieldGet* instruction) {
4867 FieldAccessCallingConventionX86_64 calling_convention;
4868 codegen_->CreateUnresolvedFieldLocationSummary(
4869 instruction, instruction->GetFieldType(), calling_convention);
4870}
4871
4872void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4873 HUnresolvedStaticFieldGet* instruction) {
4874 FieldAccessCallingConventionX86_64 calling_convention;
4875 codegen_->GenerateUnresolvedFieldAccess(instruction,
4876 instruction->GetFieldType(),
4877 instruction->GetFieldIndex(),
4878 instruction->GetDexPc(),
4879 calling_convention);
4880}
4881
4882void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4883 HUnresolvedStaticFieldSet* instruction) {
4884 FieldAccessCallingConventionX86_64 calling_convention;
4885 codegen_->CreateUnresolvedFieldLocationSummary(
4886 instruction, instruction->GetFieldType(), calling_convention);
4887}
4888
4889void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4890 HUnresolvedStaticFieldSet* instruction) {
4891 FieldAccessCallingConventionX86_64 calling_convention;
4892 codegen_->GenerateUnresolvedFieldAccess(instruction,
4893 instruction->GetFieldType(),
4894 instruction->GetFieldIndex(),
4895 instruction->GetDexPc(),
4896 calling_convention);
4897}
4898
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004900 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4901 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4902 ? Location::RequiresRegister()
4903 : Location::Any();
4904 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004905}
4906
Calin Juravle2ae48182016-03-16 14:05:09 +00004907void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4908 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004909 return;
4910 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004911 LocationSummary* locations = instruction->GetLocations();
4912 Location obj = locations->InAt(0);
4913
4914 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004915 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004916}
4917
Calin Juravle2ae48182016-03-16 14:05:09 +00004918void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004919 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004920 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921
4922 LocationSummary* locations = instruction->GetLocations();
4923 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004924
4925 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004926 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004927 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004928 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004929 } else {
4930 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004931 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004932 __ jmp(slow_path->GetEntryLabel());
4933 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934 }
4935 __ j(kEqual, slow_path->GetEntryLabel());
4936}
4937
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004938void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004939 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004940}
4941
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004943 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004944 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004945 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004946 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4947 object_array_get_with_read_barrier
4948 ? LocationSummary::kCallOnSlowPath
4949 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004950 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004951 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004952 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004953 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004954 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004956 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4957 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004958 // The output overlaps for an object array get when read barriers
4959 // are enabled: we do not want the move to overwrite the array's
4960 // location, as we need it to emit the read barrier.
4961 locations->SetOut(
4962 Location::RequiresRegister(),
4963 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004964 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004965}
4966
4967void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4968 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004969 Location obj_loc = locations->InAt(0);
4970 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004972 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004973 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004974
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004975 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004976 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004977 case DataType::Type::kBool:
4978 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004979 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004980 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 break;
4982 }
4983
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004984 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004985 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004986 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004987 break;
4988 }
4989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004990 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004991 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004992 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4993 // Branch cases into compressed and uncompressed for each index's type.
4994 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4995 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004996 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004997 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004998 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4999 "Expecting 0=compressed, 1=uncompressed");
5000 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005001 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
5002 __ jmp(&done);
5003 __ Bind(&not_compressed);
5004 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5005 __ Bind(&done);
5006 } else {
5007 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5008 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 break;
5010 }
5011
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005012 case DataType::Type::kInt16: {
5013 CpuRegister out = out_loc.AsRegister<CpuRegister>();
5014 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5015 break;
5016 }
5017
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005018 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005019 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005020 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021 break;
5022 }
5023
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005024 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005025 static_assert(
5026 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5027 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005028 // /* HeapReference<Object> */ out =
5029 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5030 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005031 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005032 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005033 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005034 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005035 } else {
5036 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005037 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
5038 codegen_->MaybeRecordImplicitNullCheck(instruction);
5039 // If read barriers are enabled, emit read barriers other than
5040 // Baker's using a slow path (and also unpoison the loaded
5041 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005042 if (index.IsConstant()) {
5043 uint32_t offset =
5044 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005045 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5046 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005047 codegen_->MaybeGenerateReadBarrierSlow(
5048 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5049 }
5050 }
5051 break;
5052 }
5053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005055 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005056 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 break;
5058 }
5059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005060 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005061 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005062 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005063 break;
5064 }
5065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005066 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005067 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005068 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005069 break;
5070 }
5071
Aart Bik66c158e2018-01-31 12:55:04 -08005072 case DataType::Type::kUint32:
5073 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005074 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005075 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005076 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005077 }
Roland Levillain4d027112015-07-01 15:41:14 +01005078
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005080 // Potential implicit null checks, in the case of reference
5081 // arrays, are handled in the previous switch statement.
5082 } else {
5083 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005084 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085}
5086
5087void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005088 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005089
5090 bool needs_write_barrier =
5091 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005092 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005093
Vladimir Markoca6fff82017-10-03 14:49:14 +01005094 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005095 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005096 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005097 LocationSummary::kCallOnSlowPath :
5098 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005099
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005100 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04005101 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04005103 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005104 } else {
5105 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5106 }
5107
5108 if (needs_write_barrier) {
5109 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005110 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005111 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113}
5114
5115void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
5116 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005117 Location array_loc = locations->InAt(0);
5118 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005120 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005122 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005123 bool needs_write_barrier =
5124 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5126 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5127 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128
5129 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005130 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005131 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005132 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005133 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005134 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005135 if (value.IsRegister()) {
5136 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005138 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005140 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 break;
5142 }
5143
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005144 case DataType::Type::kUint16:
5145 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005146 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005147 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005148 if (value.IsRegister()) {
5149 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005151 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01005152 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155 break;
5156 }
5157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005158 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005160 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005161
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005162 if (!value.IsRegister()) {
5163 // Just setting null.
5164 DCHECK(instruction->InputAt(2)->IsNullConstant());
5165 DCHECK(value.IsConstant()) << value;
5166 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005167 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005168 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005169 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005170 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005171 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005172
5173 DCHECK(needs_write_barrier);
5174 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005175 // We cannot use a NearLabel for `done`, as its range may be too
5176 // short when Baker read barriers are enabled.
5177 Label done;
5178 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005179 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005180 Location temp_loc = locations->GetTemp(0);
5181 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005182 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005183 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 codegen_->AddSlowPath(slow_path);
5185 if (instruction->GetValueCanBeNull()) {
5186 __ testl(register_value, register_value);
5187 __ j(kNotEqual, &not_null);
5188 __ movl(address, Immediate(0));
5189 codegen_->MaybeRecordImplicitNullCheck(instruction);
5190 __ jmp(&done);
5191 __ Bind(&not_null);
5192 }
5193
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005194 // Note that when Baker read barriers are enabled, the type
5195 // checks are performed without read barriers. This is fine,
5196 // even in the case where a class object is in the from-space
5197 // after the flip, as a comparison involving such a type would
5198 // not produce a false positive; it may of course produce a
5199 // false negative, in which case we would take the ArraySet
5200 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005201
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005202 // /* HeapReference<Class> */ temp = array->klass_
5203 __ movl(temp, Address(array, class_offset));
5204 codegen_->MaybeRecordImplicitNullCheck(instruction);
5205 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005206
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005207 // /* HeapReference<Class> */ temp = temp->component_type_
5208 __ movl(temp, Address(temp, component_offset));
5209 // If heap poisoning is enabled, no need to unpoison `temp`
5210 // nor the object reference in `register_value->klass`, as
5211 // we are comparing two poisoned references.
5212 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005213
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005214 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5215 __ j(kEqual, &do_put);
5216 // If heap poisoning is enabled, the `temp` reference has
5217 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005218 __ MaybeUnpoisonHeapReference(temp);
5219
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005220 // If heap poisoning is enabled, no need to unpoison the
5221 // heap reference loaded below, as it is only used for a
5222 // comparison with null.
5223 __ cmpl(Address(temp, super_offset), Immediate(0));
5224 __ j(kNotEqual, slow_path->GetEntryLabel());
5225 __ Bind(&do_put);
5226 } else {
5227 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005228 }
5229 }
5230
5231 if (kPoisonHeapReferences) {
5232 __ movl(temp, register_value);
5233 __ PoisonHeapReference(temp);
5234 __ movl(address, temp);
5235 } else {
5236 __ movl(address, register_value);
5237 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005238 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 codegen_->MaybeRecordImplicitNullCheck(instruction);
5240 }
5241
5242 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5243 codegen_->MarkGCCard(
5244 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
5245 __ Bind(&done);
5246
5247 if (slow_path != nullptr) {
5248 __ Bind(slow_path->GetExitLabel());
5249 }
5250
5251 break;
5252 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005254 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005256 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 if (value.IsRegister()) {
5258 __ movl(address, value.AsRegister<CpuRegister>());
5259 } else {
5260 DCHECK(value.IsConstant()) << value;
5261 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5262 __ movl(address, Immediate(v));
5263 }
5264 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 break;
5266 }
5267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005268 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005270 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 if (value.IsRegister()) {
5272 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005273 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005276 Address address_high =
5277 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005278 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 }
5280 break;
5281 }
5282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005283 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005285 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005286 if (value.IsFpuRegister()) {
5287 __ movss(address, value.AsFpuRegister<XmmRegister>());
5288 } else {
5289 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005290 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005291 __ movl(address, Immediate(v));
5292 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 break;
5295 }
5296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005299 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005300 if (value.IsFpuRegister()) {
5301 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5302 codegen_->MaybeRecordImplicitNullCheck(instruction);
5303 } else {
5304 int64_t v =
5305 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005306 Address address_high =
5307 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005308 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5309 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 break;
5311 }
5312
Aart Bik66c158e2018-01-31 12:55:04 -08005313 case DataType::Type::kUint32:
5314 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005317 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 }
5319}
5320
5321void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005322 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005323 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005324 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005325 if (!instruction->IsEmittedAtUseSite()) {
5326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5327 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328}
5329
5330void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005331 if (instruction->IsEmittedAtUseSite()) {
5332 return;
5333 }
5334
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005336 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005337 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5338 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005340 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005341 // Mask out most significant bit in case the array is String's array of char.
5342 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005343 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005344 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345}
5346
5347void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005348 RegisterSet caller_saves = RegisterSet::Empty();
5349 InvokeRuntimeCallingConvention calling_convention;
5350 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5351 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5352 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005353 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005354 HInstruction* length = instruction->InputAt(1);
5355 if (!length->IsEmittedAtUseSite()) {
5356 locations->SetInAt(1, Location::RegisterOrConstant(length));
5357 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358}
5359
5360void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5361 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005362 Location index_loc = locations->InAt(0);
5363 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005364 SlowPathCode* slow_path =
5365 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366
Mark Mendell99dbd682015-04-22 16:18:52 -04005367 if (length_loc.IsConstant()) {
5368 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5369 if (index_loc.IsConstant()) {
5370 // BCE will remove the bounds check if we are guarenteed to pass.
5371 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5372 if (index < 0 || index >= length) {
5373 codegen_->AddSlowPath(slow_path);
5374 __ jmp(slow_path->GetEntryLabel());
5375 } else {
5376 // Some optimization after BCE may have generated this, and we should not
5377 // generate a bounds check if it is a valid range.
5378 }
5379 return;
5380 }
5381
5382 // We have to reverse the jump condition because the length is the constant.
5383 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5384 __ cmpl(index_reg, Immediate(length));
5385 codegen_->AddSlowPath(slow_path);
5386 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005387 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005388 HInstruction* array_length = instruction->InputAt(1);
5389 if (array_length->IsEmittedAtUseSite()) {
5390 // Address the length field in the array.
5391 DCHECK(array_length->IsArrayLength());
5392 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5393 Location array_loc = array_length->GetLocations()->InAt(0);
5394 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005395 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005396 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5397 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005398 CpuRegister length_reg = CpuRegister(TMP);
5399 __ movl(length_reg, array_len);
5400 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005401 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005402 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005403 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005404 // Checking the bound for general case:
5405 // Array of char or String's array when the compression feature off.
5406 if (index_loc.IsConstant()) {
5407 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5408 __ cmpl(array_len, Immediate(value));
5409 } else {
5410 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5411 }
5412 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005413 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005414 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005415 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005416 }
5417 codegen_->AddSlowPath(slow_path);
5418 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005419 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420}
5421
5422void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5423 CpuRegister card,
5424 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005425 CpuRegister value,
5426 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005427 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005428 if (value_can_be_null) {
5429 __ testl(value, value);
5430 __ j(kEqual, &is_null);
5431 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005432 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005433 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 __ movq(temp, object);
5435 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005436 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005437 if (value_can_be_null) {
5438 __ Bind(&is_null);
5439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440}
5441
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005442void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005443 LOG(FATAL) << "Unimplemented";
5444}
5445
5446void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005447 if (instruction->GetNext()->IsSuspendCheck() &&
5448 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5449 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5450 // The back edge will generate the suspend check.
5451 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5452 }
5453
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005454 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5455}
5456
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005457void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005458 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5459 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005460 // In suspend check slow path, usually there are no caller-save registers at all.
5461 // If SIMD instructions are present, however, we force spilling all live SIMD
5462 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005463 locations->SetCustomSlowPathCallerSaves(
5464 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005465}
5466
5467void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005468 HBasicBlock* block = instruction->GetBlock();
5469 if (block->GetLoopInformation() != nullptr) {
5470 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5471 // The back edge will generate the suspend check.
5472 return;
5473 }
5474 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5475 // The goto will generate the suspend check.
5476 return;
5477 }
5478 GenerateSuspendCheck(instruction, nullptr);
5479}
5480
5481void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5482 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005483 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005484 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5485 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005486 slow_path =
5487 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005488 instruction->SetSlowPath(slow_path);
5489 codegen_->AddSlowPath(slow_path);
5490 if (successor != nullptr) {
5491 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005492 }
5493 } else {
5494 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5495 }
5496
Andreas Gampe542451c2016-07-26 09:02:02 -07005497 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005498 /* no_rip */ true),
5499 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005500 if (successor == nullptr) {
5501 __ j(kNotEqual, slow_path->GetEntryLabel());
5502 __ Bind(slow_path->GetReturnLabel());
5503 } else {
5504 __ j(kEqual, codegen_->GetLabelOf(successor));
5505 __ jmp(slow_path->GetEntryLabel());
5506 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005507}
5508
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005509X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5510 return codegen_->GetAssembler();
5511}
5512
5513void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005514 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005515 Location source = move->GetSource();
5516 Location destination = move->GetDestination();
5517
5518 if (source.IsRegister()) {
5519 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005520 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005521 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005522 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005523 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005524 } else {
5525 DCHECK(destination.IsDoubleStackSlot());
5526 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005527 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005528 }
5529 } else if (source.IsStackSlot()) {
5530 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005531 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005532 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005533 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005534 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005535 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005536 } else {
5537 DCHECK(destination.IsStackSlot());
5538 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5539 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5540 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005541 } else if (source.IsDoubleStackSlot()) {
5542 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005543 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005544 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005545 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005546 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5547 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005548 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005549 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005550 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5551 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5552 }
Aart Bik5576f372017-03-23 16:17:37 -07005553 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005554 if (destination.IsFpuRegister()) {
5555 __ movups(destination.AsFpuRegister<XmmRegister>(),
5556 Address(CpuRegister(RSP), source.GetStackIndex()));
5557 } else {
5558 DCHECK(destination.IsSIMDStackSlot());
5559 size_t high = kX86_64WordSize;
5560 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5561 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5562 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5563 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5564 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005565 } else if (source.IsConstant()) {
5566 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005567 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5568 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005569 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005570 if (value == 0) {
5571 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5572 } else {
5573 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5574 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005575 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005576 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005577 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005578 }
5579 } else if (constant->IsLongConstant()) {
5580 int64_t value = constant->AsLongConstant()->GetValue();
5581 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005582 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005583 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005584 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005585 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005586 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005587 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005588 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005589 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005590 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005591 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005592 } else {
5593 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005594 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005595 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5596 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005597 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005598 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005599 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005600 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005601 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005602 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005603 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005604 } else {
5605 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005606 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005607 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005608 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005609 } else if (source.IsFpuRegister()) {
5610 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005611 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005612 } else if (destination.IsStackSlot()) {
5613 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005614 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005615 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005616 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005617 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005618 } else {
5619 DCHECK(destination.IsSIMDStackSlot());
5620 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5621 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005622 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005623 }
5624}
5625
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005626void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005627 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005628 __ movl(Address(CpuRegister(RSP), mem), reg);
5629 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005630}
5631
Mark Mendell8a1c7282015-06-29 15:41:28 -04005632void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5633 __ movq(CpuRegister(TMP), reg1);
5634 __ movq(reg1, reg2);
5635 __ movq(reg2, CpuRegister(TMP));
5636}
5637
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005638void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5639 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5640 __ movq(Address(CpuRegister(RSP), mem), reg);
5641 __ movq(reg, CpuRegister(TMP));
5642}
5643
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005644void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5645 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5646 __ movss(Address(CpuRegister(RSP), mem), reg);
5647 __ movd(reg, CpuRegister(TMP));
5648}
5649
5650void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5651 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5652 __ movsd(Address(CpuRegister(RSP), mem), reg);
5653 __ movd(reg, CpuRegister(TMP));
5654}
5655
Aart Bikcfe50bb2017-12-12 14:54:12 -08005656void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5657 size_t extra_slot = 2 * kX86_64WordSize;
5658 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5659 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5660 ExchangeMemory64(0, mem + extra_slot, 2);
5661 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5662 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5663}
5664
5665void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5666 ScratchRegisterScope ensure_scratch(
5667 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5668
5669 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5670 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5671 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5672 Address(CpuRegister(RSP), mem2 + stack_offset));
5673 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5674 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5675 CpuRegister(ensure_scratch.GetRegister()));
5676}
5677
5678void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5679 ScratchRegisterScope ensure_scratch(
5680 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5681
5682 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5683
5684 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5685 for (int i = 0; i < num_of_qwords; i++) {
5686 __ movq(CpuRegister(TMP),
5687 Address(CpuRegister(RSP), mem1 + stack_offset));
5688 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5689 Address(CpuRegister(RSP), mem2 + stack_offset));
5690 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5691 CpuRegister(TMP));
5692 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5693 CpuRegister(ensure_scratch.GetRegister()));
5694 stack_offset += kX86_64WordSize;
5695 }
5696}
5697
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005698void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005699 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005700 Location source = move->GetSource();
5701 Location destination = move->GetDestination();
5702
5703 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005704 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005705 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005706 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005707 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005708 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005709 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005710 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005711 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005712 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005713 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005714 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005715 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005716 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005717 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005718 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5719 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5720 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005721 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005722 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005723 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005724 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005725 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005726 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005727 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005728 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005729 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5730 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5731 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5732 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5733 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5734 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005735 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005736 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005737 }
5738}
5739
5740
5741void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5742 __ pushq(CpuRegister(reg));
5743}
5744
5745
5746void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5747 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005748}
5749
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005750void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005751 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005752 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5753 const size_t status_byte_offset =
5754 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5755 constexpr uint32_t shifted_initialized_value =
5756 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5757
5758 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005759 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005760 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005761 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005762}
5763
Vladimir Marko175e7862018-03-27 09:03:13 +00005764void InstructionCodeGeneratorX86_64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
5765 CpuRegister temp) {
5766 uint32_t path_to_root = check->GetBitstringPathToRoot();
5767 uint32_t mask = check->GetBitstringMask();
5768 DCHECK(IsPowerOfTwo(mask + 1));
5769 size_t mask_bits = WhichPowerOf2(mask + 1);
5770
5771 if (mask_bits == 16u) {
5772 // Compare the bitstring in memory.
5773 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
5774 } else {
5775 // /* uint32_t */ temp = temp->status_
5776 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
5777 // Compare the bitstring bits using SUB.
5778 __ subl(temp, Immediate(path_to_root));
5779 // Shift out bits that do not contribute to the comparison.
5780 __ shll(temp, Immediate(32u - mask_bits));
5781 }
5782}
5783
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005784HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5785 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005786 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005787 case HLoadClass::LoadKind::kInvalid:
5788 LOG(FATAL) << "UNREACHABLE";
5789 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 case HLoadClass::LoadKind::kReferrersClass:
5791 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005792 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005793 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005794 case HLoadClass::LoadKind::kBssEntry:
5795 DCHECK(!Runtime::Current()->UseJitCompilation());
5796 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005797 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005798 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005799 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005801 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005802 break;
5803 }
5804 return desired_class_load_kind;
5805}
5806
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005807void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005808 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005809 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005810 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005811 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005812 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005813 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005814 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 return;
5816 }
Vladimir Marko41559982017-01-06 14:04:23 +00005817 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005819 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5820 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005821 ? LocationSummary::kCallOnSlowPath
5822 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005823 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005824 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005825 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005826 }
5827
Vladimir Marko41559982017-01-06 14:04:23 +00005828 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005829 locations->SetInAt(0, Location::RequiresRegister());
5830 }
5831 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005832 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5833 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5834 // Rely on the type resolution and/or initialization to save everything.
5835 // Custom calling convention: RAX serves as both input and output.
5836 RegisterSet caller_saves = RegisterSet::Empty();
5837 caller_saves.Add(Location::RegisterLocation(RAX));
5838 locations->SetCustomSlowPathCallerSaves(caller_saves);
5839 } else {
5840 // For non-Baker read barrier we have a temp-clobbering call.
5841 }
5842 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005843}
5844
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005845Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005846 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005847 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005848 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005849 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005850 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005851 PatchInfo<Label>* info = &jit_class_patches_.back();
5852 return &info->label;
5853}
5854
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005855// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5856// move.
5857void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005858 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005859 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005860 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005861 return;
5862 }
Vladimir Marko41559982017-01-06 14:04:23 +00005863 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005864
Vladimir Marko41559982017-01-06 14:04:23 +00005865 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866 Location out_loc = locations->Out();
5867 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005868
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005869 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5870 ? kWithoutReadBarrier
5871 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005872 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005873 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005874 case HLoadClass::LoadKind::kReferrersClass: {
5875 DCHECK(!cls->CanCallRuntime());
5876 DCHECK(!cls->MustGenerateClinitCheck());
5877 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5878 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5879 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005880 cls,
5881 out_loc,
5882 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005883 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005884 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005885 break;
5886 }
5887 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005888 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005889 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005890 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005891 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005892 break;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005893 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005894 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5895 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005896 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005897 break;
5898 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005899 case HLoadClass::LoadKind::kBssEntry: {
5900 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5901 /* no_rip */ false);
5902 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5903 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5904 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5905 generate_null_check = true;
5906 break;
5907 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005908 case HLoadClass::LoadKind::kJitBootImageAddress: {
5909 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5910 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5911 DCHECK_NE(address, 0u);
5912 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
5913 break;
5914 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005915 case HLoadClass::LoadKind::kJitTableAddress: {
5916 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5917 /* no_rip */ true);
5918 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005919 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005920 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005921 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005922 break;
5923 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005924 default:
5925 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5926 UNREACHABLE();
5927 }
5928
5929 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5930 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005931 SlowPathCode* slow_path =
5932 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005933 codegen_->AddSlowPath(slow_path);
5934 if (generate_null_check) {
5935 __ testl(out, out);
5936 __ j(kEqual, slow_path->GetEntryLabel());
5937 }
5938 if (cls->MustGenerateClinitCheck()) {
5939 GenerateClassInitializationCheck(slow_path, out);
5940 } else {
5941 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005942 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005943 }
5944}
5945
5946void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5947 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005948 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 locations->SetInAt(0, Location::RequiresRegister());
5950 if (check->HasUses()) {
5951 locations->SetOut(Location::SameAsFirstInput());
5952 }
5953}
5954
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005955void LocationsBuilderX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5956 // Custom calling convention: RAX serves as both input and output.
5957 Location location = Location::RegisterLocation(RAX);
5958 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5959}
5960
5961void InstructionCodeGeneratorX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5962 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5963}
5964
Orion Hodson18259d72018-04-12 11:18:23 +01005965void LocationsBuilderX86_64::VisitLoadMethodType(HLoadMethodType* load) {
5966 // Custom calling convention: RAX serves as both input and output.
5967 Location location = Location::RegisterLocation(RAX);
5968 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5969}
5970
5971void InstructionCodeGeneratorX86_64::VisitLoadMethodType(HLoadMethodType* load) {
5972 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5973}
5974
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005975void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005976 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005977 SlowPathCode* slow_path =
5978 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005979 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005980 GenerateClassInitializationCheck(slow_path,
5981 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005982}
5983
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005984HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5985 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005986 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005988 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005989 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005990 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005992 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005993 case HLoadString::LoadKind::kJitTableAddress:
5994 DCHECK(Runtime::Current()->UseJitCompilation());
5995 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005996 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005997 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 }
5999 return desired_string_load_kind;
6000}
6001
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006002void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006003 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006004 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006005 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006006 locations->SetOut(Location::RegisterLocation(RAX));
6007 } else {
6008 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006009 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
6010 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006011 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006012 // Custom calling convention: RAX serves as both input and output.
6013 RegisterSet caller_saves = RegisterSet::Empty();
6014 caller_saves.Add(Location::RegisterLocation(RAX));
6015 locations->SetCustomSlowPathCallerSaves(caller_saves);
6016 } else {
6017 // For non-Baker read barrier we have a temp-clobbering call.
6018 }
6019 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006021}
6022
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006023Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006024 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006025 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006026 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006027 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006028 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006029 PatchInfo<Label>* info = &jit_string_patches_.back();
6030 return &info->label;
6031}
6032
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006033// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6034// move.
6035void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006036 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037 Location out_loc = locations->Out();
6038 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006040 switch (load->GetLoadKind()) {
6041 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006042 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006043 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006044 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006045 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006046 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006047 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006048 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6049 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006050 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006051 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006052 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006053 case HLoadString::LoadKind::kBssEntry: {
6054 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6055 /* no_rip */ false);
6056 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6057 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006058 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006059 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006060 codegen_->AddSlowPath(slow_path);
6061 __ testl(out, out);
6062 __ j(kEqual, slow_path->GetEntryLabel());
6063 __ Bind(slow_path->GetExitLabel());
6064 return;
6065 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006066 case HLoadString::LoadKind::kJitBootImageAddress: {
6067 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6068 DCHECK_NE(address, 0u);
6069 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
6070 return;
6071 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006072 case HLoadString::LoadKind::kJitTableAddress: {
6073 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6074 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006075 Label* fixup_label = codegen_->NewJitRootStringPatch(
6076 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006077 // /* GcRoot<mirror::String> */ out = *address
6078 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6079 return;
6080 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006081 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006082 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006083 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006084
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006085 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006086 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006087 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006088 codegen_->InvokeRuntime(kQuickResolveString,
6089 load,
6090 load->GetDexPc());
6091 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006092}
6093
David Brazdilcb1c0552015-08-04 16:22:25 +01006094static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006095 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006096 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01006097}
6098
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006099void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
6100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006101 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006102 locations->SetOut(Location::RequiresRegister());
6103}
6104
6105void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006106 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
6107}
6108
6109void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006110 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006111}
6112
6113void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6114 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006115}
6116
6117void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006118 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6119 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006120 InvokeRuntimeCallingConvention calling_convention;
6121 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6122}
6123
6124void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006125 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006126 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006127}
6128
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006129// Temp is used for read barrier.
6130static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6131 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006132 !kUseBakerReadBarrier &&
6133 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006134 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006135 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6136 return 1;
6137 }
6138 return 0;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006139}
6140
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006141// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6142// interface pointer, the current interface is compared in memory.
6143// The other checks have one temp for loading the object's class.
6144static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6145 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6146 return 2;
6147 }
6148 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006149}
6150
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006151void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006154 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 case TypeCheckKind::kExactCheck:
6157 case TypeCheckKind::kAbstractClassCheck:
6158 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006159 case TypeCheckKind::kArrayObjectCheck: {
6160 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6161 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6162 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006164 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166 case TypeCheckKind::kUnresolvedCheck:
6167 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 call_kind = LocationSummary::kCallOnSlowPath;
6169 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006170 case TypeCheckKind::kBitstringCheck:
6171 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173
Vladimir Markoca6fff82017-10-03 14:49:14 +01006174 LocationSummary* locations =
6175 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006176 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006177 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006178 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006179 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006180 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6181 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6182 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6183 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6184 } else {
6185 locations->SetInAt(1, Location::Any());
6186 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
6188 locations->SetOut(Location::RequiresRegister());
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006189 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006190}
6191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006192void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006193 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006194 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195 Location obj_loc = locations->InAt(0);
6196 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006197 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198 Location out_loc = locations->Out();
6199 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006200 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6201 DCHECK_LE(num_temps, 1u);
6202 Location maybe_temp_loc = (num_temps >= 1u) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006203 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6205 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6206 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006207 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006209
6210 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006211 // Avoid null check if we know obj is not null.
6212 if (instruction->MustDoNullCheck()) {
6213 __ testl(obj, obj);
6214 __ j(kEqual, &zero);
6215 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006217 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006219 ReadBarrierOption read_barrier_option =
6220 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006221 // /* HeapReference<Class> */ out = obj->klass_
6222 GenerateReferenceLoadTwoRegisters(instruction,
6223 out_loc,
6224 obj_loc,
6225 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006226 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006227 if (cls.IsRegister()) {
6228 __ cmpl(out, cls.AsRegister<CpuRegister>());
6229 } else {
6230 DCHECK(cls.IsStackSlot()) << cls;
6231 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6232 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006233 if (zero.IsLinked()) {
6234 // Classes must be equal for the instanceof to succeed.
6235 __ j(kNotEqual, &zero);
6236 __ movl(out, Immediate(1));
6237 __ jmp(&done);
6238 } else {
6239 __ setcc(kEqual, out);
6240 // setcc only sets the low byte.
6241 __ andl(out, Immediate(1));
6242 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 break;
6244 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006247 ReadBarrierOption read_barrier_option =
6248 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006249 // /* HeapReference<Class> */ out = obj->klass_
6250 GenerateReferenceLoadTwoRegisters(instruction,
6251 out_loc,
6252 obj_loc,
6253 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006254 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 // If the class is abstract, we eagerly fetch the super class of the
6256 // object to avoid doing a comparison we know will fail.
6257 NearLabel loop, success;
6258 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006260 GenerateReferenceLoadOneRegister(instruction,
6261 out_loc,
6262 super_offset,
6263 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006264 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 __ testl(out, out);
6266 // If `out` is null, we use it for the result, and jump to `done`.
6267 __ j(kEqual, &done);
6268 if (cls.IsRegister()) {
6269 __ cmpl(out, cls.AsRegister<CpuRegister>());
6270 } else {
6271 DCHECK(cls.IsStackSlot()) << cls;
6272 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6273 }
6274 __ j(kNotEqual, &loop);
6275 __ movl(out, Immediate(1));
6276 if (zero.IsLinked()) {
6277 __ jmp(&done);
6278 }
6279 break;
6280 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006283 ReadBarrierOption read_barrier_option =
6284 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006285 // /* HeapReference<Class> */ out = obj->klass_
6286 GenerateReferenceLoadTwoRegisters(instruction,
6287 out_loc,
6288 obj_loc,
6289 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006290 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 // Walk over the class hierarchy to find a match.
6292 NearLabel loop, success;
6293 __ Bind(&loop);
6294 if (cls.IsRegister()) {
6295 __ cmpl(out, cls.AsRegister<CpuRegister>());
6296 } else {
6297 DCHECK(cls.IsStackSlot()) << cls;
6298 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6299 }
6300 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006302 GenerateReferenceLoadOneRegister(instruction,
6303 out_loc,
6304 super_offset,
6305 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006306 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006307 __ testl(out, out);
6308 __ j(kNotEqual, &loop);
6309 // If `out` is null, we use it for the result, and jump to `done`.
6310 __ jmp(&done);
6311 __ Bind(&success);
6312 __ movl(out, Immediate(1));
6313 if (zero.IsLinked()) {
6314 __ jmp(&done);
6315 }
6316 break;
6317 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006318
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006320 ReadBarrierOption read_barrier_option =
6321 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006322 // /* HeapReference<Class> */ out = obj->klass_
6323 GenerateReferenceLoadTwoRegisters(instruction,
6324 out_loc,
6325 obj_loc,
6326 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006327 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006328 // Do an exact check.
6329 NearLabel exact_check;
6330 if (cls.IsRegister()) {
6331 __ cmpl(out, cls.AsRegister<CpuRegister>());
6332 } else {
6333 DCHECK(cls.IsStackSlot()) << cls;
6334 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6335 }
6336 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006339 GenerateReferenceLoadOneRegister(instruction,
6340 out_loc,
6341 component_offset,
6342 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006343 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006344 __ testl(out, out);
6345 // If `out` is null, we use it for the result, and jump to `done`.
6346 __ j(kEqual, &done);
6347 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6348 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006349 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006350 __ movl(out, Immediate(1));
6351 __ jmp(&done);
6352 break;
6353 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006355 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006356 // No read barrier since the slow path will retry upon failure.
6357 // /* HeapReference<Class> */ out = obj->klass_
6358 GenerateReferenceLoadTwoRegisters(instruction,
6359 out_loc,
6360 obj_loc,
6361 class_offset,
6362 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363 if (cls.IsRegister()) {
6364 __ cmpl(out, cls.AsRegister<CpuRegister>());
6365 } else {
6366 DCHECK(cls.IsStackSlot()) << cls;
6367 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6368 }
6369 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006370 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6371 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 codegen_->AddSlowPath(slow_path);
6373 __ j(kNotEqual, slow_path->GetEntryLabel());
6374 __ movl(out, Immediate(1));
6375 if (zero.IsLinked()) {
6376 __ jmp(&done);
6377 }
6378 break;
6379 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006380
Calin Juravle98893e12015-10-02 21:05:03 +01006381 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 case TypeCheckKind::kInterfaceCheck: {
6383 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006384 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385 // cases.
6386 //
6387 // We cannot directly call the InstanceofNonTrivial runtime
6388 // entry point without resorting to a type checking slow path
6389 // here (i.e. by calling InvokeRuntime directly), as it would
6390 // require to assign fixed registers for the inputs of this
6391 // HInstanceOf instruction (following the runtime calling
6392 // convention), which might be cluttered by the potential first
6393 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006394 //
6395 // TODO: Introduce a new runtime entry point taking the object
6396 // to test (instead of its class) as argument, and let it deal
6397 // with the read barrier issues. This will let us refactor this
6398 // case of the `switch` code as it was previously (with a direct
6399 // call to the runtime not using a type checking slow path).
6400 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006402 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6403 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 codegen_->AddSlowPath(slow_path);
6405 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 if (zero.IsLinked()) {
6407 __ jmp(&done);
6408 }
6409 break;
6410 }
Vladimir Marko175e7862018-03-27 09:03:13 +00006411
6412 case TypeCheckKind::kBitstringCheck: {
6413 // /* HeapReference<Class> */ temp = obj->klass_
6414 GenerateReferenceLoadTwoRegisters(instruction,
6415 out_loc,
6416 obj_loc,
6417 class_offset,
6418 kWithoutReadBarrier);
6419
6420 GenerateBitstringTypeCheckCompare(instruction, out);
6421 if (zero.IsLinked()) {
6422 __ j(kNotEqual, &zero);
6423 __ movl(out, Immediate(1));
6424 __ jmp(&done);
6425 } else {
6426 __ setcc(kEqual, out);
6427 // setcc only sets the low byte.
6428 __ andl(out, Immediate(1));
6429 }
6430 break;
6431 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006432 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006433
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006435 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006436 __ xorl(out, out);
6437 }
6438
6439 if (done.IsLinked()) {
6440 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006441 }
6442
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006443 if (slow_path != nullptr) {
6444 __ Bind(slow_path->GetExitLabel());
6445 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006446}
6447
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006448void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006449 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006450 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006451 LocationSummary* locations =
6452 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006453 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006454 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6455 // Require a register for the interface check since there is a loop that compares the class to
6456 // a memory address.
6457 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006458 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6459 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6460 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6461 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006462 } else {
6463 locations->SetInAt(1, Location::Any());
6464 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006465 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
6466 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006467}
6468
6469void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006470 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006471 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472 Location obj_loc = locations->InAt(0);
6473 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006474 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475 Location temp_loc = locations->GetTemp(0);
6476 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006477 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6478 DCHECK_GE(num_temps, 1u);
6479 DCHECK_LE(num_temps, 2u);
6480 Location maybe_temp2_loc = (num_temps >= 2u) ? locations->GetTemp(1) : Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006481 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6482 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6483 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6484 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6485 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6486 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006487 const uint32_t object_array_data_offset =
6488 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489
Vladimir Marko87584542017-12-12 17:47:52 +00006490 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006492 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6493 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006495
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006496
6497 NearLabel done;
6498 // Avoid null check if we know obj is not null.
6499 if (instruction->MustDoNullCheck()) {
6500 __ testl(obj, obj);
6501 __ j(kEqual, &done);
6502 }
6503
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 case TypeCheckKind::kExactCheck:
6506 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006507 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006508 GenerateReferenceLoadTwoRegisters(instruction,
6509 temp_loc,
6510 obj_loc,
6511 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006512 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 if (cls.IsRegister()) {
6514 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6515 } else {
6516 DCHECK(cls.IsStackSlot()) << cls;
6517 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6518 }
6519 // Jump to slow path for throwing the exception or doing a
6520 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 break;
6523 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006525 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006526 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006527 GenerateReferenceLoadTwoRegisters(instruction,
6528 temp_loc,
6529 obj_loc,
6530 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006531 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 // If the class is abstract, we eagerly fetch the super class of the
6533 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006534 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006537 GenerateReferenceLoadOneRegister(instruction,
6538 temp_loc,
6539 super_offset,
6540 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006541 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006543 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6544 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006545 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006546 // Otherwise, compare the classes.
6547 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 if (cls.IsRegister()) {
6549 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6550 } else {
6551 DCHECK(cls.IsStackSlot()) << cls;
6552 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6553 }
6554 __ j(kNotEqual, &loop);
6555 break;
6556 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006559 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006560 GenerateReferenceLoadTwoRegisters(instruction,
6561 temp_loc,
6562 obj_loc,
6563 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006564 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006566 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 __ Bind(&loop);
6568 if (cls.IsRegister()) {
6569 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6570 } else {
6571 DCHECK(cls.IsStackSlot()) << cls;
6572 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6573 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006574 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006577 GenerateReferenceLoadOneRegister(instruction,
6578 temp_loc,
6579 super_offset,
6580 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006581 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582
6583 // If the class reference currently in `temp` is not null, jump
6584 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006586 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 break;
6590 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006593 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006594 GenerateReferenceLoadTwoRegisters(instruction,
6595 temp_loc,
6596 obj_loc,
6597 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006598 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006599 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006600 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006601 if (cls.IsRegister()) {
6602 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6603 } else {
6604 DCHECK(cls.IsStackSlot()) << cls;
6605 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6606 }
6607 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608
6609 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006611 GenerateReferenceLoadOneRegister(instruction,
6612 temp_loc,
6613 component_offset,
6614 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006615 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616
6617 // If the component type is not null (i.e. the object is indeed
6618 // an array), jump to label `check_non_primitive_component_type`
6619 // to further check that this component type is not a primitive
6620 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006621 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006623 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006624 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006625 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006626 break;
6627 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006628
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006629 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006630 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 //
6632 // We cannot directly call the CheckCast runtime entry point
6633 // without resorting to a type checking slow path here (i.e. by
6634 // calling InvokeRuntime directly), as it would require to
6635 // assign fixed registers for the inputs of this HInstanceOf
6636 // instruction (following the runtime calling convention), which
6637 // might be cluttered by the potential first read barrier
6638 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006639 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006640 break;
6641 }
6642
Vladimir Marko175e7862018-03-27 09:03:13 +00006643 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006644 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6645 // We can not get false positives by doing this.
6646 // /* HeapReference<Class> */ temp = obj->klass_
6647 GenerateReferenceLoadTwoRegisters(instruction,
6648 temp_loc,
6649 obj_loc,
6650 class_offset,
6651 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006652
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006653 // /* HeapReference<Class> */ temp = temp->iftable_
6654 GenerateReferenceLoadTwoRegisters(instruction,
6655 temp_loc,
6656 temp_loc,
6657 iftable_offset,
6658 kWithoutReadBarrier);
6659 // Iftable is never null.
6660 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6661 // Maybe poison the `cls` for direct comparison with memory.
6662 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6663 // Loop through the iftable and check if any class matches.
6664 NearLabel start_loop;
6665 __ Bind(&start_loop);
6666 // Need to subtract first to handle the empty array case.
6667 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6668 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6669 // Go to next interface if the classes do not match.
6670 __ cmpl(cls.AsRegister<CpuRegister>(),
6671 CodeGeneratorX86_64::ArrayAddress(temp,
6672 maybe_temp2_loc,
6673 TIMES_4,
6674 object_array_data_offset));
6675 __ j(kNotEqual, &start_loop); // Return if same class.
6676 // If `cls` was poisoned above, unpoison it.
6677 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006678 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006679 }
6680
6681 case TypeCheckKind::kBitstringCheck: {
6682 // /* HeapReference<Class> */ temp = obj->klass_
6683 GenerateReferenceLoadTwoRegisters(instruction,
6684 temp_loc,
6685 obj_loc,
6686 class_offset,
6687 kWithoutReadBarrier);
6688
6689 GenerateBitstringTypeCheckCompare(instruction, temp);
6690 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
6691 break;
6692 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006693 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006694
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006695 if (done.IsLinked()) {
6696 __ Bind(&done);
6697 }
6698
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006700}
6701
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006702void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006703 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6704 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006705 InvokeRuntimeCallingConvention calling_convention;
6706 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6707}
6708
6709void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006710 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006711 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006712 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006713 if (instruction->IsEnter()) {
6714 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6715 } else {
6716 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6717 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006718}
6719
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006720void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6721void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6722void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6723
6724void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6725 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006726 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6728 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006729 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006730 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006731 locations->SetOut(Location::SameAsFirstInput());
6732}
6733
6734void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6735 HandleBitwiseOperation(instruction);
6736}
6737
6738void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6739 HandleBitwiseOperation(instruction);
6740}
6741
6742void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6743 HandleBitwiseOperation(instruction);
6744}
6745
6746void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6747 LocationSummary* locations = instruction->GetLocations();
6748 Location first = locations->InAt(0);
6749 Location second = locations->InAt(1);
6750 DCHECK(first.Equals(locations->Out()));
6751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006752 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006753 if (second.IsRegister()) {
6754 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006755 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006756 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006757 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006758 } else {
6759 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006760 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006761 }
6762 } else if (second.IsConstant()) {
6763 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6764 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006765 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006766 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006767 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006768 } else {
6769 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006770 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006771 }
6772 } else {
6773 Address address(CpuRegister(RSP), second.GetStackIndex());
6774 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006775 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006776 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006777 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006778 } else {
6779 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006780 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006781 }
6782 }
6783 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006784 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006785 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6786 bool second_is_constant = false;
6787 int64_t value = 0;
6788 if (second.IsConstant()) {
6789 second_is_constant = true;
6790 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006791 }
Mark Mendell40741f32015-04-20 22:10:34 -04006792 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006793
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006794 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006795 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006796 if (is_int32_value) {
6797 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6798 } else {
6799 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6800 }
6801 } else if (second.IsDoubleStackSlot()) {
6802 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006803 } else {
6804 __ andq(first_reg, second.AsRegister<CpuRegister>());
6805 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006806 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006807 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006808 if (is_int32_value) {
6809 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6810 } else {
6811 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6812 }
6813 } else if (second.IsDoubleStackSlot()) {
6814 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006815 } else {
6816 __ orq(first_reg, second.AsRegister<CpuRegister>());
6817 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006818 } else {
6819 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006820 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006821 if (is_int32_value) {
6822 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6823 } else {
6824 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6825 }
6826 } else if (second.IsDoubleStackSlot()) {
6827 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006828 } else {
6829 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6830 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006831 }
6832 }
6833}
6834
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006835void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6836 HInstruction* instruction,
6837 Location out,
6838 uint32_t offset,
6839 Location maybe_temp,
6840 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006841 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006842 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006843 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006844 if (kUseBakerReadBarrier) {
6845 // Load with fast path based Baker's read barrier.
6846 // /* HeapReference<Object> */ out = *(out + offset)
6847 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006848 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006849 } else {
6850 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006851 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006852 // in the following move operation, as we will need it for the
6853 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006854 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006855 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006856 // /* HeapReference<Object> */ out = *(out + offset)
6857 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006858 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006859 }
6860 } else {
6861 // Plain load with no read barrier.
6862 // /* HeapReference<Object> */ out = *(out + offset)
6863 __ movl(out_reg, Address(out_reg, offset));
6864 __ MaybeUnpoisonHeapReference(out_reg);
6865 }
6866}
6867
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006868void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6869 HInstruction* instruction,
6870 Location out,
6871 Location obj,
6872 uint32_t offset,
6873 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006874 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6875 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006876 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006877 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006878 if (kUseBakerReadBarrier) {
6879 // Load with fast path based Baker's read barrier.
6880 // /* HeapReference<Object> */ out = *(obj + offset)
6881 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006882 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006883 } else {
6884 // Load with slow path based read barrier.
6885 // /* HeapReference<Object> */ out = *(obj + offset)
6886 __ movl(out_reg, Address(obj_reg, offset));
6887 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6888 }
6889 } else {
6890 // Plain load with no read barrier.
6891 // /* HeapReference<Object> */ out = *(obj + offset)
6892 __ movl(out_reg, Address(obj_reg, offset));
6893 __ MaybeUnpoisonHeapReference(out_reg);
6894 }
6895}
6896
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006897void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6898 HInstruction* instruction,
6899 Location root,
6900 const Address& address,
6901 Label* fixup_label,
6902 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006903 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006904 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006905 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006906 if (kUseBakerReadBarrier) {
6907 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6908 // Baker's read barrier are used:
6909 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006910 // root = obj.field;
6911 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6912 // if (temp != null) {
6913 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006914 // }
6915
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006916 // /* GcRoot<mirror::Object> */ root = *address
6917 __ movl(root_reg, address);
6918 if (fixup_label != nullptr) {
6919 __ Bind(fixup_label);
6920 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006921 static_assert(
6922 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6923 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6924 "have different sizes.");
6925 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6926 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6927 "have different sizes.");
6928
Vladimir Marko953437b2016-08-24 08:30:46 +00006929 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006930 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006931 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006932 codegen_->AddSlowPath(slow_path);
6933
Roland Levillaind966ce72017-02-09 16:20:14 +00006934 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6935 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006936 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006937 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6938 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006939 __ j(kNotEqual, slow_path->GetEntryLabel());
6940 __ Bind(slow_path->GetExitLabel());
6941 } else {
6942 // GC root loaded through a slow path for read barriers other
6943 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006944 // /* GcRoot<mirror::Object>* */ root = address
6945 __ leaq(root_reg, address);
6946 if (fixup_label != nullptr) {
6947 __ Bind(fixup_label);
6948 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006949 // /* mirror::Object* */ root = root->Read()
6950 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6951 }
6952 } else {
6953 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006954 // /* GcRoot<mirror::Object> */ root = *address
6955 __ movl(root_reg, address);
6956 if (fixup_label != nullptr) {
6957 __ Bind(fixup_label);
6958 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006959 // Note that GC roots are not affected by heap poisoning, thus we
6960 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006961 }
6962}
6963
6964void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6965 Location ref,
6966 CpuRegister obj,
6967 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006968 bool needs_null_check) {
6969 DCHECK(kEmitCompilerReadBarrier);
6970 DCHECK(kUseBakerReadBarrier);
6971
6972 // /* HeapReference<Object> */ ref = *(obj + offset)
6973 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006974 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006975}
6976
6977void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6978 Location ref,
6979 CpuRegister obj,
6980 uint32_t data_offset,
6981 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006982 bool needs_null_check) {
6983 DCHECK(kEmitCompilerReadBarrier);
6984 DCHECK(kUseBakerReadBarrier);
6985
Roland Levillain3d312422016-06-23 13:53:42 +01006986 static_assert(
6987 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6988 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006989 // /* HeapReference<Object> */ ref =
6990 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006991 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006992 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006993}
6994
6995void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6996 Location ref,
6997 CpuRegister obj,
6998 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006999 bool needs_null_check,
7000 bool always_update_field,
7001 CpuRegister* temp1,
7002 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007003 DCHECK(kEmitCompilerReadBarrier);
7004 DCHECK(kUseBakerReadBarrier);
7005
7006 // In slow path based read barriers, the read barrier call is
7007 // inserted after the original load. However, in fast path based
7008 // Baker's read barriers, we need to perform the load of
7009 // mirror::Object::monitor_ *before* the original reference load.
7010 // This load-load ordering is required by the read barrier.
7011 // The fast path/slow path (for Baker's algorithm) should look like:
7012 //
7013 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7014 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7015 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007016 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007017 // if (is_gray) {
7018 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7019 // }
7020 //
7021 // Note: the original implementation in ReadBarrier::Barrier is
7022 // slightly more complex as:
7023 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007024 // the high-bits of rb_state, which are expected to be all zeroes
7025 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
7026 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007027 // - it performs additional checks that we do not do here for
7028 // performance reasons.
7029
7030 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007031 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7032
Vladimir Marko953437b2016-08-24 08:30:46 +00007033 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007034 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7035 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007036 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7037 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7038 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7039
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007040 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007041 // ref = ReadBarrier::Mark(ref);
7042 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7043 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007044 if (needs_null_check) {
7045 MaybeRecordImplicitNullCheck(instruction);
7046 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007047
7048 // Load fence to prevent load-load reordering.
7049 // Note that this is a no-op, thanks to the x86-64 memory model.
7050 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7051
7052 // The actual reference load.
7053 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007054 __ movl(ref_reg, src); // Flags are unaffected.
7055
7056 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7057 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007058 SlowPathCode* slow_path;
7059 if (always_update_field) {
7060 DCHECK(temp1 != nullptr);
7061 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007062 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007063 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
7064 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007065 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007066 instruction, ref, /* unpoison_ref_before_marking */ true);
7067 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007068 AddSlowPath(slow_path);
7069
7070 // We have done the "if" of the gray bit check above, now branch based on the flags.
7071 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007072
7073 // Object* ref = ref_addr->AsMirrorPtr()
7074 __ MaybeUnpoisonHeapReference(ref_reg);
7075
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007076 __ Bind(slow_path->GetExitLabel());
7077}
7078
7079void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
7080 Location out,
7081 Location ref,
7082 Location obj,
7083 uint32_t offset,
7084 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007085 DCHECK(kEmitCompilerReadBarrier);
7086
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007087 // Insert a slow path based read barrier *after* the reference load.
7088 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007089 // If heap poisoning is enabled, the unpoisoning of the loaded
7090 // reference will be carried out by the runtime within the slow
7091 // path.
7092 //
7093 // Note that `ref` currently does not get unpoisoned (when heap
7094 // poisoning is enabled), which is alright as the `ref` argument is
7095 // not used by the artReadBarrierSlow entry point.
7096 //
7097 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007098 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007099 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
7100 AddSlowPath(slow_path);
7101
Roland Levillain0d5a2812015-11-13 10:07:31 +00007102 __ jmp(slow_path->GetEntryLabel());
7103 __ Bind(slow_path->GetExitLabel());
7104}
7105
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007106void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7107 Location out,
7108 Location ref,
7109 Location obj,
7110 uint32_t offset,
7111 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007112 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007113 // Baker's read barriers shall be handled by the fast path
7114 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
7115 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007116 // If heap poisoning is enabled, unpoisoning will be taken care of
7117 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007118 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007119 } else if (kPoisonHeapReferences) {
7120 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
7121 }
7122}
7123
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007124void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7125 Location out,
7126 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007127 DCHECK(kEmitCompilerReadBarrier);
7128
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007129 // Insert a slow path based read barrier *after* the GC root load.
7130 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007131 // Note that GC roots are not affected by heap poisoning, so we do
7132 // not need to do anything special for this here.
7133 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007134 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 AddSlowPath(slow_path);
7136
Roland Levillain0d5a2812015-11-13 10:07:31 +00007137 __ jmp(slow_path->GetEntryLabel());
7138 __ Bind(slow_path->GetExitLabel());
7139}
7140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007141void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007142 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007143 LOG(FATAL) << "Unreachable";
7144}
7145
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007146void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007147 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007148 LOG(FATAL) << "Unreachable";
7149}
7150
Mark Mendellfe57faa2015-09-18 09:26:15 -04007151// Simple implementation of packed switch - generate cascaded compare/jumps.
7152void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7153 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007154 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007155 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04007156 locations->AddTemp(Location::RequiresRegister());
7157 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04007158}
7159
7160void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7161 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007162 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007163 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04007164 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
7165 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
7166 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007167 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7168
7169 // Should we generate smaller inline compare/jumps?
7170 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7171 // Figure out the correct compare values and jump conditions.
7172 // Handle the first compare/branch as a special case because it might
7173 // jump to the default case.
7174 DCHECK_GT(num_entries, 2u);
7175 Condition first_condition;
7176 uint32_t index;
7177 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7178 if (lower_bound != 0) {
7179 first_condition = kLess;
7180 __ cmpl(value_reg_in, Immediate(lower_bound));
7181 __ j(first_condition, codegen_->GetLabelOf(default_block));
7182 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
7183
7184 index = 1;
7185 } else {
7186 // Handle all the compare/jumps below.
7187 first_condition = kBelow;
7188 index = 0;
7189 }
7190
7191 // Handle the rest of the compare/jumps.
7192 for (; index + 1 < num_entries; index += 2) {
7193 int32_t compare_to_value = lower_bound + index + 1;
7194 __ cmpl(value_reg_in, Immediate(compare_to_value));
7195 // Jump to successors[index] if value < case_value[index].
7196 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7197 // Jump to successors[index + 1] if value == case_value[index + 1].
7198 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7199 }
7200
7201 if (index != num_entries) {
7202 // There are an odd number of entries. Handle the last one.
7203 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00007204 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007205 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
7206 }
7207
7208 // And the default for any other value.
7209 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7210 __ jmp(codegen_->GetLabelOf(default_block));
7211 }
7212 return;
7213 }
Mark Mendell9c86b482015-09-18 13:36:07 -04007214
7215 // Remove the bias, if needed.
7216 Register value_reg_out = value_reg_in.AsRegister();
7217 if (lower_bound != 0) {
7218 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
7219 value_reg_out = temp_reg.AsRegister();
7220 }
7221 CpuRegister value_reg(value_reg_out);
7222
7223 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04007224 __ cmpl(value_reg, Immediate(num_entries - 1));
7225 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007226
Mark Mendell9c86b482015-09-18 13:36:07 -04007227 // We are in the range of the table.
7228 // Load the address of the jump table in the constant area.
7229 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007230
Mark Mendell9c86b482015-09-18 13:36:07 -04007231 // Load the (signed) offset from the jump table.
7232 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
7233
7234 // Add the offset to the address of the table base.
7235 __ addq(temp_reg, base_reg);
7236
7237 // And jump.
7238 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007239}
7240
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007241void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7242 ATTRIBUTE_UNUSED) {
7243 LOG(FATAL) << "Unreachable";
7244}
7245
7246void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7247 ATTRIBUTE_UNUSED) {
7248 LOG(FATAL) << "Unreachable";
7249}
7250
Aart Bikc5d47542016-01-27 17:00:35 -08007251void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
7252 if (value == 0) {
7253 __ xorl(dest, dest);
7254 } else {
7255 __ movl(dest, Immediate(value));
7256 }
7257}
7258
Mark Mendell92e83bf2015-05-07 11:25:03 -04007259void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
7260 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08007261 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007262 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00007263 } else if (IsUint<32>(value)) {
7264 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007265 __ movl(dest, Immediate(static_cast<int32_t>(value)));
7266 } else {
7267 __ movq(dest, Immediate(value));
7268 }
7269}
7270
Mark Mendell7c0b44f2016-02-01 10:08:35 -05007271void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
7272 if (value == 0) {
7273 __ xorps(dest, dest);
7274 } else {
7275 __ movss(dest, LiteralInt32Address(value));
7276 }
7277}
7278
7279void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
7280 if (value == 0) {
7281 __ xorpd(dest, dest);
7282 } else {
7283 __ movsd(dest, LiteralInt64Address(value));
7284 }
7285}
7286
7287void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
7288 Load32BitValue(dest, bit_cast<int32_t, float>(value));
7289}
7290
7291void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
7292 Load64BitValue(dest, bit_cast<int64_t, double>(value));
7293}
7294
Aart Bika19616e2016-02-01 18:57:58 -08007295void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
7296 if (value == 0) {
7297 __ testl(dest, dest);
7298 } else {
7299 __ cmpl(dest, Immediate(value));
7300 }
7301}
7302
7303void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
7304 if (IsInt<32>(value)) {
7305 if (value == 0) {
7306 __ testq(dest, dest);
7307 } else {
7308 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7309 }
7310 } else {
7311 // Value won't fit in an int.
7312 __ cmpq(dest, LiteralInt64Address(value));
7313 }
7314}
7315
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007316void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7317 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007318 GenerateIntCompare(lhs_reg, rhs);
7319}
7320
7321void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007322 if (rhs.IsConstant()) {
7323 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007324 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007325 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007326 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007327 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007328 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007329 }
7330}
7331
7332void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7333 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7334 if (rhs.IsConstant()) {
7335 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7336 Compare64BitValue(lhs_reg, value);
7337 } else if (rhs.IsDoubleStackSlot()) {
7338 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7339 } else {
7340 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7341 }
7342}
7343
7344Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7345 Location index,
7346 ScaleFactor scale,
7347 uint32_t data_offset) {
7348 return index.IsConstant() ?
7349 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7350 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7351}
7352
Mark Mendellcfa410b2015-05-25 16:02:44 -04007353void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7354 DCHECK(dest.IsDoubleStackSlot());
7355 if (IsInt<32>(value)) {
7356 // Can move directly as an int32 constant.
7357 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7358 Immediate(static_cast<int32_t>(value)));
7359 } else {
7360 Load64BitValue(CpuRegister(TMP), value);
7361 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7362 }
7363}
7364
Mark Mendell9c86b482015-09-18 13:36:07 -04007365/**
7366 * Class to handle late fixup of offsets into constant area.
7367 */
7368class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7369 public:
7370 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7371 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7372
7373 protected:
7374 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7375
7376 CodeGeneratorX86_64* codegen_;
7377
7378 private:
7379 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7380 // Patch the correct offset for the instruction. We use the address of the
7381 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7382 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7383 int32_t relative_position = constant_offset - pos;
7384
7385 // Patch in the right value.
7386 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7387 }
7388
7389 // Location in constant area that the fixup refers to.
7390 size_t offset_into_constant_area_;
7391};
7392
7393/**
7394 t * Class to handle late fixup of offsets to a jump table that will be created in the
7395 * constant area.
7396 */
7397class JumpTableRIPFixup : public RIPFixup {
7398 public:
7399 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7400 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7401
7402 void CreateJumpTable() {
7403 X86_64Assembler* assembler = codegen_->GetAssembler();
7404
7405 // Ensure that the reference to the jump table has the correct offset.
7406 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7407 SetOffset(offset_in_constant_table);
7408
7409 // Compute the offset from the start of the function to this jump table.
7410 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7411
7412 // Populate the jump table with the correct values for the jump table.
7413 int32_t num_entries = switch_instr_->GetNumEntries();
7414 HBasicBlock* block = switch_instr_->GetBlock();
7415 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7416 // The value that we want is the target offset - the position of the table.
7417 for (int32_t i = 0; i < num_entries; i++) {
7418 HBasicBlock* b = successors[i];
7419 Label* l = codegen_->GetLabelOf(b);
7420 DCHECK(l->IsBound());
7421 int32_t offset_to_block = l->Position() - current_table_offset;
7422 assembler->AppendInt32(offset_to_block);
7423 }
7424 }
7425
7426 private:
7427 const HPackedSwitch* switch_instr_;
7428};
7429
Mark Mendellf55c3e02015-03-26 21:07:46 -04007430void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7431 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007432 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007433 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7434 // 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 -04007435 assembler->Align(4, 0);
7436 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007437
7438 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007439 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007440 jump_table->CreateJumpTable();
7441 }
7442
7443 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007444 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007445 }
7446
7447 // And finish up.
7448 CodeGenerator::Finalize(allocator);
7449}
7450
Mark Mendellf55c3e02015-03-26 21:07:46 -04007451Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007452 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007453 return Address::RIP(fixup);
7454}
7455
7456Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007457 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007458 return Address::RIP(fixup);
7459}
7460
7461Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007462 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007463 return Address::RIP(fixup);
7464}
7465
7466Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007467 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007468 return Address::RIP(fixup);
7469}
7470
Andreas Gampe85b62f22015-09-09 13:15:38 -07007471// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007472void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007473 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007474 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007475 return;
7476 }
7477
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007478 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007479
7480 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7481 if (trg.Equals(return_loc)) {
7482 return;
7483 }
7484
7485 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007486 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007487 parallel_move.AddMove(return_loc, trg, type, nullptr);
7488 GetMoveResolver()->EmitNativeCode(&parallel_move);
7489}
7490
Mark Mendell9c86b482015-09-18 13:36:07 -04007491Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7492 // Create a fixup to be used to create and address the jump table.
7493 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007494 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007495
7496 // We have to populate the jump tables.
7497 fixups_to_jump_tables_.push_back(table_fixup);
7498 return Address::RIP(table_fixup);
7499}
7500
Mark Mendellea5af682015-10-22 17:35:49 -04007501void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7502 const Address& addr_high,
7503 int64_t v,
7504 HInstruction* instruction) {
7505 if (IsInt<32>(v)) {
7506 int32_t v_32 = v;
7507 __ movq(addr_low, Immediate(v_32));
7508 MaybeRecordImplicitNullCheck(instruction);
7509 } else {
7510 // Didn't fit in a register. Do it in pieces.
7511 int32_t low_v = Low32Bits(v);
7512 int32_t high_v = High32Bits(v);
7513 __ movl(addr_low, Immediate(low_v));
7514 MaybeRecordImplicitNullCheck(instruction);
7515 __ movl(addr_high, Immediate(high_v));
7516 }
7517}
7518
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007519void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7520 const uint8_t* roots_data,
7521 const PatchInfo<Label>& info,
7522 uint64_t index_in_table) const {
7523 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7524 uintptr_t address =
7525 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7526 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7527 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7528 dchecked_integral_cast<uint32_t>(address);
7529}
7530
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007531void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7532 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007533 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007534 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007535 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007536 }
7537
7538 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007539 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007540 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007541 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007542 }
7543}
7544
Roland Levillain4d027112015-07-01 15:41:14 +01007545#undef __
7546
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007547} // namespace x86_64
7548} // namespace art