blob: 7331a9e98ebf578b4c24af22eb1ca7db2c079b1e [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"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Andreas Gamped4901292017-05-30 18:41:34 -070026#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010029#include "mirror/object_reference.h"
30#include "thread.h"
31#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033#include "utils/x86_64/assembler_x86_64.h"
34#include "utils/x86_64/managed_register_x86_64.h"
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace art {
37
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010041namespace x86_64 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000045// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
46// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
47// generates less code/data with a small num_entries.
48static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000050static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000051static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
Mark Mendell24f2dfa2015-01-14 19:51:45 -050053static constexpr int kC2ConditionMask = 0x400;
54
Roland Levillain7cbd27f2016-08-11 23:53:33 +010055// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
56#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070057#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058
Andreas Gampe85b62f22015-09-09 13:15:38 -070059class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000061 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062
Alexandre Rames2ed20af2015-03-06 13:55:35 +000063 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000064 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000066 if (instruction_->CanThrowIntoCatchBlock()) {
67 // Live registers will be restored in the catch block if caught.
68 SaveLiveRegisters(codegen, instruction_->GetLocations());
69 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010070 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000071 instruction_,
72 instruction_->GetDexPc(),
73 this);
Roland Levillain888d0672015-11-23 18:53:50 +000074 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
Alexandre Rames8158f282015-08-07 10:26:17 +010077 bool IsFatal() const OVERRIDE { return true; }
78
Alexandre Rames9931f312015-06-19 14:47:01 +010079 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
80
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
83};
84
Andreas Gampe85b62f22015-09-09 13:15:38 -070085class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000086 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000087 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000088
Alexandre Rames2ed20af2015-03-06 13:55:35 +000089 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000090 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000091 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010092 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000093 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000094 }
95
Alexandre Rames8158f282015-08-07 10:26:17 +010096 bool IsFatal() const OVERRIDE { return true; }
97
Alexandre Rames9931f312015-06-19 14:47:01 +010098 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
99
Calin Juravled0d48522014-11-04 16:40:20 +0000100 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000101 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
102};
103
Andreas Gampe85b62f22015-09-09 13:15:38 -0700104class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000106 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
107 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000108
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000109 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000110 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000111 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(cpu_reg_);
114 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400115 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000116 }
117
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000118 } else {
119 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000120 if (is_div_) {
121 __ negq(cpu_reg_);
122 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400123 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000125 }
Calin Juravled0d48522014-11-04 16:40:20 +0000126 __ jmp(GetExitLabel());
127 }
128
Alexandre Rames9931f312015-06-19 14:47:01 +0100129 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
130
Calin Juravled0d48522014-11-04 16:40:20 +0000131 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000132 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000133 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000134 const bool is_div_;
135 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000136};
137
Andreas Gampe85b62f22015-09-09 13:15:38 -0700138class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100140 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000141 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000143 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700144 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000145 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700147 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100148 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000149 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700150 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 if (successor_ == nullptr) {
152 __ jmp(GetReturnLabel());
153 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000154 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 }
157
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 Label* GetReturnLabel() {
159 DCHECK(successor_ == nullptr);
160 return &return_label_;
161 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100163 HBasicBlock* GetSuccessor() const {
164 return successor_;
165 }
166
Alexandre Rames9931f312015-06-19 14:47:01 +0100167 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
168
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 Label return_label_;
172
173 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
174};
175
Andreas Gampe85b62f22015-09-09 13:15:38 -0700176class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000179 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000181 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100182 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000183 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000185 if (instruction_->CanThrowIntoCatchBlock()) {
186 // Live registers will be restored in the catch block if caught.
187 SaveLiveRegisters(codegen, instruction_->GetLocations());
188 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400189 // Are we using an array length from memory?
190 HInstruction* array_length = instruction_->InputAt(1);
191 Location length_loc = locations->InAt(1);
192 InvokeRuntimeCallingConvention calling_convention;
193 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
194 // Load the array length into our temporary.
195 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
196 Location array_loc = array_length->GetLocations()->InAt(0);
197 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
198 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
199 // Check for conflicts with index.
200 if (length_loc.Equals(locations->InAt(0))) {
201 // We know we aren't using parameter 2.
202 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
203 }
204 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700205 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100206 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700207 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400208 }
209
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000210 // We're moving two locations to locations that could overlap, so we need a parallel
211 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000212 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100213 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000214 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100215 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400216 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100217 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
218 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100219 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
220 ? kQuickThrowStringBounds
221 : kQuickThrowArrayBounds;
222 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100223 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000224 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100225 }
226
Alexandre Rames8158f282015-08-07 10:26:17 +0100227 bool IsFatal() const OVERRIDE { return true; }
228
Alexandre Rames9931f312015-06-19 14:47:01 +0100229 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
230
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100231 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100232 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
233};
234
Andreas Gampe85b62f22015-09-09 13:15:38 -0700235class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 LoadClassSlowPathX86_64(HLoadClass* cls,
238 HInstruction* at,
239 uint32_t dex_pc,
240 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000241 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
243 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000245 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000246 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000247 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251
Vladimir Markoea4c1262017-02-06 19:59:33 +0000252 // Custom calling convention: RAX serves as both input and output.
253 __ movl(CpuRegister(RAX), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100254 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000255 instruction_,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000256 dex_pc_,
257 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000258 if (do_clinit_) {
259 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
260 } else {
261 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
262 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100263
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000266 if (out.IsValid()) {
267 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000268 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 }
270
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
273 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
274 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
275 DCHECK(out.IsValid());
276 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
277 locations->Out().AsRegister<CpuRegister>());
278 Label* fixup_label = x86_64_codegen->NewTypeBssEntryPatch(cls_);
279 __ Bind(fixup_label);
280 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100281 __ jmp(GetExitLabel());
282 }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
285
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100286 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000287 // The class this slow path will load.
288 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100289
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290 // The dex PC of `at_`.
291 const uint32_t dex_pc_;
292
293 // Whether to initialize the class.
294 const bool do_clinit_;
295
296 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100297};
298
Vladimir Markoaad75c62016-10-03 08:46:48 +0000299class LoadStringSlowPathX86_64 : public SlowPathCode {
300 public:
301 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
304 LocationSummary* locations = instruction_->GetLocations();
305 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
306
307 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
308 __ Bind(GetEntryLabel());
309 SaveLiveRegisters(codegen, locations);
310
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000311 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100312 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000313 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000314 x86_64_codegen->InvokeRuntime(kQuickResolveString,
315 instruction_,
316 instruction_->GetDexPc(),
317 this);
318 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
319 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
320 RestoreLiveRegisters(codegen, locations);
321
322 // Store the resolved String to the BSS entry.
323 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
324 locations->Out().AsRegister<CpuRegister>());
325 Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
326 __ Bind(fixup_label);
327
328 __ jmp(GetExitLabel());
329 }
330
331 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
332
333 private:
334 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
335};
336
Andreas Gampe85b62f22015-09-09 13:15:38 -0700337class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000339 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000340 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 DCHECK(instruction_->IsCheckCast()
346 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347
Roland Levillain0d5a2812015-11-13 10:07:31 +0000348 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000349 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 if (!is_fatal_) {
352 SaveLiveRegisters(codegen, locations);
353 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354
355 // We're moving two locations to locations that could overlap, so we need a parallel
356 // move resolver.
357 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800359 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
360 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800362 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
363 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100365 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800366 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000367 } else {
368 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800369 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
370 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000371 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000372
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 if (!is_fatal_) {
374 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000375 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000377
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000378 RestoreLiveRegisters(codegen, locations);
379 __ jmp(GetExitLabel());
380 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 bool IsFatal() const OVERRIDE { return is_fatal_; }
386
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000387 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000388 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000389
390 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
391};
392
Andreas Gampe85b62f22015-09-09 13:15:38 -0700393class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 public:
Aart Bik42249c32016-01-07 15:33:50 -0800395 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000399 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100401 LocationSummary* locations = instruction_->GetLocations();
402 SaveLiveRegisters(codegen, locations);
403 InvokeRuntimeCallingConvention calling_convention;
404 x86_64_codegen->Load32BitValue(
405 CpuRegister(calling_convention.GetRegisterAt(0)),
406 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100407 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100408 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700409 }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
412
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700413 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700414 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
415};
416
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417class ArraySetSlowPathX86_64 : public SlowPathCode {
418 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000419 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100420
421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
422 LocationSummary* locations = instruction_->GetLocations();
423 __ Bind(GetEntryLabel());
424 SaveLiveRegisters(codegen, locations);
425
426 InvokeRuntimeCallingConvention calling_convention;
427 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
428 parallel_move.AddMove(
429 locations->InAt(0),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
431 Primitive::kPrimNot,
432 nullptr);
433 parallel_move.AddMove(
434 locations->InAt(1),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
436 Primitive::kPrimInt,
437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(2),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
441 Primitive::kPrimNot,
442 nullptr);
443 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
444
Roland Levillain0d5a2812015-11-13 10:07:31 +0000445 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100446 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000447 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100448 RestoreLiveRegisters(codegen, locations);
449 __ jmp(GetExitLabel());
450 }
451
452 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
453
454 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100455 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
456};
457
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100458// Slow path marking an object reference `ref` during a read
459// barrier. The field `obj.field` in the object `obj` holding this
460// reference does not get updated by this slow path after marking (see
461// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
462//
463// This means that after the execution of this slow path, `ref` will
464// always be up-to-date, but `obj.field` may not; i.e., after the
465// flip, `ref` will be a to-space reference, but `obj.field` will
466// probably still be a from-space reference (unless it gets updated by
467// another thread, or if another thread installed another object
468// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000469class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
470 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100471 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
472 Location ref,
473 bool unpoison_ref_before_marking)
474 : SlowPathCode(instruction),
475 ref_(ref),
476 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000477 DCHECK(kEmitCompilerReadBarrier);
478 }
479
480 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
481
482 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
483 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100484 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
485 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000486 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000488 DCHECK(instruction_->IsInstanceFieldGet() ||
489 instruction_->IsStaticFieldGet() ||
490 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100491 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000492 instruction_->IsLoadClass() ||
493 instruction_->IsLoadString() ||
494 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100495 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100496 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
497 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000498 << "Unexpected instruction in read barrier marking slow path: "
499 << instruction_->DebugName();
500
501 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000503 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000505 }
Roland Levillain4359e612016-07-20 11:32:19 +0100506 // No need to save live registers; it's taken care of by the
507 // entrypoint. Also, there is no need to update the stack mask,
508 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000509 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 DCHECK_NE(ref_reg, RSP);
511 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100512 // "Compact" slow path, saving two moves.
513 //
514 // Instead of using the standard runtime calling convention (input
515 // and output in R0):
516 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100518 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100519 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100520 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100521 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100522 // of a dedicated entrypoint:
523 //
524 // rX <- ReadBarrierMarkRegX(rX)
525 //
526 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100527 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100528 // This runtime call does not require a stack map.
529 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000530 __ jmp(GetExitLabel());
531 }
532
533 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100534 // The location (register) of the marked object reference.
535 const Location ref_;
536 // Should the reference in `ref_` be unpoisoned prior to marking it?
537 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000538
539 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
540};
541
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100542// Slow path marking an object reference `ref` during a read barrier,
543// and if needed, atomically updating the field `obj.field` in the
544// object `obj` holding this reference after marking (contrary to
545// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
546// `obj.field`).
547//
548// This means that after the execution of this slow path, both `ref`
549// and `obj.field` will be up-to-date; i.e., after the flip, both will
550// hold the same to-space reference (unless another thread installed
551// another object reference (different from `ref`) in `obj.field`).
552class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
553 public:
554 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
555 Location ref,
556 CpuRegister obj,
557 const Address& field_addr,
558 bool unpoison_ref_before_marking,
559 CpuRegister temp1,
560 CpuRegister temp2)
561 : SlowPathCode(instruction),
562 ref_(ref),
563 obj_(obj),
564 field_addr_(field_addr),
565 unpoison_ref_before_marking_(unpoison_ref_before_marking),
566 temp1_(temp1),
567 temp2_(temp2) {
568 DCHECK(kEmitCompilerReadBarrier);
569 }
570
571 const char* GetDescription() const OVERRIDE {
572 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
573 }
574
575 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
576 LocationSummary* locations = instruction_->GetLocations();
577 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
578 Register ref_reg = ref_cpu_reg.AsRegister();
579 DCHECK(locations->CanCall());
580 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
581 // This slow path is only used by the UnsafeCASObject intrinsic.
582 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
583 << "Unexpected instruction in read barrier marking and field updating slow path: "
584 << instruction_->DebugName();
585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
587
588 __ Bind(GetEntryLabel());
589 if (unpoison_ref_before_marking_) {
590 // Object* ref = ref_addr->AsMirrorPtr()
591 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
592 }
593
594 // Save the old (unpoisoned) reference.
595 __ movl(temp1_, ref_cpu_reg);
596
597 // No need to save live registers; it's taken care of by the
598 // entrypoint. Also, there is no need to update the stack mask,
599 // as this runtime call will not trigger a garbage collection.
600 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
601 DCHECK_NE(ref_reg, RSP);
602 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
603 // "Compact" slow path, saving two moves.
604 //
605 // Instead of using the standard runtime calling convention (input
606 // and output in R0):
607 //
608 // RDI <- ref
609 // RAX <- ReadBarrierMark(RDI)
610 // ref <- RAX
611 //
612 // we just use rX (the register containing `ref`) as input and output
613 // of a dedicated entrypoint:
614 //
615 // rX <- ReadBarrierMarkRegX(rX)
616 //
617 int32_t entry_point_offset =
618 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
619 // This runtime call does not require a stack map.
620 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
621
622 // If the new reference is different from the old reference,
623 // update the field in the holder (`*field_addr`).
624 //
625 // Note that this field could also hold a different object, if
626 // another thread had concurrently changed it. In that case, the
627 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
628 // operation below would abort the CAS, leaving the field as-is.
629 NearLabel done;
630 __ cmpl(temp1_, ref_cpu_reg);
631 __ j(kEqual, &done);
632
633 // Update the the holder's field atomically. This may fail if
634 // mutator updates before us, but it's OK. This is achived
635 // using a strong compare-and-set (CAS) operation with relaxed
636 // memory synchronization ordering, where the expected value is
637 // the old reference and the desired value is the new reference.
638 // This operation is implemented with a 32-bit LOCK CMPXLCHG
639 // instruction, which requires the expected value (the old
640 // reference) to be in EAX. Save RAX beforehand, and move the
641 // expected value (stored in `temp1_`) into EAX.
642 __ movq(temp2_, CpuRegister(RAX));
643 __ movl(CpuRegister(RAX), temp1_);
644
645 // Convenience aliases.
646 CpuRegister base = obj_;
647 CpuRegister expected = CpuRegister(RAX);
648 CpuRegister value = ref_cpu_reg;
649
650 bool base_equals_value = (base.AsRegister() == value.AsRegister());
651 Register value_reg = ref_reg;
652 if (kPoisonHeapReferences) {
653 if (base_equals_value) {
654 // If `base` and `value` are the same register location, move
655 // `value_reg` to a temporary register. This way, poisoning
656 // `value_reg` won't invalidate `base`.
657 value_reg = temp1_.AsRegister();
658 __ movl(CpuRegister(value_reg), base);
659 }
660
661 // Check that the register allocator did not assign the location
662 // of `expected` (RAX) to `value` nor to `base`, so that heap
663 // poisoning (when enabled) works as intended below.
664 // - If `value` were equal to `expected`, both references would
665 // be poisoned twice, meaning they would not be poisoned at
666 // all, as heap poisoning uses address negation.
667 // - If `base` were equal to `expected`, poisoning `expected`
668 // would invalidate `base`.
669 DCHECK_NE(value_reg, expected.AsRegister());
670 DCHECK_NE(base.AsRegister(), expected.AsRegister());
671
672 __ PoisonHeapReference(expected);
673 __ PoisonHeapReference(CpuRegister(value_reg));
674 }
675
676 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
677
678 // If heap poisoning is enabled, we need to unpoison the values
679 // that were poisoned earlier.
680 if (kPoisonHeapReferences) {
681 if (base_equals_value) {
682 // `value_reg` has been moved to a temporary register, no need
683 // to unpoison it.
684 } else {
685 __ UnpoisonHeapReference(CpuRegister(value_reg));
686 }
687 // No need to unpoison `expected` (RAX), as it is be overwritten below.
688 }
689
690 // Restore RAX.
691 __ movq(CpuRegister(RAX), temp2_);
692
693 __ Bind(&done);
694 __ jmp(GetExitLabel());
695 }
696
697 private:
698 // The location (register) of the marked object reference.
699 const Location ref_;
700 // The register containing the object holding the marked object reference field.
701 const CpuRegister obj_;
702 // The address of the marked reference field. The base of this address must be `obj_`.
703 const Address field_addr_;
704
705 // Should the reference in `ref_` be unpoisoned prior to marking it?
706 const bool unpoison_ref_before_marking_;
707
708 const CpuRegister temp1_;
709 const CpuRegister temp2_;
710
711 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
712};
713
Roland Levillain0d5a2812015-11-13 10:07:31 +0000714// Slow path generating a read barrier for a heap reference.
715class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
716 public:
717 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
718 Location out,
719 Location ref,
720 Location obj,
721 uint32_t offset,
722 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000723 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000724 out_(out),
725 ref_(ref),
726 obj_(obj),
727 offset_(offset),
728 index_(index) {
729 DCHECK(kEmitCompilerReadBarrier);
730 // If `obj` is equal to `out` or `ref`, it means the initial
731 // object has been overwritten by (or after) the heap object
732 // reference load to be instrumented, e.g.:
733 //
734 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000735 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000736 //
737 // In that case, we have lost the information about the original
738 // object, and the emitted read barrier cannot work properly.
739 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
740 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
741}
742
743 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
744 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
745 LocationSummary* locations = instruction_->GetLocations();
746 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
747 DCHECK(locations->CanCall());
748 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100749 DCHECK(instruction_->IsInstanceFieldGet() ||
750 instruction_->IsStaticFieldGet() ||
751 instruction_->IsArrayGet() ||
752 instruction_->IsInstanceOf() ||
753 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700754 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000755 << "Unexpected instruction in read barrier for heap reference slow path: "
756 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000757
758 __ Bind(GetEntryLabel());
759 SaveLiveRegisters(codegen, locations);
760
761 // We may have to change the index's value, but as `index_` is a
762 // constant member (like other "inputs" of this slow path),
763 // introduce a copy of it, `index`.
764 Location index = index_;
765 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100766 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000767 if (instruction_->IsArrayGet()) {
768 // Compute real offset and store it in index_.
769 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
770 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
771 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
772 // We are about to change the value of `index_reg` (see the
773 // calls to art::x86_64::X86_64Assembler::shll and
774 // art::x86_64::X86_64Assembler::AddImmediate below), but it
775 // has not been saved by the previous call to
776 // art::SlowPathCode::SaveLiveRegisters, as it is a
777 // callee-save register --
778 // art::SlowPathCode::SaveLiveRegisters does not consider
779 // callee-save registers, as it has been designed with the
780 // assumption that callee-save registers are supposed to be
781 // handled by the called function. So, as a callee-save
782 // register, `index_reg` _would_ eventually be saved onto
783 // the stack, but it would be too late: we would have
784 // changed its value earlier. Therefore, we manually save
785 // it here into another freely available register,
786 // `free_reg`, chosen of course among the caller-save
787 // registers (as a callee-save `free_reg` register would
788 // exhibit the same problem).
789 //
790 // Note we could have requested a temporary register from
791 // the register allocator instead; but we prefer not to, as
792 // this is a slow path, and we know we can find a
793 // caller-save register that is available.
794 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
795 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
796 index_reg = free_reg;
797 index = Location::RegisterLocation(index_reg);
798 } else {
799 // The initial register stored in `index_` has already been
800 // saved in the call to art::SlowPathCode::SaveLiveRegisters
801 // (as it is not a callee-save register), so we can freely
802 // use it.
803 }
804 // Shifting the index value contained in `index_reg` by the
805 // scale factor (2) cannot overflow in practice, as the
806 // runtime is unable to allocate object arrays with a size
807 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
808 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
809 static_assert(
810 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
811 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
812 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
813 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100814 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
815 // intrinsics, `index_` is not shifted by a scale factor of 2
816 // (as in the case of ArrayGet), as it is actually an offset
817 // to an object field within an object.
818 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000819 DCHECK(instruction_->GetLocations()->Intrinsified());
820 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
821 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
822 << instruction_->AsInvoke()->GetIntrinsic();
823 DCHECK_EQ(offset_, 0U);
824 DCHECK(index_.IsRegister());
825 }
826 }
827
828 // We're moving two or three locations to locations that could
829 // overlap, so we need a parallel move resolver.
830 InvokeRuntimeCallingConvention calling_convention;
831 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
832 parallel_move.AddMove(ref_,
833 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
834 Primitive::kPrimNot,
835 nullptr);
836 parallel_move.AddMove(obj_,
837 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
838 Primitive::kPrimNot,
839 nullptr);
840 if (index.IsValid()) {
841 parallel_move.AddMove(index,
842 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
843 Primitive::kPrimInt,
844 nullptr);
845 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
846 } else {
847 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
848 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
849 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100850 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000851 instruction_,
852 instruction_->GetDexPc(),
853 this);
854 CheckEntrypointTypes<
855 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
856 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
857
858 RestoreLiveRegisters(codegen, locations);
859 __ jmp(GetExitLabel());
860 }
861
862 const char* GetDescription() const OVERRIDE {
863 return "ReadBarrierForHeapReferenceSlowPathX86_64";
864 }
865
866 private:
867 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
868 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
869 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
870 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
871 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
872 return static_cast<CpuRegister>(i);
873 }
874 }
875 // We shall never fail to find a free caller-save register, as
876 // there are more than two core caller-save registers on x86-64
877 // (meaning it is possible to find one which is different from
878 // `ref` and `obj`).
879 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
880 LOG(FATAL) << "Could not find a free caller-save register";
881 UNREACHABLE();
882 }
883
Roland Levillain0d5a2812015-11-13 10:07:31 +0000884 const Location out_;
885 const Location ref_;
886 const Location obj_;
887 const uint32_t offset_;
888 // An additional location containing an index to an array.
889 // Only used for HArrayGet and the UnsafeGetObject &
890 // UnsafeGetObjectVolatile intrinsics.
891 const Location index_;
892
893 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
894};
895
896// Slow path generating a read barrier for a GC root.
897class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
898 public:
899 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000900 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000901 DCHECK(kEmitCompilerReadBarrier);
902 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000903
904 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
905 LocationSummary* locations = instruction_->GetLocations();
906 DCHECK(locations->CanCall());
907 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000908 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
909 << "Unexpected instruction in read barrier for GC root slow path: "
910 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000911
912 __ Bind(GetEntryLabel());
913 SaveLiveRegisters(codegen, locations);
914
915 InvokeRuntimeCallingConvention calling_convention;
916 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
917 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100918 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000919 instruction_,
920 instruction_->GetDexPc(),
921 this);
922 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
923 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
924
925 RestoreLiveRegisters(codegen, locations);
926 __ jmp(GetExitLabel());
927 }
928
929 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
930
931 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000932 const Location out_;
933 const Location root_;
934
935 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
936};
937
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100938#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100939// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
940#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100941
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700943 switch (cond) {
944 case kCondEQ: return kEqual;
945 case kCondNE: return kNotEqual;
946 case kCondLT: return kLess;
947 case kCondLE: return kLessEqual;
948 case kCondGT: return kGreater;
949 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700950 case kCondB: return kBelow;
951 case kCondBE: return kBelowEqual;
952 case kCondA: return kAbove;
953 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700954 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100955 LOG(FATAL) << "Unreachable";
956 UNREACHABLE();
957}
958
Aart Bike9f37602015-10-09 11:15:55 -0700959// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960inline Condition X86_64FPCondition(IfCondition cond) {
961 switch (cond) {
962 case kCondEQ: return kEqual;
963 case kCondNE: return kNotEqual;
964 case kCondLT: return kBelow;
965 case kCondLE: return kBelowEqual;
966 case kCondGT: return kAbove;
967 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700968 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100969 };
970 LOG(FATAL) << "Unreachable";
971 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700972}
973
Vladimir Markodc151b22015-10-15 18:02:30 +0100974HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
975 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100976 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000977 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100978}
979
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100980void CodeGeneratorX86_64::GenerateStaticOrDirectCall(
981 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800982 // All registers are assumed to be correctly set up.
Vladimir Marko4ee8e292017-06-02 15:39:30 +0000983
Vladimir Marko58155012015-08-19 12:49:41 +0000984 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
985 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100986 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000987 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100988 uint32_t offset =
989 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
990 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000991 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100992 }
Vladimir Marko58155012015-08-19 12:49:41 +0000993 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000994 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000995 break;
Vladimir Marko65979462017-05-19 17:25:12 +0100996 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
997 DCHECK(GetCompilerOptions().IsBootImage());
998 __ leal(temp.AsRegister<CpuRegister>(),
999 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
1000 RecordBootMethodPatch(invoke);
1001 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001002 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +00001003 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +00001004 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001005 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00001006 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001007 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001008 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001009 __ Bind(NewMethodBssEntryPatch(
1010 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko58155012015-08-19 12:49:41 +00001011 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001012 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001013 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
1014 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
1015 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01001016 }
Vladimir Marko58155012015-08-19 12:49:41 +00001017 }
1018
1019 switch (invoke->GetCodePtrLocation()) {
1020 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1021 __ call(&frame_entry_label_);
1022 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001023 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1024 // (callee_method + offset_of_quick_compiled_code)()
1025 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1026 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001027 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001028 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001029 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001030 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001031
1032 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001033}
1034
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001035void CodeGeneratorX86_64::GenerateVirtualCall(
1036 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001037 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1038 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1039 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001040
1041 // Use the calling convention instead of the location of the receiver, as
1042 // intrinsics may have put the receiver in a different register. In the intrinsics
1043 // slow path, the arguments have been moved to the right place, so here we are
1044 // guaranteed that the receiver is the first register of the calling convention.
1045 InvokeDexCallingConvention calling_convention;
1046 Register receiver = calling_convention.GetRegisterAt(0);
1047
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001048 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001049 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001050 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001051 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001052 // Instead of simply (possibly) unpoisoning `temp` here, we should
1053 // emit a read barrier for the previous class reference load.
1054 // However this is not required in practice, as this is an
1055 // intermediate/temporary reference and because the current
1056 // concurrent copying collector keeps the from-space memory
1057 // intact/accessible until the end of the marking phase (the
1058 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001059 __ MaybeUnpoisonHeapReference(temp);
1060 // temp = temp->GetMethodAt(method_offset);
1061 __ movq(temp, Address(temp, method_offset));
1062 // call temp->GetEntryPoint();
1063 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001064 kX86_64PointerSize).SizeValue()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001065 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001066}
1067
Vladimir Marko65979462017-05-19 17:25:12 +01001068void CodeGeneratorX86_64::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
1069 boot_image_method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
1070 invoke->GetTargetMethod().dex_method_index);
1071 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001072}
1073
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001074Label* CodeGeneratorX86_64::NewMethodBssEntryPatch(MethodReference target_method) {
1075 // Add a patch entry and return the label.
1076 method_bss_entry_patches_.emplace_back(*target_method.dex_file, target_method.dex_method_index);
1077 return &method_bss_entry_patches_.back().label;
1078}
1079
Vladimir Marko1998cd02017-01-13 13:02:58 +00001080void CodeGeneratorX86_64::RecordBootTypePatch(HLoadClass* load_class) {
1081 boot_image_type_patches_.emplace_back(load_class->GetDexFile(),
1082 load_class->GetTypeIndex().index_);
1083 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001084}
1085
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001086Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001087 type_bss_entry_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
1088 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001089}
1090
Vladimir Marko65979462017-05-19 17:25:12 +01001091void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
1092 DCHECK(GetCompilerOptions().IsBootImage());
1093 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
1094 __ Bind(&string_patches_.back().label);
1095}
1096
Vladimir Markoaad75c62016-10-03 08:46:48 +00001097Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1098 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001099 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001100 return &string_patches_.back().label;
1101}
1102
Vladimir Markoaad75c62016-10-03 08:46:48 +00001103// The label points to the end of the "movl" or another instruction but the literal offset
1104// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1105constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1106
1107template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1108inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1109 const ArenaDeque<PatchInfo<Label>>& infos,
1110 ArenaVector<LinkerPatch>* linker_patches) {
1111 for (const PatchInfo<Label>& info : infos) {
1112 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1113 linker_patches->push_back(
1114 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1115 }
1116}
1117
Vladimir Marko58155012015-08-19 12:49:41 +00001118void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1119 DCHECK(linker_patches->empty());
1120 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001121 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001122 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001123 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001124 type_bss_entry_patches_.size() +
1125 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001126 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001127 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01001128 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
1129 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001130 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
1131 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001132 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001133 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01001134 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01001135 DCHECK(boot_image_type_patches_.empty());
1136 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001137 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001138 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1139 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001140 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1141 linker_patches);
1142 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001143}
1144
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001145void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001146 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147}
1148
1149void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001150 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151}
1152
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001153size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1155 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001156}
1157
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001158size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1159 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1160 return kX86_64WordSize;
1161}
1162
1163size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001164 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001165 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001166 } else {
1167 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1168 }
1169 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001170}
1171
1172size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001173 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001174 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001175 } else {
1176 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1177 }
1178 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001179}
1180
Calin Juravle175dc732015-08-25 15:42:32 +01001181void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1182 HInstruction* instruction,
1183 uint32_t dex_pc,
1184 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001185 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001186 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1187 if (EntrypointRequiresStackMap(entrypoint)) {
1188 RecordPcInfo(instruction, dex_pc, slow_path);
1189 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001190}
1191
Roland Levillaindec8f632016-07-22 17:10:06 +01001192void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1193 HInstruction* instruction,
1194 SlowPathCode* slow_path) {
1195 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001196 GenerateInvokeRuntime(entry_point_offset);
1197}
1198
1199void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001200 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1201}
1202
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001203static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001204// Use a fake return address register to mimic Quick.
1205static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001206CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001207 const X86_64InstructionSetFeatures& isa_features,
1208 const CompilerOptions& compiler_options,
1209 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001210 : CodeGenerator(graph,
1211 kNumberOfCpuRegisters,
1212 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001213 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001214 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1215 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001216 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001217 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1218 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001219 compiler_options,
1220 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001221 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001223 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001224 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001225 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001226 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001227 constant_area_start_(0),
Vladimir Marko65979462017-05-19 17:25:12 +01001228 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001229 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001230 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1231 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001232 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001233 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001234 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1235 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001236 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1237}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001238
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001239InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1240 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001241 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242 assembler_(codegen->GetAssembler()),
1243 codegen_(codegen) {}
1244
David Brazdil58282f42016-01-14 12:45:10 +00001245void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001246 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001247 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001249 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001250 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251}
1252
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001253static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001254 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001255}
David Srbecky9d8606d2015-04-12 09:35:32 +01001256
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001257static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001258 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001259}
1260
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001261void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001262 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001263 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001264 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001265 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001266 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001267
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001268 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001269 __ testq(CpuRegister(RAX), Address(
1270 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001271 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001272 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001273
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001274 if (HasEmptyFrame()) {
1275 return;
1276 }
1277
Nicolas Geoffray98893962015-01-21 12:32:32 +00001278 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001279 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001280 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001281 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001282 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1283 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001284 }
1285 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001286
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287 int adjust = GetFrameSize() - GetCoreSpillSize();
1288 __ subq(CpuRegister(RSP), Immediate(adjust));
1289 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001290 uint32_t xmm_spill_location = GetFpuSpillStart();
1291 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001292
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001293 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1294 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001295 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1296 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1297 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001298 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001299 }
1300
Mingyao Yang063fc772016-08-02 11:02:54 -07001301 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1302 // Initialize should_deoptimize flag to 0.
1303 __ movl(Address(CpuRegister(RSP), xmm_spill_location - kShouldDeoptimizeFlagSize),
1304 Immediate(0));
1305 }
1306
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001307 // Save the current method if we need it. Note that we do not
1308 // do this in HCurrentMethod, as the instruction might have been removed
1309 // in the SSA graph.
1310 if (RequiresCurrentMethod()) {
1311 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1312 CpuRegister(kMethodRegisterArgument));
1313 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001314}
1315
1316void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001317 __ cfi().RememberState();
1318 if (!HasEmptyFrame()) {
1319 uint32_t xmm_spill_location = GetFpuSpillStart();
1320 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1321 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1322 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1323 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1324 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1325 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1326 }
1327 }
1328
1329 int adjust = GetFrameSize() - GetCoreSpillSize();
1330 __ addq(CpuRegister(RSP), Immediate(adjust));
1331 __ cfi().AdjustCFAOffset(-adjust);
1332
1333 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1334 Register reg = kCoreCalleeSaves[i];
1335 if (allocated_registers_.ContainsCoreRegister(reg)) {
1336 __ popq(CpuRegister(reg));
1337 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1338 __ cfi().Restore(DWARFReg(reg));
1339 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001340 }
1341 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001342 __ ret();
1343 __ cfi().RestoreState();
1344 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001345}
1346
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001347void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1348 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001349}
1350
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001351void CodeGeneratorX86_64::Move(Location destination, Location source) {
1352 if (source.Equals(destination)) {
1353 return;
1354 }
1355 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001356 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001357 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001358 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001359 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001360 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001361 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001362 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1363 } else if (source.IsConstant()) {
1364 HConstant* constant = source.GetConstant();
1365 if (constant->IsLongConstant()) {
1366 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1367 } else {
1368 Load32BitValue(dest, GetInt32ValueOf(constant));
1369 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001370 } else {
1371 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001372 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001373 }
1374 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001375 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001376 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001377 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001379 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1380 } else if (source.IsConstant()) {
1381 HConstant* constant = source.GetConstant();
1382 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1383 if (constant->IsFloatConstant()) {
1384 Load32BitValue(dest, static_cast<int32_t>(value));
1385 } else {
1386 Load64BitValue(dest, value);
1387 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001388 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001389 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001390 } else {
1391 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001392 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001393 }
1394 } else if (destination.IsStackSlot()) {
1395 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001396 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001397 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001398 } else if (source.IsFpuRegister()) {
1399 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001401 } else if (source.IsConstant()) {
1402 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001403 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001404 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001405 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001406 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001407 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1408 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001409 }
1410 } else {
1411 DCHECK(destination.IsDoubleStackSlot());
1412 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001413 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001414 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001415 } else if (source.IsFpuRegister()) {
1416 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001417 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001418 } else if (source.IsConstant()) {
1419 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001420 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1421 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001422 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001423 } else {
1424 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001425 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1426 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001427 }
1428 }
1429}
1430
Calin Juravle175dc732015-08-25 15:42:32 +01001431void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1432 DCHECK(location.IsRegister());
1433 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1434}
1435
Calin Juravlee460d1d2015-09-29 04:52:17 +01001436void CodeGeneratorX86_64::MoveLocation(
1437 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1438 Move(dst, src);
1439}
1440
1441void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1442 if (location.IsRegister()) {
1443 locations->AddTemp(location);
1444 } else {
1445 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1446 }
1447}
1448
David Brazdilfc6a86a2015-06-26 10:33:45 +00001449void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001450 DCHECK(!successor->IsExitBlock());
1451
1452 HBasicBlock* block = got->GetBlock();
1453 HInstruction* previous = got->GetPrevious();
1454
1455 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001456 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001457 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1458 return;
1459 }
1460
1461 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1462 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1463 }
1464 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001465 __ jmp(codegen_->GetLabelOf(successor));
1466 }
1467}
1468
David Brazdilfc6a86a2015-06-26 10:33:45 +00001469void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1470 got->SetLocations(nullptr);
1471}
1472
1473void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1474 HandleGoto(got, got->GetSuccessor());
1475}
1476
1477void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1478 try_boundary->SetLocations(nullptr);
1479}
1480
1481void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1482 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1483 if (!successor->IsExitBlock()) {
1484 HandleGoto(try_boundary, successor);
1485 }
1486}
1487
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1489 exit->SetLocations(nullptr);
1490}
1491
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001492void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493}
1494
Mark Mendell152408f2015-12-31 12:28:50 -05001495template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001496void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001497 LabelType* true_label,
1498 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001499 if (cond->IsFPConditionTrueIfNaN()) {
1500 __ j(kUnordered, true_label);
1501 } else if (cond->IsFPConditionFalseIfNaN()) {
1502 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001503 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001504 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001505}
1506
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001507void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001508 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001509
Mark Mendellc4701932015-04-10 13:18:51 -04001510 Location left = locations->InAt(0);
1511 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001512 Primitive::Type type = condition->InputAt(0)->GetType();
1513 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001514 case Primitive::kPrimBoolean:
1515 case Primitive::kPrimByte:
1516 case Primitive::kPrimChar:
1517 case Primitive::kPrimShort:
1518 case Primitive::kPrimInt:
1519 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001520 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001521 break;
1522 }
Mark Mendellc4701932015-04-10 13:18:51 -04001523 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001524 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001525 break;
1526 }
1527 case Primitive::kPrimFloat: {
1528 if (right.IsFpuRegister()) {
1529 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1530 } else if (right.IsConstant()) {
1531 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1532 codegen_->LiteralFloatAddress(
1533 right.GetConstant()->AsFloatConstant()->GetValue()));
1534 } else {
1535 DCHECK(right.IsStackSlot());
1536 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1537 Address(CpuRegister(RSP), right.GetStackIndex()));
1538 }
Mark Mendellc4701932015-04-10 13:18:51 -04001539 break;
1540 }
1541 case Primitive::kPrimDouble: {
1542 if (right.IsFpuRegister()) {
1543 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1544 } else if (right.IsConstant()) {
1545 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1546 codegen_->LiteralDoubleAddress(
1547 right.GetConstant()->AsDoubleConstant()->GetValue()));
1548 } else {
1549 DCHECK(right.IsDoubleStackSlot());
1550 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1551 Address(CpuRegister(RSP), right.GetStackIndex()));
1552 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001553 break;
1554 }
1555 default:
1556 LOG(FATAL) << "Unexpected condition type " << type;
1557 }
1558}
1559
1560template<class LabelType>
1561void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1562 LabelType* true_target_in,
1563 LabelType* false_target_in) {
1564 // Generated branching requires both targets to be explicit. If either of the
1565 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1566 LabelType fallthrough_target;
1567 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1568 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1569
1570 // Generate the comparison to set the CC.
1571 GenerateCompareTest(condition);
1572
1573 // Now generate the correct jump(s).
1574 Primitive::Type type = condition->InputAt(0)->GetType();
1575 switch (type) {
1576 case Primitive::kPrimLong: {
1577 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1578 break;
1579 }
1580 case Primitive::kPrimFloat: {
1581 GenerateFPJumps(condition, true_target, false_target);
1582 break;
1583 }
1584 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001585 GenerateFPJumps(condition, true_target, false_target);
1586 break;
1587 }
1588 default:
1589 LOG(FATAL) << "Unexpected condition type " << type;
1590 }
1591
David Brazdil0debae72015-11-12 18:37:00 +00001592 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001593 __ jmp(false_target);
1594 }
David Brazdil0debae72015-11-12 18:37:00 +00001595
1596 if (fallthrough_target.IsLinked()) {
1597 __ Bind(&fallthrough_target);
1598 }
Mark Mendellc4701932015-04-10 13:18:51 -04001599}
1600
David Brazdil0debae72015-11-12 18:37:00 +00001601static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1602 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1603 // are set only strictly before `branch`. We can't use the eflags on long
1604 // conditions if they are materialized due to the complex branching.
1605 return cond->IsCondition() &&
1606 cond->GetNext() == branch &&
1607 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1608}
1609
Mark Mendell152408f2015-12-31 12:28:50 -05001610template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001612 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001613 LabelType* true_target,
1614 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001615 HInstruction* cond = instruction->InputAt(condition_input_index);
1616
1617 if (true_target == nullptr && false_target == nullptr) {
1618 // Nothing to do. The code always falls through.
1619 return;
1620 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001621 // Constant condition, statically compared against "true" (integer value 1).
1622 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001623 if (true_target != nullptr) {
1624 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001625 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001626 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001627 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001628 if (false_target != nullptr) {
1629 __ jmp(false_target);
1630 }
1631 }
1632 return;
1633 }
1634
1635 // The following code generates these patterns:
1636 // (1) true_target == nullptr && false_target != nullptr
1637 // - opposite condition true => branch to false_target
1638 // (2) true_target != nullptr && false_target == nullptr
1639 // - condition true => branch to true_target
1640 // (3) true_target != nullptr && false_target != nullptr
1641 // - condition true => branch to true_target
1642 // - branch to false_target
1643 if (IsBooleanValueOrMaterializedCondition(cond)) {
1644 if (AreEflagsSetFrom(cond, instruction)) {
1645 if (true_target == nullptr) {
1646 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1647 } else {
1648 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1649 }
1650 } else {
1651 // Materialized condition, compare against 0.
1652 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1653 if (lhs.IsRegister()) {
1654 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1655 } else {
1656 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1657 }
1658 if (true_target == nullptr) {
1659 __ j(kEqual, false_target);
1660 } else {
1661 __ j(kNotEqual, true_target);
1662 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001663 }
1664 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001665 // Condition has not been materialized, use its inputs as the
1666 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001667 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001668
David Brazdil0debae72015-11-12 18:37:00 +00001669 // If this is a long or FP comparison that has been folded into
1670 // the HCondition, generate the comparison directly.
1671 Primitive::Type type = condition->InputAt(0)->GetType();
1672 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1673 GenerateCompareTestAndBranch(condition, true_target, false_target);
1674 return;
1675 }
1676
1677 Location lhs = condition->GetLocations()->InAt(0);
1678 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001679 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001680 if (true_target == nullptr) {
1681 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1682 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001683 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001684 }
Dave Allison20dfc792014-06-16 20:44:29 -07001685 }
David Brazdil0debae72015-11-12 18:37:00 +00001686
1687 // If neither branch falls through (case 3), the conditional branch to `true_target`
1688 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1689 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001690 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001691 }
1692}
1693
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001694void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001695 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1696 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001697 locations->SetInAt(0, Location::Any());
1698 }
1699}
1700
1701void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001702 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1703 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1704 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1705 nullptr : codegen_->GetLabelOf(true_successor);
1706 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1707 nullptr : codegen_->GetLabelOf(false_successor);
1708 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001709}
1710
1711void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1712 LocationSummary* locations = new (GetGraph()->GetArena())
1713 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001714 InvokeRuntimeCallingConvention calling_convention;
1715 RegisterSet caller_saves = RegisterSet::Empty();
1716 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1717 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001718 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001719 locations->SetInAt(0, Location::Any());
1720 }
1721}
1722
1723void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001724 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001725 GenerateTestAndBranch<Label>(deoptimize,
1726 /* condition_input_index */ 0,
1727 slow_path->GetEntryLabel(),
1728 /* false_target */ nullptr);
1729}
1730
Mingyao Yang063fc772016-08-02 11:02:54 -07001731void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1732 LocationSummary* locations = new (GetGraph()->GetArena())
1733 LocationSummary(flag, LocationSummary::kNoCall);
1734 locations->SetOut(Location::RequiresRegister());
1735}
1736
1737void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1738 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1739 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1740}
1741
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001742static bool SelectCanUseCMOV(HSelect* select) {
1743 // There are no conditional move instructions for XMMs.
1744 if (Primitive::IsFloatingPointType(select->GetType())) {
1745 return false;
1746 }
1747
1748 // A FP condition doesn't generate the single CC that we need.
1749 HInstruction* condition = select->GetCondition();
1750 if (condition->IsCondition() &&
1751 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1752 return false;
1753 }
1754
1755 // We can generate a CMOV for this Select.
1756 return true;
1757}
1758
David Brazdil74eb1b22015-12-14 11:44:01 +00001759void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1761 if (Primitive::IsFloatingPointType(select->GetType())) {
1762 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001763 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001764 } else {
1765 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001766 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001767 if (select->InputAt(1)->IsConstant()) {
1768 locations->SetInAt(1, Location::RequiresRegister());
1769 } else {
1770 locations->SetInAt(1, Location::Any());
1771 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001772 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001773 locations->SetInAt(1, Location::Any());
1774 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001775 }
1776 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1777 locations->SetInAt(2, Location::RequiresRegister());
1778 }
1779 locations->SetOut(Location::SameAsFirstInput());
1780}
1781
1782void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1783 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001784 if (SelectCanUseCMOV(select)) {
1785 // If both the condition and the source types are integer, we can generate
1786 // a CMOV to implement Select.
1787 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001788 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001789 DCHECK(locations->InAt(0).Equals(locations->Out()));
1790
1791 HInstruction* select_condition = select->GetCondition();
1792 Condition cond = kNotEqual;
1793
1794 // Figure out how to test the 'condition'.
1795 if (select_condition->IsCondition()) {
1796 HCondition* condition = select_condition->AsCondition();
1797 if (!condition->IsEmittedAtUseSite()) {
1798 // This was a previously materialized condition.
1799 // Can we use the existing condition code?
1800 if (AreEflagsSetFrom(condition, select)) {
1801 // Materialization was the previous instruction. Condition codes are right.
1802 cond = X86_64IntegerCondition(condition->GetCondition());
1803 } else {
1804 // No, we have to recreate the condition code.
1805 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1806 __ testl(cond_reg, cond_reg);
1807 }
1808 } else {
1809 GenerateCompareTest(condition);
1810 cond = X86_64IntegerCondition(condition->GetCondition());
1811 }
1812 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001813 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001814 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1815 __ testl(cond_reg, cond_reg);
1816 }
1817
1818 // If the condition is true, overwrite the output, which already contains false.
1819 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001820 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1821 if (value_true_loc.IsRegister()) {
1822 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1823 } else {
1824 __ cmov(cond,
1825 value_false,
1826 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1827 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001828 } else {
1829 NearLabel false_target;
1830 GenerateTestAndBranch<NearLabel>(select,
1831 /* condition_input_index */ 2,
1832 /* true_target */ nullptr,
1833 &false_target);
1834 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1835 __ Bind(&false_target);
1836 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001837}
1838
David Srbecky0cf44932015-12-09 14:09:59 +00001839void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1840 new (GetGraph()->GetArena()) LocationSummary(info);
1841}
1842
David Srbeckyd28f4a02016-03-14 17:14:24 +00001843void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1844 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001845}
1846
1847void CodeGeneratorX86_64::GenerateNop() {
1848 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001849}
1850
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001853 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001854 // Handle the long/FP comparisons made in instruction simplification.
1855 switch (cond->InputAt(0)->GetType()) {
1856 case Primitive::kPrimLong:
1857 locations->SetInAt(0, Location::RequiresRegister());
1858 locations->SetInAt(1, Location::Any());
1859 break;
1860 case Primitive::kPrimFloat:
1861 case Primitive::kPrimDouble:
1862 locations->SetInAt(0, Location::RequiresFpuRegister());
1863 locations->SetInAt(1, Location::Any());
1864 break;
1865 default:
1866 locations->SetInAt(0, Location::RequiresRegister());
1867 locations->SetInAt(1, Location::Any());
1868 break;
1869 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001870 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001871 locations->SetOut(Location::RequiresRegister());
1872 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873}
1874
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001875void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001876 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001877 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001878 }
Mark Mendellc4701932015-04-10 13:18:51 -04001879
1880 LocationSummary* locations = cond->GetLocations();
1881 Location lhs = locations->InAt(0);
1882 Location rhs = locations->InAt(1);
1883 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001884 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001885
1886 switch (cond->InputAt(0)->GetType()) {
1887 default:
1888 // Integer case.
1889
1890 // Clear output register: setcc only sets the low byte.
1891 __ xorl(reg, reg);
1892
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001893 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001894 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001895 return;
1896 case Primitive::kPrimLong:
1897 // Clear output register: setcc only sets the low byte.
1898 __ xorl(reg, reg);
1899
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001900 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001901 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001902 return;
1903 case Primitive::kPrimFloat: {
1904 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1905 if (rhs.IsConstant()) {
1906 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1907 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1908 } else if (rhs.IsStackSlot()) {
1909 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1910 } else {
1911 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1912 }
1913 GenerateFPJumps(cond, &true_label, &false_label);
1914 break;
1915 }
1916 case Primitive::kPrimDouble: {
1917 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1918 if (rhs.IsConstant()) {
1919 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1920 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1921 } else if (rhs.IsDoubleStackSlot()) {
1922 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1923 } else {
1924 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1925 }
1926 GenerateFPJumps(cond, &true_label, &false_label);
1927 break;
1928 }
1929 }
1930
1931 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001932 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001933
Roland Levillain4fa13f62015-07-06 18:11:54 +01001934 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001935 __ Bind(&false_label);
1936 __ xorl(reg, reg);
1937 __ jmp(&done_label);
1938
Roland Levillain4fa13f62015-07-06 18:11:54 +01001939 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001940 __ Bind(&true_label);
1941 __ movl(reg, Immediate(1));
1942 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001955}
1956
1957void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001987}
1988
1989void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001991}
1992
Aart Bike9f37602015-10-09 11:15:55 -07001993void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001995}
1996
1997void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001999}
2000
2001void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002003}
2004
2005void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002007}
2008
2009void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
2021void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002022 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002023}
2024
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002025void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002026 LocationSummary* locations =
2027 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002028 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002029 case Primitive::kPrimBoolean:
2030 case Primitive::kPrimByte:
2031 case Primitive::kPrimShort:
2032 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002033 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002034 case Primitive::kPrimLong: {
2035 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002036 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2038 break;
2039 }
2040 case Primitive::kPrimFloat:
2041 case Primitive::kPrimDouble: {
2042 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002043 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002044 locations->SetOut(Location::RequiresRegister());
2045 break;
2046 }
2047 default:
2048 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2049 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002050}
2051
2052void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002053 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002055 Location left = locations->InAt(0);
2056 Location right = locations->InAt(1);
2057
Mark Mendell0c9497d2015-08-21 09:30:05 -04002058 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002059 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002060 Condition less_cond = kLess;
2061
Calin Juravleddb7df22014-11-25 20:56:51 +00002062 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002063 case Primitive::kPrimBoolean:
2064 case Primitive::kPrimByte:
2065 case Primitive::kPrimShort:
2066 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002067 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002068 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002069 break;
2070 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002071 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002072 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002073 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002074 }
2075 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002076 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2077 if (right.IsConstant()) {
2078 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2079 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2080 } else if (right.IsStackSlot()) {
2081 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2082 } else {
2083 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2084 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002085 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002086 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002087 break;
2088 }
2089 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002090 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2091 if (right.IsConstant()) {
2092 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2093 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2094 } else if (right.IsDoubleStackSlot()) {
2095 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2096 } else {
2097 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2098 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002099 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002100 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002101 break;
2102 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002103 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002104 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002105 }
Aart Bika19616e2016-02-01 18:57:58 -08002106
Calin Juravleddb7df22014-11-25 20:56:51 +00002107 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002108 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002109 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002110
Calin Juravle91debbc2014-11-26 19:01:09 +00002111 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002112 __ movl(out, Immediate(1));
2113 __ jmp(&done);
2114
2115 __ Bind(&less);
2116 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002117
2118 __ Bind(&done);
2119}
2120
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002121void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002122 LocationSummary* locations =
2123 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002124 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125}
2126
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002127void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002128 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002129}
2130
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002131void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2132 LocationSummary* locations =
2133 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2134 locations->SetOut(Location::ConstantLocation(constant));
2135}
2136
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002137void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002138 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002139}
2140
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002141void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002142 LocationSummary* locations =
2143 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002144 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002145}
2146
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002147void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002148 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002149}
2150
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002151void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2152 LocationSummary* locations =
2153 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2154 locations->SetOut(Location::ConstantLocation(constant));
2155}
2156
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002157void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002158 // Will be generated at use site.
2159}
2160
2161void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2162 LocationSummary* locations =
2163 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2164 locations->SetOut(Location::ConstantLocation(constant));
2165}
2166
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002167void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2168 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002169 // Will be generated at use site.
2170}
2171
Igor Murashkind01745e2017-04-05 16:40:31 -07002172void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2173 constructor_fence->SetLocations(nullptr);
2174}
2175
2176void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2177 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2178 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2179}
2180
Calin Juravle27df7582015-04-17 19:12:31 +01002181void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2182 memory_barrier->SetLocations(nullptr);
2183}
2184
2185void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002186 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002187}
2188
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002189void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2190 ret->SetLocations(nullptr);
2191}
2192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002193void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002195}
2196
2197void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002198 LocationSummary* locations =
2199 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002200 switch (ret->InputAt(0)->GetType()) {
2201 case Primitive::kPrimBoolean:
2202 case Primitive::kPrimByte:
2203 case Primitive::kPrimChar:
2204 case Primitive::kPrimShort:
2205 case Primitive::kPrimInt:
2206 case Primitive::kPrimNot:
2207 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002208 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002209 break;
2210
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 case Primitive::kPrimFloat:
2212 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002213 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002214 break;
2215
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002217 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002218 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002219}
2220
2221void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2222 if (kIsDebugBuild) {
2223 switch (ret->InputAt(0)->GetType()) {
2224 case Primitive::kPrimBoolean:
2225 case Primitive::kPrimByte:
2226 case Primitive::kPrimChar:
2227 case Primitive::kPrimShort:
2228 case Primitive::kPrimInt:
2229 case Primitive::kPrimNot:
2230 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002231 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002232 break;
2233
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002234 case Primitive::kPrimFloat:
2235 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002237 XMM0);
2238 break;
2239
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002240 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002241 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002242 }
2243 }
2244 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002245}
2246
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002247Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2248 switch (type) {
2249 case Primitive::kPrimBoolean:
2250 case Primitive::kPrimByte:
2251 case Primitive::kPrimChar:
2252 case Primitive::kPrimShort:
2253 case Primitive::kPrimInt:
2254 case Primitive::kPrimNot:
2255 case Primitive::kPrimLong:
2256 return Location::RegisterLocation(RAX);
2257
2258 case Primitive::kPrimVoid:
2259 return Location::NoLocation();
2260
2261 case Primitive::kPrimDouble:
2262 case Primitive::kPrimFloat:
2263 return Location::FpuRegisterLocation(XMM0);
2264 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002265
2266 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002267}
2268
2269Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2270 return Location::RegisterLocation(kMethodRegisterArgument);
2271}
2272
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002273Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002274 switch (type) {
2275 case Primitive::kPrimBoolean:
2276 case Primitive::kPrimByte:
2277 case Primitive::kPrimChar:
2278 case Primitive::kPrimShort:
2279 case Primitive::kPrimInt:
2280 case Primitive::kPrimNot: {
2281 uint32_t index = gp_index_++;
2282 stack_index_++;
2283 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002284 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002285 } else {
2286 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2287 }
2288 }
2289
2290 case Primitive::kPrimLong: {
2291 uint32_t index = gp_index_;
2292 stack_index_ += 2;
2293 if (index < calling_convention.GetNumberOfRegisters()) {
2294 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002295 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002296 } else {
2297 gp_index_ += 2;
2298 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2299 }
2300 }
2301
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002302 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002303 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 stack_index_++;
2305 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002306 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 } else {
2308 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2309 }
2310 }
2311
2312 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002313 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002314 stack_index_ += 2;
2315 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002316 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002317 } else {
2318 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2319 }
2320 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002321
2322 case Primitive::kPrimVoid:
2323 LOG(FATAL) << "Unexpected parameter type " << type;
2324 break;
2325 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002326 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002327}
2328
Calin Juravle175dc732015-08-25 15:42:32 +01002329void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2330 // The trampoline uses the same calling convention as dex calling conventions,
2331 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2332 // the method_idx.
2333 HandleInvoke(invoke);
2334}
2335
2336void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2337 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2338}
2339
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002340void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002341 // Explicit clinit checks triggered by static invokes must have been pruned by
2342 // art::PrepareForRegisterAllocation.
2343 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002344
Mark Mendellfb8d2792015-03-31 22:16:59 -04002345 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002346 if (intrinsic.TryDispatch(invoke)) {
2347 return;
2348 }
2349
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002350 HandleInvoke(invoke);
2351}
2352
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002353static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2354 if (invoke->GetLocations()->Intrinsified()) {
2355 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2356 intrinsic.Dispatch(invoke);
2357 return true;
2358 }
2359 return false;
2360}
2361
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002362void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002363 // Explicit clinit checks triggered by static invokes must have been pruned by
2364 // art::PrepareForRegisterAllocation.
2365 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002366
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002367 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2368 return;
2369 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002370
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002371 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002372 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002373 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002374}
2375
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002377 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002378 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002379}
2380
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002381void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002382 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002383 if (intrinsic.TryDispatch(invoke)) {
2384 return;
2385 }
2386
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002387 HandleInvoke(invoke);
2388}
2389
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002390void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002391 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2392 return;
2393 }
2394
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002395 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002396 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002397}
2398
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002399void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2400 HandleInvoke(invoke);
2401 // Add the hidden argument.
2402 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2403}
2404
2405void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2406 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002407 LocationSummary* locations = invoke->GetLocations();
2408 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2409 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002410 Location receiver = locations->InAt(0);
2411 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2412
Roland Levillain0d5a2812015-11-13 10:07:31 +00002413 // Set the hidden argument. This is safe to do this here, as RAX
2414 // won't be modified thereafter, before the `call` instruction.
2415 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002416 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002417
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002418 if (receiver.IsStackSlot()) {
2419 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002420 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002421 __ movl(temp, Address(temp, class_offset));
2422 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002423 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002424 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002426 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002427 // Instead of simply (possibly) unpoisoning `temp` here, we should
2428 // emit a read barrier for the previous class reference load.
2429 // However this is not required in practice, as this is an
2430 // intermediate/temporary reference and because the current
2431 // concurrent copying collector keeps the from-space memory
2432 // intact/accessible until the end of the marking phase (the
2433 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002434 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002435 // temp = temp->GetAddressOfIMT()
2436 __ movq(temp,
2437 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2438 // temp = temp->GetImtEntryAt(method_offset);
2439 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002440 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002442 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002443 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002444 __ call(Address(
2445 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002446
2447 DCHECK(!codegen_->IsLeafMethod());
2448 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2449}
2450
Orion Hodsonac141392017-01-13 11:53:47 +00002451void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2452 HandleInvoke(invoke);
2453}
2454
2455void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2456 codegen_->GenerateInvokePolymorphicCall(invoke);
2457}
2458
Roland Levillain88cb1752014-10-20 16:36:47 +01002459void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2460 LocationSummary* locations =
2461 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2462 switch (neg->GetResultType()) {
2463 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002464 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002465 locations->SetInAt(0, Location::RequiresRegister());
2466 locations->SetOut(Location::SameAsFirstInput());
2467 break;
2468
Roland Levillain88cb1752014-10-20 16:36:47 +01002469 case Primitive::kPrimFloat:
2470 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002471 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002472 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002473 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002474 break;
2475
2476 default:
2477 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2478 }
2479}
2480
2481void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2482 LocationSummary* locations = neg->GetLocations();
2483 Location out = locations->Out();
2484 Location in = locations->InAt(0);
2485 switch (neg->GetResultType()) {
2486 case Primitive::kPrimInt:
2487 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002488 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002490 break;
2491
2492 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002493 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002494 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002496 break;
2497
Roland Levillain5368c212014-11-27 15:03:41 +00002498 case Primitive::kPrimFloat: {
2499 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002500 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002501 // Implement float negation with an exclusive or with value
2502 // 0x80000000 (mask for bit 31, representing the sign of a
2503 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002504 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002506 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002507 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002508
Roland Levillain5368c212014-11-27 15:03:41 +00002509 case Primitive::kPrimDouble: {
2510 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002511 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002512 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002513 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002514 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002515 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002517 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002518 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002519
2520 default:
2521 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2522 }
2523}
2524
Roland Levillaindff1f282014-11-05 14:15:05 +00002525void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2526 LocationSummary* locations =
2527 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2528 Primitive::Type result_type = conversion->GetResultType();
2529 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002530 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002531
David Brazdilb2bd1c52015-03-25 11:17:37 +00002532 // The Java language does not allow treating boolean as an integral type but
2533 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002536 case Primitive::kPrimByte:
2537 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002538 case Primitive::kPrimLong:
2539 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002542 case Primitive::kPrimShort:
2543 case Primitive::kPrimInt:
2544 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002545 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002546 locations->SetInAt(0, Location::Any());
2547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554 break;
2555
Roland Levillain01a8d712014-11-14 16:27:39 +00002556 case Primitive::kPrimShort:
2557 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002558 case Primitive::kPrimLong:
2559 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002560 case Primitive::kPrimBoolean:
2561 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002562 case Primitive::kPrimByte:
2563 case Primitive::kPrimInt:
2564 case Primitive::kPrimChar:
2565 // Processing a Dex `int-to-short' instruction.
2566 locations->SetInAt(0, Location::Any());
2567 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2568 break;
2569
2570 default:
2571 LOG(FATAL) << "Unexpected type conversion from " << input_type
2572 << " to " << result_type;
2573 }
2574 break;
2575
Roland Levillain946e1432014-11-11 17:35:19 +00002576 case Primitive::kPrimInt:
2577 switch (input_type) {
2578 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002579 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002580 locations->SetInAt(0, Location::Any());
2581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2582 break;
2583
2584 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002585 // Processing a Dex `float-to-int' instruction.
2586 locations->SetInAt(0, Location::RequiresFpuRegister());
2587 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002588 break;
2589
Roland Levillain946e1432014-11-11 17:35:19 +00002590 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002591 // Processing a Dex `double-to-int' instruction.
2592 locations->SetInAt(0, Location::RequiresFpuRegister());
2593 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002594 break;
2595
2596 default:
2597 LOG(FATAL) << "Unexpected type conversion from " << input_type
2598 << " to " << result_type;
2599 }
2600 break;
2601
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 case Primitive::kPrimLong:
2603 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002604 case Primitive::kPrimBoolean:
2605 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002606 case Primitive::kPrimByte:
2607 case Primitive::kPrimShort:
2608 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002609 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002610 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002611 // TODO: We would benefit from a (to-be-implemented)
2612 // Location::RegisterOrStackSlot requirement for this input.
2613 locations->SetInAt(0, Location::RequiresRegister());
2614 locations->SetOut(Location::RequiresRegister());
2615 break;
2616
2617 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002618 // Processing a Dex `float-to-long' instruction.
2619 locations->SetInAt(0, Location::RequiresFpuRegister());
2620 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002621 break;
2622
Roland Levillaindff1f282014-11-05 14:15:05 +00002623 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002624 // Processing a Dex `double-to-long' instruction.
2625 locations->SetInAt(0, Location::RequiresFpuRegister());
2626 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002627 break;
2628
2629 default:
2630 LOG(FATAL) << "Unexpected type conversion from " << input_type
2631 << " to " << result_type;
2632 }
2633 break;
2634
Roland Levillain981e4542014-11-14 11:47:14 +00002635 case Primitive::kPrimChar:
2636 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002637 case Primitive::kPrimLong:
2638 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002639 case Primitive::kPrimBoolean:
2640 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002641 case Primitive::kPrimByte:
2642 case Primitive::kPrimShort:
2643 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002644 // Processing a Dex `int-to-char' instruction.
2645 locations->SetInAt(0, Location::Any());
2646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2647 break;
2648
2649 default:
2650 LOG(FATAL) << "Unexpected type conversion from " << input_type
2651 << " to " << result_type;
2652 }
2653 break;
2654
Roland Levillaindff1f282014-11-05 14:15:05 +00002655 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002656 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002657 case Primitive::kPrimBoolean:
2658 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002659 case Primitive::kPrimByte:
2660 case Primitive::kPrimShort:
2661 case Primitive::kPrimInt:
2662 case Primitive::kPrimChar:
2663 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002664 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002665 locations->SetOut(Location::RequiresFpuRegister());
2666 break;
2667
2668 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002669 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002670 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002671 locations->SetOut(Location::RequiresFpuRegister());
2672 break;
2673
Roland Levillaincff13742014-11-17 14:32:17 +00002674 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002675 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002676 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002677 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002678 break;
2679
2680 default:
2681 LOG(FATAL) << "Unexpected type conversion from " << input_type
2682 << " to " << result_type;
2683 };
2684 break;
2685
Roland Levillaindff1f282014-11-05 14:15:05 +00002686 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002687 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002688 case Primitive::kPrimBoolean:
2689 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002690 case Primitive::kPrimByte:
2691 case Primitive::kPrimShort:
2692 case Primitive::kPrimInt:
2693 case Primitive::kPrimChar:
2694 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002695 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002696 locations->SetOut(Location::RequiresFpuRegister());
2697 break;
2698
2699 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002700 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002701 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002702 locations->SetOut(Location::RequiresFpuRegister());
2703 break;
2704
Roland Levillaincff13742014-11-17 14:32:17 +00002705 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002706 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002707 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002708 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002715 break;
2716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 }
2721}
2722
2723void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2724 LocationSummary* locations = conversion->GetLocations();
2725 Location out = locations->Out();
2726 Location in = locations->InAt(0);
2727 Primitive::Type result_type = conversion->GetResultType();
2728 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002729 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002730 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002731 case Primitive::kPrimByte:
2732 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002733 case Primitive::kPrimLong:
2734 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002735 case Primitive::kPrimBoolean:
2736 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002737 case Primitive::kPrimShort:
2738 case Primitive::kPrimInt:
2739 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002740 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002741 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002743 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002744 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002745 Address(CpuRegister(RSP), in.GetStackIndex()));
2746 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002748 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002749 }
2750 break;
2751
2752 default:
2753 LOG(FATAL) << "Unexpected type conversion from " << input_type
2754 << " to " << result_type;
2755 }
2756 break;
2757
Roland Levillain01a8d712014-11-14 16:27:39 +00002758 case Primitive::kPrimShort:
2759 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002760 case Primitive::kPrimLong:
2761 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002762 case Primitive::kPrimBoolean:
2763 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002764 case Primitive::kPrimByte:
2765 case Primitive::kPrimInt:
2766 case Primitive::kPrimChar:
2767 // Processing a Dex `int-to-short' instruction.
2768 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002770 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002772 Address(CpuRegister(RSP), in.GetStackIndex()));
2773 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002775 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002776 }
2777 break;
2778
2779 default:
2780 LOG(FATAL) << "Unexpected type conversion from " << input_type
2781 << " to " << result_type;
2782 }
2783 break;
2784
Roland Levillain946e1432014-11-11 17:35:19 +00002785 case Primitive::kPrimInt:
2786 switch (input_type) {
2787 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002788 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002789 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002791 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002792 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002793 Address(CpuRegister(RSP), in.GetStackIndex()));
2794 } else {
2795 DCHECK(in.IsConstant());
2796 DCHECK(in.GetConstant()->IsLongConstant());
2797 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002798 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002799 }
2800 break;
2801
Roland Levillain3f8f9362014-12-02 17:45:01 +00002802 case Primitive::kPrimFloat: {
2803 // Processing a Dex `float-to-int' instruction.
2804 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2805 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002806 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002807
2808 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002809 // if input >= (float)INT_MAX goto done
2810 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002811 __ j(kAboveEqual, &done);
2812 // if input == NaN goto nan
2813 __ j(kUnordered, &nan);
2814 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002815 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002816 __ jmp(&done);
2817 __ Bind(&nan);
2818 // output = 0
2819 __ xorl(output, output);
2820 __ Bind(&done);
2821 break;
2822 }
2823
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002824 case Primitive::kPrimDouble: {
2825 // Processing a Dex `double-to-int' instruction.
2826 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2827 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002828 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002829
2830 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002831 // if input >= (double)INT_MAX goto done
2832 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002833 __ j(kAboveEqual, &done);
2834 // if input == NaN goto nan
2835 __ j(kUnordered, &nan);
2836 // output = double-to-int-truncate(input)
2837 __ cvttsd2si(output, input);
2838 __ jmp(&done);
2839 __ Bind(&nan);
2840 // output = 0
2841 __ xorl(output, output);
2842 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002843 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002844 }
Roland Levillain946e1432014-11-11 17:35:19 +00002845
2846 default:
2847 LOG(FATAL) << "Unexpected type conversion from " << input_type
2848 << " to " << result_type;
2849 }
2850 break;
2851
Roland Levillaindff1f282014-11-05 14:15:05 +00002852 case Primitive::kPrimLong:
2853 switch (input_type) {
2854 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002855 case Primitive::kPrimBoolean:
2856 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002857 case Primitive::kPrimByte:
2858 case Primitive::kPrimShort:
2859 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002860 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002861 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002862 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002863 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002864 break;
2865
Roland Levillain624279f2014-12-04 11:54:28 +00002866 case Primitive::kPrimFloat: {
2867 // Processing a Dex `float-to-long' instruction.
2868 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2869 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002870 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002871
Mark Mendell92e83bf2015-05-07 11:25:03 -04002872 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002873 // if input >= (float)LONG_MAX goto done
2874 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002875 __ j(kAboveEqual, &done);
2876 // if input == NaN goto nan
2877 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002878 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002879 __ cvttss2si(output, input, true);
2880 __ jmp(&done);
2881 __ Bind(&nan);
2882 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002883 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002884 __ Bind(&done);
2885 break;
2886 }
2887
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002888 case Primitive::kPrimDouble: {
2889 // Processing a Dex `double-to-long' instruction.
2890 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2891 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002892 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002893
Mark Mendell92e83bf2015-05-07 11:25:03 -04002894 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002895 // if input >= (double)LONG_MAX goto done
2896 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002897 __ j(kAboveEqual, &done);
2898 // if input == NaN goto nan
2899 __ j(kUnordered, &nan);
2900 // output = double-to-long-truncate(input)
2901 __ cvttsd2si(output, input, true);
2902 __ jmp(&done);
2903 __ Bind(&nan);
2904 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002905 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002906 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002907 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002908 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002909
2910 default:
2911 LOG(FATAL) << "Unexpected type conversion from " << input_type
2912 << " to " << result_type;
2913 }
2914 break;
2915
Roland Levillain981e4542014-11-14 11:47:14 +00002916 case Primitive::kPrimChar:
2917 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002918 case Primitive::kPrimLong:
2919 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002920 case Primitive::kPrimBoolean:
2921 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002922 case Primitive::kPrimByte:
2923 case Primitive::kPrimShort:
2924 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002925 // Processing a Dex `int-to-char' instruction.
2926 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002927 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002928 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002929 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002930 Address(CpuRegister(RSP), in.GetStackIndex()));
2931 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002932 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002933 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002934 }
2935 break;
2936
2937 default:
2938 LOG(FATAL) << "Unexpected type conversion from " << input_type
2939 << " to " << result_type;
2940 }
2941 break;
2942
Roland Levillaindff1f282014-11-05 14:15:05 +00002943 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002944 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002945 case Primitive::kPrimBoolean:
2946 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002947 case Primitive::kPrimByte:
2948 case Primitive::kPrimShort:
2949 case Primitive::kPrimInt:
2950 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002951 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002952 if (in.IsRegister()) {
2953 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2954 } else if (in.IsConstant()) {
2955 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2956 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002957 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002958 } else {
2959 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2960 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2961 }
Roland Levillaincff13742014-11-17 14:32:17 +00002962 break;
2963
2964 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002965 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002966 if (in.IsRegister()) {
2967 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2968 } else if (in.IsConstant()) {
2969 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2970 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002971 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002972 } else {
2973 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2974 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2975 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002976 break;
2977
Roland Levillaincff13742014-11-17 14:32:17 +00002978 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002979 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002980 if (in.IsFpuRegister()) {
2981 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2982 } else if (in.IsConstant()) {
2983 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2984 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002985 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002986 } else {
2987 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2988 Address(CpuRegister(RSP), in.GetStackIndex()));
2989 }
Roland Levillaincff13742014-11-17 14:32:17 +00002990 break;
2991
2992 default:
2993 LOG(FATAL) << "Unexpected type conversion from " << input_type
2994 << " to " << result_type;
2995 };
2996 break;
2997
Roland Levillaindff1f282014-11-05 14:15:05 +00002998 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002999 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003000 case Primitive::kPrimBoolean:
3001 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003002 case Primitive::kPrimByte:
3003 case Primitive::kPrimShort:
3004 case Primitive::kPrimInt:
3005 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003006 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003007 if (in.IsRegister()) {
3008 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3009 } else if (in.IsConstant()) {
3010 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3011 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003012 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003013 } else {
3014 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3015 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3016 }
Roland Levillaincff13742014-11-17 14:32:17 +00003017 break;
3018
3019 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003020 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003021 if (in.IsRegister()) {
3022 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3023 } else if (in.IsConstant()) {
3024 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3025 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003026 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003027 } else {
3028 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3029 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3030 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003031 break;
3032
Roland Levillaincff13742014-11-17 14:32:17 +00003033 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003034 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003035 if (in.IsFpuRegister()) {
3036 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3037 } else if (in.IsConstant()) {
3038 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3039 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003040 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003041 } else {
3042 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3043 Address(CpuRegister(RSP), in.GetStackIndex()));
3044 }
Roland Levillaincff13742014-11-17 14:32:17 +00003045 break;
3046
3047 default:
3048 LOG(FATAL) << "Unexpected type conversion from " << input_type
3049 << " to " << result_type;
3050 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003051 break;
3052
3053 default:
3054 LOG(FATAL) << "Unexpected type conversion from " << input_type
3055 << " to " << result_type;
3056 }
3057}
3058
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003060 LocationSummary* locations =
3061 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003062 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003063 case Primitive::kPrimInt: {
3064 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003065 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003067 break;
3068 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003069
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003071 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003072 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003073 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003075 break;
3076 }
3077
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003078 case Primitive::kPrimDouble:
3079 case Primitive::kPrimFloat: {
3080 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003081 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003082 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003083 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003084 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003085
3086 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003087 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089}
3090
3091void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3092 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003093 Location first = locations->InAt(0);
3094 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003095 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003096
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003097 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003098 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003099 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003100 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3101 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003102 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3103 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003104 } else {
3105 __ leal(out.AsRegister<CpuRegister>(), Address(
3106 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3107 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003108 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003109 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3110 __ addl(out.AsRegister<CpuRegister>(),
3111 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3112 } else {
3113 __ leal(out.AsRegister<CpuRegister>(), Address(
3114 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3115 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003116 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003117 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003118 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003119 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003120 break;
3121 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003122
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003124 if (second.IsRegister()) {
3125 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3126 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003127 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3128 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003129 } else {
3130 __ leaq(out.AsRegister<CpuRegister>(), Address(
3131 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3132 }
3133 } else {
3134 DCHECK(second.IsConstant());
3135 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3136 int32_t int32_value = Low32Bits(value);
3137 DCHECK_EQ(int32_value, value);
3138 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3139 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3140 } else {
3141 __ leaq(out.AsRegister<CpuRegister>(), Address(
3142 first.AsRegister<CpuRegister>(), int32_value));
3143 }
3144 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003145 break;
3146 }
3147
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003148 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003149 if (second.IsFpuRegister()) {
3150 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3151 } else if (second.IsConstant()) {
3152 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003153 codegen_->LiteralFloatAddress(
3154 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003155 } else {
3156 DCHECK(second.IsStackSlot());
3157 __ addss(first.AsFpuRegister<XmmRegister>(),
3158 Address(CpuRegister(RSP), second.GetStackIndex()));
3159 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003160 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003161 }
3162
3163 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003164 if (second.IsFpuRegister()) {
3165 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3166 } else if (second.IsConstant()) {
3167 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003168 codegen_->LiteralDoubleAddress(
3169 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003170 } else {
3171 DCHECK(second.IsDoubleStackSlot());
3172 __ addsd(first.AsFpuRegister<XmmRegister>(),
3173 Address(CpuRegister(RSP), second.GetStackIndex()));
3174 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003175 break;
3176 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003177
3178 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003179 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003180 }
3181}
3182
3183void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003184 LocationSummary* locations =
3185 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003186 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003187 case Primitive::kPrimInt: {
3188 locations->SetInAt(0, Location::RequiresRegister());
3189 locations->SetInAt(1, Location::Any());
3190 locations->SetOut(Location::SameAsFirstInput());
3191 break;
3192 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003193 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003194 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003195 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003196 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003197 break;
3198 }
Calin Juravle11351682014-10-23 15:38:15 +01003199 case Primitive::kPrimFloat:
3200 case Primitive::kPrimDouble: {
3201 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003202 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003203 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003204 break;
Calin Juravle11351682014-10-23 15:38:15 +01003205 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003206 default:
Calin Juravle11351682014-10-23 15:38:15 +01003207 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003208 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003209}
3210
3211void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3212 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003213 Location first = locations->InAt(0);
3214 Location second = locations->InAt(1);
3215 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003216 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003217 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003218 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003220 } else if (second.IsConstant()) {
3221 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003223 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003225 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003226 break;
3227 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003228 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003229 if (second.IsConstant()) {
3230 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3231 DCHECK(IsInt<32>(value));
3232 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3233 } else {
3234 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3235 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003236 break;
3237 }
3238
Calin Juravle11351682014-10-23 15:38:15 +01003239 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003240 if (second.IsFpuRegister()) {
3241 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3242 } else if (second.IsConstant()) {
3243 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003244 codegen_->LiteralFloatAddress(
3245 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003246 } else {
3247 DCHECK(second.IsStackSlot());
3248 __ subss(first.AsFpuRegister<XmmRegister>(),
3249 Address(CpuRegister(RSP), second.GetStackIndex()));
3250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003251 break;
Calin Juravle11351682014-10-23 15:38:15 +01003252 }
3253
3254 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003255 if (second.IsFpuRegister()) {
3256 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3257 } else if (second.IsConstant()) {
3258 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003259 codegen_->LiteralDoubleAddress(
3260 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003261 } else {
3262 DCHECK(second.IsDoubleStackSlot());
3263 __ subsd(first.AsFpuRegister<XmmRegister>(),
3264 Address(CpuRegister(RSP), second.GetStackIndex()));
3265 }
Calin Juravle11351682014-10-23 15:38:15 +01003266 break;
3267 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003268
3269 default:
Calin Juravle11351682014-10-23 15:38:15 +01003270 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003271 }
3272}
3273
Calin Juravle34bacdf2014-10-07 20:23:36 +01003274void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3275 LocationSummary* locations =
3276 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3277 switch (mul->GetResultType()) {
3278 case Primitive::kPrimInt: {
3279 locations->SetInAt(0, Location::RequiresRegister());
3280 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003281 if (mul->InputAt(1)->IsIntConstant()) {
3282 // Can use 3 operand multiply.
3283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3284 } else {
3285 locations->SetOut(Location::SameAsFirstInput());
3286 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003287 break;
3288 }
3289 case Primitive::kPrimLong: {
3290 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003291 locations->SetInAt(1, Location::Any());
3292 if (mul->InputAt(1)->IsLongConstant() &&
3293 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003294 // Can use 3 operand multiply.
3295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3296 } else {
3297 locations->SetOut(Location::SameAsFirstInput());
3298 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003299 break;
3300 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003301 case Primitive::kPrimFloat:
3302 case Primitive::kPrimDouble: {
3303 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003304 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003305 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003306 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003307 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003308
3309 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003310 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311 }
3312}
3313
3314void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3315 LocationSummary* locations = mul->GetLocations();
3316 Location first = locations->InAt(0);
3317 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003318 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003320 case Primitive::kPrimInt:
3321 // The constant may have ended up in a register, so test explicitly to avoid
3322 // problems where the output may not be the same as the first operand.
3323 if (mul->InputAt(1)->IsIntConstant()) {
3324 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3325 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3326 } else if (second.IsRegister()) {
3327 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003330 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003331 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003332 __ imull(first.AsRegister<CpuRegister>(),
3333 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334 }
3335 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003336 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003337 // The constant may have ended up in a register, so test explicitly to avoid
3338 // problems where the output may not be the same as the first operand.
3339 if (mul->InputAt(1)->IsLongConstant()) {
3340 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3341 if (IsInt<32>(value)) {
3342 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3343 Immediate(static_cast<int32_t>(value)));
3344 } else {
3345 // Have to use the constant area.
3346 DCHECK(first.Equals(out));
3347 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3348 }
3349 } else if (second.IsRegister()) {
3350 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003351 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003352 } else {
3353 DCHECK(second.IsDoubleStackSlot());
3354 DCHECK(first.Equals(out));
3355 __ imulq(first.AsRegister<CpuRegister>(),
3356 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003357 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003358 break;
3359 }
3360
Calin Juravleb5bfa962014-10-21 18:02:24 +01003361 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003362 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003363 if (second.IsFpuRegister()) {
3364 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3365 } else if (second.IsConstant()) {
3366 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003367 codegen_->LiteralFloatAddress(
3368 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003369 } else {
3370 DCHECK(second.IsStackSlot());
3371 __ mulss(first.AsFpuRegister<XmmRegister>(),
3372 Address(CpuRegister(RSP), second.GetStackIndex()));
3373 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003375 }
3376
3377 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003378 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003379 if (second.IsFpuRegister()) {
3380 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3381 } else if (second.IsConstant()) {
3382 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003383 codegen_->LiteralDoubleAddress(
3384 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003385 } else {
3386 DCHECK(second.IsDoubleStackSlot());
3387 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3388 Address(CpuRegister(RSP), second.GetStackIndex()));
3389 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003390 break;
3391 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003392
3393 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003394 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003395 }
3396}
3397
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003398void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3399 uint32_t stack_adjustment, bool is_float) {
3400 if (source.IsStackSlot()) {
3401 DCHECK(is_float);
3402 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3403 } else if (source.IsDoubleStackSlot()) {
3404 DCHECK(!is_float);
3405 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3406 } else {
3407 // Write the value to the temporary location on the stack and load to FP stack.
3408 if (is_float) {
3409 Location stack_temp = Location::StackSlot(temp_offset);
3410 codegen_->Move(stack_temp, source);
3411 __ flds(Address(CpuRegister(RSP), temp_offset));
3412 } else {
3413 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3414 codegen_->Move(stack_temp, source);
3415 __ fldl(Address(CpuRegister(RSP), temp_offset));
3416 }
3417 }
3418}
3419
3420void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3421 Primitive::Type type = rem->GetResultType();
3422 bool is_float = type == Primitive::kPrimFloat;
3423 size_t elem_size = Primitive::ComponentSize(type);
3424 LocationSummary* locations = rem->GetLocations();
3425 Location first = locations->InAt(0);
3426 Location second = locations->InAt(1);
3427 Location out = locations->Out();
3428
3429 // Create stack space for 2 elements.
3430 // TODO: enhance register allocator to ask for stack temporaries.
3431 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3432
3433 // Load the values to the FP stack in reverse order, using temporaries if needed.
3434 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3435 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3436
3437 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003438 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003439 __ Bind(&retry);
3440 __ fprem();
3441
3442 // Move FP status to AX.
3443 __ fstsw();
3444
3445 // And see if the argument reduction is complete. This is signaled by the
3446 // C2 FPU flag bit set to 0.
3447 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3448 __ j(kNotEqual, &retry);
3449
3450 // We have settled on the final value. Retrieve it into an XMM register.
3451 // Store FP top of stack to real stack.
3452 if (is_float) {
3453 __ fsts(Address(CpuRegister(RSP), 0));
3454 } else {
3455 __ fstl(Address(CpuRegister(RSP), 0));
3456 }
3457
3458 // Pop the 2 items from the FP stack.
3459 __ fucompp();
3460
3461 // Load the value from the stack into an XMM register.
3462 DCHECK(out.IsFpuRegister()) << out;
3463 if (is_float) {
3464 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3465 } else {
3466 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3467 }
3468
3469 // And remove the temporary stack space we allocated.
3470 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3471}
3472
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003473void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3474 DCHECK(instruction->IsDiv() || instruction->IsRem());
3475
3476 LocationSummary* locations = instruction->GetLocations();
3477 Location second = locations->InAt(1);
3478 DCHECK(second.IsConstant());
3479
3480 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3481 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003482 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483
3484 DCHECK(imm == 1 || imm == -1);
3485
3486 switch (instruction->GetResultType()) {
3487 case Primitive::kPrimInt: {
3488 if (instruction->IsRem()) {
3489 __ xorl(output_register, output_register);
3490 } else {
3491 __ movl(output_register, input_register);
3492 if (imm == -1) {
3493 __ negl(output_register);
3494 }
3495 }
3496 break;
3497 }
3498
3499 case Primitive::kPrimLong: {
3500 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003501 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 } else {
3503 __ movq(output_register, input_register);
3504 if (imm == -1) {
3505 __ negq(output_register);
3506 }
3507 }
3508 break;
3509 }
3510
3511 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003512 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513 }
3514}
3515
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003516void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 LocationSummary* locations = instruction->GetLocations();
3518 Location second = locations->InAt(1);
3519
3520 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3521 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3522
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003523 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003524 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3525 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003526
3527 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3528
3529 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003530 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531 __ testl(numerator, numerator);
3532 __ cmov(kGreaterEqual, tmp, numerator);
3533 int shift = CTZ(imm);
3534 __ sarl(tmp, Immediate(shift));
3535
3536 if (imm < 0) {
3537 __ negl(tmp);
3538 }
3539
3540 __ movl(output_register, tmp);
3541 } else {
3542 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3543 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3544
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003545 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 __ addq(rdx, numerator);
3547 __ testq(numerator, numerator);
3548 __ cmov(kGreaterEqual, rdx, numerator);
3549 int shift = CTZ(imm);
3550 __ sarq(rdx, Immediate(shift));
3551
3552 if (imm < 0) {
3553 __ negq(rdx);
3554 }
3555
3556 __ movq(output_register, rdx);
3557 }
3558}
3559
3560void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3561 DCHECK(instruction->IsDiv() || instruction->IsRem());
3562
3563 LocationSummary* locations = instruction->GetLocations();
3564 Location second = locations->InAt(1);
3565
3566 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3567 : locations->GetTemp(0).AsRegister<CpuRegister>();
3568 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3569 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3570 : locations->Out().AsRegister<CpuRegister>();
3571 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3572
3573 DCHECK_EQ(RAX, eax.AsRegister());
3574 DCHECK_EQ(RDX, edx.AsRegister());
3575 if (instruction->IsDiv()) {
3576 DCHECK_EQ(RAX, out.AsRegister());
3577 } else {
3578 DCHECK_EQ(RDX, out.AsRegister());
3579 }
3580
3581 int64_t magic;
3582 int shift;
3583
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003584 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 if (instruction->GetResultType() == Primitive::kPrimInt) {
3586 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3587
3588 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3589
3590 __ movl(numerator, eax);
3591
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 __ movl(eax, Immediate(magic));
3593 __ imull(numerator);
3594
3595 if (imm > 0 && magic < 0) {
3596 __ addl(edx, numerator);
3597 } else if (imm < 0 && magic > 0) {
3598 __ subl(edx, numerator);
3599 }
3600
3601 if (shift != 0) {
3602 __ sarl(edx, Immediate(shift));
3603 }
3604
3605 __ movl(eax, edx);
3606 __ shrl(edx, Immediate(31));
3607 __ addl(edx, eax);
3608
3609 if (instruction->IsRem()) {
3610 __ movl(eax, numerator);
3611 __ imull(edx, Immediate(imm));
3612 __ subl(eax, edx);
3613 __ movl(edx, eax);
3614 } else {
3615 __ movl(eax, edx);
3616 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003617 } else {
3618 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3619
3620 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3621
3622 CpuRegister rax = eax;
3623 CpuRegister rdx = edx;
3624
3625 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3626
3627 // Save the numerator.
3628 __ movq(numerator, rax);
3629
3630 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003631 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632
3633 // RDX:RAX = magic * numerator
3634 __ imulq(numerator);
3635
3636 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003637 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003638 __ addq(rdx, numerator);
3639 } else if (imm < 0 && magic > 0) {
3640 // RDX -= numerator
3641 __ subq(rdx, numerator);
3642 }
3643
3644 // Shift if needed.
3645 if (shift != 0) {
3646 __ sarq(rdx, Immediate(shift));
3647 }
3648
3649 // RDX += 1 if RDX < 0
3650 __ movq(rax, rdx);
3651 __ shrq(rdx, Immediate(63));
3652 __ addq(rdx, rax);
3653
3654 if (instruction->IsRem()) {
3655 __ movq(rax, numerator);
3656
3657 if (IsInt<32>(imm)) {
3658 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3659 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003660 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003661 }
3662
3663 __ subq(rax, rdx);
3664 __ movq(rdx, rax);
3665 } else {
3666 __ movq(rax, rdx);
3667 }
3668 }
3669}
3670
Calin Juravlebacfec32014-11-14 15:54:36 +00003671void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3672 DCHECK(instruction->IsDiv() || instruction->IsRem());
3673 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003674 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravlebacfec32014-11-14 15:54:36 +00003675
3676 bool is_div = instruction->IsDiv();
3677 LocationSummary* locations = instruction->GetLocations();
3678
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003679 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3680 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003681
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003683 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003684
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003685 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003686 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003687
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003688 if (imm == 0) {
3689 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3690 } else if (imm == 1 || imm == -1) {
3691 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003692 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003693 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003694 } else {
3695 DCHECK(imm <= -2 || imm >= 2);
3696 GenerateDivRemWithAnyConstant(instruction);
3697 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003698 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003699 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003700 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003701 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003702 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003703
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003704 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3705 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3706 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3707 // so it's safe to just use negl instead of more complex comparisons.
3708 if (type == Primitive::kPrimInt) {
3709 __ cmpl(second_reg, Immediate(-1));
3710 __ j(kEqual, slow_path->GetEntryLabel());
3711 // edx:eax <- sign-extended of eax
3712 __ cdq();
3713 // eax = quotient, edx = remainder
3714 __ idivl(second_reg);
3715 } else {
3716 __ cmpq(second_reg, Immediate(-1));
3717 __ j(kEqual, slow_path->GetEntryLabel());
3718 // rdx:rax <- sign-extended of rax
3719 __ cqo();
3720 // rax = quotient, rdx = remainder
3721 __ idivq(second_reg);
3722 }
3723 __ Bind(slow_path->GetExitLabel());
3724 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003725}
3726
Calin Juravle7c4954d2014-10-28 16:57:40 +00003727void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3728 LocationSummary* locations =
3729 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3730 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003731 case Primitive::kPrimInt:
3732 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003733 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003734 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003735 locations->SetOut(Location::SameAsFirstInput());
3736 // Intel uses edx:eax as the dividend.
3737 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003738 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3739 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3740 // output and request another temp.
3741 if (div->InputAt(1)->IsConstant()) {
3742 locations->AddTemp(Location::RequiresRegister());
3743 }
Calin Juravled0d48522014-11-04 16:40:20 +00003744 break;
3745 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003746
Calin Juravle7c4954d2014-10-28 16:57:40 +00003747 case Primitive::kPrimFloat:
3748 case Primitive::kPrimDouble: {
3749 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003750 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003751 locations->SetOut(Location::SameAsFirstInput());
3752 break;
3753 }
3754
3755 default:
3756 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3757 }
3758}
3759
3760void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3761 LocationSummary* locations = div->GetLocations();
3762 Location first = locations->InAt(0);
3763 Location second = locations->InAt(1);
3764 DCHECK(first.Equals(locations->Out()));
3765
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003766 Primitive::Type type = div->GetResultType();
3767 switch (type) {
3768 case Primitive::kPrimInt:
3769 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003770 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003771 break;
3772 }
3773
Calin Juravle7c4954d2014-10-28 16:57:40 +00003774 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003775 if (second.IsFpuRegister()) {
3776 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3777 } else if (second.IsConstant()) {
3778 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003779 codegen_->LiteralFloatAddress(
3780 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003781 } else {
3782 DCHECK(second.IsStackSlot());
3783 __ divss(first.AsFpuRegister<XmmRegister>(),
3784 Address(CpuRegister(RSP), second.GetStackIndex()));
3785 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003786 break;
3787 }
3788
3789 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003790 if (second.IsFpuRegister()) {
3791 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3792 } else if (second.IsConstant()) {
3793 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003794 codegen_->LiteralDoubleAddress(
3795 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003796 } else {
3797 DCHECK(second.IsDoubleStackSlot());
3798 __ divsd(first.AsFpuRegister<XmmRegister>(),
3799 Address(CpuRegister(RSP), second.GetStackIndex()));
3800 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003801 break;
3802 }
3803
3804 default:
3805 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3806 }
3807}
3808
Calin Juravlebacfec32014-11-14 15:54:36 +00003809void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003810 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003811 LocationSummary* locations =
3812 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003813
3814 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003815 case Primitive::kPrimInt:
3816 case Primitive::kPrimLong: {
3817 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003818 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003819 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3820 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003821 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3822 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3823 // output and request another temp.
3824 if (rem->InputAt(1)->IsConstant()) {
3825 locations->AddTemp(Location::RequiresRegister());
3826 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003827 break;
3828 }
3829
3830 case Primitive::kPrimFloat:
3831 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003832 locations->SetInAt(0, Location::Any());
3833 locations->SetInAt(1, Location::Any());
3834 locations->SetOut(Location::RequiresFpuRegister());
3835 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003836 break;
3837 }
3838
3839 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003840 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003841 }
3842}
3843
3844void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3845 Primitive::Type type = rem->GetResultType();
3846 switch (type) {
3847 case Primitive::kPrimInt:
3848 case Primitive::kPrimLong: {
3849 GenerateDivRemIntegral(rem);
3850 break;
3851 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003852 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003853 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003854 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003855 break;
3856 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003857 default:
3858 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3859 }
3860}
3861
Calin Juravled0d48522014-11-04 16:40:20 +00003862void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003863 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003864 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003865}
3866
3867void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003868 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003869 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3870 codegen_->AddSlowPath(slow_path);
3871
3872 LocationSummary* locations = instruction->GetLocations();
3873 Location value = locations->InAt(0);
3874
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003875 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003876 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003877 case Primitive::kPrimByte:
3878 case Primitive::kPrimChar:
3879 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003880 case Primitive::kPrimInt: {
3881 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003882 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003883 __ j(kEqual, slow_path->GetEntryLabel());
3884 } else if (value.IsStackSlot()) {
3885 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3886 __ j(kEqual, slow_path->GetEntryLabel());
3887 } else {
3888 DCHECK(value.IsConstant()) << value;
3889 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003890 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003891 }
3892 }
3893 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003894 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003895 case Primitive::kPrimLong: {
3896 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003897 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003898 __ j(kEqual, slow_path->GetEntryLabel());
3899 } else if (value.IsDoubleStackSlot()) {
3900 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3901 __ j(kEqual, slow_path->GetEntryLabel());
3902 } else {
3903 DCHECK(value.IsConstant()) << value;
3904 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003905 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003906 }
3907 }
3908 break;
3909 }
3910 default:
3911 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003912 }
Calin Juravled0d48522014-11-04 16:40:20 +00003913}
3914
Calin Juravle9aec02f2014-11-18 23:06:35 +00003915void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3916 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3917
3918 LocationSummary* locations =
3919 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3920
3921 switch (op->GetResultType()) {
3922 case Primitive::kPrimInt:
3923 case Primitive::kPrimLong: {
3924 locations->SetInAt(0, Location::RequiresRegister());
3925 // The shift count needs to be in CL.
3926 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3927 locations->SetOut(Location::SameAsFirstInput());
3928 break;
3929 }
3930 default:
3931 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3932 }
3933}
3934
3935void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3936 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3937
3938 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003939 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940 Location second = locations->InAt(1);
3941
3942 switch (op->GetResultType()) {
3943 case Primitive::kPrimInt: {
3944 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003945 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003946 if (op->IsShl()) {
3947 __ shll(first_reg, second_reg);
3948 } else if (op->IsShr()) {
3949 __ sarl(first_reg, second_reg);
3950 } else {
3951 __ shrl(first_reg, second_reg);
3952 }
3953 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003954 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955 if (op->IsShl()) {
3956 __ shll(first_reg, imm);
3957 } else if (op->IsShr()) {
3958 __ sarl(first_reg, imm);
3959 } else {
3960 __ shrl(first_reg, imm);
3961 }
3962 }
3963 break;
3964 }
3965 case Primitive::kPrimLong: {
3966 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003967 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003968 if (op->IsShl()) {
3969 __ shlq(first_reg, second_reg);
3970 } else if (op->IsShr()) {
3971 __ sarq(first_reg, second_reg);
3972 } else {
3973 __ shrq(first_reg, second_reg);
3974 }
3975 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003976 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003977 if (op->IsShl()) {
3978 __ shlq(first_reg, imm);
3979 } else if (op->IsShr()) {
3980 __ sarq(first_reg, imm);
3981 } else {
3982 __ shrq(first_reg, imm);
3983 }
3984 }
3985 break;
3986 }
3987 default:
3988 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003989 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003990 }
3991}
3992
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003993void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3994 LocationSummary* locations =
3995 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3996
3997 switch (ror->GetResultType()) {
3998 case Primitive::kPrimInt:
3999 case Primitive::kPrimLong: {
4000 locations->SetInAt(0, Location::RequiresRegister());
4001 // The shift count needs to be in CL (unless it is a constant).
4002 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4003 locations->SetOut(Location::SameAsFirstInput());
4004 break;
4005 }
4006 default:
4007 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4008 UNREACHABLE();
4009 }
4010}
4011
4012void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4013 LocationSummary* locations = ror->GetLocations();
4014 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4015 Location second = locations->InAt(1);
4016
4017 switch (ror->GetResultType()) {
4018 case Primitive::kPrimInt:
4019 if (second.IsRegister()) {
4020 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4021 __ rorl(first_reg, second_reg);
4022 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004023 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004024 __ rorl(first_reg, imm);
4025 }
4026 break;
4027 case Primitive::kPrimLong:
4028 if (second.IsRegister()) {
4029 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4030 __ rorq(first_reg, second_reg);
4031 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004032 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004033 __ rorq(first_reg, imm);
4034 }
4035 break;
4036 default:
4037 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4038 UNREACHABLE();
4039 }
4040}
4041
Calin Juravle9aec02f2014-11-18 23:06:35 +00004042void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4043 HandleShift(shl);
4044}
4045
4046void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4047 HandleShift(shl);
4048}
4049
4050void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4051 HandleShift(shr);
4052}
4053
4054void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4055 HandleShift(shr);
4056}
4057
4058void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4059 HandleShift(ushr);
4060}
4061
4062void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4063 HandleShift(ushr);
4064}
4065
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004066void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004067 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004069 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004070 if (instruction->IsStringAlloc()) {
4071 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4072 } else {
4073 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004074 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004075 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004076}
4077
4078void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004079 // Note: if heap poisoning is enabled, the entry point takes cares
4080 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004081 if (instruction->IsStringAlloc()) {
4082 // String is allocated through StringFactory. Call NewEmptyString entry point.
4083 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004084 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004085 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4086 __ call(Address(temp, code_offset.SizeValue()));
4087 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4088 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004089 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004090 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004091 DCHECK(!codegen_->IsLeafMethod());
4092 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004093}
4094
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004095void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4096 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004098 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004099 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004100 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4101 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004102}
4103
4104void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004105 // Note: if heap poisoning is enabled, the entry point takes cares
4106 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004107 QuickEntrypointEnum entrypoint =
4108 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4109 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004110 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004111 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004112}
4113
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004114void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004115 LocationSummary* locations =
4116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004117 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4118 if (location.IsStackSlot()) {
4119 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4120 } else if (location.IsDoubleStackSlot()) {
4121 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4122 }
4123 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004124}
4125
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004126void InstructionCodeGeneratorX86_64::VisitParameterValue(
4127 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004128 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004129}
4130
4131void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4132 LocationSummary* locations =
4133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4134 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4135}
4136
4137void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4138 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4139 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004140}
4141
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004142void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4143 LocationSummary* locations =
4144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4145 locations->SetInAt(0, Location::RequiresRegister());
4146 locations->SetOut(Location::RequiresRegister());
4147}
4148
4149void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4150 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004151 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004152 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004153 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004154 __ movq(locations->Out().AsRegister<CpuRegister>(),
4155 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004156 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004157 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004158 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004159 __ movq(locations->Out().AsRegister<CpuRegister>(),
4160 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4161 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004162 __ movq(locations->Out().AsRegister<CpuRegister>(),
4163 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004164 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004165}
4166
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004167void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004168 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004169 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004170 locations->SetInAt(0, Location::RequiresRegister());
4171 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004172}
4173
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004174void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4175 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004176 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4177 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004178 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004179 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004180 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004182 break;
4183
4184 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004185 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004186 break;
4187
4188 default:
4189 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4190 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004191}
4192
David Brazdil66d126e2015-04-03 16:02:44 +01004193void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4194 LocationSummary* locations =
4195 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4196 locations->SetInAt(0, Location::RequiresRegister());
4197 locations->SetOut(Location::SameAsFirstInput());
4198}
4199
4200void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004201 LocationSummary* locations = bool_not->GetLocations();
4202 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4203 locations->Out().AsRegister<CpuRegister>().AsRegister());
4204 Location out = locations->Out();
4205 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4206}
4207
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004208void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004209 LocationSummary* locations =
4210 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004211 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004212 locations->SetInAt(i, Location::Any());
4213 }
4214 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004215}
4216
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004217void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004218 LOG(FATAL) << "Unimplemented";
4219}
4220
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004221void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004222 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004223 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004224 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004225 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4226 */
4227 switch (kind) {
4228 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004229 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004230 break;
4231 }
4232 case MemBarrierKind::kAnyStore:
4233 case MemBarrierKind::kLoadAny:
4234 case MemBarrierKind::kStoreStore: {
4235 // nop
4236 break;
4237 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004238 case MemBarrierKind::kNTStoreStore:
4239 // Non-Temporal Store/Store needs an explicit fence.
4240 MemoryFence(/* non-temporal */ true);
4241 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004242 }
4243}
4244
4245void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4246 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4247
Roland Levillain0d5a2812015-11-13 10:07:31 +00004248 bool object_field_get_with_read_barrier =
4249 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004250 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004251 new (GetGraph()->GetArena()) LocationSummary(instruction,
4252 object_field_get_with_read_barrier ?
4253 LocationSummary::kCallOnSlowPath :
4254 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004255 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004256 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004257 }
Calin Juravle52c48962014-12-16 17:02:57 +00004258 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004259 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4260 locations->SetOut(Location::RequiresFpuRegister());
4261 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004262 // The output overlaps for an object field get when read barriers
4263 // are enabled: we do not want the move to overwrite the object's
4264 // location, as we need it to emit the read barrier.
4265 locations->SetOut(
4266 Location::RequiresRegister(),
4267 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004268 }
Calin Juravle52c48962014-12-16 17:02:57 +00004269}
4270
4271void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4272 const FieldInfo& field_info) {
4273 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4274
4275 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004276 Location base_loc = locations->InAt(0);
4277 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004278 Location out = locations->Out();
4279 bool is_volatile = field_info.IsVolatile();
4280 Primitive::Type field_type = field_info.GetFieldType();
4281 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4282
4283 switch (field_type) {
4284 case Primitive::kPrimBoolean: {
4285 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4286 break;
4287 }
4288
4289 case Primitive::kPrimByte: {
4290 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4291 break;
4292 }
4293
4294 case Primitive::kPrimShort: {
4295 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4296 break;
4297 }
4298
4299 case Primitive::kPrimChar: {
4300 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4301 break;
4302 }
4303
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004304 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004305 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4306 break;
4307 }
4308
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004309 case Primitive::kPrimNot: {
4310 // /* HeapReference<Object> */ out = *(base + offset)
4311 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004312 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004313 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004314 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004315 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004316 if (is_volatile) {
4317 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4318 }
4319 } else {
4320 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4321 codegen_->MaybeRecordImplicitNullCheck(instruction);
4322 if (is_volatile) {
4323 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4324 }
4325 // If read barriers are enabled, emit read barriers other than
4326 // Baker's using a slow path (and also unpoison the loaded
4327 // reference, if heap poisoning is enabled).
4328 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4329 }
4330 break;
4331 }
4332
Calin Juravle52c48962014-12-16 17:02:57 +00004333 case Primitive::kPrimLong: {
4334 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4335 break;
4336 }
4337
4338 case Primitive::kPrimFloat: {
4339 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4340 break;
4341 }
4342
4343 case Primitive::kPrimDouble: {
4344 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4345 break;
4346 }
4347
4348 case Primitive::kPrimVoid:
4349 LOG(FATAL) << "Unreachable type " << field_type;
4350 UNREACHABLE();
4351 }
4352
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004353 if (field_type == Primitive::kPrimNot) {
4354 // Potential implicit null checks, in the case of reference
4355 // fields, are handled in the previous switch statement.
4356 } else {
4357 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004358 }
Roland Levillain4d027112015-07-01 15:41:14 +01004359
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004360 if (is_volatile) {
4361 if (field_type == Primitive::kPrimNot) {
4362 // Memory barriers, in the case of references, are also handled
4363 // in the previous switch statement.
4364 } else {
4365 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4366 }
Roland Levillain4d027112015-07-01 15:41:14 +01004367 }
Calin Juravle52c48962014-12-16 17:02:57 +00004368}
4369
4370void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4371 const FieldInfo& field_info) {
4372 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4373
4374 LocationSummary* locations =
4375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004376 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004377 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004378 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004379 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004380
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004381 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004382 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004383 if (is_volatile) {
4384 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4385 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4386 } else {
4387 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4388 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004389 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004390 if (is_volatile) {
4391 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4392 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4393 } else {
4394 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4395 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004396 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004397 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004398 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004399 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004400 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004401 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4402 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004403 locations->AddTemp(Location::RequiresRegister());
4404 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405}
4406
Calin Juravle52c48962014-12-16 17:02:57 +00004407void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004408 const FieldInfo& field_info,
4409 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004410 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4411
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004413 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4414 Location value = locations->InAt(1);
4415 bool is_volatile = field_info.IsVolatile();
4416 Primitive::Type field_type = field_info.GetFieldType();
4417 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4418
4419 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004420 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004421 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004422
Mark Mendellea5af682015-10-22 17:35:49 -04004423 bool maybe_record_implicit_null_check_done = false;
4424
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425 switch (field_type) {
4426 case Primitive::kPrimBoolean:
4427 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004428 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004429 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004430 __ movb(Address(base, offset), Immediate(v));
4431 } else {
4432 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4433 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004434 break;
4435 }
4436
4437 case Primitive::kPrimShort:
4438 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004439 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004440 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004441 __ movw(Address(base, offset), Immediate(v));
4442 } else {
4443 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4444 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445 break;
4446 }
4447
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004450 if (value.IsConstant()) {
4451 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004452 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4453 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4454 // Note: if heap poisoning is enabled, no need to poison
4455 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004456 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004457 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004458 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4459 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4460 __ movl(temp, value.AsRegister<CpuRegister>());
4461 __ PoisonHeapReference(temp);
4462 __ movl(Address(base, offset), temp);
4463 } else {
4464 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4465 }
Mark Mendell40741f32015-04-20 22:10:34 -04004466 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004467 break;
4468 }
4469
4470 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004471 if (value.IsConstant()) {
4472 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004473 codegen_->MoveInt64ToAddress(Address(base, offset),
4474 Address(base, offset + sizeof(int32_t)),
4475 v,
4476 instruction);
4477 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004478 } else {
4479 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4480 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481 break;
4482 }
4483
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004484 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004485 if (value.IsConstant()) {
4486 int32_t v =
4487 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4488 __ movl(Address(base, offset), Immediate(v));
4489 } else {
4490 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4491 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004492 break;
4493 }
4494
4495 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004496 if (value.IsConstant()) {
4497 int64_t v =
4498 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4499 codegen_->MoveInt64ToAddress(Address(base, offset),
4500 Address(base, offset + sizeof(int32_t)),
4501 v,
4502 instruction);
4503 maybe_record_implicit_null_check_done = true;
4504 } else {
4505 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4506 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004507 break;
4508 }
4509
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510 case Primitive::kPrimVoid:
4511 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004512 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513 }
Calin Juravle52c48962014-12-16 17:02:57 +00004514
Mark Mendellea5af682015-10-22 17:35:49 -04004515 if (!maybe_record_implicit_null_check_done) {
4516 codegen_->MaybeRecordImplicitNullCheck(instruction);
4517 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004518
4519 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4520 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4521 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004522 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004523 }
4524
Calin Juravle52c48962014-12-16 17:02:57 +00004525 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004526 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004527 }
4528}
4529
4530void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4531 HandleFieldSet(instruction, instruction->GetFieldInfo());
4532}
4533
4534void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004535 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536}
4537
4538void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004539 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004540}
4541
4542void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004543 HandleFieldGet(instruction, instruction->GetFieldInfo());
4544}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545
Calin Juravle52c48962014-12-16 17:02:57 +00004546void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4547 HandleFieldGet(instruction);
4548}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549
Calin Juravle52c48962014-12-16 17:02:57 +00004550void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4551 HandleFieldGet(instruction, instruction->GetFieldInfo());
4552}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553
Calin Juravle52c48962014-12-16 17:02:57 +00004554void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4555 HandleFieldSet(instruction, instruction->GetFieldInfo());
4556}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004557
Calin Juravle52c48962014-12-16 17:02:57 +00004558void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004559 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560}
4561
Calin Juravlee460d1d2015-09-29 04:52:17 +01004562void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4563 HUnresolvedInstanceFieldGet* instruction) {
4564 FieldAccessCallingConventionX86_64 calling_convention;
4565 codegen_->CreateUnresolvedFieldLocationSummary(
4566 instruction, instruction->GetFieldType(), calling_convention);
4567}
4568
4569void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4570 HUnresolvedInstanceFieldGet* instruction) {
4571 FieldAccessCallingConventionX86_64 calling_convention;
4572 codegen_->GenerateUnresolvedFieldAccess(instruction,
4573 instruction->GetFieldType(),
4574 instruction->GetFieldIndex(),
4575 instruction->GetDexPc(),
4576 calling_convention);
4577}
4578
4579void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4580 HUnresolvedInstanceFieldSet* instruction) {
4581 FieldAccessCallingConventionX86_64 calling_convention;
4582 codegen_->CreateUnresolvedFieldLocationSummary(
4583 instruction, instruction->GetFieldType(), calling_convention);
4584}
4585
4586void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4587 HUnresolvedInstanceFieldSet* instruction) {
4588 FieldAccessCallingConventionX86_64 calling_convention;
4589 codegen_->GenerateUnresolvedFieldAccess(instruction,
4590 instruction->GetFieldType(),
4591 instruction->GetFieldIndex(),
4592 instruction->GetDexPc(),
4593 calling_convention);
4594}
4595
4596void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4597 HUnresolvedStaticFieldGet* instruction) {
4598 FieldAccessCallingConventionX86_64 calling_convention;
4599 codegen_->CreateUnresolvedFieldLocationSummary(
4600 instruction, instruction->GetFieldType(), calling_convention);
4601}
4602
4603void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4604 HUnresolvedStaticFieldGet* instruction) {
4605 FieldAccessCallingConventionX86_64 calling_convention;
4606 codegen_->GenerateUnresolvedFieldAccess(instruction,
4607 instruction->GetFieldType(),
4608 instruction->GetFieldIndex(),
4609 instruction->GetDexPc(),
4610 calling_convention);
4611}
4612
4613void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4614 HUnresolvedStaticFieldSet* instruction) {
4615 FieldAccessCallingConventionX86_64 calling_convention;
4616 codegen_->CreateUnresolvedFieldLocationSummary(
4617 instruction, instruction->GetFieldType(), calling_convention);
4618}
4619
4620void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4621 HUnresolvedStaticFieldSet* instruction) {
4622 FieldAccessCallingConventionX86_64 calling_convention;
4623 codegen_->GenerateUnresolvedFieldAccess(instruction,
4624 instruction->GetFieldType(),
4625 instruction->GetFieldIndex(),
4626 instruction->GetDexPc(),
4627 calling_convention);
4628}
4629
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004631 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4632 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4633 ? Location::RequiresRegister()
4634 : Location::Any();
4635 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004636}
4637
Calin Juravle2ae48182016-03-16 14:05:09 +00004638void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4639 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004640 return;
4641 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004642 LocationSummary* locations = instruction->GetLocations();
4643 Location obj = locations->InAt(0);
4644
4645 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004646 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004647}
4648
Calin Juravle2ae48182016-03-16 14:05:09 +00004649void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004650 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004651 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652
4653 LocationSummary* locations = instruction->GetLocations();
4654 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655
4656 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004657 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004658 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004660 } else {
4661 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004662 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004663 __ jmp(slow_path->GetEntryLabel());
4664 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665 }
4666 __ j(kEqual, slow_path->GetEntryLabel());
4667}
4668
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004669void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004670 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004671}
4672
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004674 bool object_array_get_with_read_barrier =
4675 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004676 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004677 new (GetGraph()->GetArena()) LocationSummary(instruction,
4678 object_array_get_with_read_barrier ?
4679 LocationSummary::kCallOnSlowPath :
4680 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004681 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004682 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004683 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004684 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004685 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004686 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4687 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4688 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004689 // The output overlaps for an object array get when read barriers
4690 // are enabled: we do not want the move to overwrite the array's
4691 // location, as we need it to emit the read barrier.
4692 locations->SetOut(
4693 Location::RequiresRegister(),
4694 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004695 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004696}
4697
4698void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4699 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004700 Location obj_loc = locations->InAt(0);
4701 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004703 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004704 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004705
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004706 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004707 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004708 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004709 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004710 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 break;
4712 }
4713
4714 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004715 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004716 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004717 break;
4718 }
4719
4720 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004721 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004722 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004723 break;
4724 }
4725
4726 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004727 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004728 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4729 // Branch cases into compressed and uncompressed for each index's type.
4730 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4731 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004732 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004733 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004734 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4735 "Expecting 0=compressed, 1=uncompressed");
4736 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004737 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4738 __ jmp(&done);
4739 __ Bind(&not_compressed);
4740 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4741 __ Bind(&done);
4742 } else {
4743 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004745 break;
4746 }
4747
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004748 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004749 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004750 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004751 break;
4752 }
4753
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004754 case Primitive::kPrimNot: {
4755 static_assert(
4756 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4757 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004758 // /* HeapReference<Object> */ out =
4759 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4760 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004761 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004762 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004763 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004764 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004765 } else {
4766 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004767 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4768 codegen_->MaybeRecordImplicitNullCheck(instruction);
4769 // If read barriers are enabled, emit read barriers other than
4770 // Baker's using a slow path (and also unpoison the loaded
4771 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004772 if (index.IsConstant()) {
4773 uint32_t offset =
4774 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004775 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4776 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004777 codegen_->MaybeGenerateReadBarrierSlow(
4778 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4779 }
4780 }
4781 break;
4782 }
4783
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004785 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004786 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787 break;
4788 }
4789
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004790 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004791 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004792 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004793 break;
4794 }
4795
4796 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004797 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004798 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004799 break;
4800 }
4801
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004802 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004803 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004804 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004805 }
Roland Levillain4d027112015-07-01 15:41:14 +01004806
4807 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004808 // Potential implicit null checks, in the case of reference
4809 // arrays, are handled in the previous switch statement.
4810 } else {
4811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004812 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004813}
4814
4815void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004816 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004817
4818 bool needs_write_barrier =
4819 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004820 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004821
Nicolas Geoffray39468442014-09-02 15:17:15 +01004822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004824 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004825 LocationSummary::kCallOnSlowPath :
4826 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004827
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004828 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004829 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4830 if (Primitive::IsFloatingPointType(value_type)) {
4831 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 } else {
4833 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4834 }
4835
4836 if (needs_write_barrier) {
4837 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004838 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004840 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841}
4842
4843void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4844 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004845 Location array_loc = locations->InAt(0);
4846 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004847 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004848 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004849 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004850 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004851 bool needs_write_barrier =
4852 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4854 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4855 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004856
4857 switch (value_type) {
4858 case Primitive::kPrimBoolean:
4859 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004860 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004861 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004862 if (value.IsRegister()) {
4863 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004865 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004867 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868 break;
4869 }
4870
4871 case Primitive::kPrimShort:
4872 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004873 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004874 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004875 if (value.IsRegister()) {
4876 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004878 DCHECK(value.IsConstant()) << value;
4879 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882 break;
4883 }
4884
4885 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004886 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004887 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004888
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004889 if (!value.IsRegister()) {
4890 // Just setting null.
4891 DCHECK(instruction->InputAt(2)->IsNullConstant());
4892 DCHECK(value.IsConstant()) << value;
4893 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004894 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004895 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004896 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004897 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004898 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004899
4900 DCHECK(needs_write_barrier);
4901 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004902 // We cannot use a NearLabel for `done`, as its range may be too
4903 // short when Baker read barriers are enabled.
4904 Label done;
4905 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004906 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004907 Location temp_loc = locations->GetTemp(0);
4908 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004909 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004910 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4911 codegen_->AddSlowPath(slow_path);
4912 if (instruction->GetValueCanBeNull()) {
4913 __ testl(register_value, register_value);
4914 __ j(kNotEqual, &not_null);
4915 __ movl(address, Immediate(0));
4916 codegen_->MaybeRecordImplicitNullCheck(instruction);
4917 __ jmp(&done);
4918 __ Bind(&not_null);
4919 }
4920
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004921 // Note that when Baker read barriers are enabled, the type
4922 // checks are performed without read barriers. This is fine,
4923 // even in the case where a class object is in the from-space
4924 // after the flip, as a comparison involving such a type would
4925 // not produce a false positive; it may of course produce a
4926 // false negative, in which case we would take the ArraySet
4927 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004928
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004929 // /* HeapReference<Class> */ temp = array->klass_
4930 __ movl(temp, Address(array, class_offset));
4931 codegen_->MaybeRecordImplicitNullCheck(instruction);
4932 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004933
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004934 // /* HeapReference<Class> */ temp = temp->component_type_
4935 __ movl(temp, Address(temp, component_offset));
4936 // If heap poisoning is enabled, no need to unpoison `temp`
4937 // nor the object reference in `register_value->klass`, as
4938 // we are comparing two poisoned references.
4939 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004940
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004941 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4942 __ j(kEqual, &do_put);
4943 // If heap poisoning is enabled, the `temp` reference has
4944 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004945 __ MaybeUnpoisonHeapReference(temp);
4946
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004947 // If heap poisoning is enabled, no need to unpoison the
4948 // heap reference loaded below, as it is only used for a
4949 // comparison with null.
4950 __ cmpl(Address(temp, super_offset), Immediate(0));
4951 __ j(kNotEqual, slow_path->GetEntryLabel());
4952 __ Bind(&do_put);
4953 } else {
4954 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004955 }
4956 }
4957
4958 if (kPoisonHeapReferences) {
4959 __ movl(temp, register_value);
4960 __ PoisonHeapReference(temp);
4961 __ movl(address, temp);
4962 } else {
4963 __ movl(address, register_value);
4964 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004965 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004966 codegen_->MaybeRecordImplicitNullCheck(instruction);
4967 }
4968
4969 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4970 codegen_->MarkGCCard(
4971 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4972 __ Bind(&done);
4973
4974 if (slow_path != nullptr) {
4975 __ Bind(slow_path->GetExitLabel());
4976 }
4977
4978 break;
4979 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004980
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004981 case Primitive::kPrimInt: {
4982 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004983 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004984 if (value.IsRegister()) {
4985 __ movl(address, value.AsRegister<CpuRegister>());
4986 } else {
4987 DCHECK(value.IsConstant()) << value;
4988 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4989 __ movl(address, Immediate(v));
4990 }
4991 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 break;
4993 }
4994
4995 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004996 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004997 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004998 if (value.IsRegister()) {
4999 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005000 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005002 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005003 Address address_high =
5004 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005005 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005006 }
5007 break;
5008 }
5009
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005010 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005011 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005012 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005013 if (value.IsFpuRegister()) {
5014 __ movss(address, value.AsFpuRegister<XmmRegister>());
5015 } else {
5016 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005017 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005018 __ movl(address, Immediate(v));
5019 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005020 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005021 break;
5022 }
5023
5024 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005025 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005026 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005027 if (value.IsFpuRegister()) {
5028 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5029 codegen_->MaybeRecordImplicitNullCheck(instruction);
5030 } else {
5031 int64_t v =
5032 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005033 Address address_high =
5034 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005035 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5036 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005037 break;
5038 }
5039
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 case Primitive::kPrimVoid:
5041 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005042 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043 }
5044}
5045
5046void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005047 LocationSummary* locations =
5048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005049 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005050 if (!instruction->IsEmittedAtUseSite()) {
5051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5052 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053}
5054
5055void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005056 if (instruction->IsEmittedAtUseSite()) {
5057 return;
5058 }
5059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005061 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5063 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005065 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005066 // Mask out most significant bit in case the array is String's array of char.
5067 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005068 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005069 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070}
5071
5072void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005073 RegisterSet caller_saves = RegisterSet::Empty();
5074 InvokeRuntimeCallingConvention calling_convention;
5075 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5076 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5077 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005078 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005079 HInstruction* length = instruction->InputAt(1);
5080 if (!length->IsEmittedAtUseSite()) {
5081 locations->SetInAt(1, Location::RegisterOrConstant(length));
5082 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083}
5084
5085void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5086 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005087 Location index_loc = locations->InAt(0);
5088 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005089 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090
Mark Mendell99dbd682015-04-22 16:18:52 -04005091 if (length_loc.IsConstant()) {
5092 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5093 if (index_loc.IsConstant()) {
5094 // BCE will remove the bounds check if we are guarenteed to pass.
5095 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5096 if (index < 0 || index >= length) {
5097 codegen_->AddSlowPath(slow_path);
5098 __ jmp(slow_path->GetEntryLabel());
5099 } else {
5100 // Some optimization after BCE may have generated this, and we should not
5101 // generate a bounds check if it is a valid range.
5102 }
5103 return;
5104 }
5105
5106 // We have to reverse the jump condition because the length is the constant.
5107 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5108 __ cmpl(index_reg, Immediate(length));
5109 codegen_->AddSlowPath(slow_path);
5110 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005111 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005112 HInstruction* array_length = instruction->InputAt(1);
5113 if (array_length->IsEmittedAtUseSite()) {
5114 // Address the length field in the array.
5115 DCHECK(array_length->IsArrayLength());
5116 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5117 Location array_loc = array_length->GetLocations()->InAt(0);
5118 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005119 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005120 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5121 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005122 CpuRegister length_reg = CpuRegister(TMP);
5123 __ movl(length_reg, array_len);
5124 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005125 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005126 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005127 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005128 // Checking the bound for general case:
5129 // Array of char or String's array when the compression feature off.
5130 if (index_loc.IsConstant()) {
5131 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5132 __ cmpl(array_len, Immediate(value));
5133 } else {
5134 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5135 }
5136 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005137 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005138 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005139 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005140 }
5141 codegen_->AddSlowPath(slow_path);
5142 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144}
5145
5146void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5147 CpuRegister card,
5148 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005149 CpuRegister value,
5150 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005151 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005152 if (value_can_be_null) {
5153 __ testl(value, value);
5154 __ j(kEqual, &is_null);
5155 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005156 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005157 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158 __ movq(temp, object);
5159 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005160 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005161 if (value_can_be_null) {
5162 __ Bind(&is_null);
5163 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005164}
5165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005166void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005167 LOG(FATAL) << "Unimplemented";
5168}
5169
5170void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005171 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5172}
5173
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005174void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005175 LocationSummary* locations =
5176 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005177 // In suspend check slow path, usually there are no caller-save registers at all.
5178 // If SIMD instructions are present, however, we force spilling all live SIMD
5179 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005180 locations->SetCustomSlowPathCallerSaves(
5181 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005182}
5183
5184void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005185 HBasicBlock* block = instruction->GetBlock();
5186 if (block->GetLoopInformation() != nullptr) {
5187 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5188 // The back edge will generate the suspend check.
5189 return;
5190 }
5191 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5192 // The goto will generate the suspend check.
5193 return;
5194 }
5195 GenerateSuspendCheck(instruction, nullptr);
5196}
5197
5198void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5199 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005200 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005201 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5202 if (slow_path == nullptr) {
5203 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5204 instruction->SetSlowPath(slow_path);
5205 codegen_->AddSlowPath(slow_path);
5206 if (successor != nullptr) {
5207 DCHECK(successor->IsLoopHeader());
5208 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5209 }
5210 } else {
5211 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5212 }
5213
Andreas Gampe542451c2016-07-26 09:02:02 -07005214 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005215 /* no_rip */ true),
5216 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005217 if (successor == nullptr) {
5218 __ j(kNotEqual, slow_path->GetEntryLabel());
5219 __ Bind(slow_path->GetReturnLabel());
5220 } else {
5221 __ j(kEqual, codegen_->GetLabelOf(successor));
5222 __ jmp(slow_path->GetEntryLabel());
5223 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005224}
5225
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005226X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5227 return codegen_->GetAssembler();
5228}
5229
5230void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005231 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005232 Location source = move->GetSource();
5233 Location destination = move->GetDestination();
5234
5235 if (source.IsRegister()) {
5236 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005237 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005238 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005239 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005240 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005241 } else {
5242 DCHECK(destination.IsDoubleStackSlot());
5243 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005244 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005245 }
5246 } else if (source.IsStackSlot()) {
5247 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005248 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005249 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005250 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005251 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005252 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005253 } else {
5254 DCHECK(destination.IsStackSlot());
5255 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5256 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5257 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005258 } else if (source.IsDoubleStackSlot()) {
5259 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005260 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005261 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005263 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5264 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005265 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005266 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005267 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5268 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5269 }
Aart Bik5576f372017-03-23 16:17:37 -07005270 } else if (source.IsSIMDStackSlot()) {
5271 DCHECK(destination.IsFpuRegister());
5272 __ movups(destination.AsFpuRegister<XmmRegister>(),
5273 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005274 } else if (source.IsConstant()) {
5275 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005276 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5277 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005278 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005279 if (value == 0) {
5280 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5281 } else {
5282 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5283 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005284 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005285 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005286 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005287 }
5288 } else if (constant->IsLongConstant()) {
5289 int64_t value = constant->AsLongConstant()->GetValue();
5290 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005291 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005292 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005293 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005294 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005295 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005296 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005297 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005299 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005300 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 } else {
5302 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005303 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5305 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005306 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005307 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005308 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005309 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005311 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005312 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005313 } else {
5314 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005315 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005317 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005318 } else if (source.IsFpuRegister()) {
5319 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005320 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005321 } else if (destination.IsStackSlot()) {
5322 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005323 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005324 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005325 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005326 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005327 } else {
5328 DCHECK(destination.IsSIMDStackSlot());
5329 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5330 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005331 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005332 }
5333}
5334
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005335void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005336 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005337 __ movl(Address(CpuRegister(RSP), mem), reg);
5338 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005339}
5340
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005341void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005342 ScratchRegisterScope ensure_scratch(
5343 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5344
5345 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5346 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5347 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5348 Address(CpuRegister(RSP), mem2 + stack_offset));
5349 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5350 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5351 CpuRegister(ensure_scratch.GetRegister()));
5352}
5353
Mark Mendell8a1c7282015-06-29 15:41:28 -04005354void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5355 __ movq(CpuRegister(TMP), reg1);
5356 __ movq(reg1, reg2);
5357 __ movq(reg2, CpuRegister(TMP));
5358}
5359
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005360void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5361 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5362 __ movq(Address(CpuRegister(RSP), mem), reg);
5363 __ movq(reg, CpuRegister(TMP));
5364}
5365
5366void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5367 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005368 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005369
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005370 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5371 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5372 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5373 Address(CpuRegister(RSP), mem2 + stack_offset));
5374 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5375 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5376 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005377}
5378
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005379void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5380 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5381 __ movss(Address(CpuRegister(RSP), mem), reg);
5382 __ movd(reg, CpuRegister(TMP));
5383}
5384
5385void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5386 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5387 __ movsd(Address(CpuRegister(RSP), mem), reg);
5388 __ movd(reg, CpuRegister(TMP));
5389}
5390
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005391void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005392 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005393 Location source = move->GetSource();
5394 Location destination = move->GetDestination();
5395
5396 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005397 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005398 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005399 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005400 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005401 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005402 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005403 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5404 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005406 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005407 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005408 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5409 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005410 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005411 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5412 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5413 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005414 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005415 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005416 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005417 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005418 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005419 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005420 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005421 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005422 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005423 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005424 }
5425}
5426
5427
5428void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5429 __ pushq(CpuRegister(reg));
5430}
5431
5432
5433void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5434 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005435}
5436
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005437void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005438 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005439 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5440 Immediate(mirror::Class::kStatusInitialized));
5441 __ j(kLess, slow_path->GetEntryLabel());
5442 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005443 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005444}
5445
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005446HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5447 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005448 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005449 case HLoadClass::LoadKind::kInvalid:
5450 LOG(FATAL) << "UNREACHABLE";
5451 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005452 case HLoadClass::LoadKind::kReferrersClass:
5453 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005454 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005455 case HLoadClass::LoadKind::kBssEntry:
5456 DCHECK(!Runtime::Current()->UseJitCompilation());
5457 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005458 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005459 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005460 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005461 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005462 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005463 break;
5464 }
5465 return desired_class_load_kind;
5466}
5467
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005468void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005469 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005470 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005471 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005472 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005473 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005474 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005475 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005476 return;
5477 }
Vladimir Marko41559982017-01-06 14:04:23 +00005478 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005479
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005480 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5481 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005482 ? LocationSummary::kCallOnSlowPath
5483 : LocationSummary::kNoCall;
5484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005485 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005486 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005487 }
5488
Vladimir Marko41559982017-01-06 14:04:23 +00005489 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005490 locations->SetInAt(0, Location::RequiresRegister());
5491 }
5492 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005493 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5494 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5495 // Rely on the type resolution and/or initialization to save everything.
5496 // Custom calling convention: RAX serves as both input and output.
5497 RegisterSet caller_saves = RegisterSet::Empty();
5498 caller_saves.Add(Location::RegisterLocation(RAX));
5499 locations->SetCustomSlowPathCallerSaves(caller_saves);
5500 } else {
5501 // For non-Baker read barrier we have a temp-clobbering call.
5502 }
5503 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005504}
5505
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005506Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
5507 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005508 Handle<mirror::Class> handle) {
5509 jit_class_roots_.Overwrite(
5510 TypeReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005511 // Add a patch entry and return the label.
5512 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
5513 PatchInfo<Label>* info = &jit_class_patches_.back();
5514 return &info->label;
5515}
5516
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005517// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5518// move.
5519void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005520 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005521 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005522 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005523 return;
5524 }
Vladimir Marko41559982017-01-06 14:04:23 +00005525 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005526
Vladimir Marko41559982017-01-06 14:04:23 +00005527 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005528 Location out_loc = locations->Out();
5529 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005530
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005531 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5532 ? kWithoutReadBarrier
5533 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005534 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005535 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005536 case HLoadClass::LoadKind::kReferrersClass: {
5537 DCHECK(!cls->CanCallRuntime());
5538 DCHECK(!cls->MustGenerateClinitCheck());
5539 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5540 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5541 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005542 cls,
5543 out_loc,
5544 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005545 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005546 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 break;
5548 }
5549 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005550 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005551 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005552 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005553 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005554 break;
5555 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005556 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005557 uint32_t address = dchecked_integral_cast<uint32_t>(
5558 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5559 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005560 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005561 break;
5562 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005563 case HLoadClass::LoadKind::kBssEntry: {
5564 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5565 /* no_rip */ false);
5566 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5567 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5568 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5569 generate_null_check = true;
5570 break;
5571 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005572 case HLoadClass::LoadKind::kJitTableAddress: {
5573 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5574 /* no_rip */ true);
5575 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005576 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005578 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005579 break;
5580 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 default:
5582 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5583 UNREACHABLE();
5584 }
5585
5586 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5587 DCHECK(cls->CanCallRuntime());
5588 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5589 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5590 codegen_->AddSlowPath(slow_path);
5591 if (generate_null_check) {
5592 __ testl(out, out);
5593 __ j(kEqual, slow_path->GetEntryLabel());
5594 }
5595 if (cls->MustGenerateClinitCheck()) {
5596 GenerateClassInitializationCheck(slow_path, out);
5597 } else {
5598 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005599 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600 }
5601}
5602
5603void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5604 LocationSummary* locations =
5605 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5606 locations->SetInAt(0, Location::RequiresRegister());
5607 if (check->HasUses()) {
5608 locations->SetOut(Location::SameAsFirstInput());
5609 }
5610}
5611
5612void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005613 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005614 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005615 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005616 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005617 GenerateClassInitializationCheck(slow_path,
5618 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005619}
5620
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005621HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5622 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005623 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005624 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005625 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005626 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005627 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005628 case HLoadString::LoadKind::kJitTableAddress:
5629 DCHECK(Runtime::Current()->UseJitCompilation());
5630 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005631 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005632 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005633 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005634 }
5635 return desired_string_load_kind;
5636}
5637
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005638void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005639 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005641 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005642 locations->SetOut(Location::RegisterLocation(RAX));
5643 } else {
5644 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005645 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5646 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005647 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005648 // Custom calling convention: RAX serves as both input and output.
5649 RegisterSet caller_saves = RegisterSet::Empty();
5650 caller_saves.Add(Location::RegisterLocation(RAX));
5651 locations->SetCustomSlowPathCallerSaves(caller_saves);
5652 } else {
5653 // For non-Baker read barrier we have a temp-clobbering call.
5654 }
5655 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005656 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005657}
5658
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005659Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005660 dex::StringIndex dex_index,
5661 Handle<mirror::String> handle) {
5662 jit_string_roots_.Overwrite(
5663 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005664 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005665 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005666 PatchInfo<Label>* info = &jit_string_patches_.back();
5667 return &info->label;
5668}
5669
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005670// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5671// move.
5672void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005673 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005674 Location out_loc = locations->Out();
5675 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005676
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005677 switch (load->GetLoadKind()) {
5678 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005679 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005680 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005681 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005682 return; // No dex cache slow path.
5683 }
5684 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005685 uint32_t address = dchecked_integral_cast<uint32_t>(
5686 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5687 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005688 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005689 return; // No dex cache slow path.
5690 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005691 case HLoadString::LoadKind::kBssEntry: {
5692 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5693 /* no_rip */ false);
5694 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5695 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005696 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005697 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5698 codegen_->AddSlowPath(slow_path);
5699 __ testl(out, out);
5700 __ j(kEqual, slow_path->GetEntryLabel());
5701 __ Bind(slow_path->GetExitLabel());
5702 return;
5703 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005704 case HLoadString::LoadKind::kJitTableAddress: {
5705 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5706 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005707 Label* fixup_label = codegen_->NewJitRootStringPatch(
5708 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005709 // /* GcRoot<mirror::String> */ out = *address
5710 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5711 return;
5712 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005713 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005714 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005715 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005716
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005717 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005718 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005719 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005720 codegen_->InvokeRuntime(kQuickResolveString,
5721 load,
5722 load->GetDexPc());
5723 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005724}
5725
David Brazdilcb1c0552015-08-04 16:22:25 +01005726static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005727 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005728 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005729}
5730
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005731void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5732 LocationSummary* locations =
5733 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5734 locations->SetOut(Location::RequiresRegister());
5735}
5736
5737void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005738 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5739}
5740
5741void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5742 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5743}
5744
5745void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5746 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005747}
5748
5749void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5750 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005752 InvokeRuntimeCallingConvention calling_convention;
5753 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5754}
5755
5756void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005757 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005758 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005759}
5760
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005761static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5762 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005763 // We need a temporary for holding the iftable length.
5764 return true;
5765 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005766 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005767 !kUseBakerReadBarrier &&
5768 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005769 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5770 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5771}
5772
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005773static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5774 return kEmitCompilerReadBarrier &&
5775 !kUseBakerReadBarrier &&
5776 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5777 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5778 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5779}
5780
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005781void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005782 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005783 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005784 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 case TypeCheckKind::kExactCheck:
5787 case TypeCheckKind::kAbstractClassCheck:
5788 case TypeCheckKind::kClassHierarchyCheck:
5789 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005790 call_kind =
5791 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005792 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 break;
5794 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005795 case TypeCheckKind::kUnresolvedCheck:
5796 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 call_kind = LocationSummary::kCallOnSlowPath;
5798 break;
5799 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005800
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005802 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005803 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005804 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805 locations->SetInAt(0, Location::RequiresRegister());
5806 locations->SetInAt(1, Location::Any());
5807 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5808 locations->SetOut(Location::RequiresRegister());
5809 // When read barriers are enabled, we need a temporary register for
5810 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005811 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005812 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005813 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005814}
5815
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005816void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005817 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 Location obj_loc = locations->InAt(0);
5820 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005821 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 Location out_loc = locations->Out();
5823 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005824 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005825 locations->GetTemp(0) :
5826 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005827 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5829 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5830 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005831 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005832 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005833
5834 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005835 // Avoid null check if we know obj is not null.
5836 if (instruction->MustDoNullCheck()) {
5837 __ testl(obj, obj);
5838 __ j(kEqual, &zero);
5839 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005841 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005843 // /* HeapReference<Class> */ out = obj->klass_
5844 GenerateReferenceLoadTwoRegisters(instruction,
5845 out_loc,
5846 obj_loc,
5847 class_offset,
5848 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849 if (cls.IsRegister()) {
5850 __ cmpl(out, cls.AsRegister<CpuRegister>());
5851 } else {
5852 DCHECK(cls.IsStackSlot()) << cls;
5853 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5854 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005855 if (zero.IsLinked()) {
5856 // Classes must be equal for the instanceof to succeed.
5857 __ j(kNotEqual, &zero);
5858 __ movl(out, Immediate(1));
5859 __ jmp(&done);
5860 } else {
5861 __ setcc(kEqual, out);
5862 // setcc only sets the low byte.
5863 __ andl(out, Immediate(1));
5864 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 break;
5866 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005867
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005868 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005869 // /* HeapReference<Class> */ out = obj->klass_
5870 GenerateReferenceLoadTwoRegisters(instruction,
5871 out_loc,
5872 obj_loc,
5873 class_offset,
5874 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 // If the class is abstract, we eagerly fetch the super class of the
5876 // object to avoid doing a comparison we know will fail.
5877 NearLabel loop, success;
5878 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005879 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005880 GenerateReferenceLoadOneRegister(instruction,
5881 out_loc,
5882 super_offset,
5883 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005884 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 __ testl(out, out);
5886 // If `out` is null, we use it for the result, and jump to `done`.
5887 __ j(kEqual, &done);
5888 if (cls.IsRegister()) {
5889 __ cmpl(out, cls.AsRegister<CpuRegister>());
5890 } else {
5891 DCHECK(cls.IsStackSlot()) << cls;
5892 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5893 }
5894 __ j(kNotEqual, &loop);
5895 __ movl(out, Immediate(1));
5896 if (zero.IsLinked()) {
5897 __ jmp(&done);
5898 }
5899 break;
5900 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005903 // /* HeapReference<Class> */ out = obj->klass_
5904 GenerateReferenceLoadTwoRegisters(instruction,
5905 out_loc,
5906 obj_loc,
5907 class_offset,
5908 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005909 // Walk over the class hierarchy to find a match.
5910 NearLabel loop, success;
5911 __ Bind(&loop);
5912 if (cls.IsRegister()) {
5913 __ cmpl(out, cls.AsRegister<CpuRegister>());
5914 } else {
5915 DCHECK(cls.IsStackSlot()) << cls;
5916 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5917 }
5918 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005920 GenerateReferenceLoadOneRegister(instruction,
5921 out_loc,
5922 super_offset,
5923 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005924 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 __ testl(out, out);
5926 __ j(kNotEqual, &loop);
5927 // If `out` is null, we use it for the result, and jump to `done`.
5928 __ jmp(&done);
5929 __ Bind(&success);
5930 __ movl(out, Immediate(1));
5931 if (zero.IsLinked()) {
5932 __ jmp(&done);
5933 }
5934 break;
5935 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005938 // /* HeapReference<Class> */ out = obj->klass_
5939 GenerateReferenceLoadTwoRegisters(instruction,
5940 out_loc,
5941 obj_loc,
5942 class_offset,
5943 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005944 // Do an exact check.
5945 NearLabel exact_check;
5946 if (cls.IsRegister()) {
5947 __ cmpl(out, cls.AsRegister<CpuRegister>());
5948 } else {
5949 DCHECK(cls.IsStackSlot()) << cls;
5950 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5951 }
5952 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005953 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005955 GenerateReferenceLoadOneRegister(instruction,
5956 out_loc,
5957 component_offset,
5958 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005959 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 __ testl(out, out);
5961 // If `out` is null, we use it for the result, and jump to `done`.
5962 __ j(kEqual, &done);
5963 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5964 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005965 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005966 __ movl(out, Immediate(1));
5967 __ jmp(&done);
5968 break;
5969 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005971 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005972 // No read barrier since the slow path will retry upon failure.
5973 // /* HeapReference<Class> */ out = obj->klass_
5974 GenerateReferenceLoadTwoRegisters(instruction,
5975 out_loc,
5976 obj_loc,
5977 class_offset,
5978 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005979 if (cls.IsRegister()) {
5980 __ cmpl(out, cls.AsRegister<CpuRegister>());
5981 } else {
5982 DCHECK(cls.IsStackSlot()) << cls;
5983 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5984 }
5985 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5987 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 codegen_->AddSlowPath(slow_path);
5989 __ j(kNotEqual, slow_path->GetEntryLabel());
5990 __ movl(out, Immediate(1));
5991 if (zero.IsLinked()) {
5992 __ jmp(&done);
5993 }
5994 break;
5995 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996
Calin Juravle98893e12015-10-02 21:05:03 +01005997 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 case TypeCheckKind::kInterfaceCheck: {
5999 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006000 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006001 // cases.
6002 //
6003 // We cannot directly call the InstanceofNonTrivial runtime
6004 // entry point without resorting to a type checking slow path
6005 // here (i.e. by calling InvokeRuntime directly), as it would
6006 // require to assign fixed registers for the inputs of this
6007 // HInstanceOf instruction (following the runtime calling
6008 // convention), which might be cluttered by the potential first
6009 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006010 //
6011 // TODO: Introduce a new runtime entry point taking the object
6012 // to test (instead of its class) as argument, and let it deal
6013 // with the read barrier issues. This will let us refactor this
6014 // case of the `switch` code as it was previously (with a direct
6015 // call to the runtime not using a type checking slow path).
6016 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006017 DCHECK(locations->OnlyCallsOnSlowPath());
6018 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6019 /* is_fatal */ false);
6020 codegen_->AddSlowPath(slow_path);
6021 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 if (zero.IsLinked()) {
6023 __ jmp(&done);
6024 }
6025 break;
6026 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006027 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006028
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006030 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006031 __ xorl(out, out);
6032 }
6033
6034 if (done.IsLinked()) {
6035 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006036 }
6037
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006038 if (slow_path != nullptr) {
6039 __ Bind(slow_path->GetExitLabel());
6040 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006041}
6042
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006043static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006045 case TypeCheckKind::kExactCheck:
6046 case TypeCheckKind::kAbstractClassCheck:
6047 case TypeCheckKind::kClassHierarchyCheck:
6048 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006049 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006050 case TypeCheckKind::kInterfaceCheck:
6051 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006053 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006054 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006055 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006056 LOG(FATAL) << "Unreachable";
6057 UNREACHABLE();
6058}
6059
6060void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6061 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6062 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6063 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6064 LocationSummary::CallKind call_kind = is_fatal_slow_path
6065 ? LocationSummary::kNoCall
6066 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6068 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006069 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6070 // Require a register for the interface check since there is a loop that compares the class to
6071 // a memory address.
6072 locations->SetInAt(1, Location::RequiresRegister());
6073 } else {
6074 locations->SetInAt(1, Location::Any());
6075 }
6076
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6078 locations->AddTemp(Location::RequiresRegister());
6079 // When read barriers are enabled, we need an additional temporary
6080 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006081 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006082 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006083 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006084}
6085
6086void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006087 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006088 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 Location obj_loc = locations->InAt(0);
6090 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006091 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006092 Location temp_loc = locations->GetTemp(0);
6093 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006094 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006095 locations->GetTemp(1) :
6096 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006097 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6098 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6099 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6100 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6101 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6102 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006103 const uint32_t object_array_data_offset =
6104 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006106 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6107 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6108 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006109 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006110 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006111 SlowPathCode* type_check_slow_path =
6112 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6113 is_type_check_slow_path_fatal);
6114 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006116
6117 NearLabel done;
6118 // Avoid null check if we know obj is not null.
6119 if (instruction->MustDoNullCheck()) {
6120 __ testl(obj, obj);
6121 __ j(kEqual, &done);
6122 }
6123
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006125 case TypeCheckKind::kExactCheck:
6126 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006127 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006128 GenerateReferenceLoadTwoRegisters(instruction,
6129 temp_loc,
6130 obj_loc,
6131 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006132 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 if (cls.IsRegister()) {
6134 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6135 } else {
6136 DCHECK(cls.IsStackSlot()) << cls;
6137 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6138 }
6139 // Jump to slow path for throwing the exception or doing a
6140 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 break;
6143 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006146 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006147 GenerateReferenceLoadTwoRegisters(instruction,
6148 temp_loc,
6149 obj_loc,
6150 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006151 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 // If the class is abstract, we eagerly fetch the super class of the
6153 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006154 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006156 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006157 GenerateReferenceLoadOneRegister(instruction,
6158 temp_loc,
6159 super_offset,
6160 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006161 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006163 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6164 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006166 // Otherwise, compare the classes.
6167 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 if (cls.IsRegister()) {
6169 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6170 } else {
6171 DCHECK(cls.IsStackSlot()) << cls;
6172 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6173 }
6174 __ j(kNotEqual, &loop);
6175 break;
6176 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006179 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006180 GenerateReferenceLoadTwoRegisters(instruction,
6181 temp_loc,
6182 obj_loc,
6183 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006184 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006186 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187 __ Bind(&loop);
6188 if (cls.IsRegister()) {
6189 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6190 } else {
6191 DCHECK(cls.IsStackSlot()) << cls;
6192 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6193 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006194 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006197 GenerateReferenceLoadOneRegister(instruction,
6198 temp_loc,
6199 super_offset,
6200 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006201 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202
6203 // If the class reference currently in `temp` is not null, jump
6204 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006206 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 break;
6210 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006212 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006213 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006214 GenerateReferenceLoadTwoRegisters(instruction,
6215 temp_loc,
6216 obj_loc,
6217 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006218 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006219 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006221 if (cls.IsRegister()) {
6222 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6223 } else {
6224 DCHECK(cls.IsStackSlot()) << cls;
6225 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6226 }
6227 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006228
6229 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006231 GenerateReferenceLoadOneRegister(instruction,
6232 temp_loc,
6233 component_offset,
6234 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006235 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236
6237 // If the component type is not null (i.e. the object is indeed
6238 // an array), jump to label `check_non_primitive_component_type`
6239 // to further check that this component type is not a primitive
6240 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006242 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006243 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006245 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 break;
6247 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006249 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006250 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251 //
6252 // We cannot directly call the CheckCast runtime entry point
6253 // without resorting to a type checking slow path here (i.e. by
6254 // calling InvokeRuntime directly), as it would require to
6255 // assign fixed registers for the inputs of this HInstanceOf
6256 // instruction (following the runtime calling convention), which
6257 // might be cluttered by the potential first read barrier
6258 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006259 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006260 break;
6261 }
6262
6263 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006264 // Fast path for the interface check. We always go slow path for heap poisoning since
6265 // unpoisoning cls would require an extra temp.
6266 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006267 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6268 // doing this.
6269 // /* HeapReference<Class> */ temp = obj->klass_
6270 GenerateReferenceLoadTwoRegisters(instruction,
6271 temp_loc,
6272 obj_loc,
6273 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006274 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006275
6276 // /* HeapReference<Class> */ temp = temp->iftable_
6277 GenerateReferenceLoadTwoRegisters(instruction,
6278 temp_loc,
6279 temp_loc,
6280 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006281 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006282 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006283 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006284 // Loop through the iftable and check if any class matches.
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006285 NearLabel start_loop;
6286 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006287 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006288 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006289 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6290 // Go to next interface if the classes do not match.
6291 __ cmpl(cls.AsRegister<CpuRegister>(),
6292 CodeGeneratorX86_64::ArrayAddress(temp,
6293 maybe_temp2_loc,
6294 TIMES_4,
6295 object_array_data_offset));
6296 __ j(kNotEqual, &start_loop); // Return if same class.
6297 } else {
6298 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006299 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 break;
6301 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006303 if (done.IsLinked()) {
6304 __ Bind(&done);
6305 }
6306
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308}
6309
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006310void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6311 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006313 InvokeRuntimeCallingConvention calling_convention;
6314 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6315}
6316
6317void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006318 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006319 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006320 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006321 if (instruction->IsEnter()) {
6322 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6323 } else {
6324 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6325 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006326}
6327
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006328void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6329void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6330void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6331
6332void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6333 LocationSummary* locations =
6334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6335 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6336 || instruction->GetResultType() == Primitive::kPrimLong);
6337 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006338 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006339 locations->SetOut(Location::SameAsFirstInput());
6340}
6341
6342void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6343 HandleBitwiseOperation(instruction);
6344}
6345
6346void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6347 HandleBitwiseOperation(instruction);
6348}
6349
6350void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6351 HandleBitwiseOperation(instruction);
6352}
6353
6354void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6355 LocationSummary* locations = instruction->GetLocations();
6356 Location first = locations->InAt(0);
6357 Location second = locations->InAt(1);
6358 DCHECK(first.Equals(locations->Out()));
6359
6360 if (instruction->GetResultType() == Primitive::kPrimInt) {
6361 if (second.IsRegister()) {
6362 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006363 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006364 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006365 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006366 } else {
6367 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006368 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006369 }
6370 } else if (second.IsConstant()) {
6371 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6372 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006373 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006374 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006375 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006376 } else {
6377 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006378 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006379 }
6380 } else {
6381 Address address(CpuRegister(RSP), second.GetStackIndex());
6382 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006383 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006384 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006385 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006386 } else {
6387 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006388 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006389 }
6390 }
6391 } else {
6392 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006393 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6394 bool second_is_constant = false;
6395 int64_t value = 0;
6396 if (second.IsConstant()) {
6397 second_is_constant = true;
6398 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006399 }
Mark Mendell40741f32015-04-20 22:10:34 -04006400 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006401
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006402 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006403 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006404 if (is_int32_value) {
6405 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6406 } else {
6407 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6408 }
6409 } else if (second.IsDoubleStackSlot()) {
6410 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006411 } else {
6412 __ andq(first_reg, second.AsRegister<CpuRegister>());
6413 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006414 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006415 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006416 if (is_int32_value) {
6417 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6418 } else {
6419 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6420 }
6421 } else if (second.IsDoubleStackSlot()) {
6422 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006423 } else {
6424 __ orq(first_reg, second.AsRegister<CpuRegister>());
6425 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006426 } else {
6427 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006428 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006429 if (is_int32_value) {
6430 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6431 } else {
6432 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6433 }
6434 } else if (second.IsDoubleStackSlot()) {
6435 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006436 } else {
6437 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6438 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006439 }
6440 }
6441}
6442
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006443void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6444 HInstruction* instruction,
6445 Location out,
6446 uint32_t offset,
6447 Location maybe_temp,
6448 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006449 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006450 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006451 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006452 if (kUseBakerReadBarrier) {
6453 // Load with fast path based Baker's read barrier.
6454 // /* HeapReference<Object> */ out = *(out + offset)
6455 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006456 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006457 } else {
6458 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006459 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006460 // in the following move operation, as we will need it for the
6461 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006462 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006463 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006464 // /* HeapReference<Object> */ out = *(out + offset)
6465 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006466 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006467 }
6468 } else {
6469 // Plain load with no read barrier.
6470 // /* HeapReference<Object> */ out = *(out + offset)
6471 __ movl(out_reg, Address(out_reg, offset));
6472 __ MaybeUnpoisonHeapReference(out_reg);
6473 }
6474}
6475
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006476void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6477 HInstruction* instruction,
6478 Location out,
6479 Location obj,
6480 uint32_t offset,
6481 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006482 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6483 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006484 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006485 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006486 if (kUseBakerReadBarrier) {
6487 // Load with fast path based Baker's read barrier.
6488 // /* HeapReference<Object> */ out = *(obj + offset)
6489 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006490 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006491 } else {
6492 // Load with slow path based read barrier.
6493 // /* HeapReference<Object> */ out = *(obj + offset)
6494 __ movl(out_reg, Address(obj_reg, offset));
6495 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6496 }
6497 } else {
6498 // Plain load with no read barrier.
6499 // /* HeapReference<Object> */ out = *(obj + offset)
6500 __ movl(out_reg, Address(obj_reg, offset));
6501 __ MaybeUnpoisonHeapReference(out_reg);
6502 }
6503}
6504
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006505void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6506 HInstruction* instruction,
6507 Location root,
6508 const Address& address,
6509 Label* fixup_label,
6510 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006511 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006512 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006513 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006514 if (kUseBakerReadBarrier) {
6515 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6516 // Baker's read barrier are used:
6517 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006518 // root = obj.field;
6519 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6520 // if (temp != null) {
6521 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006522 // }
6523
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006524 // /* GcRoot<mirror::Object> */ root = *address
6525 __ movl(root_reg, address);
6526 if (fixup_label != nullptr) {
6527 __ Bind(fixup_label);
6528 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006529 static_assert(
6530 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6531 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6532 "have different sizes.");
6533 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6534 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6535 "have different sizes.");
6536
Vladimir Marko953437b2016-08-24 08:30:46 +00006537 // Slow path marking the GC root `root`.
6538 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006539 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006540 codegen_->AddSlowPath(slow_path);
6541
Roland Levillaind966ce72017-02-09 16:20:14 +00006542 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6543 const int32_t entry_point_offset =
6544 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
6545 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6546 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006547 __ j(kNotEqual, slow_path->GetEntryLabel());
6548 __ Bind(slow_path->GetExitLabel());
6549 } else {
6550 // GC root loaded through a slow path for read barriers other
6551 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006552 // /* GcRoot<mirror::Object>* */ root = address
6553 __ leaq(root_reg, address);
6554 if (fixup_label != nullptr) {
6555 __ Bind(fixup_label);
6556 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006557 // /* mirror::Object* */ root = root->Read()
6558 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6559 }
6560 } else {
6561 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006562 // /* GcRoot<mirror::Object> */ root = *address
6563 __ movl(root_reg, address);
6564 if (fixup_label != nullptr) {
6565 __ Bind(fixup_label);
6566 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006567 // Note that GC roots are not affected by heap poisoning, thus we
6568 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 }
6570}
6571
6572void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6573 Location ref,
6574 CpuRegister obj,
6575 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006576 bool needs_null_check) {
6577 DCHECK(kEmitCompilerReadBarrier);
6578 DCHECK(kUseBakerReadBarrier);
6579
6580 // /* HeapReference<Object> */ ref = *(obj + offset)
6581 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006582 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006583}
6584
6585void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6586 Location ref,
6587 CpuRegister obj,
6588 uint32_t data_offset,
6589 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006590 bool needs_null_check) {
6591 DCHECK(kEmitCompilerReadBarrier);
6592 DCHECK(kUseBakerReadBarrier);
6593
Roland Levillain3d312422016-06-23 13:53:42 +01006594 static_assert(
6595 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6596 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006597 // /* HeapReference<Object> */ ref =
6598 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006599 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006600 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006601}
6602
6603void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6604 Location ref,
6605 CpuRegister obj,
6606 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006607 bool needs_null_check,
6608 bool always_update_field,
6609 CpuRegister* temp1,
6610 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006611 DCHECK(kEmitCompilerReadBarrier);
6612 DCHECK(kUseBakerReadBarrier);
6613
6614 // In slow path based read barriers, the read barrier call is
6615 // inserted after the original load. However, in fast path based
6616 // Baker's read barriers, we need to perform the load of
6617 // mirror::Object::monitor_ *before* the original reference load.
6618 // This load-load ordering is required by the read barrier.
6619 // The fast path/slow path (for Baker's algorithm) should look like:
6620 //
6621 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6622 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6623 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006624 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006625 // if (is_gray) {
6626 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6627 // }
6628 //
6629 // Note: the original implementation in ReadBarrier::Barrier is
6630 // slightly more complex as:
6631 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006632 // the high-bits of rb_state, which are expected to be all zeroes
6633 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6634 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006635 // - it performs additional checks that we do not do here for
6636 // performance reasons.
6637
6638 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006639 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6640
Vladimir Marko953437b2016-08-24 08:30:46 +00006641 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006642 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6643 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006644 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6645 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6646 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6647
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006648 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006649 // ref = ReadBarrier::Mark(ref);
6650 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6651 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006652 if (needs_null_check) {
6653 MaybeRecordImplicitNullCheck(instruction);
6654 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006655
6656 // Load fence to prevent load-load reordering.
6657 // Note that this is a no-op, thanks to the x86-64 memory model.
6658 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6659
6660 // The actual reference load.
6661 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006662 __ movl(ref_reg, src); // Flags are unaffected.
6663
6664 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6665 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006666 SlowPathCode* slow_path;
6667 if (always_update_field) {
6668 DCHECK(temp1 != nullptr);
6669 DCHECK(temp2 != nullptr);
6670 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6671 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6672 } else {
6673 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6674 instruction, ref, /* unpoison_ref_before_marking */ true);
6675 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006676 AddSlowPath(slow_path);
6677
6678 // We have done the "if" of the gray bit check above, now branch based on the flags.
6679 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006680
6681 // Object* ref = ref_addr->AsMirrorPtr()
6682 __ MaybeUnpoisonHeapReference(ref_reg);
6683
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006684 __ Bind(slow_path->GetExitLabel());
6685}
6686
6687void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6688 Location out,
6689 Location ref,
6690 Location obj,
6691 uint32_t offset,
6692 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006693 DCHECK(kEmitCompilerReadBarrier);
6694
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006695 // Insert a slow path based read barrier *after* the reference load.
6696 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 // If heap poisoning is enabled, the unpoisoning of the loaded
6698 // reference will be carried out by the runtime within the slow
6699 // path.
6700 //
6701 // Note that `ref` currently does not get unpoisoned (when heap
6702 // poisoning is enabled), which is alright as the `ref` argument is
6703 // not used by the artReadBarrierSlow entry point.
6704 //
6705 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6706 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6707 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6708 AddSlowPath(slow_path);
6709
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 __ jmp(slow_path->GetEntryLabel());
6711 __ Bind(slow_path->GetExitLabel());
6712}
6713
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006714void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6715 Location out,
6716 Location ref,
6717 Location obj,
6718 uint32_t offset,
6719 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006721 // Baker's read barriers shall be handled by the fast path
6722 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6723 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006724 // If heap poisoning is enabled, unpoisoning will be taken care of
6725 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006726 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 } else if (kPoisonHeapReferences) {
6728 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6729 }
6730}
6731
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006732void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6733 Location out,
6734 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006735 DCHECK(kEmitCompilerReadBarrier);
6736
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006737 // Insert a slow path based read barrier *after* the GC root load.
6738 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 // Note that GC roots are not affected by heap poisoning, so we do
6740 // not need to do anything special for this here.
6741 SlowPathCode* slow_path =
6742 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6743 AddSlowPath(slow_path);
6744
Roland Levillain0d5a2812015-11-13 10:07:31 +00006745 __ jmp(slow_path->GetEntryLabel());
6746 __ Bind(slow_path->GetExitLabel());
6747}
6748
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006749void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006750 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006751 LOG(FATAL) << "Unreachable";
6752}
6753
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006754void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006755 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006756 LOG(FATAL) << "Unreachable";
6757}
6758
Mark Mendellfe57faa2015-09-18 09:26:15 -04006759// Simple implementation of packed switch - generate cascaded compare/jumps.
6760void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6761 LocationSummary* locations =
6762 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6763 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006764 locations->AddTemp(Location::RequiresRegister());
6765 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006766}
6767
6768void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6769 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006770 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006771 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006772 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6773 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6774 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006775 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6776
6777 // Should we generate smaller inline compare/jumps?
6778 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6779 // Figure out the correct compare values and jump conditions.
6780 // Handle the first compare/branch as a special case because it might
6781 // jump to the default case.
6782 DCHECK_GT(num_entries, 2u);
6783 Condition first_condition;
6784 uint32_t index;
6785 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6786 if (lower_bound != 0) {
6787 first_condition = kLess;
6788 __ cmpl(value_reg_in, Immediate(lower_bound));
6789 __ j(first_condition, codegen_->GetLabelOf(default_block));
6790 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6791
6792 index = 1;
6793 } else {
6794 // Handle all the compare/jumps below.
6795 first_condition = kBelow;
6796 index = 0;
6797 }
6798
6799 // Handle the rest of the compare/jumps.
6800 for (; index + 1 < num_entries; index += 2) {
6801 int32_t compare_to_value = lower_bound + index + 1;
6802 __ cmpl(value_reg_in, Immediate(compare_to_value));
6803 // Jump to successors[index] if value < case_value[index].
6804 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6805 // Jump to successors[index + 1] if value == case_value[index + 1].
6806 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6807 }
6808
6809 if (index != num_entries) {
6810 // There are an odd number of entries. Handle the last one.
6811 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006812 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006813 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6814 }
6815
6816 // And the default for any other value.
6817 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6818 __ jmp(codegen_->GetLabelOf(default_block));
6819 }
6820 return;
6821 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006822
6823 // Remove the bias, if needed.
6824 Register value_reg_out = value_reg_in.AsRegister();
6825 if (lower_bound != 0) {
6826 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6827 value_reg_out = temp_reg.AsRegister();
6828 }
6829 CpuRegister value_reg(value_reg_out);
6830
6831 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006832 __ cmpl(value_reg, Immediate(num_entries - 1));
6833 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006834
Mark Mendell9c86b482015-09-18 13:36:07 -04006835 // We are in the range of the table.
6836 // Load the address of the jump table in the constant area.
6837 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006838
Mark Mendell9c86b482015-09-18 13:36:07 -04006839 // Load the (signed) offset from the jump table.
6840 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6841
6842 // Add the offset to the address of the table base.
6843 __ addq(temp_reg, base_reg);
6844
6845 // And jump.
6846 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006847}
6848
Aart Bikc5d47542016-01-27 17:00:35 -08006849void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6850 if (value == 0) {
6851 __ xorl(dest, dest);
6852 } else {
6853 __ movl(dest, Immediate(value));
6854 }
6855}
6856
Mark Mendell92e83bf2015-05-07 11:25:03 -04006857void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6858 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006859 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006860 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006861 } else if (IsUint<32>(value)) {
6862 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006863 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6864 } else {
6865 __ movq(dest, Immediate(value));
6866 }
6867}
6868
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006869void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6870 if (value == 0) {
6871 __ xorps(dest, dest);
6872 } else {
6873 __ movss(dest, LiteralInt32Address(value));
6874 }
6875}
6876
6877void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6878 if (value == 0) {
6879 __ xorpd(dest, dest);
6880 } else {
6881 __ movsd(dest, LiteralInt64Address(value));
6882 }
6883}
6884
6885void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6886 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6887}
6888
6889void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6890 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6891}
6892
Aart Bika19616e2016-02-01 18:57:58 -08006893void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6894 if (value == 0) {
6895 __ testl(dest, dest);
6896 } else {
6897 __ cmpl(dest, Immediate(value));
6898 }
6899}
6900
6901void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6902 if (IsInt<32>(value)) {
6903 if (value == 0) {
6904 __ testq(dest, dest);
6905 } else {
6906 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6907 }
6908 } else {
6909 // Value won't fit in an int.
6910 __ cmpq(dest, LiteralInt64Address(value));
6911 }
6912}
6913
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006914void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6915 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006916 GenerateIntCompare(lhs_reg, rhs);
6917}
6918
6919void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006920 if (rhs.IsConstant()) {
6921 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006922 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006923 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006924 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006925 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006926 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006927 }
6928}
6929
6930void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6931 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6932 if (rhs.IsConstant()) {
6933 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6934 Compare64BitValue(lhs_reg, value);
6935 } else if (rhs.IsDoubleStackSlot()) {
6936 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6937 } else {
6938 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6939 }
6940}
6941
6942Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6943 Location index,
6944 ScaleFactor scale,
6945 uint32_t data_offset) {
6946 return index.IsConstant() ?
6947 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6948 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6949}
6950
Mark Mendellcfa410b2015-05-25 16:02:44 -04006951void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6952 DCHECK(dest.IsDoubleStackSlot());
6953 if (IsInt<32>(value)) {
6954 // Can move directly as an int32 constant.
6955 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6956 Immediate(static_cast<int32_t>(value)));
6957 } else {
6958 Load64BitValue(CpuRegister(TMP), value);
6959 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6960 }
6961}
6962
Mark Mendell9c86b482015-09-18 13:36:07 -04006963/**
6964 * Class to handle late fixup of offsets into constant area.
6965 */
6966class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6967 public:
6968 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6969 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6970
6971 protected:
6972 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6973
6974 CodeGeneratorX86_64* codegen_;
6975
6976 private:
6977 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6978 // Patch the correct offset for the instruction. We use the address of the
6979 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6980 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6981 int32_t relative_position = constant_offset - pos;
6982
6983 // Patch in the right value.
6984 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6985 }
6986
6987 // Location in constant area that the fixup refers to.
6988 size_t offset_into_constant_area_;
6989};
6990
6991/**
6992 t * Class to handle late fixup of offsets to a jump table that will be created in the
6993 * constant area.
6994 */
6995class JumpTableRIPFixup : public RIPFixup {
6996 public:
6997 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6998 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6999
7000 void CreateJumpTable() {
7001 X86_64Assembler* assembler = codegen_->GetAssembler();
7002
7003 // Ensure that the reference to the jump table has the correct offset.
7004 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7005 SetOffset(offset_in_constant_table);
7006
7007 // Compute the offset from the start of the function to this jump table.
7008 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7009
7010 // Populate the jump table with the correct values for the jump table.
7011 int32_t num_entries = switch_instr_->GetNumEntries();
7012 HBasicBlock* block = switch_instr_->GetBlock();
7013 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7014 // The value that we want is the target offset - the position of the table.
7015 for (int32_t i = 0; i < num_entries; i++) {
7016 HBasicBlock* b = successors[i];
7017 Label* l = codegen_->GetLabelOf(b);
7018 DCHECK(l->IsBound());
7019 int32_t offset_to_block = l->Position() - current_table_offset;
7020 assembler->AppendInt32(offset_to_block);
7021 }
7022 }
7023
7024 private:
7025 const HPackedSwitch* switch_instr_;
7026};
7027
Mark Mendellf55c3e02015-03-26 21:07:46 -04007028void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7029 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007030 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007031 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7032 // 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 -04007033 assembler->Align(4, 0);
7034 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007035
7036 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007037 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007038 jump_table->CreateJumpTable();
7039 }
7040
7041 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007042 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007043 }
7044
7045 // And finish up.
7046 CodeGenerator::Finalize(allocator);
7047}
7048
Mark Mendellf55c3e02015-03-26 21:07:46 -04007049Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7050 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7051 return Address::RIP(fixup);
7052}
7053
7054Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7055 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7056 return Address::RIP(fixup);
7057}
7058
7059Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7060 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7061 return Address::RIP(fixup);
7062}
7063
7064Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7065 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7066 return Address::RIP(fixup);
7067}
7068
Andreas Gampe85b62f22015-09-09 13:15:38 -07007069// TODO: trg as memory.
7070void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7071 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007072 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007073 return;
7074 }
7075
7076 DCHECK_NE(type, Primitive::kPrimVoid);
7077
7078 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7079 if (trg.Equals(return_loc)) {
7080 return;
7081 }
7082
7083 // Let the parallel move resolver take care of all of this.
7084 HParallelMove parallel_move(GetGraph()->GetArena());
7085 parallel_move.AddMove(return_loc, trg, type, nullptr);
7086 GetMoveResolver()->EmitNativeCode(&parallel_move);
7087}
7088
Mark Mendell9c86b482015-09-18 13:36:07 -04007089Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7090 // Create a fixup to be used to create and address the jump table.
7091 JumpTableRIPFixup* table_fixup =
7092 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7093
7094 // We have to populate the jump tables.
7095 fixups_to_jump_tables_.push_back(table_fixup);
7096 return Address::RIP(table_fixup);
7097}
7098
Mark Mendellea5af682015-10-22 17:35:49 -04007099void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7100 const Address& addr_high,
7101 int64_t v,
7102 HInstruction* instruction) {
7103 if (IsInt<32>(v)) {
7104 int32_t v_32 = v;
7105 __ movq(addr_low, Immediate(v_32));
7106 MaybeRecordImplicitNullCheck(instruction);
7107 } else {
7108 // Didn't fit in a register. Do it in pieces.
7109 int32_t low_v = Low32Bits(v);
7110 int32_t high_v = High32Bits(v);
7111 __ movl(addr_low, Immediate(low_v));
7112 MaybeRecordImplicitNullCheck(instruction);
7113 __ movl(addr_high, Immediate(high_v));
7114 }
7115}
7116
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007117void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7118 const uint8_t* roots_data,
7119 const PatchInfo<Label>& info,
7120 uint64_t index_in_table) const {
7121 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7122 uintptr_t address =
7123 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7124 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7125 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7126 dchecked_integral_cast<uint32_t>(address);
7127}
7128
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007129void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7130 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007131 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007132 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007133 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007134 uint64_t index_in_table = it->second;
7135 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007136 }
7137
7138 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007139 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007140 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7141 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007142 uint64_t index_in_table = it->second;
7143 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007144 }
7145}
7146
Roland Levillain4d027112015-07-01 15:41:14 +01007147#undef __
7148
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007149} // namespace x86_64
7150} // namespace art