blob: bea3da070af200aeb1e16b62d4de9d97a5377005 [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:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242 LoadClassSlowPathX86_64(HLoadClass* cls,
243 HInstruction* at,
244 uint32_t dex_pc,
245 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000246 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
248 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000251 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000252 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100253 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000255 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256
Vladimir Markoea4c1262017-02-06 19:59:33 +0000257 // Custom calling convention: RAX serves as both input and output.
258 __ movl(CpuRegister(RAX), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100259 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000260 instruction_,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000261 dex_pc_,
262 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100268
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000269 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000273 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
275
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100277 __ jmp(GetExitLabel());
278 }
279
Alexandre Rames9931f312015-06-19 14:47:01 +0100280 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
281
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100282 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 // The class this slow path will load.
284 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 // The dex PC of `at_`.
287 const uint32_t dex_pc_;
288
289 // Whether to initialize the class.
290 const bool do_clinit_;
291
292 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100293};
294
Vladimir Markoaad75c62016-10-03 08:46:48 +0000295class LoadStringSlowPathX86_64 : public SlowPathCode {
296 public:
297 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
298
299 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
300 LocationSummary* locations = instruction_->GetLocations();
301 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
302
303 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
304 __ Bind(GetEntryLabel());
305 SaveLiveRegisters(codegen, locations);
306
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000307 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100308 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000309 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000310 x86_64_codegen->InvokeRuntime(kQuickResolveString,
311 instruction_,
312 instruction_->GetDexPc(),
313 this);
314 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
315 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
316 RestoreLiveRegisters(codegen, locations);
317
Vladimir Markoaad75c62016-10-03 08:46:48 +0000318 __ jmp(GetExitLabel());
319 }
320
321 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
322
323 private:
324 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
325};
326
Andreas Gampe85b62f22015-09-09 13:15:38 -0700327class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000330 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000332 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100334 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 DCHECK(instruction_->IsCheckCast()
336 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
Roland Levillain0d5a2812015-11-13 10:07:31 +0000338 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000341 if (kPoisonHeapReferences &&
342 instruction_->IsCheckCast() &&
343 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
344 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
345 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<CpuRegister>());
346 }
347
Vladimir Marko87584542017-12-12 17:47:52 +0000348 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349 SaveLiveRegisters(codegen, locations);
350 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000351
352 // We're moving two locations to locations that could overlap, so we need a parallel
353 // move resolver.
354 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800355 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800356 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100357 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800359 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100360 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100362 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364 } else {
365 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800366 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
367 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 if (!is_fatal_) {
371 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000372 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000374
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 RestoreLiveRegisters(codegen, locations);
376 __ jmp(GetExitLabel());
377 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378 }
379
Alexandre Rames9931f312015-06-19 14:47:01 +0100380 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
381
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000382 bool IsFatal() const OVERRIDE { return is_fatal_; }
383
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386
387 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
388};
389
Andreas Gampe85b62f22015-09-09 13:15:38 -0700390class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 public:
Aart Bik42249c32016-01-07 15:33:50 -0800392 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394
395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000396 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100398 LocationSummary* locations = instruction_->GetLocations();
399 SaveLiveRegisters(codegen, locations);
400 InvokeRuntimeCallingConvention calling_convention;
401 x86_64_codegen->Load32BitValue(
402 CpuRegister(calling_convention.GetRegisterAt(0)),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100404 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 }
407
Alexandre Rames9931f312015-06-19 14:47:01 +0100408 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
409
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
412};
413
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414class ArraySetSlowPathX86_64 : public SlowPathCode {
415 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100428 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
Roland Levillain0d5a2812015-11-13 10:07:31 +0000442 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100443 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
450
451 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
453};
454
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000466class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
467 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
469 Location ref,
470 bool unpoison_ref_before_marking)
471 : SlowPathCode(instruction),
472 ref_(ref),
473 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000474 DCHECK(kEmitCompilerReadBarrier);
475 }
476
477 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
478
479 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
480 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100481 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
482 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000483 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100484 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000485 DCHECK(instruction_->IsInstanceFieldGet() ||
486 instruction_->IsStaticFieldGet() ||
487 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100488 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000489 instruction_->IsLoadClass() ||
490 instruction_->IsLoadString() ||
491 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100492 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100493 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
494 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000495 << "Unexpected instruction in read barrier marking slow path: "
496 << instruction_->DebugName();
497
498 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100501 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000502 }
Roland Levillain4359e612016-07-20 11:32:19 +0100503 // No need to save live registers; it's taken care of by the
504 // entrypoint. Also, there is no need to update the stack mask,
505 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000506 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 DCHECK_NE(ref_reg, RSP);
508 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100509 // "Compact" slow path, saving two moves.
510 //
511 // Instead of using the standard runtime calling convention (input
512 // and output in R0):
513 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100517 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100519 // of a dedicated entrypoint:
520 //
521 // rX <- ReadBarrierMarkRegX(rX)
522 //
523 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100524 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100525 // This runtime call does not require a stack map.
526 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000527 __ jmp(GetExitLabel());
528 }
529
530 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100531 // The location (register) of the marked object reference.
532 const Location ref_;
533 // Should the reference in `ref_` be unpoisoned prior to marking it?
534 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000535
536 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
537};
538
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100539// Slow path marking an object reference `ref` during a read barrier,
540// and if needed, atomically updating the field `obj.field` in the
541// object `obj` holding this reference after marking (contrary to
542// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
543// `obj.field`).
544//
545// This means that after the execution of this slow path, both `ref`
546// and `obj.field` will be up-to-date; i.e., after the flip, both will
547// hold the same to-space reference (unless another thread installed
548// another object reference (different from `ref`) in `obj.field`).
549class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
550 public:
551 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
552 Location ref,
553 CpuRegister obj,
554 const Address& field_addr,
555 bool unpoison_ref_before_marking,
556 CpuRegister temp1,
557 CpuRegister temp2)
558 : SlowPathCode(instruction),
559 ref_(ref),
560 obj_(obj),
561 field_addr_(field_addr),
562 unpoison_ref_before_marking_(unpoison_ref_before_marking),
563 temp1_(temp1),
564 temp2_(temp2) {
565 DCHECK(kEmitCompilerReadBarrier);
566 }
567
568 const char* GetDescription() const OVERRIDE {
569 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
570 }
571
572 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
573 LocationSummary* locations = instruction_->GetLocations();
574 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
575 Register ref_reg = ref_cpu_reg.AsRegister();
576 DCHECK(locations->CanCall());
577 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
578 // This slow path is only used by the UnsafeCASObject intrinsic.
579 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
580 << "Unexpected instruction in read barrier marking and field updating slow path: "
581 << instruction_->DebugName();
582 DCHECK(instruction_->GetLocations()->Intrinsified());
583 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
584
585 __ Bind(GetEntryLabel());
586 if (unpoison_ref_before_marking_) {
587 // Object* ref = ref_addr->AsMirrorPtr()
588 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
589 }
590
591 // Save the old (unpoisoned) reference.
592 __ movl(temp1_, ref_cpu_reg);
593
594 // No need to save live registers; it's taken care of by the
595 // entrypoint. Also, there is no need to update the stack mask,
596 // as this runtime call will not trigger a garbage collection.
597 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
598 DCHECK_NE(ref_reg, RSP);
599 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
600 // "Compact" slow path, saving two moves.
601 //
602 // Instead of using the standard runtime calling convention (input
603 // and output in R0):
604 //
605 // RDI <- ref
606 // RAX <- ReadBarrierMark(RDI)
607 // ref <- RAX
608 //
609 // we just use rX (the register containing `ref`) as input and output
610 // of a dedicated entrypoint:
611 //
612 // rX <- ReadBarrierMarkRegX(rX)
613 //
614 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100615 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100616 // This runtime call does not require a stack map.
617 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
618
619 // If the new reference is different from the old reference,
620 // update the field in the holder (`*field_addr`).
621 //
622 // Note that this field could also hold a different object, if
623 // another thread had concurrently changed it. In that case, the
624 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
625 // operation below would abort the CAS, leaving the field as-is.
626 NearLabel done;
627 __ cmpl(temp1_, ref_cpu_reg);
628 __ j(kEqual, &done);
629
630 // Update the the holder's field atomically. This may fail if
631 // mutator updates before us, but it's OK. This is achived
632 // using a strong compare-and-set (CAS) operation with relaxed
633 // memory synchronization ordering, where the expected value is
634 // the old reference and the desired value is the new reference.
635 // This operation is implemented with a 32-bit LOCK CMPXLCHG
636 // instruction, which requires the expected value (the old
637 // reference) to be in EAX. Save RAX beforehand, and move the
638 // expected value (stored in `temp1_`) into EAX.
639 __ movq(temp2_, CpuRegister(RAX));
640 __ movl(CpuRegister(RAX), temp1_);
641
642 // Convenience aliases.
643 CpuRegister base = obj_;
644 CpuRegister expected = CpuRegister(RAX);
645 CpuRegister value = ref_cpu_reg;
646
647 bool base_equals_value = (base.AsRegister() == value.AsRegister());
648 Register value_reg = ref_reg;
649 if (kPoisonHeapReferences) {
650 if (base_equals_value) {
651 // If `base` and `value` are the same register location, move
652 // `value_reg` to a temporary register. This way, poisoning
653 // `value_reg` won't invalidate `base`.
654 value_reg = temp1_.AsRegister();
655 __ movl(CpuRegister(value_reg), base);
656 }
657
658 // Check that the register allocator did not assign the location
659 // of `expected` (RAX) to `value` nor to `base`, so that heap
660 // poisoning (when enabled) works as intended below.
661 // - If `value` were equal to `expected`, both references would
662 // be poisoned twice, meaning they would not be poisoned at
663 // all, as heap poisoning uses address negation.
664 // - If `base` were equal to `expected`, poisoning `expected`
665 // would invalidate `base`.
666 DCHECK_NE(value_reg, expected.AsRegister());
667 DCHECK_NE(base.AsRegister(), expected.AsRegister());
668
669 __ PoisonHeapReference(expected);
670 __ PoisonHeapReference(CpuRegister(value_reg));
671 }
672
673 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
674
675 // If heap poisoning is enabled, we need to unpoison the values
676 // that were poisoned earlier.
677 if (kPoisonHeapReferences) {
678 if (base_equals_value) {
679 // `value_reg` has been moved to a temporary register, no need
680 // to unpoison it.
681 } else {
682 __ UnpoisonHeapReference(CpuRegister(value_reg));
683 }
684 // No need to unpoison `expected` (RAX), as it is be overwritten below.
685 }
686
687 // Restore RAX.
688 __ movq(CpuRegister(RAX), temp2_);
689
690 __ Bind(&done);
691 __ jmp(GetExitLabel());
692 }
693
694 private:
695 // The location (register) of the marked object reference.
696 const Location ref_;
697 // The register containing the object holding the marked object reference field.
698 const CpuRegister obj_;
699 // The address of the marked reference field. The base of this address must be `obj_`.
700 const Address field_addr_;
701
702 // Should the reference in `ref_` be unpoisoned prior to marking it?
703 const bool unpoison_ref_before_marking_;
704
705 const CpuRegister temp1_;
706 const CpuRegister temp2_;
707
708 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
709};
710
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711// Slow path generating a read barrier for a heap reference.
712class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
713 public:
714 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
715 Location out,
716 Location ref,
717 Location obj,
718 uint32_t offset,
719 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000720 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721 out_(out),
722 ref_(ref),
723 obj_(obj),
724 offset_(offset),
725 index_(index) {
726 DCHECK(kEmitCompilerReadBarrier);
727 // If `obj` is equal to `out` or `ref`, it means the initial
728 // object has been overwritten by (or after) the heap object
729 // reference load to be instrumented, e.g.:
730 //
731 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000732 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000733 //
734 // In that case, we have lost the information about the original
735 // object, and the emitted read barrier cannot work properly.
736 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
737 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
738}
739
740 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
741 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
742 LocationSummary* locations = instruction_->GetLocations();
743 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
744 DCHECK(locations->CanCall());
745 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100746 DCHECK(instruction_->IsInstanceFieldGet() ||
747 instruction_->IsStaticFieldGet() ||
748 instruction_->IsArrayGet() ||
749 instruction_->IsInstanceOf() ||
750 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700751 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000752 << "Unexpected instruction in read barrier for heap reference slow path: "
753 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000754
755 __ Bind(GetEntryLabel());
756 SaveLiveRegisters(codegen, locations);
757
758 // We may have to change the index's value, but as `index_` is a
759 // constant member (like other "inputs" of this slow path),
760 // introduce a copy of it, `index`.
761 Location index = index_;
762 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100763 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000764 if (instruction_->IsArrayGet()) {
765 // Compute real offset and store it in index_.
766 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
767 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
768 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
769 // We are about to change the value of `index_reg` (see the
770 // calls to art::x86_64::X86_64Assembler::shll and
771 // art::x86_64::X86_64Assembler::AddImmediate below), but it
772 // has not been saved by the previous call to
773 // art::SlowPathCode::SaveLiveRegisters, as it is a
774 // callee-save register --
775 // art::SlowPathCode::SaveLiveRegisters does not consider
776 // callee-save registers, as it has been designed with the
777 // assumption that callee-save registers are supposed to be
778 // handled by the called function. So, as a callee-save
779 // register, `index_reg` _would_ eventually be saved onto
780 // the stack, but it would be too late: we would have
781 // changed its value earlier. Therefore, we manually save
782 // it here into another freely available register,
783 // `free_reg`, chosen of course among the caller-save
784 // registers (as a callee-save `free_reg` register would
785 // exhibit the same problem).
786 //
787 // Note we could have requested a temporary register from
788 // the register allocator instead; but we prefer not to, as
789 // this is a slow path, and we know we can find a
790 // caller-save register that is available.
791 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
792 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
793 index_reg = free_reg;
794 index = Location::RegisterLocation(index_reg);
795 } else {
796 // The initial register stored in `index_` has already been
797 // saved in the call to art::SlowPathCode::SaveLiveRegisters
798 // (as it is not a callee-save register), so we can freely
799 // use it.
800 }
801 // Shifting the index value contained in `index_reg` by the
802 // scale factor (2) cannot overflow in practice, as the
803 // runtime is unable to allocate object arrays with a size
804 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
805 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
806 static_assert(
807 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
808 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
809 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
810 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100811 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
812 // intrinsics, `index_` is not shifted by a scale factor of 2
813 // (as in the case of ArrayGet), as it is actually an offset
814 // to an object field within an object.
815 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000816 DCHECK(instruction_->GetLocations()->Intrinsified());
817 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
818 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
819 << instruction_->AsInvoke()->GetIntrinsic();
820 DCHECK_EQ(offset_, 0U);
821 DCHECK(index_.IsRegister());
822 }
823 }
824
825 // We're moving two or three locations to locations that could
826 // overlap, so we need a parallel move resolver.
827 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100828 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 parallel_move.AddMove(ref_,
830 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100831 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000832 nullptr);
833 parallel_move.AddMove(obj_,
834 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100835 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000836 nullptr);
837 if (index.IsValid()) {
838 parallel_move.AddMove(index,
839 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100840 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000841 nullptr);
842 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
843 } else {
844 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
845 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
846 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100847 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000848 instruction_,
849 instruction_->GetDexPc(),
850 this);
851 CheckEntrypointTypes<
852 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
853 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
854
855 RestoreLiveRegisters(codegen, locations);
856 __ jmp(GetExitLabel());
857 }
858
859 const char* GetDescription() const OVERRIDE {
860 return "ReadBarrierForHeapReferenceSlowPathX86_64";
861 }
862
863 private:
864 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
865 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
866 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
867 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
868 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
869 return static_cast<CpuRegister>(i);
870 }
871 }
872 // We shall never fail to find a free caller-save register, as
873 // there are more than two core caller-save registers on x86-64
874 // (meaning it is possible to find one which is different from
875 // `ref` and `obj`).
876 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
877 LOG(FATAL) << "Could not find a free caller-save register";
878 UNREACHABLE();
879 }
880
Roland Levillain0d5a2812015-11-13 10:07:31 +0000881 const Location out_;
882 const Location ref_;
883 const Location obj_;
884 const uint32_t offset_;
885 // An additional location containing an index to an array.
886 // Only used for HArrayGet and the UnsafeGetObject &
887 // UnsafeGetObjectVolatile intrinsics.
888 const Location index_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
891};
892
893// Slow path generating a read barrier for a GC root.
894class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
895 public:
896 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000897 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000898 DCHECK(kEmitCompilerReadBarrier);
899 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000900
901 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
902 LocationSummary* locations = instruction_->GetLocations();
903 DCHECK(locations->CanCall());
904 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000905 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
906 << "Unexpected instruction in read barrier for GC root slow path: "
907 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000908
909 __ Bind(GetEntryLabel());
910 SaveLiveRegisters(codegen, locations);
911
912 InvokeRuntimeCallingConvention calling_convention;
913 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
914 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100915 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000916 instruction_,
917 instruction_->GetDexPc(),
918 this);
919 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
920 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
921
922 RestoreLiveRegisters(codegen, locations);
923 __ jmp(GetExitLabel());
924 }
925
926 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
927
928 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000929 const Location out_;
930 const Location root_;
931
932 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
933};
934
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100935#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100936// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
937#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100938
Roland Levillain4fa13f62015-07-06 18:11:54 +0100939inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700940 switch (cond) {
941 case kCondEQ: return kEqual;
942 case kCondNE: return kNotEqual;
943 case kCondLT: return kLess;
944 case kCondLE: return kLessEqual;
945 case kCondGT: return kGreater;
946 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700947 case kCondB: return kBelow;
948 case kCondBE: return kBelowEqual;
949 case kCondA: return kAbove;
950 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700951 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100952 LOG(FATAL) << "Unreachable";
953 UNREACHABLE();
954}
955
Aart Bike9f37602015-10-09 11:15:55 -0700956// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100957inline Condition X86_64FPCondition(IfCondition cond) {
958 switch (cond) {
959 case kCondEQ: return kEqual;
960 case kCondNE: return kNotEqual;
961 case kCondLT: return kBelow;
962 case kCondLE: return kBelowEqual;
963 case kCondGT: return kAbove;
964 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700965 default: break; // should not happen
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800966 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100967 LOG(FATAL) << "Unreachable";
968 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700969}
970
Vladimir Markodc151b22015-10-15 18:02:30 +0100971HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
972 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100973 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000974 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100975}
976
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100977void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
978 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800979 // All registers are assumed to be correctly set up.
Vladimir Marko4ee8e292017-06-02 15:39:30 +0000980
Vladimir Marko58155012015-08-19 12:49:41 +0000981 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
982 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100983 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000984 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100985 uint32_t offset =
986 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
987 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000988 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100989 }
Vladimir Marko58155012015-08-19 12:49:41 +0000990 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000991 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000992 break;
Vladimir Marko65979462017-05-19 17:25:12 +0100993 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
994 DCHECK(GetCompilerOptions().IsBootImage());
995 __ leal(temp.AsRegister<CpuRegister>(),
996 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000997 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +0100998 break;
Vladimir Marko58155012015-08-19 12:49:41 +0000999 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +00001000 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +00001001 break;
Vladimir Markob066d432018-01-03 13:14:37 +00001002 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
1003 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1004 __ movl(temp.AsRegister<CpuRegister>(),
1005 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001006 RecordBootImageRelRoPatch(GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00001007 break;
1008 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001009 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001010 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001011 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001012 RecordMethodBssEntryPatch(invoke);
Vladimir Marko58155012015-08-19 12:49:41 +00001013 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001014 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001015 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1016 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1017 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001018 }
Vladimir Marko58155012015-08-19 12:49:41 +00001019 }
1020
1021 switch (invoke->GetCodePtrLocation()) {
1022 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1023 __ call(&frame_entry_label_);
1024 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001025 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1026 // (callee_method + offset_of_quick_compiled_code)()
1027 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1028 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001029 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001030 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001031 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001032 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001033
1034 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001035}
1036
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001037void CodeGeneratorX86_64::GenerateVirtualCall(
1038 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001039 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1040 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1041 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001042
1043 // Use the calling convention instead of the location of the receiver, as
1044 // intrinsics may have put the receiver in a different register. In the intrinsics
1045 // slow path, the arguments have been moved to the right place, so here we are
1046 // guaranteed that the receiver is the first register of the calling convention.
1047 InvokeDexCallingConvention calling_convention;
1048 Register receiver = calling_convention.GetRegisterAt(0);
1049
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001050 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001051 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001052 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001053 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001054 // Instead of simply (possibly) unpoisoning `temp` here, we should
1055 // emit a read barrier for the previous class reference load.
1056 // However this is not required in practice, as this is an
1057 // intermediate/temporary reference and because the current
1058 // concurrent copying collector keeps the from-space memory
1059 // intact/accessible until the end of the marking phase (the
1060 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001061 __ MaybeUnpoisonHeapReference(temp);
1062 // temp = temp->GetMethodAt(method_offset);
1063 __ movq(temp, Address(temp, method_offset));
1064 // call temp->GetEntryPoint();
1065 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001066 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001067 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001068}
1069
Vladimir Marko6fd16062018-06-26 11:02:04 +01001070void CodeGeneratorX86_64::RecordBootImageIntrinsicPatch(uint32_t intrinsic_data) {
1071 boot_image_intrinsic_patches_.emplace_back(/* target_dex_file */ nullptr, intrinsic_data);
1072 __ Bind(&boot_image_intrinsic_patches_.back().label);
1073}
1074
Vladimir Markob066d432018-01-03 13:14:37 +00001075void CodeGeneratorX86_64::RecordBootImageRelRoPatch(uint32_t boot_image_offset) {
1076 boot_image_method_patches_.emplace_back(/* target_dex_file */ nullptr, boot_image_offset);
1077 __ Bind(&boot_image_method_patches_.back().label);
1078}
1079
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001080void CodeGeneratorX86_64::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
1081 boot_image_method_patches_.emplace_back(
1082 invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01001083 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001084}
1085
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001086void CodeGeneratorX86_64::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
1087 method_bss_entry_patches_.emplace_back(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
1088 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001089}
1090
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001091void CodeGeneratorX86_64::RecordBootImageTypePatch(HLoadClass* load_class) {
1092 boot_image_type_patches_.emplace_back(
1093 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001094 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001095}
1096
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001097Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001098 type_bss_entry_patches_.emplace_back(
1099 &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001100 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001101}
1102
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001103void CodeGeneratorX86_64::RecordBootImageStringPatch(HLoadString* load_string) {
1104 boot_image_string_patches_.emplace_back(
1105 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
1106 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01001107}
1108
Vladimir Markoaad75c62016-10-03 08:46:48 +00001109Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1110 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001111 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001112 &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001113 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001114}
1115
Vladimir Marko6fd16062018-06-26 11:02:04 +01001116void CodeGeneratorX86_64::LoadBootImageAddress(CpuRegister reg, uint32_t boot_image_reference) {
1117 if (GetCompilerOptions().IsBootImage()) {
1118 __ leal(reg, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
1119 RecordBootImageIntrinsicPatch(boot_image_reference);
1120 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01001121 DCHECK(Runtime::Current()->IsAotCompiler());
1122 __ 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 {
1125 gc::Heap* heap = Runtime::Current()->GetHeap();
1126 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001127 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001128 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
1129 }
1130}
1131
Vladimir Marko6fd16062018-06-26 11:02:04 +01001132void CodeGeneratorX86_64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1133 uint32_t boot_image_offset) {
1134 DCHECK(invoke->IsStatic());
1135 InvokeRuntimeCallingConvention calling_convention;
1136 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
1137 if (GetCompilerOptions().IsBootImage()) {
1138 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1139 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1140 __ leal(argument,
1141 Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
1142 MethodReference target_method = invoke->GetTargetMethod();
1143 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1144 boot_image_type_patches_.emplace_back(target_method.dex_file, type_idx.index_);
1145 __ Bind(&boot_image_type_patches_.back().label);
1146 } else {
1147 LoadBootImageAddress(argument, boot_image_offset);
1148 }
1149 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1150 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1151}
1152
Vladimir Markoaad75c62016-10-03 08:46:48 +00001153// The label points to the end of the "movl" or another instruction but the literal offset
1154// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1155constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1156
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001157template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001158inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1159 const ArenaDeque<PatchInfo<Label>>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001160 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001161 for (const PatchInfo<Label>& info : infos) {
1162 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1163 linker_patches->push_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001164 Factory(literal_offset, info.target_dex_file, info.label.Position(), info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001165 }
1166}
1167
Vladimir Marko6fd16062018-06-26 11:02:04 +01001168template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1169linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1170 const DexFile* target_dex_file,
1171 uint32_t pc_insn_offset,
1172 uint32_t boot_image_offset) {
1173 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1174 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001175}
1176
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001177void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00001178 DCHECK(linker_patches->empty());
1179 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001180 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001181 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001182 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001183 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001184 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001185 string_bss_entry_patches_.size() +
1186 boot_image_intrinsic_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001187 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001188 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001189 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1190 boot_image_method_patches_, linker_patches);
1191 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1192 boot_image_type_patches_, linker_patches);
1193 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001194 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001195 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1196 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001197 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001198 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001199 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001200 DCHECK(boot_image_type_patches_.empty());
1201 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001202 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001203 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001204 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1205 method_bss_entry_patches_, linker_patches);
1206 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1207 type_bss_entry_patches_, linker_patches);
1208 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1209 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001210 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001211}
1212
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001213void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001214 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215}
1216
1217void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001218 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219}
1220
Vladimir Markoa0431112018-06-25 09:32:54 +01001221const X86_64InstructionSetFeatures& CodeGeneratorX86_64::GetInstructionSetFeatures() const {
1222 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86_64InstructionSetFeatures();
1223}
1224
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001225size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1226 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1227 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001228}
1229
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001230size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1231 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1232 return kX86_64WordSize;
1233}
1234
1235size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001236 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001237 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001238 } else {
1239 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1240 }
1241 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001242}
1243
1244size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001245 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001246 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001247 } else {
1248 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1249 }
1250 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001251}
1252
Calin Juravle175dc732015-08-25 15:42:32 +01001253void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1254 HInstruction* instruction,
1255 uint32_t dex_pc,
1256 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001257 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001258 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1259 if (EntrypointRequiresStackMap(entrypoint)) {
1260 RecordPcInfo(instruction, dex_pc, slow_path);
1261 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001262}
1263
Roland Levillaindec8f632016-07-22 17:10:06 +01001264void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1265 HInstruction* instruction,
1266 SlowPathCode* slow_path) {
1267 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001268 GenerateInvokeRuntime(entry_point_offset);
1269}
1270
1271void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001272 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1273}
1274
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001275static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001276// Use a fake return address register to mimic Quick.
1277static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001278CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001279 const CompilerOptions& compiler_options,
1280 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001281 : CodeGenerator(graph,
1282 kNumberOfCpuRegisters,
1283 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001284 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001285 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1286 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001287 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001288 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1289 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001290 compiler_options,
1291 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001292 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001293 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001294 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001295 move_resolver_(graph->GetAllocator(), this),
1296 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001297 constant_area_start_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001298 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1299 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1300 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1301 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001302 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001303 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001304 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001305 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1306 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1307 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001308 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1309}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001310
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001311InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1312 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001313 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001314 assembler_(codegen->GetAssembler()),
1315 codegen_(codegen) {}
1316
David Brazdil58282f42016-01-14 12:45:10 +00001317void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001318 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001319 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001320
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001321 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001322 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001323}
1324
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001325static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001326 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001327}
David Srbecky9d8606d2015-04-12 09:35:32 +01001328
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001329static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001330 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001331}
1332
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001333void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001334 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001335 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001336 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001337 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001338 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001339
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001340 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1341 __ addw(Address(CpuRegister(kMethodRegisterArgument),
1342 ArtMethod::HotnessCountOffset().Int32Value()),
1343 Immediate(1));
1344 }
1345
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001346 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001347 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86_64);
1348 __ testq(CpuRegister(RAX), Address(CpuRegister(RSP), -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001349 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001350 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001351
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001352 if (HasEmptyFrame()) {
1353 return;
1354 }
1355
Nicolas Geoffray98893962015-01-21 12:32:32 +00001356 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001357 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001358 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001359 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001360 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1361 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001362 }
1363 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001364
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001365 int adjust = GetFrameSize() - GetCoreSpillSize();
1366 __ subq(CpuRegister(RSP), Immediate(adjust));
1367 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001368 uint32_t xmm_spill_location = GetFpuSpillStart();
1369 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001370
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001371 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1372 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001373 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1374 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1375 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001376 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001377 }
1378
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001379 // Save the current method if we need it. Note that we do not
1380 // do this in HCurrentMethod, as the instruction might have been removed
1381 // in the SSA graph.
1382 if (RequiresCurrentMethod()) {
1383 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1384 CpuRegister(kMethodRegisterArgument));
1385 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001386
1387 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1388 // Initialize should_deoptimize flag to 0.
1389 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1390 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001391}
1392
1393void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001394 __ cfi().RememberState();
1395 if (!HasEmptyFrame()) {
1396 uint32_t xmm_spill_location = GetFpuSpillStart();
1397 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1398 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1399 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1400 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1401 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1402 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1403 }
1404 }
1405
1406 int adjust = GetFrameSize() - GetCoreSpillSize();
1407 __ addq(CpuRegister(RSP), Immediate(adjust));
1408 __ cfi().AdjustCFAOffset(-adjust);
1409
1410 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1411 Register reg = kCoreCalleeSaves[i];
1412 if (allocated_registers_.ContainsCoreRegister(reg)) {
1413 __ popq(CpuRegister(reg));
1414 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1415 __ cfi().Restore(DWARFReg(reg));
1416 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001417 }
1418 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001419 __ ret();
1420 __ cfi().RestoreState();
1421 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001422}
1423
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001424void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1425 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001426}
1427
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001428void CodeGeneratorX86_64::Move(Location destination, Location source) {
1429 if (source.Equals(destination)) {
1430 return;
1431 }
1432 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001433 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001434 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001435 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001436 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001437 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001438 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001439 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1440 } else if (source.IsConstant()) {
1441 HConstant* constant = source.GetConstant();
1442 if (constant->IsLongConstant()) {
1443 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1444 } else {
1445 Load32BitValue(dest, GetInt32ValueOf(constant));
1446 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001447 } else {
1448 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001449 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001450 }
1451 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001452 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001453 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001454 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001455 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001456 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1457 } else if (source.IsConstant()) {
1458 HConstant* constant = source.GetConstant();
1459 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1460 if (constant->IsFloatConstant()) {
1461 Load32BitValue(dest, static_cast<int32_t>(value));
1462 } else {
1463 Load64BitValue(dest, value);
1464 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001465 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001466 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001467 } else {
1468 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001469 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001470 }
1471 } else if (destination.IsStackSlot()) {
1472 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001473 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001474 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001475 } else if (source.IsFpuRegister()) {
1476 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001477 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001478 } else if (source.IsConstant()) {
1479 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001480 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001481 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001482 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001483 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001484 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1485 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001486 }
1487 } else {
1488 DCHECK(destination.IsDoubleStackSlot());
1489 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001490 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001491 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001492 } else if (source.IsFpuRegister()) {
1493 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001495 } else if (source.IsConstant()) {
1496 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001497 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1498 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001499 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001500 } else {
1501 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001502 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1503 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001504 }
1505 }
1506}
1507
Calin Juravle175dc732015-08-25 15:42:32 +01001508void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1509 DCHECK(location.IsRegister());
1510 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1511}
1512
Calin Juravlee460d1d2015-09-29 04:52:17 +01001513void CodeGeneratorX86_64::MoveLocation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 Location dst, Location src, DataType::Type dst_type ATTRIBUTE_UNUSED) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001515 Move(dst, src);
1516}
1517
1518void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1519 if (location.IsRegister()) {
1520 locations->AddTemp(location);
1521 } else {
1522 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1523 }
1524}
1525
David Brazdilfc6a86a2015-06-26 10:33:45 +00001526void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001527 if (successor->IsExitBlock()) {
1528 DCHECK(got->GetPrevious()->AlwaysThrows());
1529 return; // no code needed
1530 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001531
1532 HBasicBlock* block = got->GetBlock();
1533 HInstruction* previous = got->GetPrevious();
1534
1535 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001536 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001537 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1538 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), 0));
1539 __ addw(Address(CpuRegister(TMP), ArtMethod::HotnessCountOffset().Int32Value()),
1540 Immediate(1));
1541 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001542 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1543 return;
1544 }
1545
1546 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1547 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1548 }
1549 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001550 __ jmp(codegen_->GetLabelOf(successor));
1551 }
1552}
1553
David Brazdilfc6a86a2015-06-26 10:33:45 +00001554void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1555 got->SetLocations(nullptr);
1556}
1557
1558void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1559 HandleGoto(got, got->GetSuccessor());
1560}
1561
1562void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1563 try_boundary->SetLocations(nullptr);
1564}
1565
1566void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1567 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1568 if (!successor->IsExitBlock()) {
1569 HandleGoto(try_boundary, successor);
1570 }
1571}
1572
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001573void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1574 exit->SetLocations(nullptr);
1575}
1576
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001577void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001578}
1579
Mark Mendell152408f2015-12-31 12:28:50 -05001580template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001581void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001582 LabelType* true_label,
1583 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001584 if (cond->IsFPConditionTrueIfNaN()) {
1585 __ j(kUnordered, true_label);
1586 } else if (cond->IsFPConditionFalseIfNaN()) {
1587 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001588 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001589 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001590}
1591
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001592void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001593 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001594
Mark Mendellc4701932015-04-10 13:18:51 -04001595 Location left = locations->InAt(0);
1596 Location right = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001597 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001598 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001599 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001600 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001601 case DataType::Type::kInt8:
1602 case DataType::Type::kUint16:
1603 case DataType::Type::kInt16:
1604 case DataType::Type::kInt32:
1605 case DataType::Type::kReference: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001606 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001607 break;
1608 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001610 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001611 break;
1612 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001613 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001614 if (right.IsFpuRegister()) {
1615 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1616 } else if (right.IsConstant()) {
1617 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1618 codegen_->LiteralFloatAddress(
1619 right.GetConstant()->AsFloatConstant()->GetValue()));
1620 } else {
1621 DCHECK(right.IsStackSlot());
1622 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1623 Address(CpuRegister(RSP), right.GetStackIndex()));
1624 }
Mark Mendellc4701932015-04-10 13:18:51 -04001625 break;
1626 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001628 if (right.IsFpuRegister()) {
1629 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1630 } else if (right.IsConstant()) {
1631 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1632 codegen_->LiteralDoubleAddress(
1633 right.GetConstant()->AsDoubleConstant()->GetValue()));
1634 } else {
1635 DCHECK(right.IsDoubleStackSlot());
1636 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1637 Address(CpuRegister(RSP), right.GetStackIndex()));
1638 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001639 break;
1640 }
1641 default:
1642 LOG(FATAL) << "Unexpected condition type " << type;
1643 }
1644}
1645
1646template<class LabelType>
1647void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1648 LabelType* true_target_in,
1649 LabelType* false_target_in) {
1650 // Generated branching requires both targets to be explicit. If either of the
1651 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1652 LabelType fallthrough_target;
1653 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1654 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1655
1656 // Generate the comparison to set the CC.
1657 GenerateCompareTest(condition);
1658
1659 // Now generate the correct jump(s).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001660 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001661 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001662 case DataType::Type::kInt64: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001663 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1664 break;
1665 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001666 case DataType::Type::kFloat32: {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001667 GenerateFPJumps(condition, true_target, false_target);
1668 break;
1669 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001670 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001671 GenerateFPJumps(condition, true_target, false_target);
1672 break;
1673 }
1674 default:
1675 LOG(FATAL) << "Unexpected condition type " << type;
1676 }
1677
David Brazdil0debae72015-11-12 18:37:00 +00001678 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001679 __ jmp(false_target);
1680 }
David Brazdil0debae72015-11-12 18:37:00 +00001681
1682 if (fallthrough_target.IsLinked()) {
1683 __ Bind(&fallthrough_target);
1684 }
Mark Mendellc4701932015-04-10 13:18:51 -04001685}
1686
David Brazdil0debae72015-11-12 18:37:00 +00001687static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1688 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1689 // are set only strictly before `branch`. We can't use the eflags on long
1690 // conditions if they are materialized due to the complex branching.
1691 return cond->IsCondition() &&
1692 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001694}
1695
Mark Mendell152408f2015-12-31 12:28:50 -05001696template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001697void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001698 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001699 LabelType* true_target,
1700 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001701 HInstruction* cond = instruction->InputAt(condition_input_index);
1702
1703 if (true_target == nullptr && false_target == nullptr) {
1704 // Nothing to do. The code always falls through.
1705 return;
1706 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001707 // Constant condition, statically compared against "true" (integer value 1).
1708 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001709 if (true_target != nullptr) {
1710 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001711 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001712 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001713 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001714 if (false_target != nullptr) {
1715 __ jmp(false_target);
1716 }
1717 }
1718 return;
1719 }
1720
1721 // The following code generates these patterns:
1722 // (1) true_target == nullptr && false_target != nullptr
1723 // - opposite condition true => branch to false_target
1724 // (2) true_target != nullptr && false_target == nullptr
1725 // - condition true => branch to true_target
1726 // (3) true_target != nullptr && false_target != nullptr
1727 // - condition true => branch to true_target
1728 // - branch to false_target
1729 if (IsBooleanValueOrMaterializedCondition(cond)) {
1730 if (AreEflagsSetFrom(cond, instruction)) {
1731 if (true_target == nullptr) {
1732 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1733 } else {
1734 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1735 }
1736 } else {
1737 // Materialized condition, compare against 0.
1738 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1739 if (lhs.IsRegister()) {
1740 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1741 } else {
1742 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1743 }
1744 if (true_target == nullptr) {
1745 __ j(kEqual, false_target);
1746 } else {
1747 __ j(kNotEqual, true_target);
1748 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001749 }
1750 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001751 // Condition has not been materialized, use its inputs as the
1752 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001753 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001754
David Brazdil0debae72015-11-12 18:37:00 +00001755 // If this is a long or FP comparison that has been folded into
1756 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 DataType::Type type = condition->InputAt(0)->GetType();
1758 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001759 GenerateCompareTestAndBranch(condition, true_target, false_target);
1760 return;
1761 }
1762
1763 Location lhs = condition->GetLocations()->InAt(0);
1764 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001765 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001766 if (true_target == nullptr) {
1767 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1768 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001769 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001770 }
Dave Allison20dfc792014-06-16 20:44:29 -07001771 }
David Brazdil0debae72015-11-12 18:37:00 +00001772
1773 // If neither branch falls through (case 3), the conditional branch to `true_target`
1774 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1775 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001776 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001777 }
1778}
1779
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001780void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001781 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001782 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001783 locations->SetInAt(0, Location::Any());
1784 }
1785}
1786
1787void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001788 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1789 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1790 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1791 nullptr : codegen_->GetLabelOf(true_successor);
1792 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1793 nullptr : codegen_->GetLabelOf(false_successor);
1794 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001795}
1796
1797void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001798 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001800 InvokeRuntimeCallingConvention calling_convention;
1801 RegisterSet caller_saves = RegisterSet::Empty();
1802 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1803 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001804 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001805 locations->SetInAt(0, Location::Any());
1806 }
1807}
1808
1809void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001810 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001811 GenerateTestAndBranch<Label>(deoptimize,
1812 /* condition_input_index */ 0,
1813 slow_path->GetEntryLabel(),
1814 /* false_target */ nullptr);
1815}
1816
Mingyao Yang063fc772016-08-02 11:02:54 -07001817void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001818 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001819 LocationSummary(flag, LocationSummary::kNoCall);
1820 locations->SetOut(Location::RequiresRegister());
1821}
1822
1823void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1824 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1825 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1826}
1827
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001828static bool SelectCanUseCMOV(HSelect* select) {
1829 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001831 return false;
1832 }
1833
1834 // A FP condition doesn't generate the single CC that we need.
1835 HInstruction* condition = select->GetCondition();
1836 if (condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001837 DataType::IsFloatingPointType(condition->InputAt(0)->GetType())) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001838 return false;
1839 }
1840
1841 // We can generate a CMOV for this Select.
1842 return true;
1843}
1844
David Brazdil74eb1b22015-12-14 11:44:01 +00001845void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001846 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001848 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001849 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001850 } else {
1851 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001852 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001853 if (select->InputAt(1)->IsConstant()) {
1854 locations->SetInAt(1, Location::RequiresRegister());
1855 } else {
1856 locations->SetInAt(1, Location::Any());
1857 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001858 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001859 locations->SetInAt(1, Location::Any());
1860 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001861 }
1862 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1863 locations->SetInAt(2, Location::RequiresRegister());
1864 }
1865 locations->SetOut(Location::SameAsFirstInput());
1866}
1867
1868void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1869 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001870 if (SelectCanUseCMOV(select)) {
1871 // If both the condition and the source types are integer, we can generate
1872 // a CMOV to implement Select.
1873 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001874 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001875 DCHECK(locations->InAt(0).Equals(locations->Out()));
1876
1877 HInstruction* select_condition = select->GetCondition();
1878 Condition cond = kNotEqual;
1879
1880 // Figure out how to test the 'condition'.
1881 if (select_condition->IsCondition()) {
1882 HCondition* condition = select_condition->AsCondition();
1883 if (!condition->IsEmittedAtUseSite()) {
1884 // This was a previously materialized condition.
1885 // Can we use the existing condition code?
1886 if (AreEflagsSetFrom(condition, select)) {
1887 // Materialization was the previous instruction. Condition codes are right.
1888 cond = X86_64IntegerCondition(condition->GetCondition());
1889 } else {
1890 // No, we have to recreate the condition code.
1891 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1892 __ testl(cond_reg, cond_reg);
1893 }
1894 } else {
1895 GenerateCompareTest(condition);
1896 cond = X86_64IntegerCondition(condition->GetCondition());
1897 }
1898 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001899 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001900 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1901 __ testl(cond_reg, cond_reg);
1902 }
1903
1904 // If the condition is true, overwrite the output, which already contains false.
1905 // Generate the correct sized CMOV.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 bool is_64_bit = DataType::Is64BitType(select->GetType());
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001907 if (value_true_loc.IsRegister()) {
1908 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1909 } else {
1910 __ cmov(cond,
1911 value_false,
1912 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1913 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001914 } else {
1915 NearLabel false_target;
1916 GenerateTestAndBranch<NearLabel>(select,
1917 /* condition_input_index */ 2,
1918 /* true_target */ nullptr,
1919 &false_target);
1920 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1921 __ Bind(&false_target);
1922 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001923}
1924
David Srbecky0cf44932015-12-09 14:09:59 +00001925void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001926 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001927}
1928
David Srbeckyd28f4a02016-03-14 17:14:24 +00001929void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1930 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001931}
1932
1933void CodeGeneratorX86_64::GenerateNop() {
1934 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001935}
1936
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001938 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001939 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001940 // Handle the long/FP comparisons made in instruction simplification.
1941 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001942 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001943 locations->SetInAt(0, Location::RequiresRegister());
1944 locations->SetInAt(1, Location::Any());
1945 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001946 case DataType::Type::kFloat32:
1947 case DataType::Type::kFloat64:
Mark Mendellc4701932015-04-10 13:18:51 -04001948 locations->SetInAt(0, Location::RequiresFpuRegister());
1949 locations->SetInAt(1, Location::Any());
1950 break;
1951 default:
1952 locations->SetInAt(0, Location::RequiresRegister());
1953 locations->SetInAt(1, Location::Any());
1954 break;
1955 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001956 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001957 locations->SetOut(Location::RequiresRegister());
1958 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959}
1960
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001962 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001963 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001964 }
Mark Mendellc4701932015-04-10 13:18:51 -04001965
1966 LocationSummary* locations = cond->GetLocations();
1967 Location lhs = locations->InAt(0);
1968 Location rhs = locations->InAt(1);
1969 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001970 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001971
1972 switch (cond->InputAt(0)->GetType()) {
1973 default:
1974 // Integer case.
1975
1976 // Clear output register: setcc only sets the low byte.
1977 __ xorl(reg, reg);
1978
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001979 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001980 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001981 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001983 // Clear output register: setcc only sets the low byte.
1984 __ xorl(reg, reg);
1985
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001986 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001987 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001988 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kFloat32: {
Mark Mendellc4701932015-04-10 13:18:51 -04001990 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1991 if (rhs.IsConstant()) {
1992 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1993 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1994 } else if (rhs.IsStackSlot()) {
1995 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1996 } else {
1997 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1998 }
1999 GenerateFPJumps(cond, &true_label, &false_label);
2000 break;
2001 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002003 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
2004 if (rhs.IsConstant()) {
2005 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
2006 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
2007 } else if (rhs.IsDoubleStackSlot()) {
2008 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
2009 } else {
2010 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
2011 }
2012 GenerateFPJumps(cond, &true_label, &false_label);
2013 break;
2014 }
2015 }
2016
2017 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002018 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002019
Roland Levillain4fa13f62015-07-06 18:11:54 +01002020 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002021 __ Bind(&false_label);
2022 __ xorl(reg, reg);
2023 __ jmp(&done_label);
2024
Roland Levillain4fa13f62015-07-06 18:11:54 +01002025 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002026 __ Bind(&true_label);
2027 __ movl(reg, Immediate(1));
2028 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002029}
2030
2031void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002033}
2034
2035void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002036 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002037}
2038
2039void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002040 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002041}
2042
2043void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002044 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002045}
2046
2047void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002049}
2050
2051void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002052 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002053}
2054
2055void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002056 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002057}
2058
2059void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002060 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002061}
2062
2063void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002064 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002065}
2066
2067void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002068 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002069}
2070
2071void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002072 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002073}
2074
2075void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002076 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077}
2078
Aart Bike9f37602015-10-09 11:15:55 -07002079void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002081}
2082
2083void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002085}
2086
2087void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002088 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002089}
2090
2091void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002093}
2094
2095void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002097}
2098
2099void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002100 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002101}
2102
2103void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002105}
2106
2107void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002108 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002109}
2110
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002111void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002112 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002113 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002114 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002116 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002119 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kInt32:
2121 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002122 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002123 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2125 break;
2126 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 case DataType::Type::kFloat32:
2128 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002129 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002130 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002131 locations->SetOut(Location::RequiresRegister());
2132 break;
2133 }
2134 default:
2135 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2136 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002137}
2138
2139void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002140 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002142 Location left = locations->InAt(0);
2143 Location right = locations->InAt(1);
2144
Mark Mendell0c9497d2015-08-21 09:30:05 -04002145 NearLabel less, greater, done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 DataType::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002147 Condition less_cond = kLess;
2148
Calin Juravleddb7df22014-11-25 20:56:51 +00002149 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002151 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002154 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002155 case DataType::Type::kInt32: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002156 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002157 break;
2158 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 case DataType::Type::kInt64: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002160 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002161 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002162 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 case DataType::Type::kFloat32: {
Mark Mendell40741f32015-04-20 22:10:34 -04002164 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2165 if (right.IsConstant()) {
2166 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2167 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2168 } else if (right.IsStackSlot()) {
2169 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2170 } else {
2171 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2172 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002173 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002174 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002175 break;
2176 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 case DataType::Type::kFloat64: {
Mark Mendell40741f32015-04-20 22:10:34 -04002178 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2179 if (right.IsConstant()) {
2180 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2181 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2182 } else if (right.IsDoubleStackSlot()) {
2183 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2184 } else {
2185 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2186 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002187 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002188 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002189 break;
2190 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002191 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002192 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002193 }
Aart Bika19616e2016-02-01 18:57:58 -08002194
Calin Juravleddb7df22014-11-25 20:56:51 +00002195 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002196 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002197 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002198
Calin Juravle91debbc2014-11-26 19:01:09 +00002199 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002200 __ movl(out, Immediate(1));
2201 __ jmp(&done);
2202
2203 __ Bind(&less);
2204 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002205
2206 __ Bind(&done);
2207}
2208
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002209void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002210 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002211 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002212 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213}
2214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002215void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002216 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002217}
2218
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002219void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2220 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002221 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002222 locations->SetOut(Location::ConstantLocation(constant));
2223}
2224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002225void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002226 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002227}
2228
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002229void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002230 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002231 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002232 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002233}
2234
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002235void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002236 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237}
2238
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002239void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2240 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002242 locations->SetOut(Location::ConstantLocation(constant));
2243}
2244
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002245void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002246 // Will be generated at use site.
2247}
2248
2249void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2250 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002251 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002252 locations->SetOut(Location::ConstantLocation(constant));
2253}
2254
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002255void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2256 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002257 // Will be generated at use site.
2258}
2259
Igor Murashkind01745e2017-04-05 16:40:31 -07002260void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2261 constructor_fence->SetLocations(nullptr);
2262}
2263
2264void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2265 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2266 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2267}
2268
Calin Juravle27df7582015-04-17 19:12:31 +01002269void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2270 memory_barrier->SetLocations(nullptr);
2271}
2272
2273void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002274 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002275}
2276
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002277void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2278 ret->SetLocations(nullptr);
2279}
2280
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002281void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002282 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002283}
2284
2285void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002286 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002287 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002288 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002289 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002290 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002291 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 case DataType::Type::kInt8:
2293 case DataType::Type::kUint16:
2294 case DataType::Type::kInt16:
2295 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002297 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002298 break;
2299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kFloat32:
2301 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002302 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002303 break;
2304
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002305 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002307 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002308}
2309
2310void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2311 if (kIsDebugBuild) {
2312 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002313 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002315 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002316 case DataType::Type::kInt8:
2317 case DataType::Type::kUint16:
2318 case DataType::Type::kInt16:
2319 case DataType::Type::kInt32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002321 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002322 break;
2323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kFloat32:
2325 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002327 XMM0);
2328 break;
2329
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002330 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002331 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002332 }
2333 }
2334 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002335}
2336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002338 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002339 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002341 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 case DataType::Type::kInt8:
2343 case DataType::Type::kUint16:
2344 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002345 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 case DataType::Type::kInt32:
Aart Bik66c158e2018-01-31 12:55:04 -08002347 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002349 return Location::RegisterLocation(RAX);
2350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002352 return Location::NoLocation();
2353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kFloat64:
2355 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002356 return Location::FpuRegisterLocation(XMM0);
2357 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002358
2359 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002360}
2361
2362Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2363 return Location::RegisterLocation(kMethodRegisterArgument);
2364}
2365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(DataType::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002367 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002368 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002369 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002370 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002371 case DataType::Type::kInt8:
2372 case DataType::Type::kUint16:
2373 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002374 case DataType::Type::kInt32: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002375 uint32_t index = gp_index_++;
2376 stack_index_++;
2377 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002378 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002379 } else {
2380 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2381 }
2382 }
2383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kInt64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002385 uint32_t index = gp_index_;
2386 stack_index_ += 2;
2387 if (index < calling_convention.GetNumberOfRegisters()) {
2388 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002389 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002390 } else {
2391 gp_index_ += 2;
2392 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2393 }
2394 }
2395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002397 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002398 stack_index_++;
2399 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002400 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002401 } else {
2402 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2403 }
2404 }
2405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002407 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002408 stack_index_ += 2;
2409 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002410 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002411 } else {
2412 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2413 }
2414 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002415
Aart Bik66c158e2018-01-31 12:55:04 -08002416 case DataType::Type::kUint32:
2417 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 case DataType::Type::kVoid:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002419 LOG(FATAL) << "Unexpected parameter type " << type;
2420 break;
2421 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002422 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002423}
2424
Calin Juravle175dc732015-08-25 15:42:32 +01002425void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2426 // The trampoline uses the same calling convention as dex calling conventions,
2427 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2428 // the method_idx.
2429 HandleInvoke(invoke);
2430}
2431
2432void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2433 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2434}
2435
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002436void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002437 // Explicit clinit checks triggered by static invokes must have been pruned by
2438 // art::PrepareForRegisterAllocation.
2439 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002440
Mark Mendellfb8d2792015-03-31 22:16:59 -04002441 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002442 if (intrinsic.TryDispatch(invoke)) {
2443 return;
2444 }
2445
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002446 HandleInvoke(invoke);
2447}
2448
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002449static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2450 if (invoke->GetLocations()->Intrinsified()) {
2451 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2452 intrinsic.Dispatch(invoke);
2453 return true;
2454 }
2455 return false;
2456}
2457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002458void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002459 // Explicit clinit checks triggered by static invokes must have been pruned by
2460 // art::PrepareForRegisterAllocation.
2461 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002462
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002463 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2464 return;
2465 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002466
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002467 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002468 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002469 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002470}
2471
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002472void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002473 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002474 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002475}
2476
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002477void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002478 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002479 if (intrinsic.TryDispatch(invoke)) {
2480 return;
2481 }
2482
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002483 HandleInvoke(invoke);
2484}
2485
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002486void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002487 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2488 return;
2489 }
2490
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002491 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002492 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002493}
2494
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002495void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2496 HandleInvoke(invoke);
2497 // Add the hidden argument.
2498 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2499}
2500
2501void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2502 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002503 LocationSummary* locations = invoke->GetLocations();
2504 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2505 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002506 Location receiver = locations->InAt(0);
2507 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2508
Roland Levillain0d5a2812015-11-13 10:07:31 +00002509 // Set the hidden argument. This is safe to do this here, as RAX
2510 // won't be modified thereafter, before the `call` instruction.
2511 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002512 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002513
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002514 if (receiver.IsStackSlot()) {
2515 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002516 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002517 __ movl(temp, Address(temp, class_offset));
2518 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002519 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002520 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002521 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002522 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002523 // Instead of simply (possibly) unpoisoning `temp` here, we should
2524 // emit a read barrier for the previous class reference load.
2525 // However this is not required in practice, as this is an
2526 // intermediate/temporary reference and because the current
2527 // concurrent copying collector keeps the from-space memory
2528 // intact/accessible until the end of the marking phase (the
2529 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002530 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002531 // temp = temp->GetAddressOfIMT()
2532 __ movq(temp,
2533 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2534 // temp = temp->GetImtEntryAt(method_offset);
2535 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002536 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002537 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002538 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002539 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002540 __ call(Address(
2541 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002542
2543 DCHECK(!codegen_->IsLeafMethod());
2544 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2545}
2546
Orion Hodsonac141392017-01-13 11:53:47 +00002547void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2548 HandleInvoke(invoke);
2549}
2550
2551void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2552 codegen_->GenerateInvokePolymorphicCall(invoke);
2553}
2554
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002555void LocationsBuilderX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2556 HandleInvoke(invoke);
2557}
2558
2559void InstructionCodeGeneratorX86_64::VisitInvokeCustom(HInvokeCustom* invoke) {
2560 codegen_->GenerateInvokeCustomCall(invoke);
2561}
2562
Roland Levillain88cb1752014-10-20 16:36:47 +01002563void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2564 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002565 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002566 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 case DataType::Type::kInt32:
2568 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002569 locations->SetInAt(0, Location::RequiresRegister());
2570 locations->SetOut(Location::SameAsFirstInput());
2571 break;
2572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002573 case DataType::Type::kFloat32:
2574 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002575 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002576 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002577 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2582 }
2583}
2584
2585void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2586 LocationSummary* locations = neg->GetLocations();
2587 Location out = locations->Out();
2588 Location in = locations->InAt(0);
2589 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002591 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002592 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002594 break;
2595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002597 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002598 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002600 break;
2601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002603 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002604 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002605 // Implement float negation with an exclusive or with value
2606 // 0x80000000 (mask for bit 31, representing the sign of a
2607 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002608 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002609 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002610 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002611 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002614 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002615 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002616 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002617 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002618 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002619 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002621 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002622 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002623
2624 default:
2625 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2626 }
2627}
2628
Roland Levillaindff1f282014-11-05 14:15:05 +00002629void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2630 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002631 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 DataType::Type result_type = conversion->GetResultType();
2633 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002634 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2635 << input_type << " -> " << result_type;
David Brazdil46e2a392015-03-16 17:31:52 +00002636
Roland Levillaindff1f282014-11-05 14:15:05 +00002637 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002638 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002640 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002641 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002642 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2643 locations->SetInAt(0, Location::Any());
2644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002645 break;
2646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002648 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002650 locations->SetInAt(0, Location::Any());
2651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2652 break;
2653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002655 locations->SetInAt(0, Location::RequiresFpuRegister());
2656 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002657 break;
2658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002659 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002660 locations->SetInAt(0, Location::RequiresFpuRegister());
2661 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002662 break;
2663
2664 default:
2665 LOG(FATAL) << "Unexpected type conversion from " << input_type
2666 << " to " << result_type;
2667 }
2668 break;
2669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002671 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002675 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kInt16:
2677 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002678 // TODO: We would benefit from a (to-be-implemented)
2679 // Location::RegisterOrStackSlot requirement for this input.
2680 locations->SetInAt(0, Location::RequiresRegister());
2681 locations->SetOut(Location::RequiresRegister());
2682 break;
2683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002685 locations->SetInAt(0, Location::RequiresFpuRegister());
2686 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002687 break;
2688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002690 locations->SetInAt(0, Location::RequiresFpuRegister());
2691 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002692 break;
2693
2694 default:
2695 LOG(FATAL) << "Unexpected type conversion from " << input_type
2696 << " to " << result_type;
2697 }
2698 break;
2699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002701 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002702 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002703 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002705 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 case DataType::Type::kInt16:
2707 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002708 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002709 locations->SetOut(Location::RequiresFpuRegister());
2710 break;
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002713 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002714 locations->SetOut(Location::RequiresFpuRegister());
2715 break;
2716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04002718 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002719 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002720 break;
2721
2722 default:
2723 LOG(FATAL) << "Unexpected type conversion from " << input_type
2724 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002725 }
Roland Levillaincff13742014-11-17 14:32:17 +00002726 break;
2727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002729 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002731 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002733 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 case DataType::Type::kInt16:
2735 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002736 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002737 locations->SetOut(Location::RequiresFpuRegister());
2738 break;
2739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002741 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002742 locations->SetOut(Location::RequiresFpuRegister());
2743 break;
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04002746 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002747 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002748 break;
2749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
2753 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002754 break;
2755
2756 default:
2757 LOG(FATAL) << "Unexpected type conversion from " << input_type
2758 << " to " << result_type;
2759 }
2760}
2761
2762void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2763 LocationSummary* locations = conversion->GetLocations();
2764 Location out = locations->Out();
2765 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 DataType::Type result_type = conversion->GetResultType();
2767 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002768 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2769 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002770 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002771 case DataType::Type::kUint8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002772 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002773 case DataType::Type::kInt8:
2774 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002775 case DataType::Type::kInt16:
2776 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002777 case DataType::Type::kInt64:
2778 if (in.IsRegister()) {
2779 __ movzxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2780 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2781 __ movzxb(out.AsRegister<CpuRegister>(),
2782 Address(CpuRegister(RSP), in.GetStackIndex()));
2783 } else {
2784 __ movl(out.AsRegister<CpuRegister>(),
2785 Immediate(static_cast<uint8_t>(Int64FromConstant(in.GetConstant()))));
2786 }
2787 break;
2788
2789 default:
2790 LOG(FATAL) << "Unexpected type conversion from " << input_type
2791 << " to " << result_type;
2792 }
2793 break;
2794
2795 case DataType::Type::kInt8:
2796 switch (input_type) {
2797 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002799 case DataType::Type::kInt16:
2800 case DataType::Type::kInt32:
2801 case DataType::Type::kInt64:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002802 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002803 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002804 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002805 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002806 Address(CpuRegister(RSP), in.GetStackIndex()));
2807 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002809 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002810 }
2811 break;
2812
2813 default:
2814 LOG(FATAL) << "Unexpected type conversion from " << input_type
2815 << " to " << result_type;
2816 }
2817 break;
2818
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002819 case DataType::Type::kUint16:
2820 switch (input_type) {
2821 case DataType::Type::kInt8:
2822 case DataType::Type::kInt16:
2823 case DataType::Type::kInt32:
2824 case DataType::Type::kInt64:
2825 if (in.IsRegister()) {
2826 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
2827 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
2828 __ movzxw(out.AsRegister<CpuRegister>(),
2829 Address(CpuRegister(RSP), in.GetStackIndex()));
2830 } else {
2831 __ movl(out.AsRegister<CpuRegister>(),
2832 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
2833 }
2834 break;
2835
2836 default:
2837 LOG(FATAL) << "Unexpected type conversion from " << input_type
2838 << " to " << result_type;
2839 }
2840 break;
2841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002843 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002845 case DataType::Type::kInt32:
2846 case DataType::Type::kInt64:
Roland Levillain01a8d712014-11-14 16:27:39 +00002847 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002849 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002851 Address(CpuRegister(RSP), in.GetStackIndex()));
2852 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002854 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002855 }
2856 break;
2857
2858 default:
2859 LOG(FATAL) << "Unexpected type conversion from " << input_type
2860 << " to " << result_type;
2861 }
2862 break;
2863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002864 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002865 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002867 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002868 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002869 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002870 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002871 Address(CpuRegister(RSP), in.GetStackIndex()));
2872 } else {
2873 DCHECK(in.IsConstant());
2874 DCHECK(in.GetConstant()->IsLongConstant());
2875 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002877 }
2878 break;
2879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002881 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2882 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002883 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002884
2885 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002886 // if input >= (float)INT_MAX goto done
2887 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002888 __ j(kAboveEqual, &done);
2889 // if input == NaN goto nan
2890 __ j(kUnordered, &nan);
2891 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002892 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002893 __ jmp(&done);
2894 __ Bind(&nan);
2895 // output = 0
2896 __ xorl(output, output);
2897 __ Bind(&done);
2898 break;
2899 }
2900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002902 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2903 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002904 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002905
2906 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002907 // if input >= (double)INT_MAX goto done
2908 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002909 __ j(kAboveEqual, &done);
2910 // if input == NaN goto nan
2911 __ j(kUnordered, &nan);
2912 // output = double-to-int-truncate(input)
2913 __ cvttsd2si(output, input);
2914 __ jmp(&done);
2915 __ Bind(&nan);
2916 // output = 0
2917 __ xorl(output, output);
2918 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002919 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002920 }
Roland Levillain946e1432014-11-11 17:35:19 +00002921
2922 default:
2923 LOG(FATAL) << "Unexpected type conversion from " << input_type
2924 << " to " << result_type;
2925 }
2926 break;
2927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002929 switch (input_type) {
2930 DCHECK(out.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002931 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002932 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002934 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 case DataType::Type::kInt16:
2936 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002937 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002939 break;
2940
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 case DataType::Type::kFloat32: {
Roland Levillain624279f2014-12-04 11:54:28 +00002942 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2943 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002944 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002945
Mark Mendell92e83bf2015-05-07 11:25:03 -04002946 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002947 // if input >= (float)LONG_MAX goto done
2948 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002949 __ j(kAboveEqual, &done);
2950 // if input == NaN goto nan
2951 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002952 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002953 __ cvttss2si(output, input, true);
2954 __ jmp(&done);
2955 __ Bind(&nan);
2956 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002957 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002958 __ Bind(&done);
2959 break;
2960 }
2961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002963 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2964 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002965 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002966
Mark Mendell92e83bf2015-05-07 11:25:03 -04002967 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002968 // if input >= (double)LONG_MAX goto done
2969 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002970 __ j(kAboveEqual, &done);
2971 // if input == NaN goto nan
2972 __ j(kUnordered, &nan);
2973 // output = double-to-long-truncate(input)
2974 __ cvttsd2si(output, input, true);
2975 __ jmp(&done);
2976 __ Bind(&nan);
2977 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002978 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002979 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002980 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002981 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002982
2983 default:
2984 LOG(FATAL) << "Unexpected type conversion from " << input_type
2985 << " to " << result_type;
2986 }
2987 break;
2988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002990 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002992 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002994 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002995 case DataType::Type::kInt16:
2996 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002997 if (in.IsRegister()) {
2998 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2999 } else if (in.IsConstant()) {
3000 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3001 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003002 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003003 } else {
3004 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3005 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3006 }
Roland Levillaincff13742014-11-17 14:32:17 +00003007 break;
3008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003010 if (in.IsRegister()) {
3011 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3012 } else if (in.IsConstant()) {
3013 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3014 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06003015 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003016 } else {
3017 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
3018 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3019 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003020 break;
3021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kFloat64:
Mark Mendell40741f32015-04-20 22:10:34 -04003023 if (in.IsFpuRegister()) {
3024 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3025 } else if (in.IsConstant()) {
3026 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
3027 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003028 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003029 } else {
3030 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
3031 Address(CpuRegister(RSP), in.GetStackIndex()));
3032 }
Roland Levillaincff13742014-11-17 14:32:17 +00003033 break;
3034
3035 default:
3036 LOG(FATAL) << "Unexpected type conversion from " << input_type
3037 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003038 }
Roland Levillaincff13742014-11-17 14:32:17 +00003039 break;
3040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003042 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003044 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003046 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003047 case DataType::Type::kInt16:
3048 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04003049 if (in.IsRegister()) {
3050 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3051 } else if (in.IsConstant()) {
3052 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3053 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003054 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003055 } else {
3056 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3057 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3058 }
Roland Levillaincff13742014-11-17 14:32:17 +00003059 break;
3060
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04003062 if (in.IsRegister()) {
3063 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3064 } else if (in.IsConstant()) {
3065 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3066 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003067 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003068 } else {
3069 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3070 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3071 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003072 break;
3073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kFloat32:
Mark Mendell40741f32015-04-20 22:10:34 -04003075 if (in.IsFpuRegister()) {
3076 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3077 } else if (in.IsConstant()) {
3078 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3079 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003080 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003081 } else {
3082 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3083 Address(CpuRegister(RSP), in.GetStackIndex()));
3084 }
Roland Levillaincff13742014-11-17 14:32:17 +00003085 break;
3086
3087 default:
3088 LOG(FATAL) << "Unexpected type conversion from " << input_type
3089 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003090 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003091 break;
3092
3093 default:
3094 LOG(FATAL) << "Unexpected type conversion from " << input_type
3095 << " to " << result_type;
3096 }
3097}
3098
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003099void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003101 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003102 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003103 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003104 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003105 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003107 break;
3108 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003111 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003112 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003113 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003115 break;
3116 }
3117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003118 case DataType::Type::kFloat64:
3119 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003120 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003121 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003122 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003124 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003125
3126 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003127 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129}
3130
3131void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3132 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003133 Location first = locations->InAt(0);
3134 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003135 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003137 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003138 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003139 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003140 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3141 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003142 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3143 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003144 } else {
3145 __ leal(out.AsRegister<CpuRegister>(), Address(
3146 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3147 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003148 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003149 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3150 __ addl(out.AsRegister<CpuRegister>(),
3151 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3152 } else {
3153 __ leal(out.AsRegister<CpuRegister>(), Address(
3154 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3155 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003156 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003157 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003159 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003160 break;
3161 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kInt64: {
Mark Mendell09b84632015-02-13 17:48:38 -05003164 if (second.IsRegister()) {
3165 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3166 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003167 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3168 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003169 } else {
3170 __ leaq(out.AsRegister<CpuRegister>(), Address(
3171 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3172 }
3173 } else {
3174 DCHECK(second.IsConstant());
3175 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3176 int32_t int32_value = Low32Bits(value);
3177 DCHECK_EQ(int32_value, value);
3178 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3179 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3180 } else {
3181 __ leaq(out.AsRegister<CpuRegister>(), Address(
3182 first.AsRegister<CpuRegister>(), int32_value));
3183 }
3184 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003185 break;
3186 }
3187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003189 if (second.IsFpuRegister()) {
3190 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3191 } else if (second.IsConstant()) {
3192 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003193 codegen_->LiteralFloatAddress(
3194 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003195 } else {
3196 DCHECK(second.IsStackSlot());
3197 __ addss(first.AsFpuRegister<XmmRegister>(),
3198 Address(CpuRegister(RSP), second.GetStackIndex()));
3199 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003200 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003201 }
3202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003203 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003204 if (second.IsFpuRegister()) {
3205 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3206 } else if (second.IsConstant()) {
3207 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003208 codegen_->LiteralDoubleAddress(
3209 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003210 } else {
3211 DCHECK(second.IsDoubleStackSlot());
3212 __ addsd(first.AsFpuRegister<XmmRegister>(),
3213 Address(CpuRegister(RSP), second.GetStackIndex()));
3214 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003215 break;
3216 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003217
3218 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003219 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003220 }
3221}
3222
3223void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003225 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003226 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kInt32: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetInAt(1, Location::Any());
3230 locations->SetOut(Location::SameAsFirstInput());
3231 break;
3232 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 case DataType::Type::kInt64: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003234 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003235 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003236 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003237 break;
3238 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 case DataType::Type::kFloat32:
3240 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003241 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003242 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003243 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003244 break;
Calin Juravle11351682014-10-23 15:38:15 +01003245 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003246 default:
Calin Juravle11351682014-10-23 15:38:15 +01003247 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003248 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003249}
3250
3251void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3252 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003253 Location first = locations->InAt(0);
3254 Location second = locations->InAt(1);
3255 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003256 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003257 case DataType::Type::kInt32: {
Calin Juravle11351682014-10-23 15:38:15 +01003258 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003260 } else if (second.IsConstant()) {
3261 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003262 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003263 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003264 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003265 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003266 break;
3267 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003268 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003269 if (second.IsConstant()) {
3270 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3271 DCHECK(IsInt<32>(value));
3272 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3273 } else {
3274 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3275 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003276 break;
3277 }
3278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003280 if (second.IsFpuRegister()) {
3281 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3282 } else if (second.IsConstant()) {
3283 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003284 codegen_->LiteralFloatAddress(
3285 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003286 } else {
3287 DCHECK(second.IsStackSlot());
3288 __ subss(first.AsFpuRegister<XmmRegister>(),
3289 Address(CpuRegister(RSP), second.GetStackIndex()));
3290 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003291 break;
Calin Juravle11351682014-10-23 15:38:15 +01003292 }
3293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003295 if (second.IsFpuRegister()) {
3296 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3297 } else if (second.IsConstant()) {
3298 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003299 codegen_->LiteralDoubleAddress(
3300 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003301 } else {
3302 DCHECK(second.IsDoubleStackSlot());
3303 __ subsd(first.AsFpuRegister<XmmRegister>(),
3304 Address(CpuRegister(RSP), second.GetStackIndex()));
3305 }
Calin Juravle11351682014-10-23 15:38:15 +01003306 break;
3307 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003308
3309 default:
Calin Juravle11351682014-10-23 15:38:15 +01003310 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003311 }
3312}
3313
Calin Juravle34bacdf2014-10-07 20:23:36 +01003314void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3315 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003316 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003317 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003318 case DataType::Type::kInt32: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319 locations->SetInAt(0, Location::RequiresRegister());
3320 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003321 if (mul->InputAt(1)->IsIntConstant()) {
3322 // Can use 3 operand multiply.
3323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3324 } else {
3325 locations->SetOut(Location::SameAsFirstInput());
3326 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003327 break;
3328 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003330 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003331 locations->SetInAt(1, Location::Any());
3332 if (mul->InputAt(1)->IsLongConstant() &&
3333 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003334 // Can use 3 operand multiply.
3335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3336 } else {
3337 locations->SetOut(Location::SameAsFirstInput());
3338 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339 break;
3340 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003341 case DataType::Type::kFloat32:
3342 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003343 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003344 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003345 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003346 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003347 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003348
3349 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003350 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003351 }
3352}
3353
3354void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3355 LocationSummary* locations = mul->GetLocations();
3356 Location first = locations->InAt(0);
3357 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003358 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003359 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003360 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003361 // The constant may have ended up in a register, so test explicitly to avoid
3362 // problems where the output may not be the same as the first operand.
3363 if (mul->InputAt(1)->IsIntConstant()) {
3364 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3365 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3366 } else if (second.IsRegister()) {
3367 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003368 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003369 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003370 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003371 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003372 __ imull(first.AsRegister<CpuRegister>(),
3373 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374 }
3375 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 case DataType::Type::kInt64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003377 // The constant may have ended up in a register, so test explicitly to avoid
3378 // problems where the output may not be the same as the first operand.
3379 if (mul->InputAt(1)->IsLongConstant()) {
3380 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3381 if (IsInt<32>(value)) {
3382 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3383 Immediate(static_cast<int32_t>(value)));
3384 } else {
3385 // Have to use the constant area.
3386 DCHECK(first.Equals(out));
3387 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3388 }
3389 } else if (second.IsRegister()) {
3390 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003391 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003392 } else {
3393 DCHECK(second.IsDoubleStackSlot());
3394 DCHECK(first.Equals(out));
3395 __ imulq(first.AsRegister<CpuRegister>(),
3396 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003397 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003398 break;
3399 }
3400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 case DataType::Type::kFloat32: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003402 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003403 if (second.IsFpuRegister()) {
3404 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3405 } else if (second.IsConstant()) {
3406 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003407 codegen_->LiteralFloatAddress(
3408 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003409 } else {
3410 DCHECK(second.IsStackSlot());
3411 __ mulss(first.AsFpuRegister<XmmRegister>(),
3412 Address(CpuRegister(RSP), second.GetStackIndex()));
3413 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003414 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003415 }
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kFloat64: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003418 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003419 if (second.IsFpuRegister()) {
3420 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3421 } else if (second.IsConstant()) {
3422 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003423 codegen_->LiteralDoubleAddress(
3424 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003425 } else {
3426 DCHECK(second.IsDoubleStackSlot());
3427 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3428 Address(CpuRegister(RSP), second.GetStackIndex()));
3429 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003430 break;
3431 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003432
3433 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003434 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003435 }
3436}
3437
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003438void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3439 uint32_t stack_adjustment, bool is_float) {
3440 if (source.IsStackSlot()) {
3441 DCHECK(is_float);
3442 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3443 } else if (source.IsDoubleStackSlot()) {
3444 DCHECK(!is_float);
3445 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3446 } else {
3447 // Write the value to the temporary location on the stack and load to FP stack.
3448 if (is_float) {
3449 Location stack_temp = Location::StackSlot(temp_offset);
3450 codegen_->Move(stack_temp, source);
3451 __ flds(Address(CpuRegister(RSP), temp_offset));
3452 } else {
3453 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3454 codegen_->Move(stack_temp, source);
3455 __ fldl(Address(CpuRegister(RSP), temp_offset));
3456 }
3457 }
3458}
3459
3460void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 DataType::Type type = rem->GetResultType();
3462 bool is_float = type == DataType::Type::kFloat32;
3463 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003464 LocationSummary* locations = rem->GetLocations();
3465 Location first = locations->InAt(0);
3466 Location second = locations->InAt(1);
3467 Location out = locations->Out();
3468
3469 // Create stack space for 2 elements.
3470 // TODO: enhance register allocator to ask for stack temporaries.
3471 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3472
3473 // Load the values to the FP stack in reverse order, using temporaries if needed.
3474 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3475 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3476
3477 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003478 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003479 __ Bind(&retry);
3480 __ fprem();
3481
3482 // Move FP status to AX.
3483 __ fstsw();
3484
3485 // And see if the argument reduction is complete. This is signaled by the
3486 // C2 FPU flag bit set to 0.
3487 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3488 __ j(kNotEqual, &retry);
3489
3490 // We have settled on the final value. Retrieve it into an XMM register.
3491 // Store FP top of stack to real stack.
3492 if (is_float) {
3493 __ fsts(Address(CpuRegister(RSP), 0));
3494 } else {
3495 __ fstl(Address(CpuRegister(RSP), 0));
3496 }
3497
3498 // Pop the 2 items from the FP stack.
3499 __ fucompp();
3500
3501 // Load the value from the stack into an XMM register.
3502 DCHECK(out.IsFpuRegister()) << out;
3503 if (is_float) {
3504 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3505 } else {
3506 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3507 }
3508
3509 // And remove the temporary stack space we allocated.
3510 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3511}
3512
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3514 DCHECK(instruction->IsDiv() || instruction->IsRem());
3515
3516 LocationSummary* locations = instruction->GetLocations();
3517 Location second = locations->InAt(1);
3518 DCHECK(second.IsConstant());
3519
3520 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3521 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003522 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523
3524 DCHECK(imm == 1 || imm == -1);
3525
3526 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003527 case DataType::Type::kInt32: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003528 if (instruction->IsRem()) {
3529 __ xorl(output_register, output_register);
3530 } else {
3531 __ movl(output_register, input_register);
3532 if (imm == -1) {
3533 __ negl(output_register);
3534 }
3535 }
3536 break;
3537 }
3538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003539 case DataType::Type::kInt64: {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003541 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 } else {
3543 __ movq(output_register, input_register);
3544 if (imm == -1) {
3545 __ negq(output_register);
3546 }
3547 }
3548 break;
3549 }
3550
3551 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003552 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003553 }
3554}
3555
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003556void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 LocationSummary* locations = instruction->GetLocations();
3558 Location second = locations->InAt(1);
3559
3560 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3561 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3562
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003563 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003564 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3565 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003566
3567 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003570 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003571 __ testl(numerator, numerator);
3572 __ cmov(kGreaterEqual, tmp, numerator);
3573 int shift = CTZ(imm);
3574 __ sarl(tmp, Immediate(shift));
3575
3576 if (imm < 0) {
3577 __ negl(tmp);
3578 }
3579
3580 __ movl(output_register, tmp);
3581 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003582 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3584
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003585 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586 __ addq(rdx, numerator);
3587 __ testq(numerator, numerator);
3588 __ cmov(kGreaterEqual, rdx, numerator);
3589 int shift = CTZ(imm);
3590 __ sarq(rdx, Immediate(shift));
3591
3592 if (imm < 0) {
3593 __ negq(rdx);
3594 }
3595
3596 __ movq(output_register, rdx);
3597 }
3598}
3599
3600void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3601 DCHECK(instruction->IsDiv() || instruction->IsRem());
3602
3603 LocationSummary* locations = instruction->GetLocations();
3604 Location second = locations->InAt(1);
3605
3606 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3607 : locations->GetTemp(0).AsRegister<CpuRegister>();
3608 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3609 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3610 : locations->Out().AsRegister<CpuRegister>();
3611 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3612
3613 DCHECK_EQ(RAX, eax.AsRegister());
3614 DCHECK_EQ(RDX, edx.AsRegister());
3615 if (instruction->IsDiv()) {
3616 DCHECK_EQ(RAX, out.AsRegister());
3617 } else {
3618 DCHECK_EQ(RDX, out.AsRegister());
3619 }
3620
3621 int64_t magic;
3622 int shift;
3623
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003624 // TODO: can these branches be written as one?
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 if (instruction->GetResultType() == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003626 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3627
3628 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3629
3630 __ movl(numerator, eax);
3631
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632 __ movl(eax, Immediate(magic));
3633 __ imull(numerator);
3634
3635 if (imm > 0 && magic < 0) {
3636 __ addl(edx, numerator);
3637 } else if (imm < 0 && magic > 0) {
3638 __ subl(edx, numerator);
3639 }
3640
3641 if (shift != 0) {
3642 __ sarl(edx, Immediate(shift));
3643 }
3644
3645 __ movl(eax, edx);
3646 __ shrl(edx, Immediate(31));
3647 __ addl(edx, eax);
3648
3649 if (instruction->IsRem()) {
3650 __ movl(eax, numerator);
3651 __ imull(edx, Immediate(imm));
3652 __ subl(eax, edx);
3653 __ movl(edx, eax);
3654 } else {
3655 __ movl(eax, edx);
3656 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003657 } else {
3658 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003661
3662 CpuRegister rax = eax;
3663 CpuRegister rdx = edx;
3664
3665 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3666
3667 // Save the numerator.
3668 __ movq(numerator, rax);
3669
3670 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003671 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003672
3673 // RDX:RAX = magic * numerator
3674 __ imulq(numerator);
3675
3676 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003677 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003678 __ addq(rdx, numerator);
3679 } else if (imm < 0 && magic > 0) {
3680 // RDX -= numerator
3681 __ subq(rdx, numerator);
3682 }
3683
3684 // Shift if needed.
3685 if (shift != 0) {
3686 __ sarq(rdx, Immediate(shift));
3687 }
3688
3689 // RDX += 1 if RDX < 0
3690 __ movq(rax, rdx);
3691 __ shrq(rdx, Immediate(63));
3692 __ addq(rdx, rax);
3693
3694 if (instruction->IsRem()) {
3695 __ movq(rax, numerator);
3696
3697 if (IsInt<32>(imm)) {
3698 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3699 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003700 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003701 }
3702
3703 __ subq(rax, rdx);
3704 __ movq(rdx, rax);
3705 } else {
3706 __ movq(rax, rdx);
3707 }
3708 }
3709}
3710
Calin Juravlebacfec32014-11-14 15:54:36 +00003711void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3712 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 DataType::Type type = instruction->GetResultType();
3714 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Calin Juravlebacfec32014-11-14 15:54:36 +00003715
3716 bool is_div = instruction->IsDiv();
3717 LocationSummary* locations = instruction->GetLocations();
3718
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003719 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3720 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003721
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003723 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003724
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003725 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003726 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003727
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003728 if (imm == 0) {
3729 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3730 } else if (imm == 1 || imm == -1) {
3731 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003732 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003733 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003734 } else {
3735 DCHECK(imm <= -2 || imm >= 2);
3736 GenerateDivRemWithAnyConstant(instruction);
3737 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003738 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003739 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003740 new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003741 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003742 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003743
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003744 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3745 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3746 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3747 // so it's safe to just use negl instead of more complex comparisons.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 if (type == DataType::Type::kInt32) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003749 __ cmpl(second_reg, Immediate(-1));
3750 __ j(kEqual, slow_path->GetEntryLabel());
3751 // edx:eax <- sign-extended of eax
3752 __ cdq();
3753 // eax = quotient, edx = remainder
3754 __ idivl(second_reg);
3755 } else {
3756 __ cmpq(second_reg, Immediate(-1));
3757 __ j(kEqual, slow_path->GetEntryLabel());
3758 // rdx:rax <- sign-extended of rax
3759 __ cqo();
3760 // rax = quotient, rdx = remainder
3761 __ idivq(second_reg);
3762 }
3763 __ Bind(slow_path->GetExitLabel());
3764 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003765}
3766
Calin Juravle7c4954d2014-10-28 16:57:40 +00003767void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3768 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003769 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003770 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kInt32:
3772 case DataType::Type::kInt64: {
Calin Juravled0d48522014-11-04 16:40:20 +00003773 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003774 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003775 locations->SetOut(Location::SameAsFirstInput());
3776 // Intel uses edx:eax as the dividend.
3777 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003778 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3779 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3780 // output and request another temp.
3781 if (div->InputAt(1)->IsConstant()) {
3782 locations->AddTemp(Location::RequiresRegister());
3783 }
Calin Juravled0d48522014-11-04 16:40:20 +00003784 break;
3785 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kFloat32:
3788 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003789 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003790 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003791 locations->SetOut(Location::SameAsFirstInput());
3792 break;
3793 }
3794
3795 default:
3796 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3797 }
3798}
3799
3800void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3801 LocationSummary* locations = div->GetLocations();
3802 Location first = locations->InAt(0);
3803 Location second = locations->InAt(1);
3804 DCHECK(first.Equals(locations->Out()));
3805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 DataType::Type type = div->GetResultType();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003808 case DataType::Type::kInt32:
3809 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003811 break;
3812 }
3813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 case DataType::Type::kFloat32: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003815 if (second.IsFpuRegister()) {
3816 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3817 } else if (second.IsConstant()) {
3818 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003819 codegen_->LiteralFloatAddress(
3820 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003821 } else {
3822 DCHECK(second.IsStackSlot());
3823 __ divss(first.AsFpuRegister<XmmRegister>(),
3824 Address(CpuRegister(RSP), second.GetStackIndex()));
3825 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003826 break;
3827 }
3828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kFloat64: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003830 if (second.IsFpuRegister()) {
3831 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3832 } else if (second.IsConstant()) {
3833 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003834 codegen_->LiteralDoubleAddress(
3835 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003836 } else {
3837 DCHECK(second.IsDoubleStackSlot());
3838 __ divsd(first.AsFpuRegister<XmmRegister>(),
3839 Address(CpuRegister(RSP), second.GetStackIndex()));
3840 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003841 break;
3842 }
3843
3844 default:
3845 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3846 }
3847}
3848
Calin Juravlebacfec32014-11-14 15:54:36 +00003849void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 DataType::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003851 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003852 new (GetGraph()->GetAllocator()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003853
3854 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003855 case DataType::Type::kInt32:
3856 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003857 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003858 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003859 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3860 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003861 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3862 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3863 // output and request another temp.
3864 if (rem->InputAt(1)->IsConstant()) {
3865 locations->AddTemp(Location::RequiresRegister());
3866 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003867 break;
3868 }
3869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003870 case DataType::Type::kFloat32:
3871 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003872 locations->SetInAt(0, Location::Any());
3873 locations->SetInAt(1, Location::Any());
3874 locations->SetOut(Location::RequiresFpuRegister());
3875 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003876 break;
3877 }
3878
3879 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003880 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003881 }
3882}
3883
3884void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003885 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003886 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003887 case DataType::Type::kInt32:
3888 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003889 GenerateDivRemIntegral(rem);
3890 break;
3891 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003892 case DataType::Type::kFloat32:
3893 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003894 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003895 break;
3896 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003897 default:
3898 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3899 }
3900}
3901
Aart Bik1f8d51b2018-02-15 10:42:37 -08003902static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3903 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3904 switch (minmax->GetResultType()) {
3905 case DataType::Type::kInt32:
3906 case DataType::Type::kInt64:
3907 locations->SetInAt(0, Location::RequiresRegister());
3908 locations->SetInAt(1, Location::RequiresRegister());
3909 locations->SetOut(Location::SameAsFirstInput());
3910 break;
3911 case DataType::Type::kFloat32:
3912 case DataType::Type::kFloat64:
3913 locations->SetInAt(0, Location::RequiresFpuRegister());
3914 locations->SetInAt(1, Location::RequiresFpuRegister());
3915 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
3916 // the second input to be the output (we can simply swap inputs).
3917 locations->SetOut(Location::SameAsFirstInput());
3918 break;
3919 default:
3920 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
3921 }
3922}
3923
Aart Bik351df3e2018-03-07 11:54:57 -08003924void InstructionCodeGeneratorX86_64::GenerateMinMaxInt(LocationSummary* locations,
3925 bool is_min,
3926 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08003927 Location op1_loc = locations->InAt(0);
3928 Location op2_loc = locations->InAt(1);
3929
3930 // Shortcut for same input locations.
3931 if (op1_loc.Equals(op2_loc)) {
3932 // Can return immediately, as op1_loc == out_loc.
3933 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
3934 // a copy here.
3935 DCHECK(locations->Out().Equals(op1_loc));
3936 return;
3937 }
3938
3939 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3940 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
3941
3942 // (out := op1)
3943 // out <=? op2
3944 // if out is min jmp done
3945 // out := op2
3946 // done:
3947
3948 if (type == DataType::Type::kInt64) {
3949 __ cmpq(out, op2);
3950 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ true);
3951 } else {
3952 DCHECK_EQ(type, DataType::Type::kInt32);
3953 __ cmpl(out, op2);
3954 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, /*is64bit*/ false);
3955 }
3956}
3957
3958void InstructionCodeGeneratorX86_64::GenerateMinMaxFP(LocationSummary* locations,
3959 bool is_min,
3960 DataType::Type type) {
3961 Location op1_loc = locations->InAt(0);
3962 Location op2_loc = locations->InAt(1);
3963 Location out_loc = locations->Out();
3964 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
3965
3966 // Shortcut for same input locations.
3967 if (op1_loc.Equals(op2_loc)) {
3968 DCHECK(out_loc.Equals(op1_loc));
3969 return;
3970 }
3971
3972 // (out := op1)
3973 // out <=? op2
3974 // if Nan jmp Nan_label
3975 // if out is min jmp done
3976 // if op2 is min jmp op2_label
3977 // handle -0/+0
3978 // jmp done
3979 // Nan_label:
3980 // out := NaN
3981 // op2_label:
3982 // out := op2
3983 // done:
3984 //
3985 // This removes one jmp, but needs to copy one input (op1) to out.
3986 //
3987 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
3988
3989 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
3990
3991 NearLabel nan, done, op2_label;
3992 if (type == DataType::Type::kFloat64) {
3993 __ ucomisd(out, op2);
3994 } else {
3995 DCHECK_EQ(type, DataType::Type::kFloat32);
3996 __ ucomiss(out, op2);
3997 }
3998
3999 __ j(Condition::kParityEven, &nan);
4000
4001 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4002 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4003
4004 // Handle 0.0/-0.0.
4005 if (is_min) {
4006 if (type == DataType::Type::kFloat64) {
4007 __ orpd(out, op2);
4008 } else {
4009 __ orps(out, op2);
4010 }
4011 } else {
4012 if (type == DataType::Type::kFloat64) {
4013 __ andpd(out, op2);
4014 } else {
4015 __ andps(out, op2);
4016 }
4017 }
4018 __ jmp(&done);
4019
4020 // NaN handling.
4021 __ Bind(&nan);
4022 if (type == DataType::Type::kFloat64) {
4023 __ movsd(out, codegen_->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
4024 } else {
4025 __ movss(out, codegen_->LiteralInt32Address(INT32_C(0x7FC00000)));
4026 }
4027 __ jmp(&done);
4028
4029 // out := op2;
4030 __ Bind(&op2_label);
4031 if (type == DataType::Type::kFloat64) {
4032 __ movsd(out, op2);
4033 } else {
4034 __ movss(out, op2);
4035 }
4036
4037 // Done.
4038 __ Bind(&done);
4039}
4040
Aart Bik351df3e2018-03-07 11:54:57 -08004041void InstructionCodeGeneratorX86_64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4042 DataType::Type type = minmax->GetResultType();
4043 switch (type) {
4044 case DataType::Type::kInt32:
4045 case DataType::Type::kInt64:
4046 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4047 break;
4048 case DataType::Type::kFloat32:
4049 case DataType::Type::kFloat64:
4050 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4051 break;
4052 default:
4053 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4054 }
4055}
4056
Aart Bik1f8d51b2018-02-15 10:42:37 -08004057void LocationsBuilderX86_64::VisitMin(HMin* min) {
4058 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4059}
4060
4061void InstructionCodeGeneratorX86_64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004062 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004063}
4064
4065void LocationsBuilderX86_64::VisitMax(HMax* max) {
4066 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4067}
4068
4069void InstructionCodeGeneratorX86_64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004070 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004071}
4072
Aart Bik3dad3412018-02-28 12:01:46 -08004073void LocationsBuilderX86_64::VisitAbs(HAbs* abs) {
4074 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4075 switch (abs->GetResultType()) {
4076 case DataType::Type::kInt32:
4077 case DataType::Type::kInt64:
4078 locations->SetInAt(0, Location::RequiresRegister());
4079 locations->SetOut(Location::SameAsFirstInput());
4080 locations->AddTemp(Location::RequiresRegister());
4081 break;
4082 case DataType::Type::kFloat32:
4083 case DataType::Type::kFloat64:
4084 locations->SetInAt(0, Location::RequiresFpuRegister());
4085 locations->SetOut(Location::SameAsFirstInput());
4086 locations->AddTemp(Location::RequiresFpuRegister());
4087 break;
4088 default:
4089 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4090 }
4091}
4092
4093void InstructionCodeGeneratorX86_64::VisitAbs(HAbs* abs) {
4094 LocationSummary* locations = abs->GetLocations();
4095 switch (abs->GetResultType()) {
4096 case DataType::Type::kInt32: {
4097 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4098 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4099 // Create mask.
4100 __ movl(mask, out);
4101 __ sarl(mask, Immediate(31));
4102 // Add mask.
4103 __ addl(out, mask);
4104 __ xorl(out, mask);
4105 break;
4106 }
4107 case DataType::Type::kInt64: {
4108 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4109 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
4110 // Create mask.
4111 __ movq(mask, out);
4112 __ sarq(mask, Immediate(63));
4113 // Add mask.
4114 __ addq(out, mask);
4115 __ xorq(out, mask);
4116 break;
4117 }
4118 case DataType::Type::kFloat32: {
4119 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4120 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4121 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
4122 __ andps(out, mask);
4123 break;
4124 }
4125 case DataType::Type::kFloat64: {
4126 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4127 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4128 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
4129 __ andpd(out, mask);
4130 break;
4131 }
4132 default:
4133 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4134 }
4135}
4136
Calin Juravled0d48522014-11-04 16:40:20 +00004137void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004138 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004139 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00004140}
4141
4142void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004143 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004144 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86_64(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004145 codegen_->AddSlowPath(slow_path);
4146
4147 LocationSummary* locations = instruction->GetLocations();
4148 Location value = locations->InAt(0);
4149
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004150 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004152 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004153 case DataType::Type::kInt8:
4154 case DataType::Type::kUint16:
4155 case DataType::Type::kInt16:
4156 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004157 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004159 __ j(kEqual, slow_path->GetEntryLabel());
4160 } else if (value.IsStackSlot()) {
4161 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4162 __ j(kEqual, slow_path->GetEntryLabel());
4163 } else {
4164 DCHECK(value.IsConstant()) << value;
4165 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004166 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004167 }
4168 }
4169 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004171 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004172 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004173 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004174 __ j(kEqual, slow_path->GetEntryLabel());
4175 } else if (value.IsDoubleStackSlot()) {
4176 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
4177 __ j(kEqual, slow_path->GetEntryLabel());
4178 } else {
4179 DCHECK(value.IsConstant()) << value;
4180 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004181 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004182 }
4183 }
4184 break;
4185 }
4186 default:
4187 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004188 }
Calin Juravled0d48522014-11-04 16:40:20 +00004189}
4190
Calin Juravle9aec02f2014-11-18 23:06:35 +00004191void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
4192 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4193
4194 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004195 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004196
4197 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004198 case DataType::Type::kInt32:
4199 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004200 locations->SetInAt(0, Location::RequiresRegister());
4201 // The shift count needs to be in CL.
4202 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
4203 locations->SetOut(Location::SameAsFirstInput());
4204 break;
4205 }
4206 default:
4207 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4208 }
4209}
4210
4211void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
4212 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4213
4214 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004215 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004216 Location second = locations->InAt(1);
4217
4218 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt32: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004220 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004221 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004222 if (op->IsShl()) {
4223 __ shll(first_reg, second_reg);
4224 } else if (op->IsShr()) {
4225 __ sarl(first_reg, second_reg);
4226 } else {
4227 __ shrl(first_reg, second_reg);
4228 }
4229 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004230 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004231 if (op->IsShl()) {
4232 __ shll(first_reg, imm);
4233 } else if (op->IsShr()) {
4234 __ sarl(first_reg, imm);
4235 } else {
4236 __ shrl(first_reg, imm);
4237 }
4238 }
4239 break;
4240 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004241 case DataType::Type::kInt64: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004242 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004243 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004244 if (op->IsShl()) {
4245 __ shlq(first_reg, second_reg);
4246 } else if (op->IsShr()) {
4247 __ sarq(first_reg, second_reg);
4248 } else {
4249 __ shrq(first_reg, second_reg);
4250 }
4251 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004252 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004253 if (op->IsShl()) {
4254 __ shlq(first_reg, imm);
4255 } else if (op->IsShr()) {
4256 __ sarq(first_reg, imm);
4257 } else {
4258 __ shrq(first_reg, imm);
4259 }
4260 }
4261 break;
4262 }
4263 default:
4264 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004265 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004266 }
4267}
4268
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004269void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004271 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004272
4273 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004274 case DataType::Type::kInt32:
4275 case DataType::Type::kInt64: {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004276 locations->SetInAt(0, Location::RequiresRegister());
4277 // The shift count needs to be in CL (unless it is a constant).
4278 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4279 locations->SetOut(Location::SameAsFirstInput());
4280 break;
4281 }
4282 default:
4283 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4284 UNREACHABLE();
4285 }
4286}
4287
4288void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4289 LocationSummary* locations = ror->GetLocations();
4290 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4291 Location second = locations->InAt(1);
4292
4293 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004294 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004295 if (second.IsRegister()) {
4296 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4297 __ rorl(first_reg, second_reg);
4298 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004299 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004300 __ rorl(first_reg, imm);
4301 }
4302 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004304 if (second.IsRegister()) {
4305 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4306 __ rorq(first_reg, second_reg);
4307 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004308 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004309 __ rorq(first_reg, imm);
4310 }
4311 break;
4312 default:
4313 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4314 UNREACHABLE();
4315 }
4316}
4317
Calin Juravle9aec02f2014-11-18 23:06:35 +00004318void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4319 HandleShift(shl);
4320}
4321
4322void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4323 HandleShift(shl);
4324}
4325
4326void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4327 HandleShift(shr);
4328}
4329
4330void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4331 HandleShift(shr);
4332}
4333
4334void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4335 HandleShift(ushr);
4336}
4337
4338void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4339 HandleShift(ushr);
4340}
4341
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004342void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004343 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4344 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004345 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004346 if (instruction->IsStringAlloc()) {
4347 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4348 } else {
4349 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004350 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004351 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004352}
4353
4354void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004355 // Note: if heap poisoning is enabled, the entry point takes cares
4356 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004357 if (instruction->IsStringAlloc()) {
4358 // String is allocated through StringFactory. Call NewEmptyString entry point.
4359 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004360 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004361 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4362 __ call(Address(temp, code_offset.SizeValue()));
4363 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4364 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004365 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004366 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004367 DCHECK(!codegen_->IsLeafMethod());
4368 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004369}
4370
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004371void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004372 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4373 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004374 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004375 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004376 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4377 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004378}
4379
4380void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004381 // Note: if heap poisoning is enabled, the entry point takes cares
4382 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004383 QuickEntrypointEnum entrypoint =
4384 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4385 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004386 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004387 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004388}
4389
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004390void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004391 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004392 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004393 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4394 if (location.IsStackSlot()) {
4395 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4396 } else if (location.IsDoubleStackSlot()) {
4397 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4398 }
4399 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004400}
4401
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004402void InstructionCodeGeneratorX86_64::VisitParameterValue(
4403 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004404 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004405}
4406
4407void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4408 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004409 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004410 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4411}
4412
4413void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4414 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4415 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004416}
4417
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004418void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4419 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004420 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004421 locations->SetInAt(0, Location::RequiresRegister());
4422 locations->SetOut(Location::RequiresRegister());
4423}
4424
4425void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4426 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004427 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004428 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004429 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004430 __ movq(locations->Out().AsRegister<CpuRegister>(),
4431 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004432 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004433 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004434 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004435 __ movq(locations->Out().AsRegister<CpuRegister>(),
4436 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4437 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004438 __ movq(locations->Out().AsRegister<CpuRegister>(),
4439 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004440 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004441}
4442
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004443void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004444 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004445 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004446 locations->SetInAt(0, Location::RequiresRegister());
4447 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004448}
4449
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004450void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4451 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004452 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4453 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004454 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004455 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004456 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004457 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004458 break;
4459
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004460 case DataType::Type::kInt64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004462 break;
4463
4464 default:
4465 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4466 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004467}
4468
David Brazdil66d126e2015-04-03 16:02:44 +01004469void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4470 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004471 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004472 locations->SetInAt(0, Location::RequiresRegister());
4473 locations->SetOut(Location::SameAsFirstInput());
4474}
4475
4476void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004477 LocationSummary* locations = bool_not->GetLocations();
4478 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4479 locations->Out().AsRegister<CpuRegister>().AsRegister());
4480 Location out = locations->Out();
4481 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4482}
4483
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004484void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004485 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004486 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004487 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004488 locations->SetInAt(i, Location::Any());
4489 }
4490 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004491}
4492
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004493void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004494 LOG(FATAL) << "Unimplemented";
4495}
4496
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004497void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004498 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004499 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004500 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004501 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4502 */
4503 switch (kind) {
4504 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004505 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004506 break;
4507 }
4508 case MemBarrierKind::kAnyStore:
4509 case MemBarrierKind::kLoadAny:
4510 case MemBarrierKind::kStoreStore: {
4511 // nop
4512 break;
4513 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004514 case MemBarrierKind::kNTStoreStore:
4515 // Non-Temporal Store/Store needs an explicit fence.
4516 MemoryFence(/* non-temporal */ true);
4517 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004518 }
4519}
4520
4521void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4522 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4523
Roland Levillain0d5a2812015-11-13 10:07:31 +00004524 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004525 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004526 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004527 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4528 object_field_get_with_read_barrier
4529 ? LocationSummary::kCallOnSlowPath
4530 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004531 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004532 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004533 }
Calin Juravle52c48962014-12-16 17:02:57 +00004534 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004535 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004536 locations->SetOut(Location::RequiresFpuRegister());
4537 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004538 // The output overlaps for an object field get when read barriers
4539 // are enabled: we do not want the move to overwrite the object's
4540 // location, as we need it to emit the read barrier.
4541 locations->SetOut(
4542 Location::RequiresRegister(),
4543 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004544 }
Calin Juravle52c48962014-12-16 17:02:57 +00004545}
4546
4547void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4548 const FieldInfo& field_info) {
4549 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4550
4551 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004552 Location base_loc = locations->InAt(0);
4553 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004554 Location out = locations->Out();
4555 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004556 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4557 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004558 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4559
Vladimir Marko61b92282017-10-11 13:23:17 +01004560 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004561 case DataType::Type::kBool:
4562 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004563 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4564 break;
4565 }
4566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004567 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004568 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4569 break;
4570 }
4571
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004572 case DataType::Type::kUint16: {
4573 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004574 break;
4575 }
4576
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004577 case DataType::Type::kInt16: {
4578 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
Calin Juravle52c48962014-12-16 17:02:57 +00004579 break;
4580 }
4581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004582 case DataType::Type::kInt32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004583 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4584 break;
4585 }
4586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004587 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004588 // /* HeapReference<Object> */ out = *(base + offset)
4589 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004590 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004591 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004592 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004593 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004594 if (is_volatile) {
4595 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4596 }
4597 } else {
4598 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4599 codegen_->MaybeRecordImplicitNullCheck(instruction);
4600 if (is_volatile) {
4601 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4602 }
4603 // If read barriers are enabled, emit read barriers other than
4604 // Baker's using a slow path (and also unpoison the loaded
4605 // reference, if heap poisoning is enabled).
4606 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4607 }
4608 break;
4609 }
4610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004611 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004612 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4613 break;
4614 }
4615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004616 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004617 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4618 break;
4619 }
4620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004621 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004622 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4623 break;
4624 }
4625
Aart Bik66c158e2018-01-31 12:55:04 -08004626 case DataType::Type::kUint32:
4627 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004628 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004629 LOG(FATAL) << "Unreachable type " << load_type;
Calin Juravle52c48962014-12-16 17:02:57 +00004630 UNREACHABLE();
4631 }
4632
Vladimir Marko61b92282017-10-11 13:23:17 +01004633 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004634 // Potential implicit null checks, in the case of reference
4635 // fields, are handled in the previous switch statement.
4636 } else {
4637 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004638 }
Roland Levillain4d027112015-07-01 15:41:14 +01004639
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004640 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004641 if (load_type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004642 // Memory barriers, in the case of references, are also handled
4643 // in the previous switch statement.
4644 } else {
4645 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4646 }
Roland Levillain4d027112015-07-01 15:41:14 +01004647 }
Calin Juravle52c48962014-12-16 17:02:57 +00004648}
4649
4650void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4651 const FieldInfo& field_info) {
4652 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4653
4654 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004655 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004656 DataType::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004657 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004658 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004659 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004660
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004661 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004662 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004663 if (is_volatile) {
4664 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4665 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4666 } else {
4667 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4668 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004669 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004670 if (is_volatile) {
4671 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4672 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4673 } else {
4674 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4675 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004676 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004677 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004678 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004679 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004680 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004681 } else if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004682 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004683 locations->AddTemp(Location::RequiresRegister());
4684 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004685}
4686
Calin Juravle52c48962014-12-16 17:02:57 +00004687void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004688 const FieldInfo& field_info,
4689 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004690 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4691
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004693 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4694 Location value = locations->InAt(1);
4695 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004696 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004697 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4698
4699 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004700 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004701 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004702
Mark Mendellea5af682015-10-22 17:35:49 -04004703 bool maybe_record_implicit_null_check_done = false;
4704
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004705 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004706 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004707 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004708 case DataType::Type::kInt8: {
Mark Mendell40741f32015-04-20 22:10:34 -04004709 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004710 __ movb(Address(base, offset),
4711 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004712 } else {
4713 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4714 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004715 break;
4716 }
4717
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004718 case DataType::Type::kUint16:
4719 case DataType::Type::kInt16: {
Mark Mendell40741f32015-04-20 22:10:34 -04004720 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004721 __ movw(Address(base, offset),
4722 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004723 } else {
4724 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4725 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004726 break;
4727 }
4728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004729 case DataType::Type::kInt32:
4730 case DataType::Type::kReference: {
Mark Mendell40741f32015-04-20 22:10:34 -04004731 if (value.IsConstant()) {
4732 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004733 // `field_type == DataType::Type::kReference` implies `v == 0`.
4734 DCHECK((field_type != DataType::Type::kReference) || (v == 0));
Roland Levillain4d027112015-07-01 15:41:14 +01004735 // Note: if heap poisoning is enabled, no need to poison
4736 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004737 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004738 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004739 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01004740 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4741 __ movl(temp, value.AsRegister<CpuRegister>());
4742 __ PoisonHeapReference(temp);
4743 __ movl(Address(base, offset), temp);
4744 } else {
4745 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4746 }
Mark Mendell40741f32015-04-20 22:10:34 -04004747 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004748 break;
4749 }
4750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004751 case DataType::Type::kInt64: {
Mark Mendell40741f32015-04-20 22:10:34 -04004752 if (value.IsConstant()) {
4753 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004754 codegen_->MoveInt64ToAddress(Address(base, offset),
4755 Address(base, offset + sizeof(int32_t)),
4756 v,
4757 instruction);
4758 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004759 } else {
4760 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4761 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004762 break;
4763 }
4764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004765 case DataType::Type::kFloat32: {
Mark Mendellea5af682015-10-22 17:35:49 -04004766 if (value.IsConstant()) {
4767 int32_t v =
4768 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4769 __ movl(Address(base, offset), Immediate(v));
4770 } else {
4771 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4772 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004773 break;
4774 }
4775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 case DataType::Type::kFloat64: {
Mark Mendellea5af682015-10-22 17:35:49 -04004777 if (value.IsConstant()) {
4778 int64_t v =
4779 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4780 codegen_->MoveInt64ToAddress(Address(base, offset),
4781 Address(base, offset + sizeof(int32_t)),
4782 v,
4783 instruction);
4784 maybe_record_implicit_null_check_done = true;
4785 } else {
4786 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4787 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004788 break;
4789 }
4790
Aart Bik66c158e2018-01-31 12:55:04 -08004791 case DataType::Type::kUint32:
4792 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004793 case DataType::Type::kVoid:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004795 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004796 }
Calin Juravle52c48962014-12-16 17:02:57 +00004797
Mark Mendellea5af682015-10-22 17:35:49 -04004798 if (!maybe_record_implicit_null_check_done) {
4799 codegen_->MaybeRecordImplicitNullCheck(instruction);
4800 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004801
4802 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4803 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4804 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004805 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004806 }
4807
Calin Juravle52c48962014-12-16 17:02:57 +00004808 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004809 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004810 }
4811}
4812
4813void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4814 HandleFieldSet(instruction, instruction->GetFieldInfo());
4815}
4816
4817void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004818 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004819}
4820
4821void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004822 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004823}
4824
4825void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004826 HandleFieldGet(instruction, instruction->GetFieldInfo());
4827}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004828
Calin Juravle52c48962014-12-16 17:02:57 +00004829void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4830 HandleFieldGet(instruction);
4831}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832
Calin Juravle52c48962014-12-16 17:02:57 +00004833void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4834 HandleFieldGet(instruction, instruction->GetFieldInfo());
4835}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836
Calin Juravle52c48962014-12-16 17:02:57 +00004837void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4838 HandleFieldSet(instruction, instruction->GetFieldInfo());
4839}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840
Calin Juravle52c48962014-12-16 17:02:57 +00004841void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004842 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004843}
4844
Calin Juravlee460d1d2015-09-29 04:52:17 +01004845void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4846 HUnresolvedInstanceFieldGet* instruction) {
4847 FieldAccessCallingConventionX86_64 calling_convention;
4848 codegen_->CreateUnresolvedFieldLocationSummary(
4849 instruction, instruction->GetFieldType(), calling_convention);
4850}
4851
4852void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4853 HUnresolvedInstanceFieldGet* instruction) {
4854 FieldAccessCallingConventionX86_64 calling_convention;
4855 codegen_->GenerateUnresolvedFieldAccess(instruction,
4856 instruction->GetFieldType(),
4857 instruction->GetFieldIndex(),
4858 instruction->GetDexPc(),
4859 calling_convention);
4860}
4861
4862void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4863 HUnresolvedInstanceFieldSet* instruction) {
4864 FieldAccessCallingConventionX86_64 calling_convention;
4865 codegen_->CreateUnresolvedFieldLocationSummary(
4866 instruction, instruction->GetFieldType(), calling_convention);
4867}
4868
4869void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4870 HUnresolvedInstanceFieldSet* instruction) {
4871 FieldAccessCallingConventionX86_64 calling_convention;
4872 codegen_->GenerateUnresolvedFieldAccess(instruction,
4873 instruction->GetFieldType(),
4874 instruction->GetFieldIndex(),
4875 instruction->GetDexPc(),
4876 calling_convention);
4877}
4878
4879void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4880 HUnresolvedStaticFieldGet* instruction) {
4881 FieldAccessCallingConventionX86_64 calling_convention;
4882 codegen_->CreateUnresolvedFieldLocationSummary(
4883 instruction, instruction->GetFieldType(), calling_convention);
4884}
4885
4886void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4887 HUnresolvedStaticFieldGet* instruction) {
4888 FieldAccessCallingConventionX86_64 calling_convention;
4889 codegen_->GenerateUnresolvedFieldAccess(instruction,
4890 instruction->GetFieldType(),
4891 instruction->GetFieldIndex(),
4892 instruction->GetDexPc(),
4893 calling_convention);
4894}
4895
4896void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4897 HUnresolvedStaticFieldSet* instruction) {
4898 FieldAccessCallingConventionX86_64 calling_convention;
4899 codegen_->CreateUnresolvedFieldLocationSummary(
4900 instruction, instruction->GetFieldType(), calling_convention);
4901}
4902
4903void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4904 HUnresolvedStaticFieldSet* instruction) {
4905 FieldAccessCallingConventionX86_64 calling_convention;
4906 codegen_->GenerateUnresolvedFieldAccess(instruction,
4907 instruction->GetFieldType(),
4908 instruction->GetFieldIndex(),
4909 instruction->GetDexPc(),
4910 calling_convention);
4911}
4912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004913void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004914 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4915 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4916 ? Location::RequiresRegister()
4917 : Location::Any();
4918 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004919}
4920
Calin Juravle2ae48182016-03-16 14:05:09 +00004921void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4922 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004923 return;
4924 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004925 LocationSummary* locations = instruction->GetLocations();
4926 Location obj = locations->InAt(0);
4927
4928 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004929 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004930}
4931
Calin Juravle2ae48182016-03-16 14:05:09 +00004932void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004933 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004934 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004935
4936 LocationSummary* locations = instruction->GetLocations();
4937 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004938
4939 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004940 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004941 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004942 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004943 } else {
4944 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004945 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004946 __ jmp(slow_path->GetEntryLabel());
4947 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948 }
4949 __ j(kEqual, slow_path->GetEntryLabel());
4950}
4951
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004952void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004953 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004954}
4955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004956void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004957 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004958 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004959 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004960 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4961 object_array_get_with_read_barrier
4962 ? LocationSummary::kCallOnSlowPath
4963 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004964 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004965 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004966 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004967 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004968 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004970 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4971 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004972 // The output overlaps for an object array get when read barriers
4973 // are enabled: we do not want the move to overwrite the array's
4974 // location, as we need it to emit the read barrier.
4975 locations->SetOut(
4976 Location::RequiresRegister(),
4977 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004979}
4980
4981void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4982 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004983 Location obj_loc = locations->InAt(0);
4984 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004985 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004986 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004987 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004989 DataType::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004990 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004991 case DataType::Type::kBool:
4992 case DataType::Type::kUint8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004993 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004994 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 break;
4996 }
4997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004998 case DataType::Type::kInt8: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004999 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005000 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 break;
5002 }
5003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005004 case DataType::Type::kUint16: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005005 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07005006 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5007 // Branch cases into compressed and uncompressed for each index's type.
5008 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5009 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005010 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005011 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005012 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5013 "Expecting 0=compressed, 1=uncompressed");
5014 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005015 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
5016 __ jmp(&done);
5017 __ Bind(&not_compressed);
5018 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5019 __ Bind(&done);
5020 } else {
5021 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023 break;
5024 }
5025
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005026 case DataType::Type::kInt16: {
5027 CpuRegister out = out_loc.AsRegister<CpuRegister>();
5028 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
5029 break;
5030 }
5031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005032 case DataType::Type::kInt32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005033 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005034 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035 break;
5036 }
5037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005038 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005039 static_assert(
5040 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5041 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005042 // /* HeapReference<Object> */ out =
5043 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5044 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005045 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005046 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005047 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005048 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005049 } else {
5050 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005051 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
5052 codegen_->MaybeRecordImplicitNullCheck(instruction);
5053 // If read barriers are enabled, emit read barriers other than
5054 // Baker's using a slow path (and also unpoison the loaded
5055 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005056 if (index.IsConstant()) {
5057 uint32_t offset =
5058 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005059 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5060 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005061 codegen_->MaybeGenerateReadBarrierSlow(
5062 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5063 }
5064 }
5065 break;
5066 }
5067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005068 case DataType::Type::kInt64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005069 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005070 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 break;
5072 }
5073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005074 case DataType::Type::kFloat32: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005075 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005076 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005077 break;
5078 }
5079
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005080 case DataType::Type::kFloat64: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005081 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005082 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005083 break;
5084 }
5085
Aart Bik66c158e2018-01-31 12:55:04 -08005086 case DataType::Type::kUint32:
5087 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005088 case DataType::Type::kVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005089 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005090 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091 }
Roland Levillain4d027112015-07-01 15:41:14 +01005092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005093 if (type == DataType::Type::kReference) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005094 // Potential implicit null checks, in the case of reference
5095 // arrays, are handled in the previous switch statement.
5096 } else {
5097 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099}
5100
5101void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005103
5104 bool needs_write_barrier =
5105 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005106 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005107
Vladimir Markoca6fff82017-10-03 14:49:14 +01005108 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005109 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005110 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005111 LocationSummary::kCallOnSlowPath :
5112 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005113
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005114 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04005115 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005116 if (DataType::IsFloatingPointType(value_type)) {
Mark Mendellea5af682015-10-22 17:35:49 -04005117 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005118 } else {
5119 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5120 }
5121
5122 if (needs_write_barrier) {
5123 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005124 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127}
5128
5129void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
5130 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005131 Location array_loc = locations->InAt(0);
5132 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005134 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 DataType::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005136 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005137 bool needs_write_barrier =
5138 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005139 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5140 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5141 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142
5143 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005144 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005145 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005146 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005147 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005148 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005149 if (value.IsRegister()) {
5150 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005152 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(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 Markod5d2f2c2017-09-26 12:37:26 +01005158 case DataType::Type::kUint16:
5159 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005160 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005161 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005162 if (value.IsRegister()) {
5163 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005164 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01005166 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005168 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 break;
5170 }
5171
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005172 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005173 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005174 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005175
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005176 if (!value.IsRegister()) {
5177 // Just setting null.
5178 DCHECK(instruction->InputAt(2)->IsNullConstant());
5179 DCHECK(value.IsConstant()) << value;
5180 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005181 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005182 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005183 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005185 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005186
5187 DCHECK(needs_write_barrier);
5188 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005189 // We cannot use a NearLabel for `done`, as its range may be too
5190 // short when Baker read barriers are enabled.
5191 Label done;
5192 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005193 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005194 Location temp_loc = locations->GetTemp(0);
5195 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005196 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005197 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86_64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005198 codegen_->AddSlowPath(slow_path);
5199 if (instruction->GetValueCanBeNull()) {
5200 __ testl(register_value, register_value);
5201 __ j(kNotEqual, &not_null);
5202 __ movl(address, Immediate(0));
5203 codegen_->MaybeRecordImplicitNullCheck(instruction);
5204 __ jmp(&done);
5205 __ Bind(&not_null);
5206 }
5207
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005208 // Note that when Baker read barriers are enabled, the type
5209 // checks are performed without read barriers. This is fine,
5210 // even in the case where a class object is in the from-space
5211 // after the flip, as a comparison involving such a type would
5212 // not produce a false positive; it may of course produce a
5213 // false negative, in which case we would take the ArraySet
5214 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005215
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005216 // /* HeapReference<Class> */ temp = array->klass_
5217 __ movl(temp, Address(array, class_offset));
5218 codegen_->MaybeRecordImplicitNullCheck(instruction);
5219 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005220
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005221 // /* HeapReference<Class> */ temp = temp->component_type_
5222 __ movl(temp, Address(temp, component_offset));
5223 // If heap poisoning is enabled, no need to unpoison `temp`
5224 // nor the object reference in `register_value->klass`, as
5225 // we are comparing two poisoned references.
5226 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005227
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005228 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5229 __ j(kEqual, &do_put);
5230 // If heap poisoning is enabled, the `temp` reference has
5231 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005232 __ MaybeUnpoisonHeapReference(temp);
5233
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005234 // If heap poisoning is enabled, no need to unpoison the
5235 // heap reference loaded below, as it is only used for a
5236 // comparison with null.
5237 __ cmpl(Address(temp, super_offset), Immediate(0));
5238 __ j(kNotEqual, slow_path->GetEntryLabel());
5239 __ Bind(&do_put);
5240 } else {
5241 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005242 }
5243 }
5244
5245 if (kPoisonHeapReferences) {
5246 __ movl(temp, register_value);
5247 __ PoisonHeapReference(temp);
5248 __ movl(address, temp);
5249 } else {
5250 __ movl(address, register_value);
5251 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005252 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 codegen_->MaybeRecordImplicitNullCheck(instruction);
5254 }
5255
5256 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
5257 codegen_->MarkGCCard(
5258 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
5259 __ Bind(&done);
5260
5261 if (slow_path != nullptr) {
5262 __ Bind(slow_path->GetExitLabel());
5263 }
5264
5265 break;
5266 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005268 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005270 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 if (value.IsRegister()) {
5272 __ movl(address, value.AsRegister<CpuRegister>());
5273 } else {
5274 DCHECK(value.IsConstant()) << value;
5275 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5276 __ movl(address, Immediate(v));
5277 }
5278 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 break;
5280 }
5281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 case DataType::Type::kInt64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005284 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 if (value.IsRegister()) {
5286 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005287 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005290 Address address_high =
5291 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005292 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 }
5294 break;
5295 }
5296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005299 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005300 if (value.IsFpuRegister()) {
5301 __ movss(address, value.AsFpuRegister<XmmRegister>());
5302 } else {
5303 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005304 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005305 __ movl(address, Immediate(v));
5306 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005307 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 break;
5309 }
5310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005313 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005314 if (value.IsFpuRegister()) {
5315 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5316 codegen_->MaybeRecordImplicitNullCheck(instruction);
5317 } else {
5318 int64_t v =
5319 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005320 Address address_high =
5321 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005322 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5323 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005324 break;
5325 }
5326
Aart Bik66c158e2018-01-31 12:55:04 -08005327 case DataType::Type::kUint32:
5328 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005329 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005331 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 }
5333}
5334
5335void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005336 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005337 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005338 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005339 if (!instruction->IsEmittedAtUseSite()) {
5340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342}
5343
5344void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005345 if (instruction->IsEmittedAtUseSite()) {
5346 return;
5347 }
5348
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005350 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005351 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5352 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005354 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005355 // Mask out most significant bit in case the array is String's array of char.
5356 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005357 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005358 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359}
5360
5361void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005362 RegisterSet caller_saves = RegisterSet::Empty();
5363 InvokeRuntimeCallingConvention calling_convention;
5364 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5365 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5366 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005367 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005368 HInstruction* length = instruction->InputAt(1);
5369 if (!length->IsEmittedAtUseSite()) {
5370 locations->SetInAt(1, Location::RegisterOrConstant(length));
5371 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372}
5373
5374void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5375 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005376 Location index_loc = locations->InAt(0);
5377 Location length_loc = locations->InAt(1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005378 SlowPathCode* slow_path =
5379 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380
Mark Mendell99dbd682015-04-22 16:18:52 -04005381 if (length_loc.IsConstant()) {
5382 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5383 if (index_loc.IsConstant()) {
5384 // BCE will remove the bounds check if we are guarenteed to pass.
5385 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5386 if (index < 0 || index >= length) {
5387 codegen_->AddSlowPath(slow_path);
5388 __ jmp(slow_path->GetEntryLabel());
5389 } else {
5390 // Some optimization after BCE may have generated this, and we should not
5391 // generate a bounds check if it is a valid range.
5392 }
5393 return;
5394 }
5395
5396 // We have to reverse the jump condition because the length is the constant.
5397 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5398 __ cmpl(index_reg, Immediate(length));
5399 codegen_->AddSlowPath(slow_path);
5400 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005401 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005402 HInstruction* array_length = instruction->InputAt(1);
5403 if (array_length->IsEmittedAtUseSite()) {
5404 // Address the length field in the array.
5405 DCHECK(array_length->IsArrayLength());
5406 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5407 Location array_loc = array_length->GetLocations()->InAt(0);
5408 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005409 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005410 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5411 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005412 CpuRegister length_reg = CpuRegister(TMP);
5413 __ movl(length_reg, array_len);
5414 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005415 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005416 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005417 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005418 // Checking the bound for general case:
5419 // Array of char or String's array when the compression feature off.
5420 if (index_loc.IsConstant()) {
5421 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5422 __ cmpl(array_len, Immediate(value));
5423 } else {
5424 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5425 }
5426 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005427 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005428 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005429 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005430 }
5431 codegen_->AddSlowPath(slow_path);
5432 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005433 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434}
5435
5436void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5437 CpuRegister card,
5438 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005439 CpuRegister value,
5440 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005441 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005442 if (value_can_be_null) {
5443 __ testl(value, value);
5444 __ j(kEqual, &is_null);
5445 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005446 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005447 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 __ movq(temp, object);
5449 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005450 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005451 if (value_can_be_null) {
5452 __ Bind(&is_null);
5453 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005454}
5455
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005456void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005457 LOG(FATAL) << "Unimplemented";
5458}
5459
5460void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005461 if (instruction->GetNext()->IsSuspendCheck() &&
5462 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5463 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5464 // The back edge will generate the suspend check.
5465 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5466 }
5467
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005468 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5469}
5470
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005471void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005472 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5473 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005474 // In suspend check slow path, usually there are no caller-save registers at all.
5475 // If SIMD instructions are present, however, we force spilling all live SIMD
5476 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005477 locations->SetCustomSlowPathCallerSaves(
5478 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005479}
5480
5481void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005482 HBasicBlock* block = instruction->GetBlock();
5483 if (block->GetLoopInformation() != nullptr) {
5484 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5485 // The back edge will generate the suspend check.
5486 return;
5487 }
5488 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5489 // The goto will generate the suspend check.
5490 return;
5491 }
5492 GenerateSuspendCheck(instruction, nullptr);
5493}
5494
5495void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5496 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005497 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005498 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5499 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005500 slow_path =
5501 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005502 instruction->SetSlowPath(slow_path);
5503 codegen_->AddSlowPath(slow_path);
5504 if (successor != nullptr) {
5505 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005506 }
5507 } else {
5508 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5509 }
5510
Andreas Gampe542451c2016-07-26 09:02:02 -07005511 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005512 /* no_rip */ true),
5513 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005514 if (successor == nullptr) {
5515 __ j(kNotEqual, slow_path->GetEntryLabel());
5516 __ Bind(slow_path->GetReturnLabel());
5517 } else {
5518 __ j(kEqual, codegen_->GetLabelOf(successor));
5519 __ jmp(slow_path->GetEntryLabel());
5520 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005521}
5522
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005523X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5524 return codegen_->GetAssembler();
5525}
5526
5527void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005528 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005529 Location source = move->GetSource();
5530 Location destination = move->GetDestination();
5531
5532 if (source.IsRegister()) {
5533 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005534 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005535 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005536 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005537 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005538 } else {
5539 DCHECK(destination.IsDoubleStackSlot());
5540 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005541 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005542 }
5543 } else if (source.IsStackSlot()) {
5544 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005545 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005546 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005547 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005548 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005549 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005550 } else {
5551 DCHECK(destination.IsStackSlot());
5552 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5553 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5554 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005555 } else if (source.IsDoubleStackSlot()) {
5556 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005557 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005558 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005559 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005560 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5561 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005562 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005563 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005564 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5565 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5566 }
Aart Bik5576f372017-03-23 16:17:37 -07005567 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005568 if (destination.IsFpuRegister()) {
5569 __ movups(destination.AsFpuRegister<XmmRegister>(),
5570 Address(CpuRegister(RSP), source.GetStackIndex()));
5571 } else {
5572 DCHECK(destination.IsSIMDStackSlot());
5573 size_t high = kX86_64WordSize;
5574 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5575 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5576 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex() + high));
5577 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex() + high), CpuRegister(TMP));
5578 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005579 } else if (source.IsConstant()) {
5580 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005581 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5582 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005583 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005584 if (value == 0) {
5585 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5586 } else {
5587 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5588 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005589 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005590 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005591 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005592 }
5593 } else if (constant->IsLongConstant()) {
5594 int64_t value = constant->AsLongConstant()->GetValue();
5595 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005596 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005597 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005598 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005599 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005600 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005601 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005602 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005603 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005604 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005605 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005606 } else {
5607 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005608 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005609 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5610 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005611 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005612 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005613 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005614 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005615 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005616 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005617 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005618 } else {
5619 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005620 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005621 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005622 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005623 } else if (source.IsFpuRegister()) {
5624 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005625 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005626 } else if (destination.IsStackSlot()) {
5627 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005628 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005629 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005630 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005631 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005632 } else {
5633 DCHECK(destination.IsSIMDStackSlot());
5634 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5635 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005636 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005637 }
5638}
5639
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005640void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005641 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005642 __ movl(Address(CpuRegister(RSP), mem), reg);
5643 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005644}
5645
Mark Mendell8a1c7282015-06-29 15:41:28 -04005646void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5647 __ movq(CpuRegister(TMP), reg1);
5648 __ movq(reg1, reg2);
5649 __ movq(reg2, CpuRegister(TMP));
5650}
5651
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005652void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5653 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5654 __ movq(Address(CpuRegister(RSP), mem), reg);
5655 __ movq(reg, CpuRegister(TMP));
5656}
5657
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005658void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5659 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5660 __ movss(Address(CpuRegister(RSP), mem), reg);
5661 __ movd(reg, CpuRegister(TMP));
5662}
5663
5664void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5665 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5666 __ movsd(Address(CpuRegister(RSP), mem), reg);
5667 __ movd(reg, CpuRegister(TMP));
5668}
5669
Aart Bikcfe50bb2017-12-12 14:54:12 -08005670void ParallelMoveResolverX86_64::Exchange128(XmmRegister reg, int mem) {
5671 size_t extra_slot = 2 * kX86_64WordSize;
5672 __ subq(CpuRegister(RSP), Immediate(extra_slot));
5673 __ movups(Address(CpuRegister(RSP), 0), XmmRegister(reg));
5674 ExchangeMemory64(0, mem + extra_slot, 2);
5675 __ movups(XmmRegister(reg), Address(CpuRegister(RSP), 0));
5676 __ addq(CpuRegister(RSP), Immediate(extra_slot));
5677}
5678
5679void ParallelMoveResolverX86_64::ExchangeMemory32(int mem1, int mem2) {
5680 ScratchRegisterScope ensure_scratch(
5681 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5682
5683 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5684 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5685 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5686 Address(CpuRegister(RSP), mem2 + stack_offset));
5687 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5688 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5689 CpuRegister(ensure_scratch.GetRegister()));
5690}
5691
5692void ParallelMoveResolverX86_64::ExchangeMemory64(int mem1, int mem2, int num_of_qwords) {
5693 ScratchRegisterScope ensure_scratch(
5694 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5695
5696 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5697
5698 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5699 for (int i = 0; i < num_of_qwords; i++) {
5700 __ movq(CpuRegister(TMP),
5701 Address(CpuRegister(RSP), mem1 + stack_offset));
5702 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5703 Address(CpuRegister(RSP), mem2 + stack_offset));
5704 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset),
5705 CpuRegister(TMP));
5706 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5707 CpuRegister(ensure_scratch.GetRegister()));
5708 stack_offset += kX86_64WordSize;
5709 }
5710}
5711
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005712void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005713 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005714 Location source = move->GetSource();
5715 Location destination = move->GetDestination();
5716
5717 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005718 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005719 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005720 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005721 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005722 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005723 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005724 ExchangeMemory32(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005725 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005726 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005727 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005728 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005729 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005730 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 1);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005731 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005732 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5733 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5734 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005735 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005736 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005737 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005738 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005739 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005740 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005741 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005742 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005743 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
5744 ExchangeMemory64(destination.GetStackIndex(), source.GetStackIndex(), 2);
5745 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
5746 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5747 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
5748 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005749 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005750 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005751 }
5752}
5753
5754
5755void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5756 __ pushq(CpuRegister(reg));
5757}
5758
5759
5760void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5761 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005762}
5763
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005764void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005765 SlowPathCode* slow_path, CpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00005766 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
5767 const size_t status_byte_offset =
5768 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
5769 constexpr uint32_t shifted_initialized_value =
5770 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
5771
5772 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00005773 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005774 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005775 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005776}
5777
Vladimir Marko175e7862018-03-27 09:03:13 +00005778void InstructionCodeGeneratorX86_64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
5779 CpuRegister temp) {
5780 uint32_t path_to_root = check->GetBitstringPathToRoot();
5781 uint32_t mask = check->GetBitstringMask();
5782 DCHECK(IsPowerOfTwo(mask + 1));
5783 size_t mask_bits = WhichPowerOf2(mask + 1);
5784
5785 if (mask_bits == 16u) {
5786 // Compare the bitstring in memory.
5787 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
5788 } else {
5789 // /* uint32_t */ temp = temp->status_
5790 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
5791 // Compare the bitstring bits using SUB.
5792 __ subl(temp, Immediate(path_to_root));
5793 // Shift out bits that do not contribute to the comparison.
5794 __ shll(temp, Immediate(32u - mask_bits));
5795 }
5796}
5797
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005798HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5799 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005801 case HLoadClass::LoadKind::kInvalid:
5802 LOG(FATAL) << "UNREACHABLE";
5803 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005804 case HLoadClass::LoadKind::kReferrersClass:
5805 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005806 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005807 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005808 case HLoadClass::LoadKind::kBssEntry:
5809 DCHECK(!Runtime::Current()->UseJitCompilation());
5810 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005811 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005812 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005813 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005814 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005815 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 break;
5817 }
5818 return desired_class_load_kind;
5819}
5820
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005821void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005822 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005823 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005824 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005825 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005827 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005828 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005829 return;
5830 }
Vladimir Marko41559982017-01-06 14:04:23 +00005831 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005832
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005833 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5834 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 ? LocationSummary::kCallOnSlowPath
5836 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005838 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005839 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005840 }
5841
Vladimir Marko41559982017-01-06 14:04:23 +00005842 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005843 locations->SetInAt(0, Location::RequiresRegister());
5844 }
5845 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005846 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5847 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5848 // Rely on the type resolution and/or initialization to save everything.
5849 // Custom calling convention: RAX serves as both input and output.
5850 RegisterSet caller_saves = RegisterSet::Empty();
5851 caller_saves.Add(Location::RegisterLocation(RAX));
5852 locations->SetCustomSlowPathCallerSaves(caller_saves);
5853 } else {
5854 // For non-Baker read barrier we have a temp-clobbering call.
5855 }
5856 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005857}
5858
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005859Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01005860 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005861 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005862 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005863 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005864 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005865 PatchInfo<Label>* info = &jit_class_patches_.back();
5866 return &info->label;
5867}
5868
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005869// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5870// move.
5871void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005872 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005873 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005874 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005875 return;
5876 }
Vladimir Marko41559982017-01-06 14:04:23 +00005877 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005878
Vladimir Marko41559982017-01-06 14:04:23 +00005879 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 Location out_loc = locations->Out();
5881 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005882
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005883 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5884 ? kWithoutReadBarrier
5885 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005886 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005887 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 case HLoadClass::LoadKind::kReferrersClass: {
5889 DCHECK(!cls->CanCallRuntime());
5890 DCHECK(!cls->MustGenerateClinitCheck());
5891 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5892 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5893 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005894 cls,
5895 out_loc,
5896 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005897 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005898 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005899 break;
5900 }
5901 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005902 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005903 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005904 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005905 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 break;
5907 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005908 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005909 uint32_t address = dchecked_integral_cast<uint32_t>(
5910 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5911 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005912 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005913 break;
5914 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005915 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005916 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5917 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005918 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005919 break;
5920 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005921 case HLoadClass::LoadKind::kBssEntry: {
5922 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5923 /* no_rip */ false);
5924 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5925 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5926 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5927 generate_null_check = true;
5928 break;
5929 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005930 case HLoadClass::LoadKind::kJitTableAddress: {
5931 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5932 /* no_rip */ true);
5933 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005934 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005935 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005936 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005937 break;
5938 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939 default:
5940 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5941 UNREACHABLE();
5942 }
5943
5944 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5945 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005946 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005947 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5948 codegen_->AddSlowPath(slow_path);
5949 if (generate_null_check) {
5950 __ testl(out, out);
5951 __ j(kEqual, slow_path->GetEntryLabel());
5952 }
5953 if (cls->MustGenerateClinitCheck()) {
5954 GenerateClassInitializationCheck(slow_path, out);
5955 } else {
5956 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005957 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958 }
5959}
5960
5961void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5962 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005963 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964 locations->SetInAt(0, Location::RequiresRegister());
5965 if (check->HasUses()) {
5966 locations->SetOut(Location::SameAsFirstInput());
5967 }
5968}
5969
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005970void LocationsBuilderX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5971 // Custom calling convention: RAX serves as both input and output.
5972 Location location = Location::RegisterLocation(RAX);
5973 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5974}
5975
5976void InstructionCodeGeneratorX86_64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5977 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5978}
5979
Orion Hodson18259d72018-04-12 11:18:23 +01005980void LocationsBuilderX86_64::VisitLoadMethodType(HLoadMethodType* load) {
5981 // Custom calling convention: RAX serves as both input and output.
5982 Location location = Location::RegisterLocation(RAX);
5983 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5984}
5985
5986void InstructionCodeGeneratorX86_64::VisitLoadMethodType(HLoadMethodType* load) {
5987 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5988}
5989
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005990void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005991 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005992 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005993 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005995 GenerateClassInitializationCheck(slow_path,
5996 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997}
5998
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005999HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
6000 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006001 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006002 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006003 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006004 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006005 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006006 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006007 case HLoadString::LoadKind::kJitTableAddress:
6008 DCHECK(Runtime::Current()->UseJitCompilation());
6009 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006010 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006011 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006012 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006013 }
6014 return desired_string_load_kind;
6015}
6016
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006017void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006018 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006019 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006020 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006021 locations->SetOut(Location::RegisterLocation(RAX));
6022 } else {
6023 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006024 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
6025 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006026 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006027 // Custom calling convention: RAX serves as both input and output.
6028 RegisterSet caller_saves = RegisterSet::Empty();
6029 caller_saves.Add(Location::RegisterLocation(RAX));
6030 locations->SetCustomSlowPathCallerSaves(caller_saves);
6031 } else {
6032 // For non-Baker read barrier we have a temp-clobbering call.
6033 }
6034 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006035 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006036}
6037
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006038Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006039 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006040 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006041 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006042 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006043 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006044 PatchInfo<Label>* info = &jit_string_patches_.back();
6045 return &info->label;
6046}
6047
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006048// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6049// move.
6050void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006051 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006052 Location out_loc = locations->Out();
6053 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006054
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006055 switch (load->GetLoadKind()) {
6056 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006057 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006058 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006059 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006060 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006061 }
6062 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006063 uint32_t address = dchecked_integral_cast<uint32_t>(
6064 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6065 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07006066 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006067 return;
6068 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006069 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006070 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6071 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006072 codegen_->RecordBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006073 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006074 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006075 case HLoadString::LoadKind::kBssEntry: {
6076 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6077 /* no_rip */ false);
6078 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6079 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006080 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006081 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86_64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006082 codegen_->AddSlowPath(slow_path);
6083 __ testl(out, out);
6084 __ j(kEqual, slow_path->GetEntryLabel());
6085 __ Bind(slow_path->GetExitLabel());
6086 return;
6087 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006088 case HLoadString::LoadKind::kJitTableAddress: {
6089 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
6090 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006091 Label* fixup_label = codegen_->NewJitRootStringPatch(
6092 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006093 // /* GcRoot<mirror::String> */ out = *address
6094 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6095 return;
6096 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006097 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006098 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006099 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006101 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006102 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006103 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07006104 codegen_->InvokeRuntime(kQuickResolveString,
6105 load,
6106 load->GetDexPc());
6107 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006108}
6109
David Brazdilcb1c0552015-08-04 16:22:25 +01006110static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006111 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006112 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01006113}
6114
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006115void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
6116 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006117 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006118 locations->SetOut(Location::RequiresRegister());
6119}
6120
6121void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006122 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
6123}
6124
6125void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006126 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006127}
6128
6129void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6130 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006131}
6132
6133void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006134 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6135 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006136 InvokeRuntimeCallingConvention calling_convention;
6137 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6138}
6139
6140void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006141 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006142 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006143}
6144
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006145// Temp is used for read barrier.
6146static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6147 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006148 !kUseBakerReadBarrier &&
6149 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006150 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006151 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6152 return 1;
6153 }
6154 return 0;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006155}
6156
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006157// Interface case has 2 temps, one for holding the number of interfaces, one for the current
6158// interface pointer, the current interface is compared in memory.
6159// The other checks have one temp for loading the object's class.
6160static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6161 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6162 return 2;
6163 }
6164 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006165}
6166
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006167void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006169 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006170 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006171 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 case TypeCheckKind::kExactCheck:
6173 case TypeCheckKind::kAbstractClassCheck:
6174 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006175 case TypeCheckKind::kArrayObjectCheck: {
6176 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6177 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6178 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006180 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182 case TypeCheckKind::kUnresolvedCheck:
6183 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 call_kind = LocationSummary::kCallOnSlowPath;
6185 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006186 case TypeCheckKind::kBitstringCheck:
6187 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189
Vladimir Markoca6fff82017-10-03 14:49:14 +01006190 LocationSummary* locations =
6191 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006192 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006193 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006194 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006196 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6197 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6198 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6199 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
6200 } else {
6201 locations->SetInAt(1, Location::Any());
6202 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006203 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
6204 locations->SetOut(Location::RequiresRegister());
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006205 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006206}
6207
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006208void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006209 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211 Location obj_loc = locations->InAt(0);
6212 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006213 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006214 Location out_loc = locations->Out();
6215 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006216 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6217 DCHECK_LE(num_temps, 1u);
6218 Location maybe_temp_loc = (num_temps >= 1u) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6221 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6222 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006223 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006225
6226 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006227 // Avoid null check if we know obj is not null.
6228 if (instruction->MustDoNullCheck()) {
6229 __ testl(obj, obj);
6230 __ j(kEqual, &zero);
6231 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006233 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006235 ReadBarrierOption read_barrier_option =
6236 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006237 // /* HeapReference<Class> */ out = obj->klass_
6238 GenerateReferenceLoadTwoRegisters(instruction,
6239 out_loc,
6240 obj_loc,
6241 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006242 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 if (cls.IsRegister()) {
6244 __ cmpl(out, cls.AsRegister<CpuRegister>());
6245 } else {
6246 DCHECK(cls.IsStackSlot()) << cls;
6247 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6248 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006249 if (zero.IsLinked()) {
6250 // Classes must be equal for the instanceof to succeed.
6251 __ j(kNotEqual, &zero);
6252 __ movl(out, Immediate(1));
6253 __ jmp(&done);
6254 } else {
6255 __ setcc(kEqual, out);
6256 // setcc only sets the low byte.
6257 __ andl(out, Immediate(1));
6258 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006259 break;
6260 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006262 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006263 ReadBarrierOption read_barrier_option =
6264 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006265 // /* HeapReference<Class> */ out = obj->klass_
6266 GenerateReferenceLoadTwoRegisters(instruction,
6267 out_loc,
6268 obj_loc,
6269 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006270 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 // If the class is abstract, we eagerly fetch the super class of the
6272 // object to avoid doing a comparison we know will fail.
6273 NearLabel loop, success;
6274 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006276 GenerateReferenceLoadOneRegister(instruction,
6277 out_loc,
6278 super_offset,
6279 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006280 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006281 __ testl(out, out);
6282 // If `out` is null, we use it for the result, and jump to `done`.
6283 __ j(kEqual, &done);
6284 if (cls.IsRegister()) {
6285 __ cmpl(out, cls.AsRegister<CpuRegister>());
6286 } else {
6287 DCHECK(cls.IsStackSlot()) << cls;
6288 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6289 }
6290 __ j(kNotEqual, &loop);
6291 __ movl(out, Immediate(1));
6292 if (zero.IsLinked()) {
6293 __ jmp(&done);
6294 }
6295 break;
6296 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006299 ReadBarrierOption read_barrier_option =
6300 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006301 // /* HeapReference<Class> */ out = obj->klass_
6302 GenerateReferenceLoadTwoRegisters(instruction,
6303 out_loc,
6304 obj_loc,
6305 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006306 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006307 // Walk over the class hierarchy to find a match.
6308 NearLabel loop, success;
6309 __ Bind(&loop);
6310 if (cls.IsRegister()) {
6311 __ cmpl(out, cls.AsRegister<CpuRegister>());
6312 } else {
6313 DCHECK(cls.IsStackSlot()) << cls;
6314 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6315 }
6316 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006317 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006318 GenerateReferenceLoadOneRegister(instruction,
6319 out_loc,
6320 super_offset,
6321 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006322 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 __ testl(out, out);
6324 __ j(kNotEqual, &loop);
6325 // If `out` is null, we use it for the result, and jump to `done`.
6326 __ jmp(&done);
6327 __ Bind(&success);
6328 __ movl(out, Immediate(1));
6329 if (zero.IsLinked()) {
6330 __ jmp(&done);
6331 }
6332 break;
6333 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006336 ReadBarrierOption read_barrier_option =
6337 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006338 // /* HeapReference<Class> */ out = obj->klass_
6339 GenerateReferenceLoadTwoRegisters(instruction,
6340 out_loc,
6341 obj_loc,
6342 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006343 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006344 // Do an exact check.
6345 NearLabel exact_check;
6346 if (cls.IsRegister()) {
6347 __ cmpl(out, cls.AsRegister<CpuRegister>());
6348 } else {
6349 DCHECK(cls.IsStackSlot()) << cls;
6350 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6351 }
6352 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006353 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006355 GenerateReferenceLoadOneRegister(instruction,
6356 out_loc,
6357 component_offset,
6358 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006359 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 __ testl(out, out);
6361 // If `out` is null, we use it for the result, and jump to `done`.
6362 __ j(kEqual, &done);
6363 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6364 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006365 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 __ movl(out, Immediate(1));
6367 __ jmp(&done);
6368 break;
6369 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006372 // No read barrier since the slow path will retry upon failure.
6373 // /* HeapReference<Class> */ out = obj->klass_
6374 GenerateReferenceLoadTwoRegisters(instruction,
6375 out_loc,
6376 obj_loc,
6377 class_offset,
6378 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 if (cls.IsRegister()) {
6380 __ cmpl(out, cls.AsRegister<CpuRegister>());
6381 } else {
6382 DCHECK(cls.IsStackSlot()) << cls;
6383 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6384 }
6385 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006386 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6387 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 codegen_->AddSlowPath(slow_path);
6389 __ j(kNotEqual, slow_path->GetEntryLabel());
6390 __ movl(out, Immediate(1));
6391 if (zero.IsLinked()) {
6392 __ jmp(&done);
6393 }
6394 break;
6395 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396
Calin Juravle98893e12015-10-02 21:05:03 +01006397 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 case TypeCheckKind::kInterfaceCheck: {
6399 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006400 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401 // cases.
6402 //
6403 // We cannot directly call the InstanceofNonTrivial runtime
6404 // entry point without resorting to a type checking slow path
6405 // here (i.e. by calling InvokeRuntime directly), as it would
6406 // require to assign fixed registers for the inputs of this
6407 // HInstanceOf instruction (following the runtime calling
6408 // convention), which might be cluttered by the potential first
6409 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006410 //
6411 // TODO: Introduce a new runtime entry point taking the object
6412 // to test (instead of its class) as argument, and let it deal
6413 // with the read barrier issues. This will let us refactor this
6414 // case of the `switch` code as it was previously (with a direct
6415 // call to the runtime not using a type checking slow path).
6416 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006418 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6419 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 codegen_->AddSlowPath(slow_path);
6421 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 if (zero.IsLinked()) {
6423 __ jmp(&done);
6424 }
6425 break;
6426 }
Vladimir Marko175e7862018-03-27 09:03:13 +00006427
6428 case TypeCheckKind::kBitstringCheck: {
6429 // /* HeapReference<Class> */ temp = obj->klass_
6430 GenerateReferenceLoadTwoRegisters(instruction,
6431 out_loc,
6432 obj_loc,
6433 class_offset,
6434 kWithoutReadBarrier);
6435
6436 GenerateBitstringTypeCheckCompare(instruction, out);
6437 if (zero.IsLinked()) {
6438 __ j(kNotEqual, &zero);
6439 __ movl(out, Immediate(1));
6440 __ jmp(&done);
6441 } else {
6442 __ setcc(kEqual, out);
6443 // setcc only sets the low byte.
6444 __ andl(out, Immediate(1));
6445 }
6446 break;
6447 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006448 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006451 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 __ xorl(out, out);
6453 }
6454
6455 if (done.IsLinked()) {
6456 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006457 }
6458
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006459 if (slow_path != nullptr) {
6460 __ Bind(slow_path->GetExitLabel());
6461 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006462}
6463
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006464void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006465 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006466 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006467 LocationSummary* locations =
6468 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006470 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6471 // Require a register for the interface check since there is a loop that compares the class to
6472 // a memory address.
6473 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00006474 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
6475 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
6476 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
6477 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006478 } else {
6479 locations->SetInAt(1, Location::Any());
6480 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006481 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
6482 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006483}
6484
6485void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006486 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006487 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006488 Location obj_loc = locations->InAt(0);
6489 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006490 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 Location temp_loc = locations->GetTemp(0);
6492 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Vladimir Marko9f8d3122018-04-06 13:47:59 +01006493 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6494 DCHECK_GE(num_temps, 1u);
6495 DCHECK_LE(num_temps, 2u);
6496 Location maybe_temp2_loc = (num_temps >= 2u) ? locations->GetTemp(1) : Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006497 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6498 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6499 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6500 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6501 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6502 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006503 const uint32_t object_array_data_offset =
6504 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505
Vladimir Marko87584542017-12-12 17:47:52 +00006506 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006507 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006508 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86_64(
6509 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006510 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006511
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006512
6513 NearLabel done;
6514 // Avoid null check if we know obj is not null.
6515 if (instruction->MustDoNullCheck()) {
6516 __ testl(obj, obj);
6517 __ j(kEqual, &done);
6518 }
6519
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 case TypeCheckKind::kExactCheck:
6522 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006523 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006524 GenerateReferenceLoadTwoRegisters(instruction,
6525 temp_loc,
6526 obj_loc,
6527 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006528 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 if (cls.IsRegister()) {
6530 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6531 } else {
6532 DCHECK(cls.IsStackSlot()) << cls;
6533 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6534 }
6535 // Jump to slow path for throwing the exception or doing a
6536 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006538 break;
6539 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006540
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006541 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006542 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006543 GenerateReferenceLoadTwoRegisters(instruction,
6544 temp_loc,
6545 obj_loc,
6546 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006547 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 // If the class is abstract, we eagerly fetch the super class of the
6549 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006550 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006553 GenerateReferenceLoadOneRegister(instruction,
6554 temp_loc,
6555 super_offset,
6556 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006557 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006559 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6560 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006562 // Otherwise, compare the classes.
6563 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 if (cls.IsRegister()) {
6565 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6566 } else {
6567 DCHECK(cls.IsStackSlot()) << cls;
6568 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6569 }
6570 __ j(kNotEqual, &loop);
6571 break;
6572 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006574 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006575 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006576 GenerateReferenceLoadTwoRegisters(instruction,
6577 temp_loc,
6578 obj_loc,
6579 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006580 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006581 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006582 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006583 __ Bind(&loop);
6584 if (cls.IsRegister()) {
6585 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6586 } else {
6587 DCHECK(cls.IsStackSlot()) << cls;
6588 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6589 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006590 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006593 GenerateReferenceLoadOneRegister(instruction,
6594 temp_loc,
6595 super_offset,
6596 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006597 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006598
6599 // If the class reference currently in `temp` is not null, jump
6600 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006601 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006602 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 break;
6606 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006609 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006610 GenerateReferenceLoadTwoRegisters(instruction,
6611 temp_loc,
6612 obj_loc,
6613 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006614 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006615 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006617 if (cls.IsRegister()) {
6618 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6619 } else {
6620 DCHECK(cls.IsStackSlot()) << cls;
6621 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6622 }
6623 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624
6625 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006626 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006627 GenerateReferenceLoadOneRegister(instruction,
6628 temp_loc,
6629 component_offset,
6630 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006631 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006632
6633 // If the component type is not null (i.e. the object is indeed
6634 // an array), jump to label `check_non_primitive_component_type`
6635 // to further check that this component type is not a primitive
6636 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006638 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006639 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006640 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006641 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006642 break;
6643 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006644
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006645 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006646 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006647 //
6648 // We cannot directly call the CheckCast runtime entry point
6649 // without resorting to a type checking slow path here (i.e. by
6650 // calling InvokeRuntime directly), as it would require to
6651 // assign fixed registers for the inputs of this HInstanceOf
6652 // instruction (following the runtime calling convention), which
6653 // might be cluttered by the potential first read barrier
6654 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006655 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006656 break;
6657 }
6658
Vladimir Marko175e7862018-03-27 09:03:13 +00006659 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006660 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6661 // We can not get false positives by doing this.
6662 // /* HeapReference<Class> */ temp = obj->klass_
6663 GenerateReferenceLoadTwoRegisters(instruction,
6664 temp_loc,
6665 obj_loc,
6666 class_offset,
6667 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006668
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006669 // /* HeapReference<Class> */ temp = temp->iftable_
6670 GenerateReferenceLoadTwoRegisters(instruction,
6671 temp_loc,
6672 temp_loc,
6673 iftable_offset,
6674 kWithoutReadBarrier);
6675 // Iftable is never null.
6676 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
6677 // Maybe poison the `cls` for direct comparison with memory.
6678 __ MaybePoisonHeapReference(cls.AsRegister<CpuRegister>());
6679 // Loop through the iftable and check if any class matches.
6680 NearLabel start_loop;
6681 __ Bind(&start_loop);
6682 // Need to subtract first to handle the empty array case.
6683 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
6684 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6685 // Go to next interface if the classes do not match.
6686 __ cmpl(cls.AsRegister<CpuRegister>(),
6687 CodeGeneratorX86_64::ArrayAddress(temp,
6688 maybe_temp2_loc,
6689 TIMES_4,
6690 object_array_data_offset));
6691 __ j(kNotEqual, &start_loop); // Return if same class.
6692 // If `cls` was poisoned above, unpoison it.
6693 __ MaybeUnpoisonHeapReference(cls.AsRegister<CpuRegister>());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006694 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00006695 }
6696
6697 case TypeCheckKind::kBitstringCheck: {
6698 // /* HeapReference<Class> */ temp = obj->klass_
6699 GenerateReferenceLoadTwoRegisters(instruction,
6700 temp_loc,
6701 obj_loc,
6702 class_offset,
6703 kWithoutReadBarrier);
6704
6705 GenerateBitstringTypeCheckCompare(instruction, temp);
6706 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
6707 break;
6708 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006710
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006711 if (done.IsLinked()) {
6712 __ Bind(&done);
6713 }
6714
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006716}
6717
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006718void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006719 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6720 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006721 InvokeRuntimeCallingConvention calling_convention;
6722 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6723}
6724
6725void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006726 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006727 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006728 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006729 if (instruction->IsEnter()) {
6730 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6731 } else {
6732 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6733 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006734}
6735
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006736void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6737void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6738void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6739
6740void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006742 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006743 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6744 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006745 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006746 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006747 locations->SetOut(Location::SameAsFirstInput());
6748}
6749
6750void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6751 HandleBitwiseOperation(instruction);
6752}
6753
6754void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6755 HandleBitwiseOperation(instruction);
6756}
6757
6758void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6759 HandleBitwiseOperation(instruction);
6760}
6761
6762void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6763 LocationSummary* locations = instruction->GetLocations();
6764 Location first = locations->InAt(0);
6765 Location second = locations->InAt(1);
6766 DCHECK(first.Equals(locations->Out()));
6767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006768 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006769 if (second.IsRegister()) {
6770 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006771 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006772 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006773 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006774 } else {
6775 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006776 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006777 }
6778 } else if (second.IsConstant()) {
6779 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6780 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006781 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006782 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006783 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006784 } else {
6785 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006786 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006787 }
6788 } else {
6789 Address address(CpuRegister(RSP), second.GetStackIndex());
6790 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006791 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006792 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006793 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006794 } else {
6795 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006796 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006797 }
6798 }
6799 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006800 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006801 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6802 bool second_is_constant = false;
6803 int64_t value = 0;
6804 if (second.IsConstant()) {
6805 second_is_constant = true;
6806 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006807 }
Mark Mendell40741f32015-04-20 22:10:34 -04006808 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006809
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006810 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006811 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006812 if (is_int32_value) {
6813 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6814 } else {
6815 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6816 }
6817 } else if (second.IsDoubleStackSlot()) {
6818 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006819 } else {
6820 __ andq(first_reg, second.AsRegister<CpuRegister>());
6821 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006822 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006823 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006824 if (is_int32_value) {
6825 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6826 } else {
6827 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6828 }
6829 } else if (second.IsDoubleStackSlot()) {
6830 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006831 } else {
6832 __ orq(first_reg, second.AsRegister<CpuRegister>());
6833 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006834 } else {
6835 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006836 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006837 if (is_int32_value) {
6838 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6839 } else {
6840 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6841 }
6842 } else if (second.IsDoubleStackSlot()) {
6843 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006844 } else {
6845 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6846 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006847 }
6848 }
6849}
6850
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006851void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6852 HInstruction* instruction,
6853 Location out,
6854 uint32_t offset,
6855 Location maybe_temp,
6856 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006857 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006858 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006859 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006860 if (kUseBakerReadBarrier) {
6861 // Load with fast path based Baker's read barrier.
6862 // /* HeapReference<Object> */ out = *(out + offset)
6863 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006864 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006865 } else {
6866 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006867 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006868 // in the following move operation, as we will need it for the
6869 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006870 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006871 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006872 // /* HeapReference<Object> */ out = *(out + offset)
6873 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006874 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006875 }
6876 } else {
6877 // Plain load with no read barrier.
6878 // /* HeapReference<Object> */ out = *(out + offset)
6879 __ movl(out_reg, Address(out_reg, offset));
6880 __ MaybeUnpoisonHeapReference(out_reg);
6881 }
6882}
6883
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006884void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6885 HInstruction* instruction,
6886 Location out,
6887 Location obj,
6888 uint32_t offset,
6889 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006890 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6891 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006892 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006893 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006894 if (kUseBakerReadBarrier) {
6895 // Load with fast path based Baker's read barrier.
6896 // /* HeapReference<Object> */ out = *(obj + offset)
6897 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006898 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006899 } else {
6900 // Load with slow path based read barrier.
6901 // /* HeapReference<Object> */ out = *(obj + offset)
6902 __ movl(out_reg, Address(obj_reg, offset));
6903 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6904 }
6905 } else {
6906 // Plain load with no read barrier.
6907 // /* HeapReference<Object> */ out = *(obj + offset)
6908 __ movl(out_reg, Address(obj_reg, offset));
6909 __ MaybeUnpoisonHeapReference(out_reg);
6910 }
6911}
6912
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006913void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6914 HInstruction* instruction,
6915 Location root,
6916 const Address& address,
6917 Label* fixup_label,
6918 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006919 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006920 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006921 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006922 if (kUseBakerReadBarrier) {
6923 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6924 // Baker's read barrier are used:
6925 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006926 // root = obj.field;
6927 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6928 // if (temp != null) {
6929 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006930 // }
6931
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006932 // /* GcRoot<mirror::Object> */ root = *address
6933 __ movl(root_reg, address);
6934 if (fixup_label != nullptr) {
6935 __ Bind(fixup_label);
6936 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006937 static_assert(
6938 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6939 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6940 "have different sizes.");
6941 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6942 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6943 "have different sizes.");
6944
Vladimir Marko953437b2016-08-24 08:30:46 +00006945 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006946 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006947 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006948 codegen_->AddSlowPath(slow_path);
6949
Roland Levillaind966ce72017-02-09 16:20:14 +00006950 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6951 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006952 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006953 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6954 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006955 __ j(kNotEqual, slow_path->GetEntryLabel());
6956 __ Bind(slow_path->GetExitLabel());
6957 } else {
6958 // GC root loaded through a slow path for read barriers other
6959 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006960 // /* GcRoot<mirror::Object>* */ root = address
6961 __ leaq(root_reg, address);
6962 if (fixup_label != nullptr) {
6963 __ Bind(fixup_label);
6964 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006965 // /* mirror::Object* */ root = root->Read()
6966 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6967 }
6968 } else {
6969 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006970 // /* GcRoot<mirror::Object> */ root = *address
6971 __ movl(root_reg, address);
6972 if (fixup_label != nullptr) {
6973 __ Bind(fixup_label);
6974 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006975 // Note that GC roots are not affected by heap poisoning, thus we
6976 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006977 }
6978}
6979
6980void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6981 Location ref,
6982 CpuRegister obj,
6983 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006984 bool needs_null_check) {
6985 DCHECK(kEmitCompilerReadBarrier);
6986 DCHECK(kUseBakerReadBarrier);
6987
6988 // /* HeapReference<Object> */ ref = *(obj + offset)
6989 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006990 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006991}
6992
6993void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6994 Location ref,
6995 CpuRegister obj,
6996 uint32_t data_offset,
6997 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006998 bool needs_null_check) {
6999 DCHECK(kEmitCompilerReadBarrier);
7000 DCHECK(kUseBakerReadBarrier);
7001
Roland Levillain3d312422016-06-23 13:53:42 +01007002 static_assert(
7003 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7004 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007005 // /* HeapReference<Object> */ ref =
7006 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007007 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007008 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007009}
7010
7011void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7012 Location ref,
7013 CpuRegister obj,
7014 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007015 bool needs_null_check,
7016 bool always_update_field,
7017 CpuRegister* temp1,
7018 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007019 DCHECK(kEmitCompilerReadBarrier);
7020 DCHECK(kUseBakerReadBarrier);
7021
7022 // In slow path based read barriers, the read barrier call is
7023 // inserted after the original load. However, in fast path based
7024 // Baker's read barriers, we need to perform the load of
7025 // mirror::Object::monitor_ *before* the original reference load.
7026 // This load-load ordering is required by the read barrier.
7027 // The fast path/slow path (for Baker's algorithm) should look like:
7028 //
7029 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7030 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7031 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007032 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007033 // if (is_gray) {
7034 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7035 // }
7036 //
7037 // Note: the original implementation in ReadBarrier::Barrier is
7038 // slightly more complex as:
7039 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007040 // the high-bits of rb_state, which are expected to be all zeroes
7041 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
7042 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007043 // - it performs additional checks that we do not do here for
7044 // performance reasons.
7045
7046 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007047 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7048
Vladimir Marko953437b2016-08-24 08:30:46 +00007049 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007050 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7051 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007052 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7053 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7054 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7055
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007056 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007057 // ref = ReadBarrier::Mark(ref);
7058 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7059 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007060 if (needs_null_check) {
7061 MaybeRecordImplicitNullCheck(instruction);
7062 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007063
7064 // Load fence to prevent load-load reordering.
7065 // Note that this is a no-op, thanks to the x86-64 memory model.
7066 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7067
7068 // The actual reference load.
7069 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007070 __ movl(ref_reg, src); // Flags are unaffected.
7071
7072 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7073 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007074 SlowPathCode* slow_path;
7075 if (always_update_field) {
7076 DCHECK(temp1 != nullptr);
7077 DCHECK(temp2 != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007078 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007079 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
7080 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007081 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007082 instruction, ref, /* unpoison_ref_before_marking */ true);
7083 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007084 AddSlowPath(slow_path);
7085
7086 // We have done the "if" of the gray bit check above, now branch based on the flags.
7087 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007088
7089 // Object* ref = ref_addr->AsMirrorPtr()
7090 __ MaybeUnpoisonHeapReference(ref_reg);
7091
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007092 __ Bind(slow_path->GetExitLabel());
7093}
7094
7095void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
7096 Location out,
7097 Location ref,
7098 Location obj,
7099 uint32_t offset,
7100 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007101 DCHECK(kEmitCompilerReadBarrier);
7102
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007103 // Insert a slow path based read barrier *after* the reference load.
7104 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007105 // If heap poisoning is enabled, the unpoisoning of the loaded
7106 // reference will be carried out by the runtime within the slow
7107 // path.
7108 //
7109 // Note that `ref` currently does not get unpoisoned (when heap
7110 // poisoning is enabled), which is alright as the `ref` argument is
7111 // not used by the artReadBarrierSlow entry point.
7112 //
7113 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007114 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007115 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
7116 AddSlowPath(slow_path);
7117
Roland Levillain0d5a2812015-11-13 10:07:31 +00007118 __ jmp(slow_path->GetEntryLabel());
7119 __ Bind(slow_path->GetExitLabel());
7120}
7121
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007122void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7123 Location out,
7124 Location ref,
7125 Location obj,
7126 uint32_t offset,
7127 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007128 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007129 // Baker's read barriers shall be handled by the fast path
7130 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
7131 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007132 // If heap poisoning is enabled, unpoisoning will be taken care of
7133 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007134 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 } else if (kPoisonHeapReferences) {
7136 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
7137 }
7138}
7139
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007140void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7141 Location out,
7142 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007143 DCHECK(kEmitCompilerReadBarrier);
7144
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007145 // Insert a slow path based read barrier *after* the GC root load.
7146 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007147 // Note that GC roots are not affected by heap poisoning, so we do
7148 // not need to do anything special for this here.
7149 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007150 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007151 AddSlowPath(slow_path);
7152
Roland Levillain0d5a2812015-11-13 10:07:31 +00007153 __ jmp(slow_path->GetEntryLabel());
7154 __ Bind(slow_path->GetExitLabel());
7155}
7156
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007157void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007158 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007159 LOG(FATAL) << "Unreachable";
7160}
7161
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007162void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007163 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007164 LOG(FATAL) << "Unreachable";
7165}
7166
Mark Mendellfe57faa2015-09-18 09:26:15 -04007167// Simple implementation of packed switch - generate cascaded compare/jumps.
7168void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007170 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007171 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04007172 locations->AddTemp(Location::RequiresRegister());
7173 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04007174}
7175
7176void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7177 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007178 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007179 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04007180 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
7181 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
7182 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007183 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7184
7185 // Should we generate smaller inline compare/jumps?
7186 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7187 // Figure out the correct compare values and jump conditions.
7188 // Handle the first compare/branch as a special case because it might
7189 // jump to the default case.
7190 DCHECK_GT(num_entries, 2u);
7191 Condition first_condition;
7192 uint32_t index;
7193 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7194 if (lower_bound != 0) {
7195 first_condition = kLess;
7196 __ cmpl(value_reg_in, Immediate(lower_bound));
7197 __ j(first_condition, codegen_->GetLabelOf(default_block));
7198 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
7199
7200 index = 1;
7201 } else {
7202 // Handle all the compare/jumps below.
7203 first_condition = kBelow;
7204 index = 0;
7205 }
7206
7207 // Handle the rest of the compare/jumps.
7208 for (; index + 1 < num_entries; index += 2) {
7209 int32_t compare_to_value = lower_bound + index + 1;
7210 __ cmpl(value_reg_in, Immediate(compare_to_value));
7211 // Jump to successors[index] if value < case_value[index].
7212 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7213 // Jump to successors[index + 1] if value == case_value[index + 1].
7214 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7215 }
7216
7217 if (index != num_entries) {
7218 // There are an odd number of entries. Handle the last one.
7219 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00007220 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007221 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
7222 }
7223
7224 // And the default for any other value.
7225 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7226 __ jmp(codegen_->GetLabelOf(default_block));
7227 }
7228 return;
7229 }
Mark Mendell9c86b482015-09-18 13:36:07 -04007230
7231 // Remove the bias, if needed.
7232 Register value_reg_out = value_reg_in.AsRegister();
7233 if (lower_bound != 0) {
7234 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
7235 value_reg_out = temp_reg.AsRegister();
7236 }
7237 CpuRegister value_reg(value_reg_out);
7238
7239 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04007240 __ cmpl(value_reg, Immediate(num_entries - 1));
7241 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007242
Mark Mendell9c86b482015-09-18 13:36:07 -04007243 // We are in the range of the table.
7244 // Load the address of the jump table in the constant area.
7245 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007246
Mark Mendell9c86b482015-09-18 13:36:07 -04007247 // Load the (signed) offset from the jump table.
7248 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
7249
7250 // Add the offset to the address of the table base.
7251 __ addq(temp_reg, base_reg);
7252
7253 // And jump.
7254 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007255}
7256
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007257void LocationsBuilderX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7258 ATTRIBUTE_UNUSED) {
7259 LOG(FATAL) << "Unreachable";
7260}
7261
7262void InstructionCodeGeneratorX86_64::VisitIntermediateAddress(HIntermediateAddress* instruction
7263 ATTRIBUTE_UNUSED) {
7264 LOG(FATAL) << "Unreachable";
7265}
7266
Aart Bikc5d47542016-01-27 17:00:35 -08007267void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
7268 if (value == 0) {
7269 __ xorl(dest, dest);
7270 } else {
7271 __ movl(dest, Immediate(value));
7272 }
7273}
7274
Mark Mendell92e83bf2015-05-07 11:25:03 -04007275void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
7276 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08007277 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007278 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00007279 } else if (IsUint<32>(value)) {
7280 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04007281 __ movl(dest, Immediate(static_cast<int32_t>(value)));
7282 } else {
7283 __ movq(dest, Immediate(value));
7284 }
7285}
7286
Mark Mendell7c0b44f2016-02-01 10:08:35 -05007287void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
7288 if (value == 0) {
7289 __ xorps(dest, dest);
7290 } else {
7291 __ movss(dest, LiteralInt32Address(value));
7292 }
7293}
7294
7295void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
7296 if (value == 0) {
7297 __ xorpd(dest, dest);
7298 } else {
7299 __ movsd(dest, LiteralInt64Address(value));
7300 }
7301}
7302
7303void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
7304 Load32BitValue(dest, bit_cast<int32_t, float>(value));
7305}
7306
7307void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
7308 Load64BitValue(dest, bit_cast<int64_t, double>(value));
7309}
7310
Aart Bika19616e2016-02-01 18:57:58 -08007311void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
7312 if (value == 0) {
7313 __ testl(dest, dest);
7314 } else {
7315 __ cmpl(dest, Immediate(value));
7316 }
7317}
7318
7319void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
7320 if (IsInt<32>(value)) {
7321 if (value == 0) {
7322 __ testq(dest, dest);
7323 } else {
7324 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
7325 }
7326 } else {
7327 // Value won't fit in an int.
7328 __ cmpq(dest, LiteralInt64Address(value));
7329 }
7330}
7331
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007332void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
7333 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07007334 GenerateIntCompare(lhs_reg, rhs);
7335}
7336
7337void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007338 if (rhs.IsConstant()) {
7339 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007340 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007341 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007342 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007343 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007344 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007345 }
7346}
7347
7348void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
7349 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
7350 if (rhs.IsConstant()) {
7351 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
7352 Compare64BitValue(lhs_reg, value);
7353 } else if (rhs.IsDoubleStackSlot()) {
7354 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
7355 } else {
7356 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
7357 }
7358}
7359
7360Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
7361 Location index,
7362 ScaleFactor scale,
7363 uint32_t data_offset) {
7364 return index.IsConstant() ?
7365 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7366 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
7367}
7368
Mark Mendellcfa410b2015-05-25 16:02:44 -04007369void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
7370 DCHECK(dest.IsDoubleStackSlot());
7371 if (IsInt<32>(value)) {
7372 // Can move directly as an int32 constant.
7373 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
7374 Immediate(static_cast<int32_t>(value)));
7375 } else {
7376 Load64BitValue(CpuRegister(TMP), value);
7377 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
7378 }
7379}
7380
Mark Mendell9c86b482015-09-18 13:36:07 -04007381/**
7382 * Class to handle late fixup of offsets into constant area.
7383 */
7384class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
7385 public:
7386 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
7387 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7388
7389 protected:
7390 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7391
7392 CodeGeneratorX86_64* codegen_;
7393
7394 private:
7395 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7396 // Patch the correct offset for the instruction. We use the address of the
7397 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7398 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7399 int32_t relative_position = constant_offset - pos;
7400
7401 // Patch in the right value.
7402 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7403 }
7404
7405 // Location in constant area that the fixup refers to.
7406 size_t offset_into_constant_area_;
7407};
7408
7409/**
7410 t * Class to handle late fixup of offsets to a jump table that will be created in the
7411 * constant area.
7412 */
7413class JumpTableRIPFixup : public RIPFixup {
7414 public:
7415 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7416 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7417
7418 void CreateJumpTable() {
7419 X86_64Assembler* assembler = codegen_->GetAssembler();
7420
7421 // Ensure that the reference to the jump table has the correct offset.
7422 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7423 SetOffset(offset_in_constant_table);
7424
7425 // Compute the offset from the start of the function to this jump table.
7426 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7427
7428 // Populate the jump table with the correct values for the jump table.
7429 int32_t num_entries = switch_instr_->GetNumEntries();
7430 HBasicBlock* block = switch_instr_->GetBlock();
7431 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7432 // The value that we want is the target offset - the position of the table.
7433 for (int32_t i = 0; i < num_entries; i++) {
7434 HBasicBlock* b = successors[i];
7435 Label* l = codegen_->GetLabelOf(b);
7436 DCHECK(l->IsBound());
7437 int32_t offset_to_block = l->Position() - current_table_offset;
7438 assembler->AppendInt32(offset_to_block);
7439 }
7440 }
7441
7442 private:
7443 const HPackedSwitch* switch_instr_;
7444};
7445
Mark Mendellf55c3e02015-03-26 21:07:46 -04007446void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7447 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007448 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007449 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7450 // 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 -04007451 assembler->Align(4, 0);
7452 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007453
7454 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007455 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007456 jump_table->CreateJumpTable();
7457 }
7458
7459 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007460 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007461 }
7462
7463 // And finish up.
7464 CodeGenerator::Finalize(allocator);
7465}
7466
Mark Mendellf55c3e02015-03-26 21:07:46 -04007467Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007468 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddDouble(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007469 return Address::RIP(fixup);
7470}
7471
7472Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007473 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddFloat(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007474 return Address::RIP(fixup);
7475}
7476
7477Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007478 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt32(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007479 return Address::RIP(fixup);
7480}
7481
7482Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007483 AssemblerFixup* fixup = new (GetGraph()->GetAllocator()) RIPFixup(*this, __ AddInt64(v));
Mark Mendellf55c3e02015-03-26 21:07:46 -04007484 return Address::RIP(fixup);
7485}
7486
Andreas Gampe85b62f22015-09-09 13:15:38 -07007487// TODO: trg as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007488void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007489 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007490 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007491 return;
7492 }
7493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007494 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007495
7496 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7497 if (trg.Equals(return_loc)) {
7498 return;
7499 }
7500
7501 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007502 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007503 parallel_move.AddMove(return_loc, trg, type, nullptr);
7504 GetMoveResolver()->EmitNativeCode(&parallel_move);
7505}
7506
Mark Mendell9c86b482015-09-18 13:36:07 -04007507Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7508 // Create a fixup to be used to create and address the jump table.
7509 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007510 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell9c86b482015-09-18 13:36:07 -04007511
7512 // We have to populate the jump tables.
7513 fixups_to_jump_tables_.push_back(table_fixup);
7514 return Address::RIP(table_fixup);
7515}
7516
Mark Mendellea5af682015-10-22 17:35:49 -04007517void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7518 const Address& addr_high,
7519 int64_t v,
7520 HInstruction* instruction) {
7521 if (IsInt<32>(v)) {
7522 int32_t v_32 = v;
7523 __ movq(addr_low, Immediate(v_32));
7524 MaybeRecordImplicitNullCheck(instruction);
7525 } else {
7526 // Didn't fit in a register. Do it in pieces.
7527 int32_t low_v = Low32Bits(v);
7528 int32_t high_v = High32Bits(v);
7529 __ movl(addr_low, Immediate(low_v));
7530 MaybeRecordImplicitNullCheck(instruction);
7531 __ movl(addr_high, Immediate(high_v));
7532 }
7533}
7534
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007535void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7536 const uint8_t* roots_data,
7537 const PatchInfo<Label>& info,
7538 uint64_t index_in_table) const {
7539 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7540 uintptr_t address =
7541 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7542 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7543 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7544 dchecked_integral_cast<uint32_t>(address);
7545}
7546
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007547void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7548 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007549 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007550 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007551 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007552 }
7553
7554 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007555 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007556 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007557 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007558 }
7559}
7560
Roland Levillain4d027112015-07-01 15:41:14 +01007561#undef __
7562
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007563} // namespace x86_64
7564} // namespace art