blob: 0c3b2ad742105b1cf753e97429c3d696c23b2779 [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 Levillain97c46462017-05-11 14:04:03 +0100527 Thread::ReadBarrierMarkEntryPointsOffset<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 =
Roland Levillain97c46462017-05-11 14:04:03 +0100618 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100619 // 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) {
Vladimir Marko65979462017-05-19 17:25:12 +01001092 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
1093 __ Bind(&string_patches_.back().label);
1094}
1095
Vladimir Markoaad75c62016-10-03 08:46:48 +00001096Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1097 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001098 string_bss_entry_patches_.emplace_back(
1099 load_string->GetDexFile(), load_string->GetStringIndex().index_);
1100 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001101}
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() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001125 string_patches_.size() +
1126 string_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001127 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01001128 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01001129 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
1130 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001131 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
1132 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001133 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001134 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01001135 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01001136 DCHECK(boot_image_type_patches_.empty());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001137 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(string_patches_,
1138 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001139 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001140 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1141 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001142 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1143 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001144 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
1145 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001146 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001147}
1148
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001150 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151}
1152
1153void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001154 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155}
1156
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001157size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1158 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1159 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001160}
1161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001162size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1163 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1164 return kX86_64WordSize;
1165}
1166
1167size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001168 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001169 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001170 } else {
1171 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1172 }
1173 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001174}
1175
1176size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001177 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001178 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001179 } else {
1180 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1181 }
1182 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001183}
1184
Calin Juravle175dc732015-08-25 15:42:32 +01001185void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1186 HInstruction* instruction,
1187 uint32_t dex_pc,
1188 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001189 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001190 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1191 if (EntrypointRequiresStackMap(entrypoint)) {
1192 RecordPcInfo(instruction, dex_pc, slow_path);
1193 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001194}
1195
Roland Levillaindec8f632016-07-22 17:10:06 +01001196void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1197 HInstruction* instruction,
1198 SlowPathCode* slow_path) {
1199 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001200 GenerateInvokeRuntime(entry_point_offset);
1201}
1202
1203void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001204 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1205}
1206
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001207static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001208// Use a fake return address register to mimic Quick.
1209static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001210CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001211 const X86_64InstructionSetFeatures& isa_features,
1212 const CompilerOptions& compiler_options,
1213 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001214 : CodeGenerator(graph,
1215 kNumberOfCpuRegisters,
1216 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001217 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001218 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1219 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001220 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001221 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1222 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001223 compiler_options,
1224 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001225 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001226 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001227 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001228 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001229 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001230 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001231 constant_area_start_(0),
Vladimir Marko65979462017-05-19 17:25:12 +01001232 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001233 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001234 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1235 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001236 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001237 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001238 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001239 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1240 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001241 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1242}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001244InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1245 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001246 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 assembler_(codegen->GetAssembler()),
1248 codegen_(codegen) {}
1249
David Brazdil58282f42016-01-14 12:45:10 +00001250void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001252 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001254 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001255 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001256}
1257
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001258static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001259 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001260}
David Srbecky9d8606d2015-04-12 09:35:32 +01001261
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001262static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001263 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001264}
1265
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001267 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001268 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001269 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001270 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001271 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001272
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001273 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001274 __ testq(CpuRegister(RAX), Address(
1275 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001276 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001277 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001278
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001279 if (HasEmptyFrame()) {
1280 return;
1281 }
1282
Nicolas Geoffray98893962015-01-21 12:32:32 +00001283 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001284 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001285 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001286 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1288 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001289 }
1290 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001291
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001292 int adjust = GetFrameSize() - GetCoreSpillSize();
1293 __ subq(CpuRegister(RSP), Immediate(adjust));
1294 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001295 uint32_t xmm_spill_location = GetFpuSpillStart();
1296 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001297
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001298 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1299 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001300 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1301 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1302 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001303 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001304 }
1305
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001306 // Save the current method if we need it. Note that we do not
1307 // do this in HCurrentMethod, as the instruction might have been removed
1308 // in the SSA graph.
1309 if (RequiresCurrentMethod()) {
1310 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1311 CpuRegister(kMethodRegisterArgument));
1312 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001313
1314 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1315 // Initialize should_deoptimize flag to 0.
1316 __ movl(Address(CpuRegister(RSP), GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1317 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001318}
1319
1320void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001321 __ cfi().RememberState();
1322 if (!HasEmptyFrame()) {
1323 uint32_t xmm_spill_location = GetFpuSpillStart();
1324 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1325 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1326 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1327 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1328 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1329 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1330 }
1331 }
1332
1333 int adjust = GetFrameSize() - GetCoreSpillSize();
1334 __ addq(CpuRegister(RSP), Immediate(adjust));
1335 __ cfi().AdjustCFAOffset(-adjust);
1336
1337 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1338 Register reg = kCoreCalleeSaves[i];
1339 if (allocated_registers_.ContainsCoreRegister(reg)) {
1340 __ popq(CpuRegister(reg));
1341 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1342 __ cfi().Restore(DWARFReg(reg));
1343 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001344 }
1345 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001346 __ ret();
1347 __ cfi().RestoreState();
1348 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001349}
1350
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001351void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1352 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001353}
1354
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001355void CodeGeneratorX86_64::Move(Location destination, Location source) {
1356 if (source.Equals(destination)) {
1357 return;
1358 }
1359 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001360 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001361 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001362 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001363 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001364 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001365 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001366 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1367 } else if (source.IsConstant()) {
1368 HConstant* constant = source.GetConstant();
1369 if (constant->IsLongConstant()) {
1370 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1371 } else {
1372 Load32BitValue(dest, GetInt32ValueOf(constant));
1373 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374 } else {
1375 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001376 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001377 }
1378 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001379 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001380 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001381 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001382 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001383 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1384 } else if (source.IsConstant()) {
1385 HConstant* constant = source.GetConstant();
1386 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1387 if (constant->IsFloatConstant()) {
1388 Load32BitValue(dest, static_cast<int32_t>(value));
1389 } else {
1390 Load64BitValue(dest, value);
1391 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001392 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001393 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001394 } else {
1395 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001396 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001397 }
1398 } else if (destination.IsStackSlot()) {
1399 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001400 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001401 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001402 } else if (source.IsFpuRegister()) {
1403 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001404 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001405 } else if (source.IsConstant()) {
1406 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001407 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001408 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001409 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001410 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001411 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1412 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001413 }
1414 } else {
1415 DCHECK(destination.IsDoubleStackSlot());
1416 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001417 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001418 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 } else if (source.IsFpuRegister()) {
1420 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001422 } else if (source.IsConstant()) {
1423 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001424 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1425 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001426 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001427 } else {
1428 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001429 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1430 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001431 }
1432 }
1433}
1434
Calin Juravle175dc732015-08-25 15:42:32 +01001435void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1436 DCHECK(location.IsRegister());
1437 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1438}
1439
Calin Juravlee460d1d2015-09-29 04:52:17 +01001440void CodeGeneratorX86_64::MoveLocation(
1441 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1442 Move(dst, src);
1443}
1444
1445void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1446 if (location.IsRegister()) {
1447 locations->AddTemp(location);
1448 } else {
1449 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1450 }
1451}
1452
David Brazdilfc6a86a2015-06-26 10:33:45 +00001453void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001454 DCHECK(!successor->IsExitBlock());
1455
1456 HBasicBlock* block = got->GetBlock();
1457 HInstruction* previous = got->GetPrevious();
1458
1459 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001460 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001461 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1462 return;
1463 }
1464
1465 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1466 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1467 }
1468 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001469 __ jmp(codegen_->GetLabelOf(successor));
1470 }
1471}
1472
David Brazdilfc6a86a2015-06-26 10:33:45 +00001473void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1474 got->SetLocations(nullptr);
1475}
1476
1477void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1478 HandleGoto(got, got->GetSuccessor());
1479}
1480
1481void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1482 try_boundary->SetLocations(nullptr);
1483}
1484
1485void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1486 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1487 if (!successor->IsExitBlock()) {
1488 HandleGoto(try_boundary, successor);
1489 }
1490}
1491
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001492void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1493 exit->SetLocations(nullptr);
1494}
1495
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001496void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001497}
1498
Mark Mendell152408f2015-12-31 12:28:50 -05001499template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001500void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001501 LabelType* true_label,
1502 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001503 if (cond->IsFPConditionTrueIfNaN()) {
1504 __ j(kUnordered, true_label);
1505 } else if (cond->IsFPConditionFalseIfNaN()) {
1506 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001507 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001508 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001509}
1510
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001511void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001512 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001513
Mark Mendellc4701932015-04-10 13:18:51 -04001514 Location left = locations->InAt(0);
1515 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001516 Primitive::Type type = condition->InputAt(0)->GetType();
1517 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001518 case Primitive::kPrimBoolean:
1519 case Primitive::kPrimByte:
1520 case Primitive::kPrimChar:
1521 case Primitive::kPrimShort:
1522 case Primitive::kPrimInt:
1523 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001524 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001525 break;
1526 }
Mark Mendellc4701932015-04-10 13:18:51 -04001527 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001528 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001529 break;
1530 }
1531 case Primitive::kPrimFloat: {
1532 if (right.IsFpuRegister()) {
1533 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1534 } else if (right.IsConstant()) {
1535 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1536 codegen_->LiteralFloatAddress(
1537 right.GetConstant()->AsFloatConstant()->GetValue()));
1538 } else {
1539 DCHECK(right.IsStackSlot());
1540 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1541 Address(CpuRegister(RSP), right.GetStackIndex()));
1542 }
Mark Mendellc4701932015-04-10 13:18:51 -04001543 break;
1544 }
1545 case Primitive::kPrimDouble: {
1546 if (right.IsFpuRegister()) {
1547 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1548 } else if (right.IsConstant()) {
1549 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1550 codegen_->LiteralDoubleAddress(
1551 right.GetConstant()->AsDoubleConstant()->GetValue()));
1552 } else {
1553 DCHECK(right.IsDoubleStackSlot());
1554 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1555 Address(CpuRegister(RSP), right.GetStackIndex()));
1556 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001557 break;
1558 }
1559 default:
1560 LOG(FATAL) << "Unexpected condition type " << type;
1561 }
1562}
1563
1564template<class LabelType>
1565void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1566 LabelType* true_target_in,
1567 LabelType* false_target_in) {
1568 // Generated branching requires both targets to be explicit. If either of the
1569 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1570 LabelType fallthrough_target;
1571 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1572 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1573
1574 // Generate the comparison to set the CC.
1575 GenerateCompareTest(condition);
1576
1577 // Now generate the correct jump(s).
1578 Primitive::Type type = condition->InputAt(0)->GetType();
1579 switch (type) {
1580 case Primitive::kPrimLong: {
1581 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1582 break;
1583 }
1584 case Primitive::kPrimFloat: {
1585 GenerateFPJumps(condition, true_target, false_target);
1586 break;
1587 }
1588 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001589 GenerateFPJumps(condition, true_target, false_target);
1590 break;
1591 }
1592 default:
1593 LOG(FATAL) << "Unexpected condition type " << type;
1594 }
1595
David Brazdil0debae72015-11-12 18:37:00 +00001596 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001597 __ jmp(false_target);
1598 }
David Brazdil0debae72015-11-12 18:37:00 +00001599
1600 if (fallthrough_target.IsLinked()) {
1601 __ Bind(&fallthrough_target);
1602 }
Mark Mendellc4701932015-04-10 13:18:51 -04001603}
1604
David Brazdil0debae72015-11-12 18:37:00 +00001605static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1606 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1607 // are set only strictly before `branch`. We can't use the eflags on long
1608 // conditions if they are materialized due to the complex branching.
1609 return cond->IsCondition() &&
1610 cond->GetNext() == branch &&
1611 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1612}
1613
Mark Mendell152408f2015-12-31 12:28:50 -05001614template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001615void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001616 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001617 LabelType* true_target,
1618 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001619 HInstruction* cond = instruction->InputAt(condition_input_index);
1620
1621 if (true_target == nullptr && false_target == nullptr) {
1622 // Nothing to do. The code always falls through.
1623 return;
1624 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001625 // Constant condition, statically compared against "true" (integer value 1).
1626 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001627 if (true_target != nullptr) {
1628 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001629 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001630 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001631 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001632 if (false_target != nullptr) {
1633 __ jmp(false_target);
1634 }
1635 }
1636 return;
1637 }
1638
1639 // The following code generates these patterns:
1640 // (1) true_target == nullptr && false_target != nullptr
1641 // - opposite condition true => branch to false_target
1642 // (2) true_target != nullptr && false_target == nullptr
1643 // - condition true => branch to true_target
1644 // (3) true_target != nullptr && false_target != nullptr
1645 // - condition true => branch to true_target
1646 // - branch to false_target
1647 if (IsBooleanValueOrMaterializedCondition(cond)) {
1648 if (AreEflagsSetFrom(cond, instruction)) {
1649 if (true_target == nullptr) {
1650 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1651 } else {
1652 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1653 }
1654 } else {
1655 // Materialized condition, compare against 0.
1656 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1657 if (lhs.IsRegister()) {
1658 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1659 } else {
1660 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1661 }
1662 if (true_target == nullptr) {
1663 __ j(kEqual, false_target);
1664 } else {
1665 __ j(kNotEqual, true_target);
1666 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001667 }
1668 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001669 // Condition has not been materialized, use its inputs as the
1670 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001671 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001672
David Brazdil0debae72015-11-12 18:37:00 +00001673 // If this is a long or FP comparison that has been folded into
1674 // the HCondition, generate the comparison directly.
1675 Primitive::Type type = condition->InputAt(0)->GetType();
1676 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1677 GenerateCompareTestAndBranch(condition, true_target, false_target);
1678 return;
1679 }
1680
1681 Location lhs = condition->GetLocations()->InAt(0);
1682 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001683 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001684 if (true_target == nullptr) {
1685 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1686 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001687 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001688 }
Dave Allison20dfc792014-06-16 20:44:29 -07001689 }
David Brazdil0debae72015-11-12 18:37:00 +00001690
1691 // If neither branch falls through (case 3), the conditional branch to `true_target`
1692 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1693 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001694 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001695 }
1696}
1697
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001698void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1700 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001701 locations->SetInAt(0, Location::Any());
1702 }
1703}
1704
1705void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001706 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1707 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1708 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1709 nullptr : codegen_->GetLabelOf(true_successor);
1710 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1711 nullptr : codegen_->GetLabelOf(false_successor);
1712 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001713}
1714
1715void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1716 LocationSummary* locations = new (GetGraph()->GetArena())
1717 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001718 InvokeRuntimeCallingConvention calling_convention;
1719 RegisterSet caller_saves = RegisterSet::Empty();
1720 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1721 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001722 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001723 locations->SetInAt(0, Location::Any());
1724 }
1725}
1726
1727void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001728 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001729 GenerateTestAndBranch<Label>(deoptimize,
1730 /* condition_input_index */ 0,
1731 slow_path->GetEntryLabel(),
1732 /* false_target */ nullptr);
1733}
1734
Mingyao Yang063fc772016-08-02 11:02:54 -07001735void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1736 LocationSummary* locations = new (GetGraph()->GetArena())
1737 LocationSummary(flag, LocationSummary::kNoCall);
1738 locations->SetOut(Location::RequiresRegister());
1739}
1740
1741void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1742 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1743 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1744}
1745
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001746static bool SelectCanUseCMOV(HSelect* select) {
1747 // There are no conditional move instructions for XMMs.
1748 if (Primitive::IsFloatingPointType(select->GetType())) {
1749 return false;
1750 }
1751
1752 // A FP condition doesn't generate the single CC that we need.
1753 HInstruction* condition = select->GetCondition();
1754 if (condition->IsCondition() &&
1755 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1756 return false;
1757 }
1758
1759 // We can generate a CMOV for this Select.
1760 return true;
1761}
1762
David Brazdil74eb1b22015-12-14 11:44:01 +00001763void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1764 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1765 if (Primitive::IsFloatingPointType(select->GetType())) {
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001767 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001768 } else {
1769 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001770 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001771 if (select->InputAt(1)->IsConstant()) {
1772 locations->SetInAt(1, Location::RequiresRegister());
1773 } else {
1774 locations->SetInAt(1, Location::Any());
1775 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001776 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001777 locations->SetInAt(1, Location::Any());
1778 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001779 }
1780 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1781 locations->SetInAt(2, Location::RequiresRegister());
1782 }
1783 locations->SetOut(Location::SameAsFirstInput());
1784}
1785
1786void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1787 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001788 if (SelectCanUseCMOV(select)) {
1789 // If both the condition and the source types are integer, we can generate
1790 // a CMOV to implement Select.
1791 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001792 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001793 DCHECK(locations->InAt(0).Equals(locations->Out()));
1794
1795 HInstruction* select_condition = select->GetCondition();
1796 Condition cond = kNotEqual;
1797
1798 // Figure out how to test the 'condition'.
1799 if (select_condition->IsCondition()) {
1800 HCondition* condition = select_condition->AsCondition();
1801 if (!condition->IsEmittedAtUseSite()) {
1802 // This was a previously materialized condition.
1803 // Can we use the existing condition code?
1804 if (AreEflagsSetFrom(condition, select)) {
1805 // Materialization was the previous instruction. Condition codes are right.
1806 cond = X86_64IntegerCondition(condition->GetCondition());
1807 } else {
1808 // No, we have to recreate the condition code.
1809 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1810 __ testl(cond_reg, cond_reg);
1811 }
1812 } else {
1813 GenerateCompareTest(condition);
1814 cond = X86_64IntegerCondition(condition->GetCondition());
1815 }
1816 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001817 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001818 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1819 __ testl(cond_reg, cond_reg);
1820 }
1821
1822 // If the condition is true, overwrite the output, which already contains false.
1823 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001824 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1825 if (value_true_loc.IsRegister()) {
1826 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1827 } else {
1828 __ cmov(cond,
1829 value_false,
1830 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1831 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001832 } else {
1833 NearLabel false_target;
1834 GenerateTestAndBranch<NearLabel>(select,
1835 /* condition_input_index */ 2,
1836 /* true_target */ nullptr,
1837 &false_target);
1838 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1839 __ Bind(&false_target);
1840 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001841}
1842
David Srbecky0cf44932015-12-09 14:09:59 +00001843void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1844 new (GetGraph()->GetArena()) LocationSummary(info);
1845}
1846
David Srbeckyd28f4a02016-03-14 17:14:24 +00001847void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1848 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001849}
1850
1851void CodeGeneratorX86_64::GenerateNop() {
1852 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001853}
1854
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001856 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001857 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001858 // Handle the long/FP comparisons made in instruction simplification.
1859 switch (cond->InputAt(0)->GetType()) {
1860 case Primitive::kPrimLong:
1861 locations->SetInAt(0, Location::RequiresRegister());
1862 locations->SetInAt(1, Location::Any());
1863 break;
1864 case Primitive::kPrimFloat:
1865 case Primitive::kPrimDouble:
1866 locations->SetInAt(0, Location::RequiresFpuRegister());
1867 locations->SetInAt(1, Location::Any());
1868 break;
1869 default:
1870 locations->SetInAt(0, Location::RequiresRegister());
1871 locations->SetInAt(1, Location::Any());
1872 break;
1873 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001874 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001875 locations->SetOut(Location::RequiresRegister());
1876 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001877}
1878
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001879void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001880 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001881 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001882 }
Mark Mendellc4701932015-04-10 13:18:51 -04001883
1884 LocationSummary* locations = cond->GetLocations();
1885 Location lhs = locations->InAt(0);
1886 Location rhs = locations->InAt(1);
1887 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001888 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001889
1890 switch (cond->InputAt(0)->GetType()) {
1891 default:
1892 // Integer case.
1893
1894 // Clear output register: setcc only sets the low byte.
1895 __ xorl(reg, reg);
1896
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001897 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001898 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001899 return;
1900 case Primitive::kPrimLong:
1901 // Clear output register: setcc only sets the low byte.
1902 __ xorl(reg, reg);
1903
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001904 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001905 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001906 return;
1907 case Primitive::kPrimFloat: {
1908 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1909 if (rhs.IsConstant()) {
1910 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1911 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1912 } else if (rhs.IsStackSlot()) {
1913 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1914 } else {
1915 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1916 }
1917 GenerateFPJumps(cond, &true_label, &false_label);
1918 break;
1919 }
1920 case Primitive::kPrimDouble: {
1921 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1922 if (rhs.IsConstant()) {
1923 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1924 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1925 } else if (rhs.IsDoubleStackSlot()) {
1926 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1927 } else {
1928 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1929 }
1930 GenerateFPJumps(cond, &true_label, &false_label);
1931 break;
1932 }
1933 }
1934
1935 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001936 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001937
Roland Levillain4fa13f62015-07-06 18:11:54 +01001938 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001939 __ Bind(&false_label);
1940 __ xorl(reg, reg);
1941 __ jmp(&done_label);
1942
Roland Levillain4fa13f62015-07-06 18:11:54 +01001943 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001944 __ Bind(&true_label);
1945 __ movl(reg, Immediate(1));
1946 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001955}
1956
1957void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001987}
1988
1989void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001991}
1992
1993void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001995}
1996
Aart Bike9f37602015-10-09 11:15:55 -07001997void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001999}
2000
2001void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002003}
2004
2005void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002007}
2008
2009void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
2021void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002022 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002023}
2024
2025void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002026 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002027}
2028
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002029void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002032 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002033 case Primitive::kPrimBoolean:
2034 case Primitive::kPrimByte:
2035 case Primitive::kPrimShort:
2036 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002037 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002038 case Primitive::kPrimLong: {
2039 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002040 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2042 break;
2043 }
2044 case Primitive::kPrimFloat:
2045 case Primitive::kPrimDouble: {
2046 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002047 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002048 locations->SetOut(Location::RequiresRegister());
2049 break;
2050 }
2051 default:
2052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002054}
2055
2056void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002057 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002059 Location left = locations->InAt(0);
2060 Location right = locations->InAt(1);
2061
Mark Mendell0c9497d2015-08-21 09:30:05 -04002062 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002063 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002064 Condition less_cond = kLess;
2065
Calin Juravleddb7df22014-11-25 20:56:51 +00002066 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002067 case Primitive::kPrimBoolean:
2068 case Primitive::kPrimByte:
2069 case Primitive::kPrimShort:
2070 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002071 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002072 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002073 break;
2074 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002075 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002076 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002077 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002078 }
2079 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002080 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2081 if (right.IsConstant()) {
2082 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2083 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2084 } else if (right.IsStackSlot()) {
2085 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2086 } else {
2087 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2088 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002089 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002090 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002091 break;
2092 }
2093 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002094 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2095 if (right.IsConstant()) {
2096 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2097 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2098 } else if (right.IsDoubleStackSlot()) {
2099 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2100 } else {
2101 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2102 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002103 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002104 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002105 break;
2106 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002107 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002108 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002109 }
Aart Bika19616e2016-02-01 18:57:58 -08002110
Calin Juravleddb7df22014-11-25 20:56:51 +00002111 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002112 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002113 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002114
Calin Juravle91debbc2014-11-26 19:01:09 +00002115 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002116 __ movl(out, Immediate(1));
2117 __ jmp(&done);
2118
2119 __ Bind(&less);
2120 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002121
2122 __ Bind(&done);
2123}
2124
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002126 LocationSummary* locations =
2127 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002128 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002129}
2130
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002131void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002132 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002133}
2134
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002135void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2136 LocationSummary* locations =
2137 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2138 locations->SetOut(Location::ConstantLocation(constant));
2139}
2140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002141void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002142 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002143}
2144
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002145void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002146 LocationSummary* locations =
2147 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002148 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002149}
2150
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002151void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002152 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002153}
2154
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002155void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2156 LocationSummary* locations =
2157 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2158 locations->SetOut(Location::ConstantLocation(constant));
2159}
2160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002161void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002162 // Will be generated at use site.
2163}
2164
2165void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2166 LocationSummary* locations =
2167 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2168 locations->SetOut(Location::ConstantLocation(constant));
2169}
2170
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002171void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2172 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002173 // Will be generated at use site.
2174}
2175
Igor Murashkind01745e2017-04-05 16:40:31 -07002176void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2177 constructor_fence->SetLocations(nullptr);
2178}
2179
2180void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2181 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2182 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2183}
2184
Calin Juravle27df7582015-04-17 19:12:31 +01002185void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2186 memory_barrier->SetLocations(nullptr);
2187}
2188
2189void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002190 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002191}
2192
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002193void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2194 ret->SetLocations(nullptr);
2195}
2196
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002197void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002198 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199}
2200
2201void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002202 LocationSummary* locations =
2203 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002204 switch (ret->InputAt(0)->GetType()) {
2205 case Primitive::kPrimBoolean:
2206 case Primitive::kPrimByte:
2207 case Primitive::kPrimChar:
2208 case Primitive::kPrimShort:
2209 case Primitive::kPrimInt:
2210 case Primitive::kPrimNot:
2211 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002212 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213 break;
2214
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002215 case Primitive::kPrimFloat:
2216 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002217 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002218 break;
2219
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002220 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002221 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002222 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002223}
2224
2225void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2226 if (kIsDebugBuild) {
2227 switch (ret->InputAt(0)->GetType()) {
2228 case Primitive::kPrimBoolean:
2229 case Primitive::kPrimByte:
2230 case Primitive::kPrimChar:
2231 case Primitive::kPrimShort:
2232 case Primitive::kPrimInt:
2233 case Primitive::kPrimNot:
2234 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002236 break;
2237
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 case Primitive::kPrimFloat:
2239 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002241 XMM0);
2242 break;
2243
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002244 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002246 }
2247 }
2248 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002249}
2250
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002251Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2252 switch (type) {
2253 case Primitive::kPrimBoolean:
2254 case Primitive::kPrimByte:
2255 case Primitive::kPrimChar:
2256 case Primitive::kPrimShort:
2257 case Primitive::kPrimInt:
2258 case Primitive::kPrimNot:
2259 case Primitive::kPrimLong:
2260 return Location::RegisterLocation(RAX);
2261
2262 case Primitive::kPrimVoid:
2263 return Location::NoLocation();
2264
2265 case Primitive::kPrimDouble:
2266 case Primitive::kPrimFloat:
2267 return Location::FpuRegisterLocation(XMM0);
2268 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002269
2270 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002271}
2272
2273Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2274 return Location::RegisterLocation(kMethodRegisterArgument);
2275}
2276
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002277Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002278 switch (type) {
2279 case Primitive::kPrimBoolean:
2280 case Primitive::kPrimByte:
2281 case Primitive::kPrimChar:
2282 case Primitive::kPrimShort:
2283 case Primitive::kPrimInt:
2284 case Primitive::kPrimNot: {
2285 uint32_t index = gp_index_++;
2286 stack_index_++;
2287 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002288 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002289 } else {
2290 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2291 }
2292 }
2293
2294 case Primitive::kPrimLong: {
2295 uint32_t index = gp_index_;
2296 stack_index_ += 2;
2297 if (index < calling_convention.GetNumberOfRegisters()) {
2298 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002299 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002300 } else {
2301 gp_index_ += 2;
2302 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2303 }
2304 }
2305
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002307 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002308 stack_index_++;
2309 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002310 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 } else {
2312 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2313 }
2314 }
2315
2316 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002317 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002318 stack_index_ += 2;
2319 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002321 } else {
2322 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2323 }
2324 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002325
2326 case Primitive::kPrimVoid:
2327 LOG(FATAL) << "Unexpected parameter type " << type;
2328 break;
2329 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002330 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002331}
2332
Calin Juravle175dc732015-08-25 15:42:32 +01002333void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2334 // The trampoline uses the same calling convention as dex calling conventions,
2335 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2336 // the method_idx.
2337 HandleInvoke(invoke);
2338}
2339
2340void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2341 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2342}
2343
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002344void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002345 // Explicit clinit checks triggered by static invokes must have been pruned by
2346 // art::PrepareForRegisterAllocation.
2347 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002348
Mark Mendellfb8d2792015-03-31 22:16:59 -04002349 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002350 if (intrinsic.TryDispatch(invoke)) {
2351 return;
2352 }
2353
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002354 HandleInvoke(invoke);
2355}
2356
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002357static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2358 if (invoke->GetLocations()->Intrinsified()) {
2359 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2360 intrinsic.Dispatch(invoke);
2361 return true;
2362 }
2363 return false;
2364}
2365
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002366void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002367 // Explicit clinit checks triggered by static invokes must have been pruned by
2368 // art::PrepareForRegisterAllocation.
2369 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002370
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002371 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2372 return;
2373 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002374
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002375 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002376 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002377 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002378}
2379
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002380void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002381 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002382 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002383}
2384
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002386 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002387 if (intrinsic.TryDispatch(invoke)) {
2388 return;
2389 }
2390
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002391 HandleInvoke(invoke);
2392}
2393
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002394void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002395 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2396 return;
2397 }
2398
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002399 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002400 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002401}
2402
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2404 HandleInvoke(invoke);
2405 // Add the hidden argument.
2406 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2407}
2408
2409void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2410 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002411 LocationSummary* locations = invoke->GetLocations();
2412 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2413 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002414 Location receiver = locations->InAt(0);
2415 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2416
Roland Levillain0d5a2812015-11-13 10:07:31 +00002417 // Set the hidden argument. This is safe to do this here, as RAX
2418 // won't be modified thereafter, before the `call` instruction.
2419 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002420 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002421
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002422 if (receiver.IsStackSlot()) {
2423 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002424 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425 __ movl(temp, Address(temp, class_offset));
2426 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002427 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002428 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002429 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002430 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002431 // Instead of simply (possibly) unpoisoning `temp` here, we should
2432 // emit a read barrier for the previous class reference load.
2433 // However this is not required in practice, as this is an
2434 // intermediate/temporary reference and because the current
2435 // concurrent copying collector keeps the from-space memory
2436 // intact/accessible until the end of the marking phase (the
2437 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002438 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002439 // temp = temp->GetAddressOfIMT()
2440 __ movq(temp,
2441 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2442 // temp = temp->GetImtEntryAt(method_offset);
2443 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002444 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002445 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002446 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002447 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002448 __ call(Address(
2449 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450
2451 DCHECK(!codegen_->IsLeafMethod());
2452 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2453}
2454
Orion Hodsonac141392017-01-13 11:53:47 +00002455void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2456 HandleInvoke(invoke);
2457}
2458
2459void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2460 codegen_->GenerateInvokePolymorphicCall(invoke);
2461}
2462
Roland Levillain88cb1752014-10-20 16:36:47 +01002463void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2464 LocationSummary* locations =
2465 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2466 switch (neg->GetResultType()) {
2467 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002468 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002469 locations->SetInAt(0, Location::RequiresRegister());
2470 locations->SetOut(Location::SameAsFirstInput());
2471 break;
2472
Roland Levillain88cb1752014-10-20 16:36:47 +01002473 case Primitive::kPrimFloat:
2474 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002475 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002476 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002477 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002478 break;
2479
2480 default:
2481 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2482 }
2483}
2484
2485void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2486 LocationSummary* locations = neg->GetLocations();
2487 Location out = locations->Out();
2488 Location in = locations->InAt(0);
2489 switch (neg->GetResultType()) {
2490 case Primitive::kPrimInt:
2491 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002492 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002493 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002494 break;
2495
2496 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002497 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002498 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002500 break;
2501
Roland Levillain5368c212014-11-27 15:03:41 +00002502 case Primitive::kPrimFloat: {
2503 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002504 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002505 // Implement float negation with an exclusive or with value
2506 // 0x80000000 (mask for bit 31, representing the sign of a
2507 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002508 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002510 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002511 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002512
Roland Levillain5368c212014-11-27 15:03:41 +00002513 case Primitive::kPrimDouble: {
2514 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002515 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002516 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002517 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002518 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002519 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002520 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002521 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002522 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002523
2524 default:
2525 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2526 }
2527}
2528
Roland Levillaindff1f282014-11-05 14:15:05 +00002529void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2530 LocationSummary* locations =
2531 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2532 Primitive::Type result_type = conversion->GetResultType();
2533 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002534 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002535
David Brazdilb2bd1c52015-03-25 11:17:37 +00002536 // The Java language does not allow treating boolean as an integral type but
2537 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002538
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002540 case Primitive::kPrimByte:
2541 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002542 case Primitive::kPrimLong:
2543 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002544 case Primitive::kPrimBoolean:
2545 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002546 case Primitive::kPrimShort:
2547 case Primitive::kPrimInt:
2548 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002549 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002550 locations->SetInAt(0, Location::Any());
2551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2552 break;
2553
2554 default:
2555 LOG(FATAL) << "Unexpected type conversion from " << input_type
2556 << " to " << result_type;
2557 }
2558 break;
2559
Roland Levillain01a8d712014-11-14 16:27:39 +00002560 case Primitive::kPrimShort:
2561 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002562 case Primitive::kPrimLong:
2563 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002564 case Primitive::kPrimBoolean:
2565 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002566 case Primitive::kPrimByte:
2567 case Primitive::kPrimInt:
2568 case Primitive::kPrimChar:
2569 // Processing a Dex `int-to-short' instruction.
2570 locations->SetInAt(0, Location::Any());
2571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2572 break;
2573
2574 default:
2575 LOG(FATAL) << "Unexpected type conversion from " << input_type
2576 << " to " << result_type;
2577 }
2578 break;
2579
Roland Levillain946e1432014-11-11 17:35:19 +00002580 case Primitive::kPrimInt:
2581 switch (input_type) {
2582 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002583 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002584 locations->SetInAt(0, Location::Any());
2585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2586 break;
2587
2588 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002589 // Processing a Dex `float-to-int' instruction.
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002592 break;
2593
Roland Levillain946e1432014-11-11 17:35:19 +00002594 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002595 // Processing a Dex `double-to-int' instruction.
2596 locations->SetInAt(0, Location::RequiresFpuRegister());
2597 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002598 break;
2599
2600 default:
2601 LOG(FATAL) << "Unexpected type conversion from " << input_type
2602 << " to " << result_type;
2603 }
2604 break;
2605
Roland Levillaindff1f282014-11-05 14:15:05 +00002606 case Primitive::kPrimLong:
2607 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002608 case Primitive::kPrimBoolean:
2609 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002610 case Primitive::kPrimByte:
2611 case Primitive::kPrimShort:
2612 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002613 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002614 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002615 // TODO: We would benefit from a (to-be-implemented)
2616 // Location::RegisterOrStackSlot requirement for this input.
2617 locations->SetInAt(0, Location::RequiresRegister());
2618 locations->SetOut(Location::RequiresRegister());
2619 break;
2620
2621 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002622 // Processing a Dex `float-to-long' instruction.
2623 locations->SetInAt(0, Location::RequiresFpuRegister());
2624 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002625 break;
2626
Roland Levillaindff1f282014-11-05 14:15:05 +00002627 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002628 // Processing a Dex `double-to-long' instruction.
2629 locations->SetInAt(0, Location::RequiresFpuRegister());
2630 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 break;
2632
2633 default:
2634 LOG(FATAL) << "Unexpected type conversion from " << input_type
2635 << " to " << result_type;
2636 }
2637 break;
2638
Roland Levillain981e4542014-11-14 11:47:14 +00002639 case Primitive::kPrimChar:
2640 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002641 case Primitive::kPrimLong:
2642 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002643 case Primitive::kPrimBoolean:
2644 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002645 case Primitive::kPrimByte:
2646 case Primitive::kPrimShort:
2647 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002648 // Processing a Dex `int-to-char' instruction.
2649 locations->SetInAt(0, Location::Any());
2650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2651 break;
2652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 }
2657 break;
2658
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002660 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002661 case Primitive::kPrimBoolean:
2662 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002663 case Primitive::kPrimByte:
2664 case Primitive::kPrimShort:
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimChar:
2667 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002668 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002669 locations->SetOut(Location::RequiresFpuRegister());
2670 break;
2671
2672 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002673 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002674 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002675 locations->SetOut(Location::RequiresFpuRegister());
2676 break;
2677
Roland Levillaincff13742014-11-17 14:32:17 +00002678 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002679 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002680 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002681 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 };
2688 break;
2689
Roland Levillaindff1f282014-11-05 14:15:05 +00002690 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002691 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002692 case Primitive::kPrimBoolean:
2693 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002694 case Primitive::kPrimByte:
2695 case Primitive::kPrimShort:
2696 case Primitive::kPrimInt:
2697 case Primitive::kPrimChar:
2698 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002699 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002700 locations->SetOut(Location::RequiresFpuRegister());
2701 break;
2702
2703 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002704 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002705 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002706 locations->SetOut(Location::RequiresFpuRegister());
2707 break;
2708
Roland Levillaincff13742014-11-17 14:32:17 +00002709 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002710 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002711 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002713 break;
2714
2715 default:
2716 LOG(FATAL) << "Unexpected type conversion from " << input_type
2717 << " to " << result_type;
2718 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002719 break;
2720
2721 default:
2722 LOG(FATAL) << "Unexpected type conversion from " << input_type
2723 << " to " << result_type;
2724 }
2725}
2726
2727void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2728 LocationSummary* locations = conversion->GetLocations();
2729 Location out = locations->Out();
2730 Location in = locations->InAt(0);
2731 Primitive::Type result_type = conversion->GetResultType();
2732 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002733 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002734 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002735 case Primitive::kPrimByte:
2736 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002737 case Primitive::kPrimLong:
2738 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002739 case Primitive::kPrimBoolean:
2740 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002741 case Primitive::kPrimShort:
2742 case Primitive::kPrimInt:
2743 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002744 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002745 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002746 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002747 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002749 Address(CpuRegister(RSP), in.GetStackIndex()));
2750 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002751 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002752 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002753 }
2754 break;
2755
2756 default:
2757 LOG(FATAL) << "Unexpected type conversion from " << input_type
2758 << " to " << result_type;
2759 }
2760 break;
2761
Roland Levillain01a8d712014-11-14 16:27:39 +00002762 case Primitive::kPrimShort:
2763 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002764 case Primitive::kPrimLong:
2765 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002766 case Primitive::kPrimBoolean:
2767 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002768 case Primitive::kPrimByte:
2769 case Primitive::kPrimInt:
2770 case Primitive::kPrimChar:
2771 // Processing a Dex `int-to-short' instruction.
2772 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002774 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002776 Address(CpuRegister(RSP), in.GetStackIndex()));
2777 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002779 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002780 }
2781 break;
2782
2783 default:
2784 LOG(FATAL) << "Unexpected type conversion from " << input_type
2785 << " to " << result_type;
2786 }
2787 break;
2788
Roland Levillain946e1432014-11-11 17:35:19 +00002789 case Primitive::kPrimInt:
2790 switch (input_type) {
2791 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002792 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002793 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002794 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002795 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002797 Address(CpuRegister(RSP), in.GetStackIndex()));
2798 } else {
2799 DCHECK(in.IsConstant());
2800 DCHECK(in.GetConstant()->IsLongConstant());
2801 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002803 }
2804 break;
2805
Roland Levillain3f8f9362014-12-02 17:45:01 +00002806 case Primitive::kPrimFloat: {
2807 // Processing a Dex `float-to-int' instruction.
2808 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2809 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002810 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002811
2812 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002813 // if input >= (float)INT_MAX goto done
2814 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002815 __ j(kAboveEqual, &done);
2816 // if input == NaN goto nan
2817 __ j(kUnordered, &nan);
2818 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002819 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002820 __ jmp(&done);
2821 __ Bind(&nan);
2822 // output = 0
2823 __ xorl(output, output);
2824 __ Bind(&done);
2825 break;
2826 }
2827
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002828 case Primitive::kPrimDouble: {
2829 // Processing a Dex `double-to-int' instruction.
2830 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2831 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002832 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002833
2834 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002835 // if input >= (double)INT_MAX goto done
2836 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002837 __ j(kAboveEqual, &done);
2838 // if input == NaN goto nan
2839 __ j(kUnordered, &nan);
2840 // output = double-to-int-truncate(input)
2841 __ cvttsd2si(output, input);
2842 __ jmp(&done);
2843 __ Bind(&nan);
2844 // output = 0
2845 __ xorl(output, output);
2846 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002847 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002848 }
Roland Levillain946e1432014-11-11 17:35:19 +00002849
2850 default:
2851 LOG(FATAL) << "Unexpected type conversion from " << input_type
2852 << " to " << result_type;
2853 }
2854 break;
2855
Roland Levillaindff1f282014-11-05 14:15:05 +00002856 case Primitive::kPrimLong:
2857 switch (input_type) {
2858 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002859 case Primitive::kPrimBoolean:
2860 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002861 case Primitive::kPrimByte:
2862 case Primitive::kPrimShort:
2863 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002864 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002865 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002866 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002867 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002868 break;
2869
Roland Levillain624279f2014-12-04 11:54:28 +00002870 case Primitive::kPrimFloat: {
2871 // Processing a Dex `float-to-long' instruction.
2872 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2873 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002874 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002875
Mark Mendell92e83bf2015-05-07 11:25:03 -04002876 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002877 // if input >= (float)LONG_MAX goto done
2878 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002879 __ j(kAboveEqual, &done);
2880 // if input == NaN goto nan
2881 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002882 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002883 __ cvttss2si(output, input, true);
2884 __ jmp(&done);
2885 __ Bind(&nan);
2886 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002887 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002888 __ Bind(&done);
2889 break;
2890 }
2891
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002892 case Primitive::kPrimDouble: {
2893 // Processing a Dex `double-to-long' instruction.
2894 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2895 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002896 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002897
Mark Mendell92e83bf2015-05-07 11:25:03 -04002898 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002899 // if input >= (double)LONG_MAX goto done
2900 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002901 __ j(kAboveEqual, &done);
2902 // if input == NaN goto nan
2903 __ j(kUnordered, &nan);
2904 // output = double-to-long-truncate(input)
2905 __ cvttsd2si(output, input, true);
2906 __ jmp(&done);
2907 __ Bind(&nan);
2908 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002909 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002910 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002911 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002912 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002913
2914 default:
2915 LOG(FATAL) << "Unexpected type conversion from " << input_type
2916 << " to " << result_type;
2917 }
2918 break;
2919
Roland Levillain981e4542014-11-14 11:47:14 +00002920 case Primitive::kPrimChar:
2921 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002922 case Primitive::kPrimLong:
2923 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002924 case Primitive::kPrimBoolean:
2925 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002926 case Primitive::kPrimByte:
2927 case Primitive::kPrimShort:
2928 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002929 // Processing a Dex `int-to-char' instruction.
2930 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002931 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002932 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002934 Address(CpuRegister(RSP), in.GetStackIndex()));
2935 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002936 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002937 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002938 }
2939 break;
2940
2941 default:
2942 LOG(FATAL) << "Unexpected type conversion from " << input_type
2943 << " to " << result_type;
2944 }
2945 break;
2946
Roland Levillaindff1f282014-11-05 14:15:05 +00002947 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002948 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002949 case Primitive::kPrimBoolean:
2950 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002951 case Primitive::kPrimByte:
2952 case Primitive::kPrimShort:
2953 case Primitive::kPrimInt:
2954 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002955 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002956 if (in.IsRegister()) {
2957 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2958 } else if (in.IsConstant()) {
2959 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2960 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002961 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002962 } else {
2963 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2964 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2965 }
Roland Levillaincff13742014-11-17 14:32:17 +00002966 break;
2967
2968 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002969 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002970 if (in.IsRegister()) {
2971 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2972 } else if (in.IsConstant()) {
2973 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2974 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002975 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002976 } else {
2977 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2978 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2979 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002980 break;
2981
Roland Levillaincff13742014-11-17 14:32:17 +00002982 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002983 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002984 if (in.IsFpuRegister()) {
2985 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2986 } else if (in.IsConstant()) {
2987 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2988 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002989 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002990 } else {
2991 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2992 Address(CpuRegister(RSP), in.GetStackIndex()));
2993 }
Roland Levillaincff13742014-11-17 14:32:17 +00002994 break;
2995
2996 default:
2997 LOG(FATAL) << "Unexpected type conversion from " << input_type
2998 << " to " << result_type;
2999 };
3000 break;
3001
Roland Levillaindff1f282014-11-05 14:15:05 +00003002 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003003 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003004 case Primitive::kPrimBoolean:
3005 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003006 case Primitive::kPrimByte:
3007 case Primitive::kPrimShort:
3008 case Primitive::kPrimInt:
3009 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003010 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003011 if (in.IsRegister()) {
3012 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3013 } else if (in.IsConstant()) {
3014 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3015 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003016 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003017 } else {
3018 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3019 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3020 }
Roland Levillaincff13742014-11-17 14:32:17 +00003021 break;
3022
3023 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003024 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003025 if (in.IsRegister()) {
3026 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3027 } else if (in.IsConstant()) {
3028 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3029 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003030 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003031 } else {
3032 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3033 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3034 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003035 break;
3036
Roland Levillaincff13742014-11-17 14:32:17 +00003037 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003038 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003039 if (in.IsFpuRegister()) {
3040 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3041 } else if (in.IsConstant()) {
3042 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3043 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003044 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003045 } else {
3046 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3047 Address(CpuRegister(RSP), in.GetStackIndex()));
3048 }
Roland Levillaincff13742014-11-17 14:32:17 +00003049 break;
3050
3051 default:
3052 LOG(FATAL) << "Unexpected type conversion from " << input_type
3053 << " to " << result_type;
3054 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003055 break;
3056
3057 default:
3058 LOG(FATAL) << "Unexpected type conversion from " << input_type
3059 << " to " << result_type;
3060 }
3061}
3062
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003063void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003064 LocationSummary* locations =
3065 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003066 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003067 case Primitive::kPrimInt: {
3068 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003069 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003071 break;
3072 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003073
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003075 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003076 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003077 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003079 break;
3080 }
3081
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003082 case Primitive::kPrimDouble:
3083 case Primitive::kPrimFloat: {
3084 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003085 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003086 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003087 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003088 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089
3090 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003091 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003092 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093}
3094
3095void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3096 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003097 Location first = locations->InAt(0);
3098 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003099 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003100
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003101 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003102 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003103 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003104 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3105 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003106 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3107 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003108 } else {
3109 __ leal(out.AsRegister<CpuRegister>(), Address(
3110 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3111 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003112 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003113 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3114 __ addl(out.AsRegister<CpuRegister>(),
3115 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3116 } else {
3117 __ leal(out.AsRegister<CpuRegister>(), Address(
3118 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3119 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003120 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003121 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003122 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003123 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003124 break;
3125 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003126
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003127 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003128 if (second.IsRegister()) {
3129 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3130 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003131 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3132 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003133 } else {
3134 __ leaq(out.AsRegister<CpuRegister>(), Address(
3135 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3136 }
3137 } else {
3138 DCHECK(second.IsConstant());
3139 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3140 int32_t int32_value = Low32Bits(value);
3141 DCHECK_EQ(int32_value, value);
3142 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3143 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3144 } else {
3145 __ leaq(out.AsRegister<CpuRegister>(), Address(
3146 first.AsRegister<CpuRegister>(), int32_value));
3147 }
3148 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003149 break;
3150 }
3151
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003152 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003153 if (second.IsFpuRegister()) {
3154 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3155 } else if (second.IsConstant()) {
3156 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003157 codegen_->LiteralFloatAddress(
3158 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003159 } else {
3160 DCHECK(second.IsStackSlot());
3161 __ addss(first.AsFpuRegister<XmmRegister>(),
3162 Address(CpuRegister(RSP), second.GetStackIndex()));
3163 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003164 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003165 }
3166
3167 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003168 if (second.IsFpuRegister()) {
3169 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3170 } else if (second.IsConstant()) {
3171 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003172 codegen_->LiteralDoubleAddress(
3173 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003174 } else {
3175 DCHECK(second.IsDoubleStackSlot());
3176 __ addsd(first.AsFpuRegister<XmmRegister>(),
3177 Address(CpuRegister(RSP), second.GetStackIndex()));
3178 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003179 break;
3180 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003181
3182 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003183 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003184 }
3185}
3186
3187void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003188 LocationSummary* locations =
3189 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003190 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003191 case Primitive::kPrimInt: {
3192 locations->SetInAt(0, Location::RequiresRegister());
3193 locations->SetInAt(1, Location::Any());
3194 locations->SetOut(Location::SameAsFirstInput());
3195 break;
3196 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003197 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003198 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003199 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003200 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003201 break;
3202 }
Calin Juravle11351682014-10-23 15:38:15 +01003203 case Primitive::kPrimFloat:
3204 case Primitive::kPrimDouble: {
3205 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003206 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003207 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003208 break;
Calin Juravle11351682014-10-23 15:38:15 +01003209 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003210 default:
Calin Juravle11351682014-10-23 15:38:15 +01003211 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003212 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003213}
3214
3215void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3216 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003217 Location first = locations->InAt(0);
3218 Location second = locations->InAt(1);
3219 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003220 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003221 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003222 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003224 } else if (second.IsConstant()) {
3225 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003227 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003229 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003230 break;
3231 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003232 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003233 if (second.IsConstant()) {
3234 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3235 DCHECK(IsInt<32>(value));
3236 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3237 } else {
3238 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3239 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003240 break;
3241 }
3242
Calin Juravle11351682014-10-23 15:38:15 +01003243 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003244 if (second.IsFpuRegister()) {
3245 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3246 } else if (second.IsConstant()) {
3247 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003248 codegen_->LiteralFloatAddress(
3249 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003250 } else {
3251 DCHECK(second.IsStackSlot());
3252 __ subss(first.AsFpuRegister<XmmRegister>(),
3253 Address(CpuRegister(RSP), second.GetStackIndex()));
3254 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003255 break;
Calin Juravle11351682014-10-23 15:38:15 +01003256 }
3257
3258 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003259 if (second.IsFpuRegister()) {
3260 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3261 } else if (second.IsConstant()) {
3262 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003263 codegen_->LiteralDoubleAddress(
3264 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003265 } else {
3266 DCHECK(second.IsDoubleStackSlot());
3267 __ subsd(first.AsFpuRegister<XmmRegister>(),
3268 Address(CpuRegister(RSP), second.GetStackIndex()));
3269 }
Calin Juravle11351682014-10-23 15:38:15 +01003270 break;
3271 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003272
3273 default:
Calin Juravle11351682014-10-23 15:38:15 +01003274 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003275 }
3276}
3277
Calin Juravle34bacdf2014-10-07 20:23:36 +01003278void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3279 LocationSummary* locations =
3280 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3281 switch (mul->GetResultType()) {
3282 case Primitive::kPrimInt: {
3283 locations->SetInAt(0, Location::RequiresRegister());
3284 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003285 if (mul->InputAt(1)->IsIntConstant()) {
3286 // Can use 3 operand multiply.
3287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3288 } else {
3289 locations->SetOut(Location::SameAsFirstInput());
3290 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003291 break;
3292 }
3293 case Primitive::kPrimLong: {
3294 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003295 locations->SetInAt(1, Location::Any());
3296 if (mul->InputAt(1)->IsLongConstant() &&
3297 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003298 // Can use 3 operand multiply.
3299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3300 } else {
3301 locations->SetOut(Location::SameAsFirstInput());
3302 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 break;
3304 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003305 case Primitive::kPrimFloat:
3306 case Primitive::kPrimDouble: {
3307 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003308 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003309 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003311 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003312
3313 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003314 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003315 }
3316}
3317
3318void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3319 LocationSummary* locations = mul->GetLocations();
3320 Location first = locations->InAt(0);
3321 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003322 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003323 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003324 case Primitive::kPrimInt:
3325 // The constant may have ended up in a register, so test explicitly to avoid
3326 // problems where the output may not be the same as the first operand.
3327 if (mul->InputAt(1)->IsIntConstant()) {
3328 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3329 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3330 } else if (second.IsRegister()) {
3331 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003332 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003333 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003334 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003335 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003336 __ imull(first.AsRegister<CpuRegister>(),
3337 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003338 }
3339 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003340 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003341 // The constant may have ended up in a register, so test explicitly to avoid
3342 // problems where the output may not be the same as the first operand.
3343 if (mul->InputAt(1)->IsLongConstant()) {
3344 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3345 if (IsInt<32>(value)) {
3346 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3347 Immediate(static_cast<int32_t>(value)));
3348 } else {
3349 // Have to use the constant area.
3350 DCHECK(first.Equals(out));
3351 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3352 }
3353 } else if (second.IsRegister()) {
3354 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003355 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003356 } else {
3357 DCHECK(second.IsDoubleStackSlot());
3358 DCHECK(first.Equals(out));
3359 __ imulq(first.AsRegister<CpuRegister>(),
3360 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003361 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003362 break;
3363 }
3364
Calin Juravleb5bfa962014-10-21 18:02:24 +01003365 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003366 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003367 if (second.IsFpuRegister()) {
3368 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3369 } else if (second.IsConstant()) {
3370 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003371 codegen_->LiteralFloatAddress(
3372 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003373 } else {
3374 DCHECK(second.IsStackSlot());
3375 __ mulss(first.AsFpuRegister<XmmRegister>(),
3376 Address(CpuRegister(RSP), second.GetStackIndex()));
3377 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003378 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003379 }
3380
3381 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003382 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003383 if (second.IsFpuRegister()) {
3384 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3385 } else if (second.IsConstant()) {
3386 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003387 codegen_->LiteralDoubleAddress(
3388 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003389 } else {
3390 DCHECK(second.IsDoubleStackSlot());
3391 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3392 Address(CpuRegister(RSP), second.GetStackIndex()));
3393 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003394 break;
3395 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003396
3397 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003398 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003399 }
3400}
3401
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003402void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3403 uint32_t stack_adjustment, bool is_float) {
3404 if (source.IsStackSlot()) {
3405 DCHECK(is_float);
3406 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3407 } else if (source.IsDoubleStackSlot()) {
3408 DCHECK(!is_float);
3409 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3410 } else {
3411 // Write the value to the temporary location on the stack and load to FP stack.
3412 if (is_float) {
3413 Location stack_temp = Location::StackSlot(temp_offset);
3414 codegen_->Move(stack_temp, source);
3415 __ flds(Address(CpuRegister(RSP), temp_offset));
3416 } else {
3417 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3418 codegen_->Move(stack_temp, source);
3419 __ fldl(Address(CpuRegister(RSP), temp_offset));
3420 }
3421 }
3422}
3423
3424void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3425 Primitive::Type type = rem->GetResultType();
3426 bool is_float = type == Primitive::kPrimFloat;
3427 size_t elem_size = Primitive::ComponentSize(type);
3428 LocationSummary* locations = rem->GetLocations();
3429 Location first = locations->InAt(0);
3430 Location second = locations->InAt(1);
3431 Location out = locations->Out();
3432
3433 // Create stack space for 2 elements.
3434 // TODO: enhance register allocator to ask for stack temporaries.
3435 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3436
3437 // Load the values to the FP stack in reverse order, using temporaries if needed.
3438 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3439 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3440
3441 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003442 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003443 __ Bind(&retry);
3444 __ fprem();
3445
3446 // Move FP status to AX.
3447 __ fstsw();
3448
3449 // And see if the argument reduction is complete. This is signaled by the
3450 // C2 FPU flag bit set to 0.
3451 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3452 __ j(kNotEqual, &retry);
3453
3454 // We have settled on the final value. Retrieve it into an XMM register.
3455 // Store FP top of stack to real stack.
3456 if (is_float) {
3457 __ fsts(Address(CpuRegister(RSP), 0));
3458 } else {
3459 __ fstl(Address(CpuRegister(RSP), 0));
3460 }
3461
3462 // Pop the 2 items from the FP stack.
3463 __ fucompp();
3464
3465 // Load the value from the stack into an XMM register.
3466 DCHECK(out.IsFpuRegister()) << out;
3467 if (is_float) {
3468 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3469 } else {
3470 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3471 }
3472
3473 // And remove the temporary stack space we allocated.
3474 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3475}
3476
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003477void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3478 DCHECK(instruction->IsDiv() || instruction->IsRem());
3479
3480 LocationSummary* locations = instruction->GetLocations();
3481 Location second = locations->InAt(1);
3482 DCHECK(second.IsConstant());
3483
3484 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3485 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487
3488 DCHECK(imm == 1 || imm == -1);
3489
3490 switch (instruction->GetResultType()) {
3491 case Primitive::kPrimInt: {
3492 if (instruction->IsRem()) {
3493 __ xorl(output_register, output_register);
3494 } else {
3495 __ movl(output_register, input_register);
3496 if (imm == -1) {
3497 __ negl(output_register);
3498 }
3499 }
3500 break;
3501 }
3502
3503 case Primitive::kPrimLong: {
3504 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003505 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 } else {
3507 __ movq(output_register, input_register);
3508 if (imm == -1) {
3509 __ negq(output_register);
3510 }
3511 }
3512 break;
3513 }
3514
3515 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003516 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 }
3518}
3519
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003520void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 LocationSummary* locations = instruction->GetLocations();
3522 Location second = locations->InAt(1);
3523
3524 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3525 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3526
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003527 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003528 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3529 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530
3531 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3532
3533 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003534 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003535 __ testl(numerator, numerator);
3536 __ cmov(kGreaterEqual, tmp, numerator);
3537 int shift = CTZ(imm);
3538 __ sarl(tmp, Immediate(shift));
3539
3540 if (imm < 0) {
3541 __ negl(tmp);
3542 }
3543
3544 __ movl(output_register, tmp);
3545 } else {
3546 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3547 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3548
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003549 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550 __ addq(rdx, numerator);
3551 __ testq(numerator, numerator);
3552 __ cmov(kGreaterEqual, rdx, numerator);
3553 int shift = CTZ(imm);
3554 __ sarq(rdx, Immediate(shift));
3555
3556 if (imm < 0) {
3557 __ negq(rdx);
3558 }
3559
3560 __ movq(output_register, rdx);
3561 }
3562}
3563
3564void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3565 DCHECK(instruction->IsDiv() || instruction->IsRem());
3566
3567 LocationSummary* locations = instruction->GetLocations();
3568 Location second = locations->InAt(1);
3569
3570 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3571 : locations->GetTemp(0).AsRegister<CpuRegister>();
3572 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3573 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3574 : locations->Out().AsRegister<CpuRegister>();
3575 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3576
3577 DCHECK_EQ(RAX, eax.AsRegister());
3578 DCHECK_EQ(RDX, edx.AsRegister());
3579 if (instruction->IsDiv()) {
3580 DCHECK_EQ(RAX, out.AsRegister());
3581 } else {
3582 DCHECK_EQ(RDX, out.AsRegister());
3583 }
3584
3585 int64_t magic;
3586 int shift;
3587
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003588 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589 if (instruction->GetResultType() == Primitive::kPrimInt) {
3590 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3591
3592 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3593
3594 __ movl(numerator, eax);
3595
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003596 __ movl(eax, Immediate(magic));
3597 __ imull(numerator);
3598
3599 if (imm > 0 && magic < 0) {
3600 __ addl(edx, numerator);
3601 } else if (imm < 0 && magic > 0) {
3602 __ subl(edx, numerator);
3603 }
3604
3605 if (shift != 0) {
3606 __ sarl(edx, Immediate(shift));
3607 }
3608
3609 __ movl(eax, edx);
3610 __ shrl(edx, Immediate(31));
3611 __ addl(edx, eax);
3612
3613 if (instruction->IsRem()) {
3614 __ movl(eax, numerator);
3615 __ imull(edx, Immediate(imm));
3616 __ subl(eax, edx);
3617 __ movl(edx, eax);
3618 } else {
3619 __ movl(eax, edx);
3620 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003621 } else {
3622 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3623
3624 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3625
3626 CpuRegister rax = eax;
3627 CpuRegister rdx = edx;
3628
3629 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3630
3631 // Save the numerator.
3632 __ movq(numerator, rax);
3633
3634 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003635 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003636
3637 // RDX:RAX = magic * numerator
3638 __ imulq(numerator);
3639
3640 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003641 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003642 __ addq(rdx, numerator);
3643 } else if (imm < 0 && magic > 0) {
3644 // RDX -= numerator
3645 __ subq(rdx, numerator);
3646 }
3647
3648 // Shift if needed.
3649 if (shift != 0) {
3650 __ sarq(rdx, Immediate(shift));
3651 }
3652
3653 // RDX += 1 if RDX < 0
3654 __ movq(rax, rdx);
3655 __ shrq(rdx, Immediate(63));
3656 __ addq(rdx, rax);
3657
3658 if (instruction->IsRem()) {
3659 __ movq(rax, numerator);
3660
3661 if (IsInt<32>(imm)) {
3662 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3663 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003664 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003665 }
3666
3667 __ subq(rax, rdx);
3668 __ movq(rdx, rax);
3669 } else {
3670 __ movq(rax, rdx);
3671 }
3672 }
3673}
3674
Calin Juravlebacfec32014-11-14 15:54:36 +00003675void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3676 DCHECK(instruction->IsDiv() || instruction->IsRem());
3677 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003678 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravlebacfec32014-11-14 15:54:36 +00003679
3680 bool is_div = instruction->IsDiv();
3681 LocationSummary* locations = instruction->GetLocations();
3682
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003683 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3684 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003685
Roland Levillain271ab9c2014-11-27 15:23:57 +00003686 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003687 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003688
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003689 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003690 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003691
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003692 if (imm == 0) {
3693 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3694 } else if (imm == 1 || imm == -1) {
3695 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003696 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003697 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003698 } else {
3699 DCHECK(imm <= -2 || imm >= 2);
3700 GenerateDivRemWithAnyConstant(instruction);
3701 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003702 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003703 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003704 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003705 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003706 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003707
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003708 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3709 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3710 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3711 // so it's safe to just use negl instead of more complex comparisons.
3712 if (type == Primitive::kPrimInt) {
3713 __ cmpl(second_reg, Immediate(-1));
3714 __ j(kEqual, slow_path->GetEntryLabel());
3715 // edx:eax <- sign-extended of eax
3716 __ cdq();
3717 // eax = quotient, edx = remainder
3718 __ idivl(second_reg);
3719 } else {
3720 __ cmpq(second_reg, Immediate(-1));
3721 __ j(kEqual, slow_path->GetEntryLabel());
3722 // rdx:rax <- sign-extended of rax
3723 __ cqo();
3724 // rax = quotient, rdx = remainder
3725 __ idivq(second_reg);
3726 }
3727 __ Bind(slow_path->GetExitLabel());
3728 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003729}
3730
Calin Juravle7c4954d2014-10-28 16:57:40 +00003731void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3732 LocationSummary* locations =
3733 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3734 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003735 case Primitive::kPrimInt:
3736 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003737 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003738 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003739 locations->SetOut(Location::SameAsFirstInput());
3740 // Intel uses edx:eax as the dividend.
3741 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003742 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3743 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3744 // output and request another temp.
3745 if (div->InputAt(1)->IsConstant()) {
3746 locations->AddTemp(Location::RequiresRegister());
3747 }
Calin Juravled0d48522014-11-04 16:40:20 +00003748 break;
3749 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003750
Calin Juravle7c4954d2014-10-28 16:57:40 +00003751 case Primitive::kPrimFloat:
3752 case Primitive::kPrimDouble: {
3753 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003754 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003755 locations->SetOut(Location::SameAsFirstInput());
3756 break;
3757 }
3758
3759 default:
3760 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3761 }
3762}
3763
3764void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3765 LocationSummary* locations = div->GetLocations();
3766 Location first = locations->InAt(0);
3767 Location second = locations->InAt(1);
3768 DCHECK(first.Equals(locations->Out()));
3769
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003770 Primitive::Type type = div->GetResultType();
3771 switch (type) {
3772 case Primitive::kPrimInt:
3773 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003774 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003775 break;
3776 }
3777
Calin Juravle7c4954d2014-10-28 16:57:40 +00003778 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003779 if (second.IsFpuRegister()) {
3780 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3781 } else if (second.IsConstant()) {
3782 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003783 codegen_->LiteralFloatAddress(
3784 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003785 } else {
3786 DCHECK(second.IsStackSlot());
3787 __ divss(first.AsFpuRegister<XmmRegister>(),
3788 Address(CpuRegister(RSP), second.GetStackIndex()));
3789 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003790 break;
3791 }
3792
3793 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003794 if (second.IsFpuRegister()) {
3795 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3796 } else if (second.IsConstant()) {
3797 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003798 codegen_->LiteralDoubleAddress(
3799 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003800 } else {
3801 DCHECK(second.IsDoubleStackSlot());
3802 __ divsd(first.AsFpuRegister<XmmRegister>(),
3803 Address(CpuRegister(RSP), second.GetStackIndex()));
3804 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003805 break;
3806 }
3807
3808 default:
3809 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3810 }
3811}
3812
Calin Juravlebacfec32014-11-14 15:54:36 +00003813void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003814 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003815 LocationSummary* locations =
3816 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003817
3818 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003819 case Primitive::kPrimInt:
3820 case Primitive::kPrimLong: {
3821 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003822 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003823 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3824 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003825 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3826 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3827 // output and request another temp.
3828 if (rem->InputAt(1)->IsConstant()) {
3829 locations->AddTemp(Location::RequiresRegister());
3830 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003831 break;
3832 }
3833
3834 case Primitive::kPrimFloat:
3835 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003836 locations->SetInAt(0, Location::Any());
3837 locations->SetInAt(1, Location::Any());
3838 locations->SetOut(Location::RequiresFpuRegister());
3839 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003840 break;
3841 }
3842
3843 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003844 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003845 }
3846}
3847
3848void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3849 Primitive::Type type = rem->GetResultType();
3850 switch (type) {
3851 case Primitive::kPrimInt:
3852 case Primitive::kPrimLong: {
3853 GenerateDivRemIntegral(rem);
3854 break;
3855 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003856 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003857 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003858 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003859 break;
3860 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003861 default:
3862 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3863 }
3864}
3865
Calin Juravled0d48522014-11-04 16:40:20 +00003866void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003867 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003868 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003869}
3870
3871void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003872 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003873 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3874 codegen_->AddSlowPath(slow_path);
3875
3876 LocationSummary* locations = instruction->GetLocations();
3877 Location value = locations->InAt(0);
3878
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003879 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003880 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003881 case Primitive::kPrimByte:
3882 case Primitive::kPrimChar:
3883 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003884 case Primitive::kPrimInt: {
3885 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003887 __ j(kEqual, slow_path->GetEntryLabel());
3888 } else if (value.IsStackSlot()) {
3889 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3890 __ j(kEqual, slow_path->GetEntryLabel());
3891 } else {
3892 DCHECK(value.IsConstant()) << value;
3893 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003894 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003895 }
3896 }
3897 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003898 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003899 case Primitive::kPrimLong: {
3900 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003901 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003902 __ j(kEqual, slow_path->GetEntryLabel());
3903 } else if (value.IsDoubleStackSlot()) {
3904 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3905 __ j(kEqual, slow_path->GetEntryLabel());
3906 } else {
3907 DCHECK(value.IsConstant()) << value;
3908 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003909 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003910 }
3911 }
3912 break;
3913 }
3914 default:
3915 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003916 }
Calin Juravled0d48522014-11-04 16:40:20 +00003917}
3918
Calin Juravle9aec02f2014-11-18 23:06:35 +00003919void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3920 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3921
3922 LocationSummary* locations =
3923 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3924
3925 switch (op->GetResultType()) {
3926 case Primitive::kPrimInt:
3927 case Primitive::kPrimLong: {
3928 locations->SetInAt(0, Location::RequiresRegister());
3929 // The shift count needs to be in CL.
3930 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3931 locations->SetOut(Location::SameAsFirstInput());
3932 break;
3933 }
3934 default:
3935 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3936 }
3937}
3938
3939void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3940 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3941
3942 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944 Location second = locations->InAt(1);
3945
3946 switch (op->GetResultType()) {
3947 case Primitive::kPrimInt: {
3948 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003950 if (op->IsShl()) {
3951 __ shll(first_reg, second_reg);
3952 } else if (op->IsShr()) {
3953 __ sarl(first_reg, second_reg);
3954 } else {
3955 __ shrl(first_reg, second_reg);
3956 }
3957 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003958 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959 if (op->IsShl()) {
3960 __ shll(first_reg, imm);
3961 } else if (op->IsShr()) {
3962 __ sarl(first_reg, imm);
3963 } else {
3964 __ shrl(first_reg, imm);
3965 }
3966 }
3967 break;
3968 }
3969 case Primitive::kPrimLong: {
3970 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003971 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003972 if (op->IsShl()) {
3973 __ shlq(first_reg, second_reg);
3974 } else if (op->IsShr()) {
3975 __ sarq(first_reg, second_reg);
3976 } else {
3977 __ shrq(first_reg, second_reg);
3978 }
3979 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003980 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003981 if (op->IsShl()) {
3982 __ shlq(first_reg, imm);
3983 } else if (op->IsShr()) {
3984 __ sarq(first_reg, imm);
3985 } else {
3986 __ shrq(first_reg, imm);
3987 }
3988 }
3989 break;
3990 }
3991 default:
3992 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003993 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003994 }
3995}
3996
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003997void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3998 LocationSummary* locations =
3999 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4000
4001 switch (ror->GetResultType()) {
4002 case Primitive::kPrimInt:
4003 case Primitive::kPrimLong: {
4004 locations->SetInAt(0, Location::RequiresRegister());
4005 // The shift count needs to be in CL (unless it is a constant).
4006 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4007 locations->SetOut(Location::SameAsFirstInput());
4008 break;
4009 }
4010 default:
4011 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4012 UNREACHABLE();
4013 }
4014}
4015
4016void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4017 LocationSummary* locations = ror->GetLocations();
4018 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4019 Location second = locations->InAt(1);
4020
4021 switch (ror->GetResultType()) {
4022 case Primitive::kPrimInt:
4023 if (second.IsRegister()) {
4024 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4025 __ rorl(first_reg, second_reg);
4026 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004027 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004028 __ rorl(first_reg, imm);
4029 }
4030 break;
4031 case Primitive::kPrimLong:
4032 if (second.IsRegister()) {
4033 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4034 __ rorq(first_reg, second_reg);
4035 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004036 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004037 __ rorq(first_reg, imm);
4038 }
4039 break;
4040 default:
4041 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4042 UNREACHABLE();
4043 }
4044}
4045
Calin Juravle9aec02f2014-11-18 23:06:35 +00004046void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4047 HandleShift(shl);
4048}
4049
4050void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4051 HandleShift(shl);
4052}
4053
4054void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4055 HandleShift(shr);
4056}
4057
4058void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4059 HandleShift(shr);
4060}
4061
4062void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4063 HandleShift(ushr);
4064}
4065
4066void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4067 HandleShift(ushr);
4068}
4069
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004070void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004071 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004072 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004073 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004074 if (instruction->IsStringAlloc()) {
4075 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4076 } else {
4077 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004078 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004079 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004080}
4081
4082void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004083 // Note: if heap poisoning is enabled, the entry point takes cares
4084 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004085 if (instruction->IsStringAlloc()) {
4086 // String is allocated through StringFactory. Call NewEmptyString entry point.
4087 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004088 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004089 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4090 __ call(Address(temp, code_offset.SizeValue()));
4091 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4092 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004093 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004094 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004095 DCHECK(!codegen_->IsLeafMethod());
4096 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004097}
4098
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004099void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4100 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004102 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004103 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4105 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004106}
4107
4108void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004109 // Note: if heap poisoning is enabled, the entry point takes cares
4110 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004111 QuickEntrypointEnum entrypoint =
4112 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4113 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004114 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004115 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004116}
4117
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004118void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004119 LocationSummary* locations =
4120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004121 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4122 if (location.IsStackSlot()) {
4123 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4124 } else if (location.IsDoubleStackSlot()) {
4125 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4126 }
4127 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004128}
4129
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004130void InstructionCodeGeneratorX86_64::VisitParameterValue(
4131 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004132 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004133}
4134
4135void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4136 LocationSummary* locations =
4137 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4138 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4139}
4140
4141void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4142 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4143 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004144}
4145
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004146void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4147 LocationSummary* locations =
4148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4149 locations->SetInAt(0, Location::RequiresRegister());
4150 locations->SetOut(Location::RequiresRegister());
4151}
4152
4153void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4154 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004155 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004156 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004157 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004158 __ movq(locations->Out().AsRegister<CpuRegister>(),
4159 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004160 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004161 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004162 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004163 __ movq(locations->Out().AsRegister<CpuRegister>(),
4164 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4165 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004166 __ movq(locations->Out().AsRegister<CpuRegister>(),
4167 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004168 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004169}
4170
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004171void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004172 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004173 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004174 locations->SetInAt(0, Location::RequiresRegister());
4175 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004176}
4177
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004178void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4179 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004180 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4181 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004182 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004183 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004184 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004185 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004186 break;
4187
4188 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004190 break;
4191
4192 default:
4193 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4194 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004195}
4196
David Brazdil66d126e2015-04-03 16:02:44 +01004197void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4198 LocationSummary* locations =
4199 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4200 locations->SetInAt(0, Location::RequiresRegister());
4201 locations->SetOut(Location::SameAsFirstInput());
4202}
4203
4204void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004205 LocationSummary* locations = bool_not->GetLocations();
4206 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4207 locations->Out().AsRegister<CpuRegister>().AsRegister());
4208 Location out = locations->Out();
4209 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4210}
4211
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004212void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004213 LocationSummary* locations =
4214 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004215 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004216 locations->SetInAt(i, Location::Any());
4217 }
4218 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004219}
4220
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004221void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004222 LOG(FATAL) << "Unimplemented";
4223}
4224
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004225void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004226 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004227 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004228 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004229 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4230 */
4231 switch (kind) {
4232 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004233 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004234 break;
4235 }
4236 case MemBarrierKind::kAnyStore:
4237 case MemBarrierKind::kLoadAny:
4238 case MemBarrierKind::kStoreStore: {
4239 // nop
4240 break;
4241 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004242 case MemBarrierKind::kNTStoreStore:
4243 // Non-Temporal Store/Store needs an explicit fence.
4244 MemoryFence(/* non-temporal */ true);
4245 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004246 }
4247}
4248
4249void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4250 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4251
Roland Levillain0d5a2812015-11-13 10:07:31 +00004252 bool object_field_get_with_read_barrier =
4253 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004254 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004255 new (GetGraph()->GetArena()) LocationSummary(instruction,
4256 object_field_get_with_read_barrier ?
4257 LocationSummary::kCallOnSlowPath :
4258 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004259 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004260 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004261 }
Calin Juravle52c48962014-12-16 17:02:57 +00004262 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004263 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4264 locations->SetOut(Location::RequiresFpuRegister());
4265 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004266 // The output overlaps for an object field get when read barriers
4267 // are enabled: we do not want the move to overwrite the object's
4268 // location, as we need it to emit the read barrier.
4269 locations->SetOut(
4270 Location::RequiresRegister(),
4271 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004272 }
Calin Juravle52c48962014-12-16 17:02:57 +00004273}
4274
4275void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4276 const FieldInfo& field_info) {
4277 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4278
4279 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004280 Location base_loc = locations->InAt(0);
4281 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004282 Location out = locations->Out();
4283 bool is_volatile = field_info.IsVolatile();
4284 Primitive::Type field_type = field_info.GetFieldType();
4285 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4286
4287 switch (field_type) {
4288 case Primitive::kPrimBoolean: {
4289 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4290 break;
4291 }
4292
4293 case Primitive::kPrimByte: {
4294 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4295 break;
4296 }
4297
4298 case Primitive::kPrimShort: {
4299 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4300 break;
4301 }
4302
4303 case Primitive::kPrimChar: {
4304 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4305 break;
4306 }
4307
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004308 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004309 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4310 break;
4311 }
4312
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004313 case Primitive::kPrimNot: {
4314 // /* HeapReference<Object> */ out = *(base + offset)
4315 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004316 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004317 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004318 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004319 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004320 if (is_volatile) {
4321 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4322 }
4323 } else {
4324 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4325 codegen_->MaybeRecordImplicitNullCheck(instruction);
4326 if (is_volatile) {
4327 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4328 }
4329 // If read barriers are enabled, emit read barriers other than
4330 // Baker's using a slow path (and also unpoison the loaded
4331 // reference, if heap poisoning is enabled).
4332 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4333 }
4334 break;
4335 }
4336
Calin Juravle52c48962014-12-16 17:02:57 +00004337 case Primitive::kPrimLong: {
4338 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4339 break;
4340 }
4341
4342 case Primitive::kPrimFloat: {
4343 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4344 break;
4345 }
4346
4347 case Primitive::kPrimDouble: {
4348 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4349 break;
4350 }
4351
4352 case Primitive::kPrimVoid:
4353 LOG(FATAL) << "Unreachable type " << field_type;
4354 UNREACHABLE();
4355 }
4356
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004357 if (field_type == Primitive::kPrimNot) {
4358 // Potential implicit null checks, in the case of reference
4359 // fields, are handled in the previous switch statement.
4360 } else {
4361 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004362 }
Roland Levillain4d027112015-07-01 15:41:14 +01004363
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004364 if (is_volatile) {
4365 if (field_type == Primitive::kPrimNot) {
4366 // Memory barriers, in the case of references, are also handled
4367 // in the previous switch statement.
4368 } else {
4369 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4370 }
Roland Levillain4d027112015-07-01 15:41:14 +01004371 }
Calin Juravle52c48962014-12-16 17:02:57 +00004372}
4373
4374void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4375 const FieldInfo& field_info) {
4376 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4377
4378 LocationSummary* locations =
4379 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004380 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004381 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004382 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004383 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004384
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004385 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004386 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004387 if (is_volatile) {
4388 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4389 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4390 } else {
4391 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4392 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004393 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004394 if (is_volatile) {
4395 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4396 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4397 } else {
4398 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4399 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004400 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004401 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004402 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004403 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004404 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004405 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4406 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004407 locations->AddTemp(Location::RequiresRegister());
4408 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004409}
4410
Calin Juravle52c48962014-12-16 17:02:57 +00004411void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004412 const FieldInfo& field_info,
4413 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004414 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4415
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004417 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4418 Location value = locations->InAt(1);
4419 bool is_volatile = field_info.IsVolatile();
4420 Primitive::Type field_type = field_info.GetFieldType();
4421 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4422
4423 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004424 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004425 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426
Mark Mendellea5af682015-10-22 17:35:49 -04004427 bool maybe_record_implicit_null_check_done = false;
4428
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 switch (field_type) {
4430 case Primitive::kPrimBoolean:
4431 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004432 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004433 __ movb(Address(base, offset),
4434 Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004435 } else {
4436 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4437 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004438 break;
4439 }
4440
4441 case Primitive::kPrimShort:
4442 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004443 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004444 __ movw(Address(base, offset),
4445 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell40741f32015-04-20 22:10:34 -04004446 } else {
4447 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4448 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449 break;
4450 }
4451
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004452 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004454 if (value.IsConstant()) {
4455 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004456 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4457 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4458 // Note: if heap poisoning is enabled, no need to poison
4459 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004460 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004461 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004462 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4463 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4464 __ movl(temp, value.AsRegister<CpuRegister>());
4465 __ PoisonHeapReference(temp);
4466 __ movl(Address(base, offset), temp);
4467 } else {
4468 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4469 }
Mark Mendell40741f32015-04-20 22:10:34 -04004470 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471 break;
4472 }
4473
4474 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004475 if (value.IsConstant()) {
4476 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004477 codegen_->MoveInt64ToAddress(Address(base, offset),
4478 Address(base, offset + sizeof(int32_t)),
4479 v,
4480 instruction);
4481 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004482 } else {
4483 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4484 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485 break;
4486 }
4487
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004488 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004489 if (value.IsConstant()) {
4490 int32_t v =
4491 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4492 __ movl(Address(base, offset), Immediate(v));
4493 } else {
4494 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4495 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004496 break;
4497 }
4498
4499 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004500 if (value.IsConstant()) {
4501 int64_t v =
4502 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4503 codegen_->MoveInt64ToAddress(Address(base, offset),
4504 Address(base, offset + sizeof(int32_t)),
4505 v,
4506 instruction);
4507 maybe_record_implicit_null_check_done = true;
4508 } else {
4509 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4510 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004511 break;
4512 }
4513
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514 case Primitive::kPrimVoid:
4515 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004516 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004517 }
Calin Juravle52c48962014-12-16 17:02:57 +00004518
Mark Mendellea5af682015-10-22 17:35:49 -04004519 if (!maybe_record_implicit_null_check_done) {
4520 codegen_->MaybeRecordImplicitNullCheck(instruction);
4521 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004522
4523 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4524 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4525 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004526 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004527 }
4528
Calin Juravle52c48962014-12-16 17:02:57 +00004529 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004530 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004531 }
4532}
4533
4534void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4535 HandleFieldSet(instruction, instruction->GetFieldInfo());
4536}
4537
4538void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004539 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004540}
4541
4542void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004543 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004544}
4545
4546void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004547 HandleFieldGet(instruction, instruction->GetFieldInfo());
4548}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549
Calin Juravle52c48962014-12-16 17:02:57 +00004550void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4551 HandleFieldGet(instruction);
4552}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553
Calin Juravle52c48962014-12-16 17:02:57 +00004554void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4555 HandleFieldGet(instruction, instruction->GetFieldInfo());
4556}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004557
Calin Juravle52c48962014-12-16 17:02:57 +00004558void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4559 HandleFieldSet(instruction, instruction->GetFieldInfo());
4560}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004561
Calin Juravle52c48962014-12-16 17:02:57 +00004562void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004563 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004564}
4565
Calin Juravlee460d1d2015-09-29 04:52:17 +01004566void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4567 HUnresolvedInstanceFieldGet* instruction) {
4568 FieldAccessCallingConventionX86_64 calling_convention;
4569 codegen_->CreateUnresolvedFieldLocationSummary(
4570 instruction, instruction->GetFieldType(), calling_convention);
4571}
4572
4573void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4574 HUnresolvedInstanceFieldGet* instruction) {
4575 FieldAccessCallingConventionX86_64 calling_convention;
4576 codegen_->GenerateUnresolvedFieldAccess(instruction,
4577 instruction->GetFieldType(),
4578 instruction->GetFieldIndex(),
4579 instruction->GetDexPc(),
4580 calling_convention);
4581}
4582
4583void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4584 HUnresolvedInstanceFieldSet* instruction) {
4585 FieldAccessCallingConventionX86_64 calling_convention;
4586 codegen_->CreateUnresolvedFieldLocationSummary(
4587 instruction, instruction->GetFieldType(), calling_convention);
4588}
4589
4590void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4591 HUnresolvedInstanceFieldSet* instruction) {
4592 FieldAccessCallingConventionX86_64 calling_convention;
4593 codegen_->GenerateUnresolvedFieldAccess(instruction,
4594 instruction->GetFieldType(),
4595 instruction->GetFieldIndex(),
4596 instruction->GetDexPc(),
4597 calling_convention);
4598}
4599
4600void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4601 HUnresolvedStaticFieldGet* instruction) {
4602 FieldAccessCallingConventionX86_64 calling_convention;
4603 codegen_->CreateUnresolvedFieldLocationSummary(
4604 instruction, instruction->GetFieldType(), calling_convention);
4605}
4606
4607void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4608 HUnresolvedStaticFieldGet* instruction) {
4609 FieldAccessCallingConventionX86_64 calling_convention;
4610 codegen_->GenerateUnresolvedFieldAccess(instruction,
4611 instruction->GetFieldType(),
4612 instruction->GetFieldIndex(),
4613 instruction->GetDexPc(),
4614 calling_convention);
4615}
4616
4617void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4618 HUnresolvedStaticFieldSet* instruction) {
4619 FieldAccessCallingConventionX86_64 calling_convention;
4620 codegen_->CreateUnresolvedFieldLocationSummary(
4621 instruction, instruction->GetFieldType(), calling_convention);
4622}
4623
4624void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4625 HUnresolvedStaticFieldSet* instruction) {
4626 FieldAccessCallingConventionX86_64 calling_convention;
4627 codegen_->GenerateUnresolvedFieldAccess(instruction,
4628 instruction->GetFieldType(),
4629 instruction->GetFieldIndex(),
4630 instruction->GetDexPc(),
4631 calling_convention);
4632}
4633
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004634void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004635 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4636 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4637 ? Location::RequiresRegister()
4638 : Location::Any();
4639 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640}
4641
Calin Juravle2ae48182016-03-16 14:05:09 +00004642void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4643 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004644 return;
4645 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004646 LocationSummary* locations = instruction->GetLocations();
4647 Location obj = locations->InAt(0);
4648
4649 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004650 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004651}
4652
Calin Juravle2ae48182016-03-16 14:05:09 +00004653void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004654 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004655 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656
4657 LocationSummary* locations = instruction->GetLocations();
4658 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659
4660 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004661 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004662 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004663 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004664 } else {
4665 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004666 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004667 __ jmp(slow_path->GetEntryLabel());
4668 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004669 }
4670 __ j(kEqual, slow_path->GetEntryLabel());
4671}
4672
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004673void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004674 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004675}
4676
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004678 bool object_array_get_with_read_barrier =
4679 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004680 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004681 new (GetGraph()->GetArena()) LocationSummary(instruction,
4682 object_array_get_with_read_barrier ?
4683 LocationSummary::kCallOnSlowPath :
4684 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004685 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004686 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004687 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004688 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004689 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004690 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4691 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4692 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004693 // The output overlaps for an object array get when read barriers
4694 // are enabled: we do not want the move to overwrite the array's
4695 // location, as we need it to emit the read barrier.
4696 locations->SetOut(
4697 Location::RequiresRegister(),
4698 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004699 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700}
4701
4702void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4703 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004704 Location obj_loc = locations->InAt(0);
4705 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004707 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004708 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004710 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004711 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004713 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004714 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004715 break;
4716 }
4717
4718 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004719 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004720 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 break;
4722 }
4723
4724 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004725 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004726 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727 break;
4728 }
4729
4730 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004731 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004732 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4733 // Branch cases into compressed and uncompressed for each index's type.
4734 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4735 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004736 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004737 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004738 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4739 "Expecting 0=compressed, 1=uncompressed");
4740 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004741 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4742 __ jmp(&done);
4743 __ Bind(&not_compressed);
4744 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4745 __ Bind(&done);
4746 } else {
4747 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4748 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749 break;
4750 }
4751
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004752 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004753 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004754 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 break;
4756 }
4757
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004758 case Primitive::kPrimNot: {
4759 static_assert(
4760 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4761 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004762 // /* HeapReference<Object> */ out =
4763 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4764 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004765 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004766 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004767 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004768 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004769 } else {
4770 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004771 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4772 codegen_->MaybeRecordImplicitNullCheck(instruction);
4773 // If read barriers are enabled, emit read barriers other than
4774 // Baker's using a slow path (and also unpoison the loaded
4775 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004776 if (index.IsConstant()) {
4777 uint32_t offset =
4778 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004779 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4780 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004781 codegen_->MaybeGenerateReadBarrierSlow(
4782 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4783 }
4784 }
4785 break;
4786 }
4787
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004789 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004790 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791 break;
4792 }
4793
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004794 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004795 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004796 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004797 break;
4798 }
4799
4800 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004801 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004802 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004803 break;
4804 }
4805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004806 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004807 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004808 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004809 }
Roland Levillain4d027112015-07-01 15:41:14 +01004810
4811 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004812 // Potential implicit null checks, in the case of reference
4813 // arrays, are handled in the previous switch statement.
4814 } else {
4815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004816 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817}
4818
4819void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004820 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004821
4822 bool needs_write_barrier =
4823 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004824 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004825
Nicolas Geoffray39468442014-09-02 15:17:15 +01004826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004828 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004829 LocationSummary::kCallOnSlowPath :
4830 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004831
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004833 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4834 if (Primitive::IsFloatingPointType(value_type)) {
4835 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004836 } else {
4837 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4838 }
4839
4840 if (needs_write_barrier) {
4841 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004842 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004843 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004845}
4846
4847void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4848 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004849 Location array_loc = locations->InAt(0);
4850 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004852 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004853 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004854 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004855 bool needs_write_barrier =
4856 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004857 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4858 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4859 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004860
4861 switch (value_type) {
4862 case Primitive::kPrimBoolean:
4863 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004864 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004865 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004866 if (value.IsRegister()) {
4867 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004869 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004871 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004872 break;
4873 }
4874
4875 case Primitive::kPrimShort:
4876 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004877 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004878 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004879 if (value.IsRegister()) {
4880 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray78612082017-07-24 14:18:53 +01004883 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004885 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886 break;
4887 }
4888
4889 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004890 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004891 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004892
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 if (!value.IsRegister()) {
4894 // Just setting null.
4895 DCHECK(instruction->InputAt(2)->IsNullConstant());
4896 DCHECK(value.IsConstant()) << value;
4897 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004898 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004899 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004900 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004902 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903
4904 DCHECK(needs_write_barrier);
4905 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004906 // We cannot use a NearLabel for `done`, as its range may be too
4907 // short when Baker read barriers are enabled.
4908 Label done;
4909 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004910 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004911 Location temp_loc = locations->GetTemp(0);
4912 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004913 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004914 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4915 codegen_->AddSlowPath(slow_path);
4916 if (instruction->GetValueCanBeNull()) {
4917 __ testl(register_value, register_value);
4918 __ j(kNotEqual, &not_null);
4919 __ movl(address, Immediate(0));
4920 codegen_->MaybeRecordImplicitNullCheck(instruction);
4921 __ jmp(&done);
4922 __ Bind(&not_null);
4923 }
4924
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004925 // Note that when Baker read barriers are enabled, the type
4926 // checks are performed without read barriers. This is fine,
4927 // even in the case where a class object is in the from-space
4928 // after the flip, as a comparison involving such a type would
4929 // not produce a false positive; it may of course produce a
4930 // false negative, in which case we would take the ArraySet
4931 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004932
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004933 // /* HeapReference<Class> */ temp = array->klass_
4934 __ movl(temp, Address(array, class_offset));
4935 codegen_->MaybeRecordImplicitNullCheck(instruction);
4936 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004937
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004938 // /* HeapReference<Class> */ temp = temp->component_type_
4939 __ movl(temp, Address(temp, component_offset));
4940 // If heap poisoning is enabled, no need to unpoison `temp`
4941 // nor the object reference in `register_value->klass`, as
4942 // we are comparing two poisoned references.
4943 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004944
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004945 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4946 __ j(kEqual, &do_put);
4947 // If heap poisoning is enabled, the `temp` reference has
4948 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004949 __ MaybeUnpoisonHeapReference(temp);
4950
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004951 // If heap poisoning is enabled, no need to unpoison the
4952 // heap reference loaded below, as it is only used for a
4953 // comparison with null.
4954 __ cmpl(Address(temp, super_offset), Immediate(0));
4955 __ j(kNotEqual, slow_path->GetEntryLabel());
4956 __ Bind(&do_put);
4957 } else {
4958 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004959 }
4960 }
4961
4962 if (kPoisonHeapReferences) {
4963 __ movl(temp, register_value);
4964 __ PoisonHeapReference(temp);
4965 __ movl(address, temp);
4966 } else {
4967 __ movl(address, register_value);
4968 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004969 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004970 codegen_->MaybeRecordImplicitNullCheck(instruction);
4971 }
4972
4973 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4974 codegen_->MarkGCCard(
4975 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4976 __ Bind(&done);
4977
4978 if (slow_path != nullptr) {
4979 __ Bind(slow_path->GetExitLabel());
4980 }
4981
4982 break;
4983 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004984
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004985 case Primitive::kPrimInt: {
4986 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004987 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004988 if (value.IsRegister()) {
4989 __ movl(address, value.AsRegister<CpuRegister>());
4990 } else {
4991 DCHECK(value.IsConstant()) << value;
4992 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4993 __ movl(address, Immediate(v));
4994 }
4995 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996 break;
4997 }
4998
4999 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005000 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005001 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005002 if (value.IsRegister()) {
5003 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005004 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005005 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005006 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005007 Address address_high =
5008 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005009 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010 }
5011 break;
5012 }
5013
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005014 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005015 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005016 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005017 if (value.IsFpuRegister()) {
5018 __ movss(address, value.AsFpuRegister<XmmRegister>());
5019 } else {
5020 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005021 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005022 __ movl(address, Immediate(v));
5023 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005024 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005025 break;
5026 }
5027
5028 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005029 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005030 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005031 if (value.IsFpuRegister()) {
5032 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5033 codegen_->MaybeRecordImplicitNullCheck(instruction);
5034 } else {
5035 int64_t v =
5036 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005037 Address address_high =
5038 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005039 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5040 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005041 break;
5042 }
5043
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 case Primitive::kPrimVoid:
5045 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005046 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047 }
5048}
5049
5050void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005051 LocationSummary* locations =
5052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005053 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005054 if (!instruction->IsEmittedAtUseSite()) {
5055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5056 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057}
5058
5059void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005060 if (instruction->IsEmittedAtUseSite()) {
5061 return;
5062 }
5063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005065 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005066 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5067 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005069 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005070 // Mask out most significant bit in case the array is String's array of char.
5071 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005072 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005073 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074}
5075
5076void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005077 RegisterSet caller_saves = RegisterSet::Empty();
5078 InvokeRuntimeCallingConvention calling_convention;
5079 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5080 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5081 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005082 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005083 HInstruction* length = instruction->InputAt(1);
5084 if (!length->IsEmittedAtUseSite()) {
5085 locations->SetInAt(1, Location::RegisterOrConstant(length));
5086 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087}
5088
5089void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5090 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005091 Location index_loc = locations->InAt(0);
5092 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005093 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094
Mark Mendell99dbd682015-04-22 16:18:52 -04005095 if (length_loc.IsConstant()) {
5096 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5097 if (index_loc.IsConstant()) {
5098 // BCE will remove the bounds check if we are guarenteed to pass.
5099 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5100 if (index < 0 || index >= length) {
5101 codegen_->AddSlowPath(slow_path);
5102 __ jmp(slow_path->GetEntryLabel());
5103 } else {
5104 // Some optimization after BCE may have generated this, and we should not
5105 // generate a bounds check if it is a valid range.
5106 }
5107 return;
5108 }
5109
5110 // We have to reverse the jump condition because the length is the constant.
5111 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5112 __ cmpl(index_reg, Immediate(length));
5113 codegen_->AddSlowPath(slow_path);
5114 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005115 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005116 HInstruction* array_length = instruction->InputAt(1);
5117 if (array_length->IsEmittedAtUseSite()) {
5118 // Address the length field in the array.
5119 DCHECK(array_length->IsArrayLength());
5120 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5121 Location array_loc = array_length->GetLocations()->InAt(0);
5122 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005123 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005124 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5125 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005126 CpuRegister length_reg = CpuRegister(TMP);
5127 __ movl(length_reg, array_len);
5128 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005129 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005130 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005131 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005132 // Checking the bound for general case:
5133 // Array of char or String's array when the compression feature off.
5134 if (index_loc.IsConstant()) {
5135 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5136 __ cmpl(array_len, Immediate(value));
5137 } else {
5138 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5139 }
5140 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005141 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005142 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005143 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005144 }
5145 codegen_->AddSlowPath(slow_path);
5146 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005147 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148}
5149
5150void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5151 CpuRegister card,
5152 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005153 CpuRegister value,
5154 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005155 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005156 if (value_can_be_null) {
5157 __ testl(value, value);
5158 __ j(kEqual, &is_null);
5159 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005160 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005161 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005162 __ movq(temp, object);
5163 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005164 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005165 if (value_can_be_null) {
5166 __ Bind(&is_null);
5167 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005168}
5169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005170void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005171 LOG(FATAL) << "Unimplemented";
5172}
5173
5174void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005175 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5176}
5177
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005178void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005179 LocationSummary* locations =
5180 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005181 // In suspend check slow path, usually there are no caller-save registers at all.
5182 // If SIMD instructions are present, however, we force spilling all live SIMD
5183 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005184 locations->SetCustomSlowPathCallerSaves(
5185 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005186}
5187
5188void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005189 HBasicBlock* block = instruction->GetBlock();
5190 if (block->GetLoopInformation() != nullptr) {
5191 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5192 // The back edge will generate the suspend check.
5193 return;
5194 }
5195 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5196 // The goto will generate the suspend check.
5197 return;
5198 }
5199 GenerateSuspendCheck(instruction, nullptr);
5200}
5201
5202void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5203 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005204 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005205 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5206 if (slow_path == nullptr) {
5207 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5208 instruction->SetSlowPath(slow_path);
5209 codegen_->AddSlowPath(slow_path);
5210 if (successor != nullptr) {
5211 DCHECK(successor->IsLoopHeader());
5212 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5213 }
5214 } else {
5215 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5216 }
5217
Andreas Gampe542451c2016-07-26 09:02:02 -07005218 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005219 /* no_rip */ true),
5220 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005221 if (successor == nullptr) {
5222 __ j(kNotEqual, slow_path->GetEntryLabel());
5223 __ Bind(slow_path->GetReturnLabel());
5224 } else {
5225 __ j(kEqual, codegen_->GetLabelOf(successor));
5226 __ jmp(slow_path->GetEntryLabel());
5227 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005228}
5229
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5231 return codegen_->GetAssembler();
5232}
5233
5234void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005235 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005236 Location source = move->GetSource();
5237 Location destination = move->GetDestination();
5238
5239 if (source.IsRegister()) {
5240 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005241 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005242 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005243 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005244 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005245 } else {
5246 DCHECK(destination.IsDoubleStackSlot());
5247 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005248 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005249 }
5250 } else if (source.IsStackSlot()) {
5251 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005252 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005253 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005255 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005256 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005257 } else {
5258 DCHECK(destination.IsStackSlot());
5259 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5260 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5261 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005262 } else if (source.IsDoubleStackSlot()) {
5263 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005264 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005265 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005266 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005267 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5268 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005269 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005270 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005271 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5272 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5273 }
Aart Bik5576f372017-03-23 16:17:37 -07005274 } else if (source.IsSIMDStackSlot()) {
5275 DCHECK(destination.IsFpuRegister());
5276 __ movups(destination.AsFpuRegister<XmmRegister>(),
5277 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005278 } else if (source.IsConstant()) {
5279 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005280 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5281 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005282 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005283 if (value == 0) {
5284 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5285 } else {
5286 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5287 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005288 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005289 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005290 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005291 }
5292 } else if (constant->IsLongConstant()) {
5293 int64_t value = constant->AsLongConstant()->GetValue();
5294 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005295 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005296 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005297 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005298 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005299 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005301 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005303 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005304 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 } else {
5306 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005307 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5309 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005310 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005312 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005313 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005315 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005316 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005317 } else {
5318 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005319 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005320 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005321 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005322 } else if (source.IsFpuRegister()) {
5323 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005324 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005325 } else if (destination.IsStackSlot()) {
5326 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005327 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005328 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005329 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005330 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005331 } else {
5332 DCHECK(destination.IsSIMDStackSlot());
5333 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5334 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005335 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005336 }
5337}
5338
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005339void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005340 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005341 __ movl(Address(CpuRegister(RSP), mem), reg);
5342 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005343}
5344
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005345void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005346 ScratchRegisterScope ensure_scratch(
5347 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5348
5349 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5350 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5351 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5352 Address(CpuRegister(RSP), mem2 + stack_offset));
5353 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5354 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5355 CpuRegister(ensure_scratch.GetRegister()));
5356}
5357
Mark Mendell8a1c7282015-06-29 15:41:28 -04005358void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5359 __ movq(CpuRegister(TMP), reg1);
5360 __ movq(reg1, reg2);
5361 __ movq(reg2, CpuRegister(TMP));
5362}
5363
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005364void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5365 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5366 __ movq(Address(CpuRegister(RSP), mem), reg);
5367 __ movq(reg, CpuRegister(TMP));
5368}
5369
5370void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5371 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005372 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005373
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005374 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5375 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5376 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5377 Address(CpuRegister(RSP), mem2 + stack_offset));
5378 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5379 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5380 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005381}
5382
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005383void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5384 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5385 __ movss(Address(CpuRegister(RSP), mem), reg);
5386 __ movd(reg, CpuRegister(TMP));
5387}
5388
5389void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5390 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5391 __ movsd(Address(CpuRegister(RSP), mem), reg);
5392 __ movd(reg, CpuRegister(TMP));
5393}
5394
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005395void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005396 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005397 Location source = move->GetSource();
5398 Location destination = move->GetDestination();
5399
5400 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005401 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005402 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005403 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005404 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005406 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005407 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5408 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005409 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005410 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005411 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005412 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5413 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005414 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005415 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5416 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5417 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005418 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005419 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005420 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005421 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005422 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005423 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005424 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005425 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005426 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005427 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005428 }
5429}
5430
5431
5432void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5433 __ pushq(CpuRegister(reg));
5434}
5435
5436
5437void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5438 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005439}
5440
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005441void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005442 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005443 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5444 Immediate(mirror::Class::kStatusInitialized));
5445 __ j(kLess, slow_path->GetEntryLabel());
5446 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005447 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005448}
5449
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005450HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5451 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005452 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005453 case HLoadClass::LoadKind::kInvalid:
5454 LOG(FATAL) << "UNREACHABLE";
5455 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005456 case HLoadClass::LoadKind::kReferrersClass:
5457 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005458 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005459 case HLoadClass::LoadKind::kBssEntry:
5460 DCHECK(!Runtime::Current()->UseJitCompilation());
5461 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005462 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005463 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005464 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005465 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005466 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005467 break;
5468 }
5469 return desired_class_load_kind;
5470}
5471
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005473 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005474 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005475 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005476 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005477 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005478 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005479 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 return;
5481 }
Vladimir Marko41559982017-01-06 14:04:23 +00005482 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005483
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005484 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5485 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005486 ? LocationSummary::kCallOnSlowPath
5487 : LocationSummary::kNoCall;
5488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005489 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005490 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005491 }
5492
Vladimir Marko41559982017-01-06 14:04:23 +00005493 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005494 locations->SetInAt(0, Location::RequiresRegister());
5495 }
5496 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005497 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5498 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5499 // Rely on the type resolution and/or initialization to save everything.
5500 // Custom calling convention: RAX serves as both input and output.
5501 RegisterSet caller_saves = RegisterSet::Empty();
5502 caller_saves.Add(Location::RegisterLocation(RAX));
5503 locations->SetCustomSlowPathCallerSaves(caller_saves);
5504 } else {
5505 // For non-Baker read barrier we have a temp-clobbering call.
5506 }
5507 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005508}
5509
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005510Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
5511 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005512 Handle<mirror::Class> handle) {
5513 jit_class_roots_.Overwrite(
5514 TypeReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005515 // Add a patch entry and return the label.
5516 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
5517 PatchInfo<Label>* info = &jit_class_patches_.back();
5518 return &info->label;
5519}
5520
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005521// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5522// move.
5523void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005524 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005525 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005526 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005527 return;
5528 }
Vladimir Marko41559982017-01-06 14:04:23 +00005529 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005530
Vladimir Marko41559982017-01-06 14:04:23 +00005531 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005532 Location out_loc = locations->Out();
5533 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005534
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005535 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5536 ? kWithoutReadBarrier
5537 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005538 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005539 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005540 case HLoadClass::LoadKind::kReferrersClass: {
5541 DCHECK(!cls->CanCallRuntime());
5542 DCHECK(!cls->MustGenerateClinitCheck());
5543 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5544 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5545 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005546 cls,
5547 out_loc,
5548 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005549 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005550 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 break;
5552 }
5553 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005554 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005555 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005556 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005557 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005558 break;
5559 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005560 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005561 uint32_t address = dchecked_integral_cast<uint32_t>(
5562 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5563 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005564 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005565 break;
5566 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005567 case HLoadClass::LoadKind::kBssEntry: {
5568 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5569 /* no_rip */ false);
5570 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5571 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5572 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5573 generate_null_check = true;
5574 break;
5575 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005576 case HLoadClass::LoadKind::kJitTableAddress: {
5577 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5578 /* no_rip */ true);
5579 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005580 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005582 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005583 break;
5584 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005585 default:
5586 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5587 UNREACHABLE();
5588 }
5589
5590 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5591 DCHECK(cls->CanCallRuntime());
5592 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5593 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5594 codegen_->AddSlowPath(slow_path);
5595 if (generate_null_check) {
5596 __ testl(out, out);
5597 __ j(kEqual, slow_path->GetEntryLabel());
5598 }
5599 if (cls->MustGenerateClinitCheck()) {
5600 GenerateClassInitializationCheck(slow_path, out);
5601 } else {
5602 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005603 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005604 }
5605}
5606
5607void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5608 LocationSummary* locations =
5609 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5610 locations->SetInAt(0, Location::RequiresRegister());
5611 if (check->HasUses()) {
5612 locations->SetOut(Location::SameAsFirstInput());
5613 }
5614}
5615
5616void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005617 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005618 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005619 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005620 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005621 GenerateClassInitializationCheck(slow_path,
5622 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005623}
5624
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005625HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5626 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005627 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005628 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005629 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005630 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005631 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005632 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005633 case HLoadString::LoadKind::kJitTableAddress:
5634 DCHECK(Runtime::Current()->UseJitCompilation());
5635 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005636 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005637 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005638 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005639 }
5640 return desired_string_load_kind;
5641}
5642
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005643void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005644 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005646 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005647 locations->SetOut(Location::RegisterLocation(RAX));
5648 } else {
5649 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005650 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5651 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005652 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005653 // Custom calling convention: RAX serves as both input and output.
5654 RegisterSet caller_saves = RegisterSet::Empty();
5655 caller_saves.Add(Location::RegisterLocation(RAX));
5656 locations->SetCustomSlowPathCallerSaves(caller_saves);
5657 } else {
5658 // For non-Baker read barrier we have a temp-clobbering call.
5659 }
5660 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005661 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005662}
5663
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005664Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005665 dex::StringIndex dex_index,
5666 Handle<mirror::String> handle) {
5667 jit_string_roots_.Overwrite(
5668 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005669 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005670 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005671 PatchInfo<Label>* info = &jit_string_patches_.back();
5672 return &info->label;
5673}
5674
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005675// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5676// move.
5677void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005678 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005679 Location out_loc = locations->Out();
5680 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005681
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005682 switch (load->GetLoadKind()) {
5683 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005684 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005685 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005686 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005687 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005688 }
5689 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005690 uint32_t address = dchecked_integral_cast<uint32_t>(
5691 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5692 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005693 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005694 return;
5695 }
5696 case HLoadString::LoadKind::kBootImageInternTable: {
5697 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5698 __ movl(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5699 codegen_->RecordBootStringPatch(load);
5700 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005701 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005702 case HLoadString::LoadKind::kBssEntry: {
5703 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5704 /* no_rip */ false);
5705 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5706 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005707 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005708 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5709 codegen_->AddSlowPath(slow_path);
5710 __ testl(out, out);
5711 __ j(kEqual, slow_path->GetEntryLabel());
5712 __ Bind(slow_path->GetExitLabel());
5713 return;
5714 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005715 case HLoadString::LoadKind::kJitTableAddress: {
5716 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5717 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005718 Label* fixup_label = codegen_->NewJitRootStringPatch(
5719 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005720 // /* GcRoot<mirror::String> */ out = *address
5721 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5722 return;
5723 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005724 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005725 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005726 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005727
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005728 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005729 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005730 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005731 codegen_->InvokeRuntime(kQuickResolveString,
5732 load,
5733 load->GetDexPc());
5734 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005735}
5736
David Brazdilcb1c0552015-08-04 16:22:25 +01005737static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005738 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005739 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005740}
5741
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005742void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5743 LocationSummary* locations =
5744 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5745 locations->SetOut(Location::RequiresRegister());
5746}
5747
5748void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005749 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5750}
5751
5752void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5753 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5754}
5755
5756void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5757 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005758}
5759
5760void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5761 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005763 InvokeRuntimeCallingConvention calling_convention;
5764 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5765}
5766
5767void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005768 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005769 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005770}
5771
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005772static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5773 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005774 // We need a temporary for holding the iftable length.
5775 return true;
5776 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005777 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005778 !kUseBakerReadBarrier &&
5779 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005780 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5781 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5782}
5783
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005784static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5785 return kEmitCompilerReadBarrier &&
5786 !kUseBakerReadBarrier &&
5787 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5788 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5789 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5790}
5791
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005792void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005795 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 case TypeCheckKind::kExactCheck:
5798 case TypeCheckKind::kAbstractClassCheck:
5799 case TypeCheckKind::kClassHierarchyCheck:
5800 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 call_kind =
5802 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005803 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005804 break;
5805 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 case TypeCheckKind::kUnresolvedCheck:
5807 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005808 call_kind = LocationSummary::kCallOnSlowPath;
5809 break;
5810 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005811
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005812 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005813 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005814 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005815 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 locations->SetInAt(0, Location::RequiresRegister());
5817 locations->SetInAt(1, Location::Any());
5818 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5819 locations->SetOut(Location::RequiresRegister());
5820 // When read barriers are enabled, we need a temporary register for
5821 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005822 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005824 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005825}
5826
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005827void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005828 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005829 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005830 Location obj_loc = locations->InAt(0);
5831 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005832 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005833 Location out_loc = locations->Out();
5834 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005835 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005836 locations->GetTemp(0) :
5837 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005838 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5840 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5841 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005842 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005843 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005844
5845 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005846 // Avoid null check if we know obj is not null.
5847 if (instruction->MustDoNullCheck()) {
5848 __ testl(obj, obj);
5849 __ j(kEqual, &zero);
5850 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005852 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005854 // /* HeapReference<Class> */ out = obj->klass_
5855 GenerateReferenceLoadTwoRegisters(instruction,
5856 out_loc,
5857 obj_loc,
5858 class_offset,
5859 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 if (cls.IsRegister()) {
5861 __ cmpl(out, cls.AsRegister<CpuRegister>());
5862 } else {
5863 DCHECK(cls.IsStackSlot()) << cls;
5864 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5865 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005866 if (zero.IsLinked()) {
5867 // Classes must be equal for the instanceof to succeed.
5868 __ j(kNotEqual, &zero);
5869 __ movl(out, Immediate(1));
5870 __ jmp(&done);
5871 } else {
5872 __ setcc(kEqual, out);
5873 // setcc only sets the low byte.
5874 __ andl(out, Immediate(1));
5875 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 break;
5877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005880 // /* HeapReference<Class> */ out = obj->klass_
5881 GenerateReferenceLoadTwoRegisters(instruction,
5882 out_loc,
5883 obj_loc,
5884 class_offset,
5885 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 // If the class is abstract, we eagerly fetch the super class of the
5887 // object to avoid doing a comparison we know will fail.
5888 NearLabel loop, success;
5889 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005891 GenerateReferenceLoadOneRegister(instruction,
5892 out_loc,
5893 super_offset,
5894 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005895 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 __ testl(out, out);
5897 // If `out` is null, we use it for the result, and jump to `done`.
5898 __ j(kEqual, &done);
5899 if (cls.IsRegister()) {
5900 __ cmpl(out, cls.AsRegister<CpuRegister>());
5901 } else {
5902 DCHECK(cls.IsStackSlot()) << cls;
5903 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5904 }
5905 __ j(kNotEqual, &loop);
5906 __ movl(out, Immediate(1));
5907 if (zero.IsLinked()) {
5908 __ jmp(&done);
5909 }
5910 break;
5911 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005914 // /* HeapReference<Class> */ out = obj->klass_
5915 GenerateReferenceLoadTwoRegisters(instruction,
5916 out_loc,
5917 obj_loc,
5918 class_offset,
5919 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005920 // Walk over the class hierarchy to find a match.
5921 NearLabel loop, success;
5922 __ Bind(&loop);
5923 if (cls.IsRegister()) {
5924 __ cmpl(out, cls.AsRegister<CpuRegister>());
5925 } else {
5926 DCHECK(cls.IsStackSlot()) << cls;
5927 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5928 }
5929 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005931 GenerateReferenceLoadOneRegister(instruction,
5932 out_loc,
5933 super_offset,
5934 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005935 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005936 __ testl(out, out);
5937 __ j(kNotEqual, &loop);
5938 // If `out` is null, we use it for the result, and jump to `done`.
5939 __ jmp(&done);
5940 __ Bind(&success);
5941 __ movl(out, Immediate(1));
5942 if (zero.IsLinked()) {
5943 __ jmp(&done);
5944 }
5945 break;
5946 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005949 // /* HeapReference<Class> */ out = obj->klass_
5950 GenerateReferenceLoadTwoRegisters(instruction,
5951 out_loc,
5952 obj_loc,
5953 class_offset,
5954 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005955 // Do an exact check.
5956 NearLabel exact_check;
5957 if (cls.IsRegister()) {
5958 __ cmpl(out, cls.AsRegister<CpuRegister>());
5959 } else {
5960 DCHECK(cls.IsStackSlot()) << cls;
5961 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5962 }
5963 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005966 GenerateReferenceLoadOneRegister(instruction,
5967 out_loc,
5968 component_offset,
5969 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005970 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005971 __ testl(out, out);
5972 // If `out` is null, we use it for the result, and jump to `done`.
5973 __ j(kEqual, &done);
5974 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5975 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005976 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 __ movl(out, Immediate(1));
5978 __ jmp(&done);
5979 break;
5980 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005983 // No read barrier since the slow path will retry upon failure.
5984 // /* HeapReference<Class> */ out = obj->klass_
5985 GenerateReferenceLoadTwoRegisters(instruction,
5986 out_loc,
5987 obj_loc,
5988 class_offset,
5989 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005990 if (cls.IsRegister()) {
5991 __ cmpl(out, cls.AsRegister<CpuRegister>());
5992 } else {
5993 DCHECK(cls.IsStackSlot()) << cls;
5994 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5995 }
5996 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5998 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 codegen_->AddSlowPath(slow_path);
6000 __ j(kNotEqual, slow_path->GetEntryLabel());
6001 __ movl(out, Immediate(1));
6002 if (zero.IsLinked()) {
6003 __ jmp(&done);
6004 }
6005 break;
6006 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006007
Calin Juravle98893e12015-10-02 21:05:03 +01006008 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 case TypeCheckKind::kInterfaceCheck: {
6010 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006011 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012 // cases.
6013 //
6014 // We cannot directly call the InstanceofNonTrivial runtime
6015 // entry point without resorting to a type checking slow path
6016 // here (i.e. by calling InvokeRuntime directly), as it would
6017 // require to assign fixed registers for the inputs of this
6018 // HInstanceOf instruction (following the runtime calling
6019 // convention), which might be cluttered by the potential first
6020 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006021 //
6022 // TODO: Introduce a new runtime entry point taking the object
6023 // to test (instead of its class) as argument, and let it deal
6024 // with the read barrier issues. This will let us refactor this
6025 // case of the `switch` code as it was previously (with a direct
6026 // call to the runtime not using a type checking slow path).
6027 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006028 DCHECK(locations->OnlyCallsOnSlowPath());
6029 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6030 /* is_fatal */ false);
6031 codegen_->AddSlowPath(slow_path);
6032 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 if (zero.IsLinked()) {
6034 __ jmp(&done);
6035 }
6036 break;
6037 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006038 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006039
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006040 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006041 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006042 __ xorl(out, out);
6043 }
6044
6045 if (done.IsLinked()) {
6046 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006047 }
6048
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006049 if (slow_path != nullptr) {
6050 __ Bind(slow_path->GetExitLabel());
6051 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006052}
6053
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006054static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006055 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006056 case TypeCheckKind::kExactCheck:
6057 case TypeCheckKind::kAbstractClassCheck:
6058 case TypeCheckKind::kClassHierarchyCheck:
6059 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006060 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006061 case TypeCheckKind::kInterfaceCheck:
6062 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006064 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006065 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006066 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006067 LOG(FATAL) << "Unreachable";
6068 UNREACHABLE();
6069}
6070
6071void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6072 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6073 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6074 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6075 LocationSummary::CallKind call_kind = is_fatal_slow_path
6076 ? LocationSummary::kNoCall
6077 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6079 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006080 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6081 // Require a register for the interface check since there is a loop that compares the class to
6082 // a memory address.
6083 locations->SetInAt(1, Location::RequiresRegister());
6084 } else {
6085 locations->SetInAt(1, Location::Any());
6086 }
6087
Roland Levillain0d5a2812015-11-13 10:07:31 +00006088 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6089 locations->AddTemp(Location::RequiresRegister());
6090 // When read barriers are enabled, we need an additional temporary
6091 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006092 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006094 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006095}
6096
6097void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006098 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006099 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100 Location obj_loc = locations->InAt(0);
6101 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006102 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103 Location temp_loc = locations->GetTemp(0);
6104 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006105 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006106 locations->GetTemp(1) :
6107 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006108 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6109 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6110 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6111 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6112 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6113 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006114 const uint32_t object_array_data_offset =
6115 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006116
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006117 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6118 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6119 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006120 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006121 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 SlowPathCode* type_check_slow_path =
6123 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6124 is_type_check_slow_path_fatal);
6125 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006127
6128 NearLabel done;
6129 // Avoid null check if we know obj is not null.
6130 if (instruction->MustDoNullCheck()) {
6131 __ testl(obj, obj);
6132 __ j(kEqual, &done);
6133 }
6134
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006136 case TypeCheckKind::kExactCheck:
6137 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006138 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006139 GenerateReferenceLoadTwoRegisters(instruction,
6140 temp_loc,
6141 obj_loc,
6142 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006143 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006144 if (cls.IsRegister()) {
6145 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6146 } else {
6147 DCHECK(cls.IsStackSlot()) << cls;
6148 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6149 }
6150 // Jump to slow path for throwing the exception or doing a
6151 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006152 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153 break;
6154 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006157 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006158 GenerateReferenceLoadTwoRegisters(instruction,
6159 temp_loc,
6160 obj_loc,
6161 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006162 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 // If the class is abstract, we eagerly fetch the super class of the
6164 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006165 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006168 GenerateReferenceLoadOneRegister(instruction,
6169 temp_loc,
6170 super_offset,
6171 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006172 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006174 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6175 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006177 // Otherwise, compare the classes.
6178 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 if (cls.IsRegister()) {
6180 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6181 } else {
6182 DCHECK(cls.IsStackSlot()) << cls;
6183 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6184 }
6185 __ j(kNotEqual, &loop);
6186 break;
6187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006190 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006191 GenerateReferenceLoadTwoRegisters(instruction,
6192 temp_loc,
6193 obj_loc,
6194 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006195 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006197 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006198 __ Bind(&loop);
6199 if (cls.IsRegister()) {
6200 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6201 } else {
6202 DCHECK(cls.IsStackSlot()) << cls;
6203 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6204 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006205 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006206
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006208 GenerateReferenceLoadOneRegister(instruction,
6209 temp_loc,
6210 super_offset,
6211 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006212 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213
6214 // If the class reference currently in `temp` is not null, jump
6215 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006217 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 break;
6221 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006224 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006225 GenerateReferenceLoadTwoRegisters(instruction,
6226 temp_loc,
6227 obj_loc,
6228 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006229 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006230 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006232 if (cls.IsRegister()) {
6233 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6234 } else {
6235 DCHECK(cls.IsStackSlot()) << cls;
6236 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6237 }
6238 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006239
6240 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006242 GenerateReferenceLoadOneRegister(instruction,
6243 temp_loc,
6244 component_offset,
6245 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006246 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006247
6248 // If the component type is not null (i.e. the object is indeed
6249 // an array), jump to label `check_non_primitive_component_type`
6250 // to further check that this component type is not a primitive
6251 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006254 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006256 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 break;
6258 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006260 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006261 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 //
6263 // We cannot directly call the CheckCast runtime entry point
6264 // without resorting to a type checking slow path here (i.e. by
6265 // calling InvokeRuntime directly), as it would require to
6266 // assign fixed registers for the inputs of this HInstanceOf
6267 // instruction (following the runtime calling convention), which
6268 // might be cluttered by the potential first read barrier
6269 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006270 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006271 break;
6272 }
6273
6274 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006275 // Fast path for the interface check. We always go slow path for heap poisoning since
6276 // unpoisoning cls would require an extra temp.
6277 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006278 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6279 // doing this.
6280 // /* HeapReference<Class> */ temp = obj->klass_
6281 GenerateReferenceLoadTwoRegisters(instruction,
6282 temp_loc,
6283 obj_loc,
6284 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006285 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006286
6287 // /* HeapReference<Class> */ temp = temp->iftable_
6288 GenerateReferenceLoadTwoRegisters(instruction,
6289 temp_loc,
6290 temp_loc,
6291 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006292 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006293 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006294 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006295 // Loop through the iftable and check if any class matches.
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006296 NearLabel start_loop;
6297 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006298 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006299 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006300 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6301 // Go to next interface if the classes do not match.
6302 __ cmpl(cls.AsRegister<CpuRegister>(),
6303 CodeGeneratorX86_64::ArrayAddress(temp,
6304 maybe_temp2_loc,
6305 TIMES_4,
6306 object_array_data_offset));
6307 __ j(kNotEqual, &start_loop); // Return if same class.
6308 } else {
6309 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006310 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006311 break;
6312 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006314 if (done.IsLinked()) {
6315 __ Bind(&done);
6316 }
6317
Roland Levillain0d5a2812015-11-13 10:07:31 +00006318 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006319}
6320
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006321void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6322 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006324 InvokeRuntimeCallingConvention calling_convention;
6325 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6326}
6327
6328void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006329 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006330 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006331 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006332 if (instruction->IsEnter()) {
6333 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6334 } else {
6335 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6336 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006337}
6338
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006339void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6340void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6341void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6342
6343void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6344 LocationSummary* locations =
6345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6346 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6347 || instruction->GetResultType() == Primitive::kPrimLong);
6348 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006349 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006350 locations->SetOut(Location::SameAsFirstInput());
6351}
6352
6353void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6354 HandleBitwiseOperation(instruction);
6355}
6356
6357void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6358 HandleBitwiseOperation(instruction);
6359}
6360
6361void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6362 HandleBitwiseOperation(instruction);
6363}
6364
6365void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6366 LocationSummary* locations = instruction->GetLocations();
6367 Location first = locations->InAt(0);
6368 Location second = locations->InAt(1);
6369 DCHECK(first.Equals(locations->Out()));
6370
6371 if (instruction->GetResultType() == Primitive::kPrimInt) {
6372 if (second.IsRegister()) {
6373 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006374 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006375 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006376 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006377 } else {
6378 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006379 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006380 }
6381 } else if (second.IsConstant()) {
6382 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6383 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006384 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006385 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006386 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006387 } else {
6388 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006389 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006390 }
6391 } else {
6392 Address address(CpuRegister(RSP), second.GetStackIndex());
6393 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006394 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006395 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006396 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006397 } else {
6398 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006399 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006400 }
6401 }
6402 } else {
6403 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006404 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6405 bool second_is_constant = false;
6406 int64_t value = 0;
6407 if (second.IsConstant()) {
6408 second_is_constant = true;
6409 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006410 }
Mark Mendell40741f32015-04-20 22:10:34 -04006411 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006412
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006413 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006414 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006415 if (is_int32_value) {
6416 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6417 } else {
6418 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6419 }
6420 } else if (second.IsDoubleStackSlot()) {
6421 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006422 } else {
6423 __ andq(first_reg, second.AsRegister<CpuRegister>());
6424 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006425 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006426 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006427 if (is_int32_value) {
6428 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6429 } else {
6430 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6431 }
6432 } else if (second.IsDoubleStackSlot()) {
6433 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006434 } else {
6435 __ orq(first_reg, second.AsRegister<CpuRegister>());
6436 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006437 } else {
6438 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006439 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006440 if (is_int32_value) {
6441 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6442 } else {
6443 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6444 }
6445 } else if (second.IsDoubleStackSlot()) {
6446 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006447 } else {
6448 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6449 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006450 }
6451 }
6452}
6453
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006454void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6455 HInstruction* instruction,
6456 Location out,
6457 uint32_t offset,
6458 Location maybe_temp,
6459 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006460 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006461 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006462 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006463 if (kUseBakerReadBarrier) {
6464 // Load with fast path based Baker's read barrier.
6465 // /* HeapReference<Object> */ out = *(out + offset)
6466 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006467 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006468 } else {
6469 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006470 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006471 // in the following move operation, as we will need it for the
6472 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006473 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006474 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006475 // /* HeapReference<Object> */ out = *(out + offset)
6476 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006477 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006478 }
6479 } else {
6480 // Plain load with no read barrier.
6481 // /* HeapReference<Object> */ out = *(out + offset)
6482 __ movl(out_reg, Address(out_reg, offset));
6483 __ MaybeUnpoisonHeapReference(out_reg);
6484 }
6485}
6486
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006487void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6488 HInstruction* instruction,
6489 Location out,
6490 Location obj,
6491 uint32_t offset,
6492 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006493 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6494 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006495 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006496 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006497 if (kUseBakerReadBarrier) {
6498 // Load with fast path based Baker's read barrier.
6499 // /* HeapReference<Object> */ out = *(obj + offset)
6500 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006501 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006502 } else {
6503 // Load with slow path based read barrier.
6504 // /* HeapReference<Object> */ out = *(obj + offset)
6505 __ movl(out_reg, Address(obj_reg, offset));
6506 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6507 }
6508 } else {
6509 // Plain load with no read barrier.
6510 // /* HeapReference<Object> */ out = *(obj + offset)
6511 __ movl(out_reg, Address(obj_reg, offset));
6512 __ MaybeUnpoisonHeapReference(out_reg);
6513 }
6514}
6515
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006516void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6517 HInstruction* instruction,
6518 Location root,
6519 const Address& address,
6520 Label* fixup_label,
6521 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006522 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006523 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006524 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006525 if (kUseBakerReadBarrier) {
6526 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6527 // Baker's read barrier are used:
6528 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006529 // root = obj.field;
6530 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6531 // if (temp != null) {
6532 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006533 // }
6534
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006535 // /* GcRoot<mirror::Object> */ root = *address
6536 __ movl(root_reg, address);
6537 if (fixup_label != nullptr) {
6538 __ Bind(fixup_label);
6539 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006540 static_assert(
6541 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6542 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6543 "have different sizes.");
6544 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6545 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6546 "have different sizes.");
6547
Vladimir Marko953437b2016-08-24 08:30:46 +00006548 // Slow path marking the GC root `root`.
6549 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006550 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006551 codegen_->AddSlowPath(slow_path);
6552
Roland Levillaind966ce72017-02-09 16:20:14 +00006553 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6554 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01006555 Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00006556 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6557 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006558 __ j(kNotEqual, slow_path->GetEntryLabel());
6559 __ Bind(slow_path->GetExitLabel());
6560 } else {
6561 // GC root loaded through a slow path for read barriers other
6562 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006563 // /* GcRoot<mirror::Object>* */ root = address
6564 __ leaq(root_reg, address);
6565 if (fixup_label != nullptr) {
6566 __ Bind(fixup_label);
6567 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006568 // /* mirror::Object* */ root = root->Read()
6569 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6570 }
6571 } else {
6572 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006573 // /* GcRoot<mirror::Object> */ root = *address
6574 __ movl(root_reg, address);
6575 if (fixup_label != nullptr) {
6576 __ Bind(fixup_label);
6577 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006578 // Note that GC roots are not affected by heap poisoning, thus we
6579 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006580 }
6581}
6582
6583void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6584 Location ref,
6585 CpuRegister obj,
6586 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006587 bool needs_null_check) {
6588 DCHECK(kEmitCompilerReadBarrier);
6589 DCHECK(kUseBakerReadBarrier);
6590
6591 // /* HeapReference<Object> */ ref = *(obj + offset)
6592 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006593 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006594}
6595
6596void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6597 Location ref,
6598 CpuRegister obj,
6599 uint32_t data_offset,
6600 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006601 bool needs_null_check) {
6602 DCHECK(kEmitCompilerReadBarrier);
6603 DCHECK(kUseBakerReadBarrier);
6604
Roland Levillain3d312422016-06-23 13:53:42 +01006605 static_assert(
6606 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6607 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006608 // /* HeapReference<Object> */ ref =
6609 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006610 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006611 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006612}
6613
6614void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6615 Location ref,
6616 CpuRegister obj,
6617 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006618 bool needs_null_check,
6619 bool always_update_field,
6620 CpuRegister* temp1,
6621 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006622 DCHECK(kEmitCompilerReadBarrier);
6623 DCHECK(kUseBakerReadBarrier);
6624
6625 // In slow path based read barriers, the read barrier call is
6626 // inserted after the original load. However, in fast path based
6627 // Baker's read barriers, we need to perform the load of
6628 // mirror::Object::monitor_ *before* the original reference load.
6629 // This load-load ordering is required by the read barrier.
6630 // The fast path/slow path (for Baker's algorithm) should look like:
6631 //
6632 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6633 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6634 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006635 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006636 // if (is_gray) {
6637 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6638 // }
6639 //
6640 // Note: the original implementation in ReadBarrier::Barrier is
6641 // slightly more complex as:
6642 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006643 // the high-bits of rb_state, which are expected to be all zeroes
6644 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6645 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006646 // - it performs additional checks that we do not do here for
6647 // performance reasons.
6648
6649 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006650 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6651
Vladimir Marko953437b2016-08-24 08:30:46 +00006652 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006653 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6654 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006655 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6656 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6657 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6658
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006659 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006660 // ref = ReadBarrier::Mark(ref);
6661 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6662 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006663 if (needs_null_check) {
6664 MaybeRecordImplicitNullCheck(instruction);
6665 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006666
6667 // Load fence to prevent load-load reordering.
6668 // Note that this is a no-op, thanks to the x86-64 memory model.
6669 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6670
6671 // The actual reference load.
6672 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006673 __ movl(ref_reg, src); // Flags are unaffected.
6674
6675 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6676 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006677 SlowPathCode* slow_path;
6678 if (always_update_field) {
6679 DCHECK(temp1 != nullptr);
6680 DCHECK(temp2 != nullptr);
6681 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6682 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6683 } else {
6684 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6685 instruction, ref, /* unpoison_ref_before_marking */ true);
6686 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006687 AddSlowPath(slow_path);
6688
6689 // We have done the "if" of the gray bit check above, now branch based on the flags.
6690 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006691
6692 // Object* ref = ref_addr->AsMirrorPtr()
6693 __ MaybeUnpoisonHeapReference(ref_reg);
6694
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006695 __ Bind(slow_path->GetExitLabel());
6696}
6697
6698void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6699 Location out,
6700 Location ref,
6701 Location obj,
6702 uint32_t offset,
6703 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 DCHECK(kEmitCompilerReadBarrier);
6705
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006706 // Insert a slow path based read barrier *after* the reference load.
6707 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708 // If heap poisoning is enabled, the unpoisoning of the loaded
6709 // reference will be carried out by the runtime within the slow
6710 // path.
6711 //
6712 // Note that `ref` currently does not get unpoisoned (when heap
6713 // poisoning is enabled), which is alright as the `ref` argument is
6714 // not used by the artReadBarrierSlow entry point.
6715 //
6716 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6717 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6718 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6719 AddSlowPath(slow_path);
6720
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 __ jmp(slow_path->GetEntryLabel());
6722 __ Bind(slow_path->GetExitLabel());
6723}
6724
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006725void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6726 Location out,
6727 Location ref,
6728 Location obj,
6729 uint32_t offset,
6730 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006731 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006732 // Baker's read barriers shall be handled by the fast path
6733 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6734 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006735 // If heap poisoning is enabled, unpoisoning will be taken care of
6736 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006737 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006738 } else if (kPoisonHeapReferences) {
6739 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6740 }
6741}
6742
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006743void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6744 Location out,
6745 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006746 DCHECK(kEmitCompilerReadBarrier);
6747
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006748 // Insert a slow path based read barrier *after* the GC root load.
6749 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750 // Note that GC roots are not affected by heap poisoning, so we do
6751 // not need to do anything special for this here.
6752 SlowPathCode* slow_path =
6753 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6754 AddSlowPath(slow_path);
6755
Roland Levillain0d5a2812015-11-13 10:07:31 +00006756 __ jmp(slow_path->GetEntryLabel());
6757 __ Bind(slow_path->GetExitLabel());
6758}
6759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006760void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006761 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006762 LOG(FATAL) << "Unreachable";
6763}
6764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006765void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006766 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006767 LOG(FATAL) << "Unreachable";
6768}
6769
Mark Mendellfe57faa2015-09-18 09:26:15 -04006770// Simple implementation of packed switch - generate cascaded compare/jumps.
6771void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6772 LocationSummary* locations =
6773 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6774 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006775 locations->AddTemp(Location::RequiresRegister());
6776 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006777}
6778
6779void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6780 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006781 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006782 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006783 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6784 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6785 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006786 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6787
6788 // Should we generate smaller inline compare/jumps?
6789 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6790 // Figure out the correct compare values and jump conditions.
6791 // Handle the first compare/branch as a special case because it might
6792 // jump to the default case.
6793 DCHECK_GT(num_entries, 2u);
6794 Condition first_condition;
6795 uint32_t index;
6796 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6797 if (lower_bound != 0) {
6798 first_condition = kLess;
6799 __ cmpl(value_reg_in, Immediate(lower_bound));
6800 __ j(first_condition, codegen_->GetLabelOf(default_block));
6801 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6802
6803 index = 1;
6804 } else {
6805 // Handle all the compare/jumps below.
6806 first_condition = kBelow;
6807 index = 0;
6808 }
6809
6810 // Handle the rest of the compare/jumps.
6811 for (; index + 1 < num_entries; index += 2) {
6812 int32_t compare_to_value = lower_bound + index + 1;
6813 __ cmpl(value_reg_in, Immediate(compare_to_value));
6814 // Jump to successors[index] if value < case_value[index].
6815 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6816 // Jump to successors[index + 1] if value == case_value[index + 1].
6817 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6818 }
6819
6820 if (index != num_entries) {
6821 // There are an odd number of entries. Handle the last one.
6822 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006823 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006824 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6825 }
6826
6827 // And the default for any other value.
6828 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6829 __ jmp(codegen_->GetLabelOf(default_block));
6830 }
6831 return;
6832 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006833
6834 // Remove the bias, if needed.
6835 Register value_reg_out = value_reg_in.AsRegister();
6836 if (lower_bound != 0) {
6837 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6838 value_reg_out = temp_reg.AsRegister();
6839 }
6840 CpuRegister value_reg(value_reg_out);
6841
6842 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006843 __ cmpl(value_reg, Immediate(num_entries - 1));
6844 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006845
Mark Mendell9c86b482015-09-18 13:36:07 -04006846 // We are in the range of the table.
6847 // Load the address of the jump table in the constant area.
6848 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006849
Mark Mendell9c86b482015-09-18 13:36:07 -04006850 // Load the (signed) offset from the jump table.
6851 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6852
6853 // Add the offset to the address of the table base.
6854 __ addq(temp_reg, base_reg);
6855
6856 // And jump.
6857 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006858}
6859
Aart Bikc5d47542016-01-27 17:00:35 -08006860void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6861 if (value == 0) {
6862 __ xorl(dest, dest);
6863 } else {
6864 __ movl(dest, Immediate(value));
6865 }
6866}
6867
Mark Mendell92e83bf2015-05-07 11:25:03 -04006868void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6869 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006870 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006871 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006872 } else if (IsUint<32>(value)) {
6873 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006874 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6875 } else {
6876 __ movq(dest, Immediate(value));
6877 }
6878}
6879
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006880void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6881 if (value == 0) {
6882 __ xorps(dest, dest);
6883 } else {
6884 __ movss(dest, LiteralInt32Address(value));
6885 }
6886}
6887
6888void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6889 if (value == 0) {
6890 __ xorpd(dest, dest);
6891 } else {
6892 __ movsd(dest, LiteralInt64Address(value));
6893 }
6894}
6895
6896void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6897 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6898}
6899
6900void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6901 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6902}
6903
Aart Bika19616e2016-02-01 18:57:58 -08006904void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6905 if (value == 0) {
6906 __ testl(dest, dest);
6907 } else {
6908 __ cmpl(dest, Immediate(value));
6909 }
6910}
6911
6912void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6913 if (IsInt<32>(value)) {
6914 if (value == 0) {
6915 __ testq(dest, dest);
6916 } else {
6917 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6918 }
6919 } else {
6920 // Value won't fit in an int.
6921 __ cmpq(dest, LiteralInt64Address(value));
6922 }
6923}
6924
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006925void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6926 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006927 GenerateIntCompare(lhs_reg, rhs);
6928}
6929
6930void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006931 if (rhs.IsConstant()) {
6932 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006933 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006934 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006935 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006936 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006937 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006938 }
6939}
6940
6941void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6942 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6943 if (rhs.IsConstant()) {
6944 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6945 Compare64BitValue(lhs_reg, value);
6946 } else if (rhs.IsDoubleStackSlot()) {
6947 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6948 } else {
6949 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6950 }
6951}
6952
6953Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6954 Location index,
6955 ScaleFactor scale,
6956 uint32_t data_offset) {
6957 return index.IsConstant() ?
6958 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6959 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6960}
6961
Mark Mendellcfa410b2015-05-25 16:02:44 -04006962void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6963 DCHECK(dest.IsDoubleStackSlot());
6964 if (IsInt<32>(value)) {
6965 // Can move directly as an int32 constant.
6966 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6967 Immediate(static_cast<int32_t>(value)));
6968 } else {
6969 Load64BitValue(CpuRegister(TMP), value);
6970 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6971 }
6972}
6973
Mark Mendell9c86b482015-09-18 13:36:07 -04006974/**
6975 * Class to handle late fixup of offsets into constant area.
6976 */
6977class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6978 public:
6979 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6980 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6981
6982 protected:
6983 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6984
6985 CodeGeneratorX86_64* codegen_;
6986
6987 private:
6988 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6989 // Patch the correct offset for the instruction. We use the address of the
6990 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6991 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6992 int32_t relative_position = constant_offset - pos;
6993
6994 // Patch in the right value.
6995 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6996 }
6997
6998 // Location in constant area that the fixup refers to.
6999 size_t offset_into_constant_area_;
7000};
7001
7002/**
7003 t * Class to handle late fixup of offsets to a jump table that will be created in the
7004 * constant area.
7005 */
7006class JumpTableRIPFixup : public RIPFixup {
7007 public:
7008 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7009 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7010
7011 void CreateJumpTable() {
7012 X86_64Assembler* assembler = codegen_->GetAssembler();
7013
7014 // Ensure that the reference to the jump table has the correct offset.
7015 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7016 SetOffset(offset_in_constant_table);
7017
7018 // Compute the offset from the start of the function to this jump table.
7019 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7020
7021 // Populate the jump table with the correct values for the jump table.
7022 int32_t num_entries = switch_instr_->GetNumEntries();
7023 HBasicBlock* block = switch_instr_->GetBlock();
7024 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7025 // The value that we want is the target offset - the position of the table.
7026 for (int32_t i = 0; i < num_entries; i++) {
7027 HBasicBlock* b = successors[i];
7028 Label* l = codegen_->GetLabelOf(b);
7029 DCHECK(l->IsBound());
7030 int32_t offset_to_block = l->Position() - current_table_offset;
7031 assembler->AppendInt32(offset_to_block);
7032 }
7033 }
7034
7035 private:
7036 const HPackedSwitch* switch_instr_;
7037};
7038
Mark Mendellf55c3e02015-03-26 21:07:46 -04007039void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7040 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007041 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007042 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7043 // 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 -04007044 assembler->Align(4, 0);
7045 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007046
7047 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007048 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007049 jump_table->CreateJumpTable();
7050 }
7051
7052 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007053 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007054 }
7055
7056 // And finish up.
7057 CodeGenerator::Finalize(allocator);
7058}
7059
Mark Mendellf55c3e02015-03-26 21:07:46 -04007060Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7061 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7062 return Address::RIP(fixup);
7063}
7064
7065Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7066 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7067 return Address::RIP(fixup);
7068}
7069
7070Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7071 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7072 return Address::RIP(fixup);
7073}
7074
7075Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7076 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7077 return Address::RIP(fixup);
7078}
7079
Andreas Gampe85b62f22015-09-09 13:15:38 -07007080// TODO: trg as memory.
7081void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7082 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007083 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007084 return;
7085 }
7086
7087 DCHECK_NE(type, Primitive::kPrimVoid);
7088
7089 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7090 if (trg.Equals(return_loc)) {
7091 return;
7092 }
7093
7094 // Let the parallel move resolver take care of all of this.
7095 HParallelMove parallel_move(GetGraph()->GetArena());
7096 parallel_move.AddMove(return_loc, trg, type, nullptr);
7097 GetMoveResolver()->EmitNativeCode(&parallel_move);
7098}
7099
Mark Mendell9c86b482015-09-18 13:36:07 -04007100Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7101 // Create a fixup to be used to create and address the jump table.
7102 JumpTableRIPFixup* table_fixup =
7103 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7104
7105 // We have to populate the jump tables.
7106 fixups_to_jump_tables_.push_back(table_fixup);
7107 return Address::RIP(table_fixup);
7108}
7109
Mark Mendellea5af682015-10-22 17:35:49 -04007110void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7111 const Address& addr_high,
7112 int64_t v,
7113 HInstruction* instruction) {
7114 if (IsInt<32>(v)) {
7115 int32_t v_32 = v;
7116 __ movq(addr_low, Immediate(v_32));
7117 MaybeRecordImplicitNullCheck(instruction);
7118 } else {
7119 // Didn't fit in a register. Do it in pieces.
7120 int32_t low_v = Low32Bits(v);
7121 int32_t high_v = High32Bits(v);
7122 __ movl(addr_low, Immediate(low_v));
7123 MaybeRecordImplicitNullCheck(instruction);
7124 __ movl(addr_high, Immediate(high_v));
7125 }
7126}
7127
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007128void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7129 const uint8_t* roots_data,
7130 const PatchInfo<Label>& info,
7131 uint64_t index_in_table) const {
7132 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7133 uintptr_t address =
7134 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7135 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7136 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7137 dchecked_integral_cast<uint32_t>(address);
7138}
7139
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007140void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7141 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007142 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007143 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007144 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007145 uint64_t index_in_table = it->second;
7146 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007147 }
7148
7149 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007150 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007151 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7152 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007153 uint64_t index_in_table = it->second;
7154 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007155 }
7156}
7157
Roland Levillain4d027112015-07-01 15:41:14 +01007158#undef __
7159
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007160} // namespace x86_64
7161} // namespace art