blob: 4474decf59044616d9445155cb9dc60562775193 [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"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain7cbd27f2016-08-11 23:53:33 +010054// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Andreas Gampe85b62f22015-09-09 13:15:38 -070058class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000060 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Alexandre Rames2ed20af2015-03-06 13:55:35 +000062 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000065 if (instruction_->CanThrowIntoCatchBlock()) {
66 // Live registers will be restored in the catch block if caught.
67 SaveLiveRegisters(codegen, instruction_->GetLocations());
68 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010069 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000070 instruction_,
71 instruction_->GetDexPc(),
72 this);
Roland Levillain888d0672015-11-23 18:53:50 +000073 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 }
75
Alexandre Rames8158f282015-08-07 10:26:17 +010076 bool IsFatal() const OVERRIDE { return true; }
77
Alexandre Rames9931f312015-06-19 14:47:01 +010078 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
79
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000086 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000089 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010091 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000092 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
101};
102
Andreas Gampe85b62f22015-09-09 13:15:38 -0700103class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000104 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000105 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
106 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 if (is_div_) {
112 __ negl(cpu_reg_);
113 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400114 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 }
116
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 } else {
118 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 if (is_div_) {
120 __ negq(cpu_reg_);
121 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400122 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 }
Calin Juravled0d48522014-11-04 16:40:20 +0000125 __ jmp(GetExitLabel());
126 }
127
Alexandre Rames9931f312015-06-19 14:47:01 +0100128 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
129
Calin Juravled0d48522014-11-04 16:40:20 +0000130 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000131 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000132 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000133 const bool is_div_;
134 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000135};
136
Andreas Gampe85b62f22015-09-09 13:15:38 -0700137class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100139 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000140 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000142 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000143 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100145 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000146 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 if (successor_ == nullptr) {
148 __ jmp(GetReturnLabel());
149 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 }
153
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 Label* GetReturnLabel() {
155 DCHECK(successor_ == nullptr);
156 return &return_label_;
157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100159 HBasicBlock* GetSuccessor() const {
160 return successor_;
161 }
162
Alexandre Rames9931f312015-06-19 14:47:01 +0100163 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
164
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 Label return_label_;
168
169 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
170};
171
Andreas Gampe85b62f22015-09-09 13:15:38 -0700172class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100174 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000175 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000177 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000179 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000181 if (instruction_->CanThrowIntoCatchBlock()) {
182 // Live registers will be restored in the catch block if caught.
183 SaveLiveRegisters(codegen, instruction_->GetLocations());
184 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400185 // Are we using an array length from memory?
186 HInstruction* array_length = instruction_->InputAt(1);
187 Location length_loc = locations->InAt(1);
188 InvokeRuntimeCallingConvention calling_convention;
189 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
190 // Load the array length into our temporary.
191 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
192 Location array_loc = array_length->GetLocations()->InAt(0);
193 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
194 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
195 // Check for conflicts with index.
196 if (length_loc.Equals(locations->InAt(0))) {
197 // We know we aren't using parameter 2.
198 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
199 }
200 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700201 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100202 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700203 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400204 }
205
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000206 // We're moving two locations to locations that could overlap, so we need a parallel
207 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000208 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100209 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000210 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100211 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400212 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100213 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
214 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100215 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
216 ? kQuickThrowStringBounds
217 : kQuickThrowArrayBounds;
218 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100219 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000220 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100221 }
222
Alexandre Rames8158f282015-08-07 10:26:17 +0100223 bool IsFatal() const OVERRIDE { return true; }
224
Alexandre Rames9931f312015-06-19 14:47:01 +0100225 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
226
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100227 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100228 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
229};
230
Andreas Gampe85b62f22015-09-09 13:15:38 -0700231class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 LoadClassSlowPathX86_64(HLoadClass* cls,
234 HInstruction* at,
235 uint32_t dex_pc,
236 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000237 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
239 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000241 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000243 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100245
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800249 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
250 Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100251 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000252 at_,
253 dex_pc_,
254 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000255 if (do_clinit_) {
256 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
257 } else {
258 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
259 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000261 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263 if (out.IsValid()) {
264 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000265 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 }
267
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100269 __ jmp(GetExitLabel());
270 }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
273
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100274 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000275 // The class this slow path will load.
276 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100277
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 // The instruction where this slow path is happening.
279 // (Might be the load class or an initialization check).
280 HInstruction* const at_;
281
282 // The dex PC of `at_`.
283 const uint32_t dex_pc_;
284
285 // Whether to initialize the class.
286 const bool do_clinit_;
287
288 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100289};
290
Vladimir Markoaad75c62016-10-03 08:46:48 +0000291class LoadStringSlowPathX86_64 : public SlowPathCode {
292 public:
293 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
294
295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296 LocationSummary* locations = instruction_->GetLocations();
297 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
298
299 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
300 __ Bind(GetEntryLabel());
301 SaveLiveRegisters(codegen, locations);
302
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800303 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex().index_;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100304 // Custom calling convention: RAX serves as both input and output.
305 __ movl(CpuRegister(RAX), Immediate(string_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000306 x86_64_codegen->InvokeRuntime(kQuickResolveString,
307 instruction_,
308 instruction_->GetDexPc(),
309 this);
310 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
311 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
312 RestoreLiveRegisters(codegen, locations);
313
314 // Store the resolved String to the BSS entry.
315 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
316 locations->Out().AsRegister<CpuRegister>());
317 Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
318 __ Bind(fixup_label);
319
320 __ jmp(GetExitLabel());
321 }
322
323 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
324
325 private:
326 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
327};
328
Andreas Gampe85b62f22015-09-09 13:15:38 -0700329class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000332 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100336 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 DCHECK(instruction_->IsCheckCast()
338 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
Roland Levillain0d5a2812015-11-13 10:07:31 +0000340 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000342
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343 if (!is_fatal_) {
344 SaveLiveRegisters(codegen, locations);
345 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
352 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800353 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
355 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100357 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 } else {
360 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800361 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
362 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 if (!is_fatal_) {
366 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000367 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 RestoreLiveRegisters(codegen, locations);
371 __ jmp(GetExitLabel());
372 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 bool IsFatal() const OVERRIDE { return is_fatal_; }
378
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000380 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381
382 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
383};
384
Andreas Gampe85b62f22015-09-09 13:15:38 -0700385class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 public:
Aart Bik42249c32016-01-07 15:33:50 -0800387 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000391 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700392 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100393 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
401};
402
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100403class ArraySetSlowPathX86_64 : public SlowPathCode {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConvention calling_convention;
413 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
414 parallel_move.AddMove(
415 locations->InAt(0),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
417 Primitive::kPrimNot,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(1),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
422 Primitive::kPrimInt,
423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(2),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
427 Primitive::kPrimNot,
428 nullptr);
429 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
430
Roland Levillain0d5a2812015-11-13 10:07:31 +0000431 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100432 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000433 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 RestoreLiveRegisters(codegen, locations);
435 __ jmp(GetExitLabel());
436 }
437
438 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
439
440 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
442};
443
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100444// Slow path marking an object reference `ref` during a read
445// barrier. The field `obj.field` in the object `obj` holding this
446// reference does not get updated by this slow path after marking (see
447// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
448//
449// This means that after the execution of this slow path, `ref` will
450// always be up-to-date, but `obj.field` may not; i.e., after the
451// flip, `ref` will be a to-space reference, but `obj.field` will
452// probably still be a from-space reference (unless it gets updated by
453// another thread, or if another thread installed another object
454// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000455class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
456 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100457 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
458 Location ref,
459 bool unpoison_ref_before_marking)
460 : SlowPathCode(instruction),
461 ref_(ref),
462 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000463 DCHECK(kEmitCompilerReadBarrier);
464 }
465
466 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
467
468 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
469 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
471 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000472 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100473 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000474 DCHECK(instruction_->IsInstanceFieldGet() ||
475 instruction_->IsStaticFieldGet() ||
476 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100477 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000478 instruction_->IsLoadClass() ||
479 instruction_->IsLoadString() ||
480 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100481 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100482 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
483 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000484 << "Unexpected instruction in read barrier marking slow path: "
485 << instruction_->DebugName();
486
487 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100488 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000489 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000491 }
Roland Levillain4359e612016-07-20 11:32:19 +0100492 // No need to save live registers; it's taken care of by the
493 // entrypoint. Also, there is no need to update the stack mask,
494 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000495 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100496 DCHECK_NE(ref_reg, RSP);
497 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100498 // "Compact" slow path, saving two moves.
499 //
500 // Instead of using the standard runtime calling convention (input
501 // and output in R0):
502 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100504 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100506 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // of a dedicated entrypoint:
509 //
510 // rX <- ReadBarrierMarkRegX(rX)
511 //
512 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100514 // This runtime call does not require a stack map.
515 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000516 __ jmp(GetExitLabel());
517 }
518
519 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 // The location (register) of the marked object reference.
521 const Location ref_;
522 // Should the reference in `ref_` be unpoisoned prior to marking it?
523 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000524
525 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
526};
527
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528// Slow path marking an object reference `ref` during a read barrier,
529// and if needed, atomically updating the field `obj.field` in the
530// object `obj` holding this reference after marking (contrary to
531// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
532// `obj.field`).
533//
534// This means that after the execution of this slow path, both `ref`
535// and `obj.field` will be up-to-date; i.e., after the flip, both will
536// hold the same to-space reference (unless another thread installed
537// another object reference (different from `ref`) in `obj.field`).
538class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
539 public:
540 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
541 Location ref,
542 CpuRegister obj,
543 const Address& field_addr,
544 bool unpoison_ref_before_marking,
545 CpuRegister temp1,
546 CpuRegister temp2)
547 : SlowPathCode(instruction),
548 ref_(ref),
549 obj_(obj),
550 field_addr_(field_addr),
551 unpoison_ref_before_marking_(unpoison_ref_before_marking),
552 temp1_(temp1),
553 temp2_(temp2) {
554 DCHECK(kEmitCompilerReadBarrier);
555 }
556
557 const char* GetDescription() const OVERRIDE {
558 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
559 }
560
561 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
562 LocationSummary* locations = instruction_->GetLocations();
563 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
564 Register ref_reg = ref_cpu_reg.AsRegister();
565 DCHECK(locations->CanCall());
566 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
567 // This slow path is only used by the UnsafeCASObject intrinsic.
568 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
569 << "Unexpected instruction in read barrier marking and field updating slow path: "
570 << instruction_->DebugName();
571 DCHECK(instruction_->GetLocations()->Intrinsified());
572 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
573
574 __ Bind(GetEntryLabel());
575 if (unpoison_ref_before_marking_) {
576 // Object* ref = ref_addr->AsMirrorPtr()
577 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
578 }
579
580 // Save the old (unpoisoned) reference.
581 __ movl(temp1_, ref_cpu_reg);
582
583 // No need to save live registers; it's taken care of by the
584 // entrypoint. Also, there is no need to update the stack mask,
585 // as this runtime call will not trigger a garbage collection.
586 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
587 DCHECK_NE(ref_reg, RSP);
588 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
589 // "Compact" slow path, saving two moves.
590 //
591 // Instead of using the standard runtime calling convention (input
592 // and output in R0):
593 //
594 // RDI <- ref
595 // RAX <- ReadBarrierMark(RDI)
596 // ref <- RAX
597 //
598 // we just use rX (the register containing `ref`) as input and output
599 // of a dedicated entrypoint:
600 //
601 // rX <- ReadBarrierMarkRegX(rX)
602 //
603 int32_t entry_point_offset =
604 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
605 // This runtime call does not require a stack map.
606 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
607
608 // If the new reference is different from the old reference,
609 // update the field in the holder (`*field_addr`).
610 //
611 // Note that this field could also hold a different object, if
612 // another thread had concurrently changed it. In that case, the
613 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
614 // operation below would abort the CAS, leaving the field as-is.
615 NearLabel done;
616 __ cmpl(temp1_, ref_cpu_reg);
617 __ j(kEqual, &done);
618
619 // Update the the holder's field atomically. This may fail if
620 // mutator updates before us, but it's OK. This is achived
621 // using a strong compare-and-set (CAS) operation with relaxed
622 // memory synchronization ordering, where the expected value is
623 // the old reference and the desired value is the new reference.
624 // This operation is implemented with a 32-bit LOCK CMPXLCHG
625 // instruction, which requires the expected value (the old
626 // reference) to be in EAX. Save RAX beforehand, and move the
627 // expected value (stored in `temp1_`) into EAX.
628 __ movq(temp2_, CpuRegister(RAX));
629 __ movl(CpuRegister(RAX), temp1_);
630
631 // Convenience aliases.
632 CpuRegister base = obj_;
633 CpuRegister expected = CpuRegister(RAX);
634 CpuRegister value = ref_cpu_reg;
635
636 bool base_equals_value = (base.AsRegister() == value.AsRegister());
637 Register value_reg = ref_reg;
638 if (kPoisonHeapReferences) {
639 if (base_equals_value) {
640 // If `base` and `value` are the same register location, move
641 // `value_reg` to a temporary register. This way, poisoning
642 // `value_reg` won't invalidate `base`.
643 value_reg = temp1_.AsRegister();
644 __ movl(CpuRegister(value_reg), base);
645 }
646
647 // Check that the register allocator did not assign the location
648 // of `expected` (RAX) to `value` nor to `base`, so that heap
649 // poisoning (when enabled) works as intended below.
650 // - If `value` were equal to `expected`, both references would
651 // be poisoned twice, meaning they would not be poisoned at
652 // all, as heap poisoning uses address negation.
653 // - If `base` were equal to `expected`, poisoning `expected`
654 // would invalidate `base`.
655 DCHECK_NE(value_reg, expected.AsRegister());
656 DCHECK_NE(base.AsRegister(), expected.AsRegister());
657
658 __ PoisonHeapReference(expected);
659 __ PoisonHeapReference(CpuRegister(value_reg));
660 }
661
662 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
663
664 // If heap poisoning is enabled, we need to unpoison the values
665 // that were poisoned earlier.
666 if (kPoisonHeapReferences) {
667 if (base_equals_value) {
668 // `value_reg` has been moved to a temporary register, no need
669 // to unpoison it.
670 } else {
671 __ UnpoisonHeapReference(CpuRegister(value_reg));
672 }
673 // No need to unpoison `expected` (RAX), as it is be overwritten below.
674 }
675
676 // Restore RAX.
677 __ movq(CpuRegister(RAX), temp2_);
678
679 __ Bind(&done);
680 __ jmp(GetExitLabel());
681 }
682
683 private:
684 // The location (register) of the marked object reference.
685 const Location ref_;
686 // The register containing the object holding the marked object reference field.
687 const CpuRegister obj_;
688 // The address of the marked reference field. The base of this address must be `obj_`.
689 const Address field_addr_;
690
691 // Should the reference in `ref_` be unpoisoned prior to marking it?
692 const bool unpoison_ref_before_marking_;
693
694 const CpuRegister temp1_;
695 const CpuRegister temp2_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
698};
699
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700// Slow path generating a read barrier for a heap reference.
701class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
702 public:
703 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
704 Location out,
705 Location ref,
706 Location obj,
707 uint32_t offset,
708 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000709 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 out_(out),
711 ref_(ref),
712 obj_(obj),
713 offset_(offset),
714 index_(index) {
715 DCHECK(kEmitCompilerReadBarrier);
716 // If `obj` is equal to `out` or `ref`, it means the initial
717 // object has been overwritten by (or after) the heap object
718 // reference load to be instrumented, e.g.:
719 //
720 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000721 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722 //
723 // In that case, we have lost the information about the original
724 // object, and the emitted read barrier cannot work properly.
725 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
726 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
727}
728
729 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
730 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
731 LocationSummary* locations = instruction_->GetLocations();
732 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
733 DCHECK(locations->CanCall());
734 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100735 DCHECK(instruction_->IsInstanceFieldGet() ||
736 instruction_->IsStaticFieldGet() ||
737 instruction_->IsArrayGet() ||
738 instruction_->IsInstanceOf() ||
739 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100740 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000741 << "Unexpected instruction in read barrier for heap reference slow path: "
742 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000743
744 __ Bind(GetEntryLabel());
745 SaveLiveRegisters(codegen, locations);
746
747 // We may have to change the index's value, but as `index_` is a
748 // constant member (like other "inputs" of this slow path),
749 // introduce a copy of it, `index`.
750 Location index = index_;
751 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000753 if (instruction_->IsArrayGet()) {
754 // Compute real offset and store it in index_.
755 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
756 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
757 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
758 // We are about to change the value of `index_reg` (see the
759 // calls to art::x86_64::X86_64Assembler::shll and
760 // art::x86_64::X86_64Assembler::AddImmediate below), but it
761 // has not been saved by the previous call to
762 // art::SlowPathCode::SaveLiveRegisters, as it is a
763 // callee-save register --
764 // art::SlowPathCode::SaveLiveRegisters does not consider
765 // callee-save registers, as it has been designed with the
766 // assumption that callee-save registers are supposed to be
767 // handled by the called function. So, as a callee-save
768 // register, `index_reg` _would_ eventually be saved onto
769 // the stack, but it would be too late: we would have
770 // changed its value earlier. Therefore, we manually save
771 // it here into another freely available register,
772 // `free_reg`, chosen of course among the caller-save
773 // registers (as a callee-save `free_reg` register would
774 // exhibit the same problem).
775 //
776 // Note we could have requested a temporary register from
777 // the register allocator instead; but we prefer not to, as
778 // this is a slow path, and we know we can find a
779 // caller-save register that is available.
780 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
781 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
782 index_reg = free_reg;
783 index = Location::RegisterLocation(index_reg);
784 } else {
785 // The initial register stored in `index_` has already been
786 // saved in the call to art::SlowPathCode::SaveLiveRegisters
787 // (as it is not a callee-save register), so we can freely
788 // use it.
789 }
790 // Shifting the index value contained in `index_reg` by the
791 // scale factor (2) cannot overflow in practice, as the
792 // runtime is unable to allocate object arrays with a size
793 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
794 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
795 static_assert(
796 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
797 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
798 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
799 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100800 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
801 // intrinsics, `index_` is not shifted by a scale factor of 2
802 // (as in the case of ArrayGet), as it is actually an offset
803 // to an object field within an object.
804 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000805 DCHECK(instruction_->GetLocations()->Intrinsified());
806 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
807 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
808 << instruction_->AsInvoke()->GetIntrinsic();
809 DCHECK_EQ(offset_, 0U);
810 DCHECK(index_.IsRegister());
811 }
812 }
813
814 // We're moving two or three locations to locations that could
815 // overlap, so we need a parallel move resolver.
816 InvokeRuntimeCallingConvention calling_convention;
817 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
818 parallel_move.AddMove(ref_,
819 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
820 Primitive::kPrimNot,
821 nullptr);
822 parallel_move.AddMove(obj_,
823 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
824 Primitive::kPrimNot,
825 nullptr);
826 if (index.IsValid()) {
827 parallel_move.AddMove(index,
828 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
829 Primitive::kPrimInt,
830 nullptr);
831 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
832 } else {
833 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
834 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
835 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100836 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000837 instruction_,
838 instruction_->GetDexPc(),
839 this);
840 CheckEntrypointTypes<
841 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
842 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
843
844 RestoreLiveRegisters(codegen, locations);
845 __ jmp(GetExitLabel());
846 }
847
848 const char* GetDescription() const OVERRIDE {
849 return "ReadBarrierForHeapReferenceSlowPathX86_64";
850 }
851
852 private:
853 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
854 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
855 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
856 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
857 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
858 return static_cast<CpuRegister>(i);
859 }
860 }
861 // We shall never fail to find a free caller-save register, as
862 // there are more than two core caller-save registers on x86-64
863 // (meaning it is possible to find one which is different from
864 // `ref` and `obj`).
865 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
866 LOG(FATAL) << "Could not find a free caller-save register";
867 UNREACHABLE();
868 }
869
Roland Levillain0d5a2812015-11-13 10:07:31 +0000870 const Location out_;
871 const Location ref_;
872 const Location obj_;
873 const uint32_t offset_;
874 // An additional location containing an index to an array.
875 // Only used for HArrayGet and the UnsafeGetObject &
876 // UnsafeGetObjectVolatile intrinsics.
877 const Location index_;
878
879 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
880};
881
882// Slow path generating a read barrier for a GC root.
883class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
884 public:
885 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000886 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000887 DCHECK(kEmitCompilerReadBarrier);
888 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889
890 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
891 LocationSummary* locations = instruction_->GetLocations();
892 DCHECK(locations->CanCall());
893 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000894 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
895 << "Unexpected instruction in read barrier for GC root slow path: "
896 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897
898 __ Bind(GetEntryLabel());
899 SaveLiveRegisters(codegen, locations);
900
901 InvokeRuntimeCallingConvention calling_convention;
902 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
903 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100904 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 instruction_,
906 instruction_->GetDexPc(),
907 this);
908 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
909 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
910
911 RestoreLiveRegisters(codegen, locations);
912 __ jmp(GetExitLabel());
913 }
914
915 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
916
917 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918 const Location out_;
919 const Location root_;
920
921 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
922};
923
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100924#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100925// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
926#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100927
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
932 case kCondLT: return kLess;
933 case kCondLE: return kLessEqual;
934 case kCondGT: return kGreater;
935 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700936 case kCondB: return kBelow;
937 case kCondBE: return kBelowEqual;
938 case kCondA: return kAbove;
939 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700940 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100941 LOG(FATAL) << "Unreachable";
942 UNREACHABLE();
943}
944
Aart Bike9f37602015-10-09 11:15:55 -0700945// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100946inline Condition X86_64FPCondition(IfCondition cond) {
947 switch (cond) {
948 case kCondEQ: return kEqual;
949 case kCondNE: return kNotEqual;
950 case kCondLT: return kBelow;
951 case kCondLE: return kBelowEqual;
952 case kCondGT: return kAbove;
953 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700954 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100955 };
956 LOG(FATAL) << "Unreachable";
957 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700958}
959
Vladimir Markodc151b22015-10-15 18:02:30 +0100960HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
961 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100962 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100963 switch (desired_dispatch_info.code_ptr_location) {
964 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
965 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
966 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
967 return HInvokeStaticOrDirect::DispatchInfo {
968 desired_dispatch_info.method_load_kind,
969 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
970 desired_dispatch_info.method_load_data,
971 0u
972 };
973 default:
974 return desired_dispatch_info;
975 }
976}
977
Serguei Katkov288c7a82016-05-16 11:53:15 +0600978Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
979 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800980 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000981 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
982 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100983 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000984 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100985 uint32_t offset =
986 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
987 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000988 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100989 }
Vladimir Marko58155012015-08-19 12:49:41 +0000990 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000991 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000992 break;
993 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
994 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
995 break;
996 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
997 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000998 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
999 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00001000 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
1001 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001002 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +00001003 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001004 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001005 // Bind a new fixup label at the end of the "movl" insn.
1006 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001007 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +00001008 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001009 }
Vladimir Marko58155012015-08-19 12:49:41 +00001010 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001011 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00001012 Register method_reg;
1013 CpuRegister reg = temp.AsRegister<CpuRegister>();
1014 if (current_method.IsRegister()) {
1015 method_reg = current_method.AsRegister<Register>();
1016 } else {
1017 DCHECK(invoke->GetLocations()->Intrinsified());
1018 DCHECK(!current_method.IsValid());
1019 method_reg = reg.AsRegister();
1020 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
1021 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001022 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01001023 __ movq(reg,
1024 Address(CpuRegister(method_reg),
1025 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01001026 // temp = temp[index_in_cache];
1027 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
1028 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00001029 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
1030 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01001031 }
Vladimir Marko58155012015-08-19 12:49:41 +00001032 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06001033 return callee_method;
1034}
1035
1036void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
1037 Location temp) {
1038 // All registers are assumed to be correctly set up.
1039 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00001040
1041 switch (invoke->GetCodePtrLocation()) {
1042 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1043 __ call(&frame_entry_label_);
1044 break;
1045 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001046 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
1047 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00001048 Label* label = &relative_call_patches_.back().label;
1049 __ call(label); // Bind to the patch label, override at link time.
1050 __ Bind(label); // Bind the label at the end of the "call" insn.
1051 break;
1052 }
1053 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
1054 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01001055 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
1056 LOG(FATAL) << "Unsupported";
1057 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00001058 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1059 // (callee_method + offset_of_quick_compiled_code)()
1060 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1061 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001062 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001063 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001064 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001065
1066 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001067}
1068
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001069void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
1070 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1071 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1072 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001073
1074 // Use the calling convention instead of the location of the receiver, as
1075 // intrinsics may have put the receiver in a different register. In the intrinsics
1076 // slow path, the arguments have been moved to the right place, so here we are
1077 // guaranteed that the receiver is the first register of the calling convention.
1078 InvokeDexCallingConvention calling_convention;
1079 Register receiver = calling_convention.GetRegisterAt(0);
1080
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001081 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001082 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001083 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001084 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001085 // Instead of simply (possibly) unpoisoning `temp` here, we should
1086 // emit a read barrier for the previous class reference load.
1087 // However this is not required in practice, as this is an
1088 // intermediate/temporary reference and because the current
1089 // concurrent copying collector keeps the from-space memory
1090 // intact/accessible until the end of the marking phase (the
1091 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001092 __ MaybeUnpoisonHeapReference(temp);
1093 // temp = temp->GetMethodAt(method_offset);
1094 __ movq(temp, Address(temp, method_offset));
1095 // call temp->GetEntryPoint();
1096 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001097 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001098}
1099
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001100void CodeGeneratorX86_64::RecordSimplePatch() {
1101 if (GetCompilerOptions().GetIncludePatchInformation()) {
1102 simple_patches_.emplace_back();
1103 __ Bind(&simple_patches_.back());
1104 }
1105}
1106
Vladimir Markoaad75c62016-10-03 08:46:48 +00001107void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
1108 DCHECK(GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001109 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001110 __ Bind(&string_patches_.back().label);
1111}
1112
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001113void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001114 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001115 __ Bind(&type_patches_.back().label);
1116}
1117
Vladimir Markoaad75c62016-10-03 08:46:48 +00001118Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1119 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001120 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001121 return &string_patches_.back().label;
1122}
1123
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001124Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
1125 uint32_t element_offset) {
1126 // Add a patch entry and return the label.
1127 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
1128 return &pc_relative_dex_cache_patches_.back().label;
1129}
1130
Vladimir Markoaad75c62016-10-03 08:46:48 +00001131// The label points to the end of the "movl" or another instruction but the literal offset
1132// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1133constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1134
1135template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1136inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1137 const ArenaDeque<PatchInfo<Label>>& infos,
1138 ArenaVector<LinkerPatch>* linker_patches) {
1139 for (const PatchInfo<Label>& info : infos) {
1140 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1141 linker_patches->push_back(
1142 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1143 }
1144}
1145
Vladimir Marko58155012015-08-19 12:49:41 +00001146void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1147 DCHECK(linker_patches->empty());
1148 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001149 method_patches_.size() +
1150 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001151 pc_relative_dex_cache_patches_.size() +
1152 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001153 string_patches_.size() +
1154 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001155 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001156 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001157 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001158 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00001159 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001160 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001161 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001162 linker_patches->push_back(
1163 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00001164 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001165 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1166 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001167 for (const Label& label : simple_patches_) {
1168 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1169 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1170 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001171 if (!GetCompilerOptions().IsBootImage()) {
1172 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
1173 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001174 // These are always PC-relative, see GetSupportedLoadStringKind().
Vladimir Markoaad75c62016-10-03 08:46:48 +00001175 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001176 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001177 // These are always PC-relative, see GetSupportedLoadClassKind().
1178 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +00001179}
1180
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001181void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001182 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001183}
1184
1185void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001186 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187}
1188
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001189size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1190 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1191 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001192}
1193
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001194size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1195 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1196 return kX86_64WordSize;
1197}
1198
1199size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1200 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1201 return kX86_64WordSize;
1202}
1203
1204size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1205 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1206 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001207}
1208
Calin Juravle175dc732015-08-25 15:42:32 +01001209void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1210 HInstruction* instruction,
1211 uint32_t dex_pc,
1212 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001213 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001214 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1215 if (EntrypointRequiresStackMap(entrypoint)) {
1216 RecordPcInfo(instruction, dex_pc, slow_path);
1217 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001218}
1219
Roland Levillaindec8f632016-07-22 17:10:06 +01001220void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1221 HInstruction* instruction,
1222 SlowPathCode* slow_path) {
1223 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001224 GenerateInvokeRuntime(entry_point_offset);
1225}
1226
1227void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001228 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1229}
1230
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001231static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001232// Use a fake return address register to mimic Quick.
1233static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001234CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001235 const X86_64InstructionSetFeatures& isa_features,
1236 const CompilerOptions& compiler_options,
1237 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001238 : CodeGenerator(graph,
1239 kNumberOfCpuRegisters,
1240 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001241 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001242 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1243 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001244 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001245 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1246 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001247 compiler_options,
1248 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001249 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001251 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001252 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001253 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001254 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001255 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001256 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1257 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001258 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001259 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1260 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001261 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001262 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1263 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001264 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1265}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001267InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1268 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001269 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001270 assembler_(codegen->GetAssembler()),
1271 codegen_(codegen) {}
1272
David Brazdil58282f42016-01-14 12:45:10 +00001273void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001275 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001277 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001278 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001279}
1280
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001281static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001282 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001283}
David Srbecky9d8606d2015-04-12 09:35:32 +01001284
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001285static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001286 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287}
1288
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001290 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001291 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001292 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001293 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001294 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001295
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001296 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001297 __ testq(CpuRegister(RAX), Address(
1298 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001300 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001301
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001302 if (HasEmptyFrame()) {
1303 return;
1304 }
1305
Nicolas Geoffray98893962015-01-21 12:32:32 +00001306 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001307 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001308 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001309 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001310 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1311 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001312 }
1313 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001314
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001315 int adjust = GetFrameSize() - GetCoreSpillSize();
1316 __ subq(CpuRegister(RSP), Immediate(adjust));
1317 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001318 uint32_t xmm_spill_location = GetFpuSpillStart();
1319 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001320
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001321 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1322 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001323 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1324 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1325 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001326 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001327 }
1328
Mingyao Yang063fc772016-08-02 11:02:54 -07001329 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1330 // Initialize should_deoptimize flag to 0.
1331 __ movl(Address(CpuRegister(RSP), xmm_spill_location - kShouldDeoptimizeFlagSize),
1332 Immediate(0));
1333 }
1334
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001335 // Save the current method if we need it. Note that we do not
1336 // do this in HCurrentMethod, as the instruction might have been removed
1337 // in the SSA graph.
1338 if (RequiresCurrentMethod()) {
1339 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1340 CpuRegister(kMethodRegisterArgument));
1341 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342}
1343
1344void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001345 __ cfi().RememberState();
1346 if (!HasEmptyFrame()) {
1347 uint32_t xmm_spill_location = GetFpuSpillStart();
1348 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1349 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1350 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1351 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1352 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1353 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1354 }
1355 }
1356
1357 int adjust = GetFrameSize() - GetCoreSpillSize();
1358 __ addq(CpuRegister(RSP), Immediate(adjust));
1359 __ cfi().AdjustCFAOffset(-adjust);
1360
1361 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1362 Register reg = kCoreCalleeSaves[i];
1363 if (allocated_registers_.ContainsCoreRegister(reg)) {
1364 __ popq(CpuRegister(reg));
1365 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1366 __ cfi().Restore(DWARFReg(reg));
1367 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001368 }
1369 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001370 __ ret();
1371 __ cfi().RestoreState();
1372 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001373}
1374
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001375void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1376 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001377}
1378
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001379void CodeGeneratorX86_64::Move(Location destination, Location source) {
1380 if (source.Equals(destination)) {
1381 return;
1382 }
1383 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001384 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001385 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001386 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001387 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001388 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001389 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001390 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1391 } else if (source.IsConstant()) {
1392 HConstant* constant = source.GetConstant();
1393 if (constant->IsLongConstant()) {
1394 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1395 } else {
1396 Load32BitValue(dest, GetInt32ValueOf(constant));
1397 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001398 } else {
1399 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001400 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001401 }
1402 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001403 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001404 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001405 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001406 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001407 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1408 } else if (source.IsConstant()) {
1409 HConstant* constant = source.GetConstant();
1410 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1411 if (constant->IsFloatConstant()) {
1412 Load32BitValue(dest, static_cast<int32_t>(value));
1413 } else {
1414 Load64BitValue(dest, value);
1415 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001416 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001417 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001418 } else {
1419 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001420 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001421 }
1422 } else if (destination.IsStackSlot()) {
1423 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001426 } else if (source.IsFpuRegister()) {
1427 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001428 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001429 } else if (source.IsConstant()) {
1430 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001431 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001432 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001433 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001434 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001435 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1436 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001437 }
1438 } else {
1439 DCHECK(destination.IsDoubleStackSlot());
1440 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001441 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001442 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001443 } else if (source.IsFpuRegister()) {
1444 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001445 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001446 } else if (source.IsConstant()) {
1447 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001448 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1449 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001450 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001451 } else {
1452 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001453 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1454 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455 }
1456 }
1457}
1458
Calin Juravle175dc732015-08-25 15:42:32 +01001459void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1460 DCHECK(location.IsRegister());
1461 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1462}
1463
Calin Juravlee460d1d2015-09-29 04:52:17 +01001464void CodeGeneratorX86_64::MoveLocation(
1465 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1466 Move(dst, src);
1467}
1468
1469void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1470 if (location.IsRegister()) {
1471 locations->AddTemp(location);
1472 } else {
1473 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1474 }
1475}
1476
David Brazdilfc6a86a2015-06-26 10:33:45 +00001477void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001478 DCHECK(!successor->IsExitBlock());
1479
1480 HBasicBlock* block = got->GetBlock();
1481 HInstruction* previous = got->GetPrevious();
1482
1483 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001484 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001485 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1486 return;
1487 }
1488
1489 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1490 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1491 }
1492 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493 __ jmp(codegen_->GetLabelOf(successor));
1494 }
1495}
1496
David Brazdilfc6a86a2015-06-26 10:33:45 +00001497void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1498 got->SetLocations(nullptr);
1499}
1500
1501void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1502 HandleGoto(got, got->GetSuccessor());
1503}
1504
1505void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1506 try_boundary->SetLocations(nullptr);
1507}
1508
1509void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1510 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1511 if (!successor->IsExitBlock()) {
1512 HandleGoto(try_boundary, successor);
1513 }
1514}
1515
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001516void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1517 exit->SetLocations(nullptr);
1518}
1519
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001520void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001521}
1522
Mark Mendell152408f2015-12-31 12:28:50 -05001523template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001524void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001525 LabelType* true_label,
1526 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001527 if (cond->IsFPConditionTrueIfNaN()) {
1528 __ j(kUnordered, true_label);
1529 } else if (cond->IsFPConditionFalseIfNaN()) {
1530 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001531 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001532 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001533}
1534
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001535void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001536 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001537
Mark Mendellc4701932015-04-10 13:18:51 -04001538 Location left = locations->InAt(0);
1539 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001540 Primitive::Type type = condition->InputAt(0)->GetType();
1541 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001542 case Primitive::kPrimBoolean:
1543 case Primitive::kPrimByte:
1544 case Primitive::kPrimChar:
1545 case Primitive::kPrimShort:
1546 case Primitive::kPrimInt:
1547 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001548 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001549 break;
1550 }
Mark Mendellc4701932015-04-10 13:18:51 -04001551 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001552 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001553 break;
1554 }
1555 case Primitive::kPrimFloat: {
1556 if (right.IsFpuRegister()) {
1557 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1558 } else if (right.IsConstant()) {
1559 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1560 codegen_->LiteralFloatAddress(
1561 right.GetConstant()->AsFloatConstant()->GetValue()));
1562 } else {
1563 DCHECK(right.IsStackSlot());
1564 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1565 Address(CpuRegister(RSP), right.GetStackIndex()));
1566 }
Mark Mendellc4701932015-04-10 13:18:51 -04001567 break;
1568 }
1569 case Primitive::kPrimDouble: {
1570 if (right.IsFpuRegister()) {
1571 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1572 } else if (right.IsConstant()) {
1573 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1574 codegen_->LiteralDoubleAddress(
1575 right.GetConstant()->AsDoubleConstant()->GetValue()));
1576 } else {
1577 DCHECK(right.IsDoubleStackSlot());
1578 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1579 Address(CpuRegister(RSP), right.GetStackIndex()));
1580 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001581 break;
1582 }
1583 default:
1584 LOG(FATAL) << "Unexpected condition type " << type;
1585 }
1586}
1587
1588template<class LabelType>
1589void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1590 LabelType* true_target_in,
1591 LabelType* false_target_in) {
1592 // Generated branching requires both targets to be explicit. If either of the
1593 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1594 LabelType fallthrough_target;
1595 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1596 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1597
1598 // Generate the comparison to set the CC.
1599 GenerateCompareTest(condition);
1600
1601 // Now generate the correct jump(s).
1602 Primitive::Type type = condition->InputAt(0)->GetType();
1603 switch (type) {
1604 case Primitive::kPrimLong: {
1605 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1606 break;
1607 }
1608 case Primitive::kPrimFloat: {
1609 GenerateFPJumps(condition, true_target, false_target);
1610 break;
1611 }
1612 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001613 GenerateFPJumps(condition, true_target, false_target);
1614 break;
1615 }
1616 default:
1617 LOG(FATAL) << "Unexpected condition type " << type;
1618 }
1619
David Brazdil0debae72015-11-12 18:37:00 +00001620 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001621 __ jmp(false_target);
1622 }
David Brazdil0debae72015-11-12 18:37:00 +00001623
1624 if (fallthrough_target.IsLinked()) {
1625 __ Bind(&fallthrough_target);
1626 }
Mark Mendellc4701932015-04-10 13:18:51 -04001627}
1628
David Brazdil0debae72015-11-12 18:37:00 +00001629static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1630 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1631 // are set only strictly before `branch`. We can't use the eflags on long
1632 // conditions if they are materialized due to the complex branching.
1633 return cond->IsCondition() &&
1634 cond->GetNext() == branch &&
1635 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1636}
1637
Mark Mendell152408f2015-12-31 12:28:50 -05001638template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001639void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001640 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001641 LabelType* true_target,
1642 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001643 HInstruction* cond = instruction->InputAt(condition_input_index);
1644
1645 if (true_target == nullptr && false_target == nullptr) {
1646 // Nothing to do. The code always falls through.
1647 return;
1648 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001649 // Constant condition, statically compared against "true" (integer value 1).
1650 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001651 if (true_target != nullptr) {
1652 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001653 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001654 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001655 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001656 if (false_target != nullptr) {
1657 __ jmp(false_target);
1658 }
1659 }
1660 return;
1661 }
1662
1663 // The following code generates these patterns:
1664 // (1) true_target == nullptr && false_target != nullptr
1665 // - opposite condition true => branch to false_target
1666 // (2) true_target != nullptr && false_target == nullptr
1667 // - condition true => branch to true_target
1668 // (3) true_target != nullptr && false_target != nullptr
1669 // - condition true => branch to true_target
1670 // - branch to false_target
1671 if (IsBooleanValueOrMaterializedCondition(cond)) {
1672 if (AreEflagsSetFrom(cond, instruction)) {
1673 if (true_target == nullptr) {
1674 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1675 } else {
1676 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1677 }
1678 } else {
1679 // Materialized condition, compare against 0.
1680 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1681 if (lhs.IsRegister()) {
1682 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1683 } else {
1684 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1685 }
1686 if (true_target == nullptr) {
1687 __ j(kEqual, false_target);
1688 } else {
1689 __ j(kNotEqual, true_target);
1690 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001691 }
1692 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001693 // Condition has not been materialized, use its inputs as the
1694 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001695 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001696
David Brazdil0debae72015-11-12 18:37:00 +00001697 // If this is a long or FP comparison that has been folded into
1698 // the HCondition, generate the comparison directly.
1699 Primitive::Type type = condition->InputAt(0)->GetType();
1700 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1701 GenerateCompareTestAndBranch(condition, true_target, false_target);
1702 return;
1703 }
1704
1705 Location lhs = condition->GetLocations()->InAt(0);
1706 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001707 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001708 if (true_target == nullptr) {
1709 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1710 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001711 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001712 }
Dave Allison20dfc792014-06-16 20:44:29 -07001713 }
David Brazdil0debae72015-11-12 18:37:00 +00001714
1715 // If neither branch falls through (case 3), the conditional branch to `true_target`
1716 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1717 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001718 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001719 }
1720}
1721
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001722void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1724 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001725 locations->SetInAt(0, Location::Any());
1726 }
1727}
1728
1729void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001730 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1731 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1732 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1733 nullptr : codegen_->GetLabelOf(true_successor);
1734 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1735 nullptr : codegen_->GetLabelOf(false_successor);
1736 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001737}
1738
1739void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1740 LocationSummary* locations = new (GetGraph()->GetArena())
1741 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001742 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001743 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001744 locations->SetInAt(0, Location::Any());
1745 }
1746}
1747
1748void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001749 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001750 GenerateTestAndBranch<Label>(deoptimize,
1751 /* condition_input_index */ 0,
1752 slow_path->GetEntryLabel(),
1753 /* false_target */ nullptr);
1754}
1755
Mingyao Yang063fc772016-08-02 11:02:54 -07001756void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1757 LocationSummary* locations = new (GetGraph()->GetArena())
1758 LocationSummary(flag, LocationSummary::kNoCall);
1759 locations->SetOut(Location::RequiresRegister());
1760}
1761
1762void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1763 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1764 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1765}
1766
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001767static bool SelectCanUseCMOV(HSelect* select) {
1768 // There are no conditional move instructions for XMMs.
1769 if (Primitive::IsFloatingPointType(select->GetType())) {
1770 return false;
1771 }
1772
1773 // A FP condition doesn't generate the single CC that we need.
1774 HInstruction* condition = select->GetCondition();
1775 if (condition->IsCondition() &&
1776 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1777 return false;
1778 }
1779
1780 // We can generate a CMOV for this Select.
1781 return true;
1782}
1783
David Brazdil74eb1b22015-12-14 11:44:01 +00001784void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1786 if (Primitive::IsFloatingPointType(select->GetType())) {
1787 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001788 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001789 } else {
1790 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001791 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001792 if (select->InputAt(1)->IsConstant()) {
1793 locations->SetInAt(1, Location::RequiresRegister());
1794 } else {
1795 locations->SetInAt(1, Location::Any());
1796 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001797 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001798 locations->SetInAt(1, Location::Any());
1799 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001800 }
1801 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1802 locations->SetInAt(2, Location::RequiresRegister());
1803 }
1804 locations->SetOut(Location::SameAsFirstInput());
1805}
1806
1807void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1808 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001809 if (SelectCanUseCMOV(select)) {
1810 // If both the condition and the source types are integer, we can generate
1811 // a CMOV to implement Select.
1812 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001813 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001814 DCHECK(locations->InAt(0).Equals(locations->Out()));
1815
1816 HInstruction* select_condition = select->GetCondition();
1817 Condition cond = kNotEqual;
1818
1819 // Figure out how to test the 'condition'.
1820 if (select_condition->IsCondition()) {
1821 HCondition* condition = select_condition->AsCondition();
1822 if (!condition->IsEmittedAtUseSite()) {
1823 // This was a previously materialized condition.
1824 // Can we use the existing condition code?
1825 if (AreEflagsSetFrom(condition, select)) {
1826 // Materialization was the previous instruction. Condition codes are right.
1827 cond = X86_64IntegerCondition(condition->GetCondition());
1828 } else {
1829 // No, we have to recreate the condition code.
1830 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1831 __ testl(cond_reg, cond_reg);
1832 }
1833 } else {
1834 GenerateCompareTest(condition);
1835 cond = X86_64IntegerCondition(condition->GetCondition());
1836 }
1837 } else {
1838 // Must be a boolean condition, which needs to be compared to 0.
1839 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1840 __ testl(cond_reg, cond_reg);
1841 }
1842
1843 // If the condition is true, overwrite the output, which already contains false.
1844 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001845 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1846 if (value_true_loc.IsRegister()) {
1847 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1848 } else {
1849 __ cmov(cond,
1850 value_false,
1851 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1852 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001853 } else {
1854 NearLabel false_target;
1855 GenerateTestAndBranch<NearLabel>(select,
1856 /* condition_input_index */ 2,
1857 /* true_target */ nullptr,
1858 &false_target);
1859 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1860 __ Bind(&false_target);
1861 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001862}
1863
David Srbecky0cf44932015-12-09 14:09:59 +00001864void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1865 new (GetGraph()->GetArena()) LocationSummary(info);
1866}
1867
David Srbeckyd28f4a02016-03-14 17:14:24 +00001868void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1869 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001870}
1871
1872void CodeGeneratorX86_64::GenerateNop() {
1873 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001874}
1875
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001876void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001877 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001878 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001879 // Handle the long/FP comparisons made in instruction simplification.
1880 switch (cond->InputAt(0)->GetType()) {
1881 case Primitive::kPrimLong:
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::Any());
1884 break;
1885 case Primitive::kPrimFloat:
1886 case Primitive::kPrimDouble:
1887 locations->SetInAt(0, Location::RequiresFpuRegister());
1888 locations->SetInAt(1, Location::Any());
1889 break;
1890 default:
1891 locations->SetInAt(0, Location::RequiresRegister());
1892 locations->SetInAt(1, Location::Any());
1893 break;
1894 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001895 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896 locations->SetOut(Location::RequiresRegister());
1897 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001898}
1899
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001900void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001901 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001902 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001903 }
Mark Mendellc4701932015-04-10 13:18:51 -04001904
1905 LocationSummary* locations = cond->GetLocations();
1906 Location lhs = locations->InAt(0);
1907 Location rhs = locations->InAt(1);
1908 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001909 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001910
1911 switch (cond->InputAt(0)->GetType()) {
1912 default:
1913 // Integer case.
1914
1915 // Clear output register: setcc only sets the low byte.
1916 __ xorl(reg, reg);
1917
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001918 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001919 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001920 return;
1921 case Primitive::kPrimLong:
1922 // Clear output register: setcc only sets the low byte.
1923 __ xorl(reg, reg);
1924
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001925 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001926 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001927 return;
1928 case Primitive::kPrimFloat: {
1929 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1930 if (rhs.IsConstant()) {
1931 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1932 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1933 } else if (rhs.IsStackSlot()) {
1934 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1935 } else {
1936 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1937 }
1938 GenerateFPJumps(cond, &true_label, &false_label);
1939 break;
1940 }
1941 case Primitive::kPrimDouble: {
1942 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1943 if (rhs.IsConstant()) {
1944 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1945 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1946 } else if (rhs.IsDoubleStackSlot()) {
1947 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1948 } else {
1949 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1950 }
1951 GenerateFPJumps(cond, &true_label, &false_label);
1952 break;
1953 }
1954 }
1955
1956 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001957 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001958
Roland Levillain4fa13f62015-07-06 18:11:54 +01001959 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001960 __ Bind(&false_label);
1961 __ xorl(reg, reg);
1962 __ jmp(&done_label);
1963
Roland Levillain4fa13f62015-07-06 18:11:54 +01001964 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001965 __ Bind(&true_label);
1966 __ movl(reg, Immediate(1));
1967 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002004}
2005
2006void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002008}
2009
2010void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002012}
2013
2014void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016}
2017
Aart Bike9f37602015-10-09 11:15:55 -07002018void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
2038void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002040}
2041
2042void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002044}
2045
2046void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002048}
2049
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002050void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002051 LocationSummary* locations =
2052 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002053 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002054 case Primitive::kPrimBoolean:
2055 case Primitive::kPrimByte:
2056 case Primitive::kPrimShort:
2057 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002058 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002059 case Primitive::kPrimLong: {
2060 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002061 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2063 break;
2064 }
2065 case Primitive::kPrimFloat:
2066 case Primitive::kPrimDouble: {
2067 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002068 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002069 locations->SetOut(Location::RequiresRegister());
2070 break;
2071 }
2072 default:
2073 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2074 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002075}
2076
2077void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002078 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 Location left = locations->InAt(0);
2081 Location right = locations->InAt(1);
2082
Mark Mendell0c9497d2015-08-21 09:30:05 -04002083 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002084 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002085 Condition less_cond = kLess;
2086
Calin Juravleddb7df22014-11-25 20:56:51 +00002087 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002088 case Primitive::kPrimBoolean:
2089 case Primitive::kPrimByte:
2090 case Primitive::kPrimShort:
2091 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002092 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002093 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002094 break;
2095 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002096 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002097 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002098 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002099 }
2100 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002101 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2102 if (right.IsConstant()) {
2103 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2104 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2105 } else if (right.IsStackSlot()) {
2106 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2107 } else {
2108 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2109 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002110 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002111 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002112 break;
2113 }
2114 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002115 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2116 if (right.IsConstant()) {
2117 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2118 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2119 } else if (right.IsDoubleStackSlot()) {
2120 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2121 } else {
2122 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2123 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002124 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002125 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002126 break;
2127 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002128 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002129 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002130 }
Aart Bika19616e2016-02-01 18:57:58 -08002131
Calin Juravleddb7df22014-11-25 20:56:51 +00002132 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002133 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002134 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002135
Calin Juravle91debbc2014-11-26 19:01:09 +00002136 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002137 __ movl(out, Immediate(1));
2138 __ jmp(&done);
2139
2140 __ Bind(&less);
2141 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002142
2143 __ Bind(&done);
2144}
2145
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002146void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 LocationSummary* locations =
2148 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002149 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150}
2151
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002152void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002153 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154}
2155
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002156void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2157 LocationSummary* locations =
2158 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2159 locations->SetOut(Location::ConstantLocation(constant));
2160}
2161
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002162void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002163 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002164}
2165
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002166void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002169 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002170}
2171
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002172void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002173 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002174}
2175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002176void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2177 LocationSummary* locations =
2178 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2179 locations->SetOut(Location::ConstantLocation(constant));
2180}
2181
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002182void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 // Will be generated at use site.
2184}
2185
2186void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2189 locations->SetOut(Location::ConstantLocation(constant));
2190}
2191
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002192void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2193 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002194 // Will be generated at use site.
2195}
2196
Calin Juravle27df7582015-04-17 19:12:31 +01002197void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2198 memory_barrier->SetLocations(nullptr);
2199}
2200
2201void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002202 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002203}
2204
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2206 ret->SetLocations(nullptr);
2207}
2208
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002209void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002210 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002211}
2212
2213void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216 switch (ret->InputAt(0)->GetType()) {
2217 case Primitive::kPrimBoolean:
2218 case Primitive::kPrimByte:
2219 case Primitive::kPrimChar:
2220 case Primitive::kPrimShort:
2221 case Primitive::kPrimInt:
2222 case Primitive::kPrimNot:
2223 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002224 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225 break;
2226
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002227 case Primitive::kPrimFloat:
2228 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002229 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 break;
2231
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002232 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002234 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002235}
2236
2237void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2238 if (kIsDebugBuild) {
2239 switch (ret->InputAt(0)->GetType()) {
2240 case Primitive::kPrimBoolean:
2241 case Primitive::kPrimByte:
2242 case Primitive::kPrimChar:
2243 case Primitive::kPrimShort:
2244 case Primitive::kPrimInt:
2245 case Primitive::kPrimNot:
2246 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002248 break;
2249
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002250 case Primitive::kPrimFloat:
2251 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002252 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002253 XMM0);
2254 break;
2255
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002256 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002257 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002258 }
2259 }
2260 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002261}
2262
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002263Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2264 switch (type) {
2265 case Primitive::kPrimBoolean:
2266 case Primitive::kPrimByte:
2267 case Primitive::kPrimChar:
2268 case Primitive::kPrimShort:
2269 case Primitive::kPrimInt:
2270 case Primitive::kPrimNot:
2271 case Primitive::kPrimLong:
2272 return Location::RegisterLocation(RAX);
2273
2274 case Primitive::kPrimVoid:
2275 return Location::NoLocation();
2276
2277 case Primitive::kPrimDouble:
2278 case Primitive::kPrimFloat:
2279 return Location::FpuRegisterLocation(XMM0);
2280 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002281
2282 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002283}
2284
2285Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2286 return Location::RegisterLocation(kMethodRegisterArgument);
2287}
2288
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002289Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002290 switch (type) {
2291 case Primitive::kPrimBoolean:
2292 case Primitive::kPrimByte:
2293 case Primitive::kPrimChar:
2294 case Primitive::kPrimShort:
2295 case Primitive::kPrimInt:
2296 case Primitive::kPrimNot: {
2297 uint32_t index = gp_index_++;
2298 stack_index_++;
2299 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002300 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002301 } else {
2302 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2303 }
2304 }
2305
2306 case Primitive::kPrimLong: {
2307 uint32_t index = gp_index_;
2308 stack_index_ += 2;
2309 if (index < calling_convention.GetNumberOfRegisters()) {
2310 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002312 } else {
2313 gp_index_ += 2;
2314 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2315 }
2316 }
2317
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002318 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002319 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002320 stack_index_++;
2321 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002323 } else {
2324 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2325 }
2326 }
2327
2328 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002329 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002330 stack_index_ += 2;
2331 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002332 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002333 } else {
2334 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2335 }
2336 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002337
2338 case Primitive::kPrimVoid:
2339 LOG(FATAL) << "Unexpected parameter type " << type;
2340 break;
2341 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002342 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002343}
2344
Calin Juravle175dc732015-08-25 15:42:32 +01002345void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2346 // The trampoline uses the same calling convention as dex calling conventions,
2347 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2348 // the method_idx.
2349 HandleInvoke(invoke);
2350}
2351
2352void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2353 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2354}
2355
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002356void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002357 // Explicit clinit checks triggered by static invokes must have been pruned by
2358 // art::PrepareForRegisterAllocation.
2359 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002360
Mark Mendellfb8d2792015-03-31 22:16:59 -04002361 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002362 if (intrinsic.TryDispatch(invoke)) {
2363 return;
2364 }
2365
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002366 HandleInvoke(invoke);
2367}
2368
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002369static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2370 if (invoke->GetLocations()->Intrinsified()) {
2371 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2372 intrinsic.Dispatch(invoke);
2373 return true;
2374 }
2375 return false;
2376}
2377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002378void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002379 // Explicit clinit checks triggered by static invokes must have been pruned by
2380 // art::PrepareForRegisterAllocation.
2381 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002382
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002383 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2384 return;
2385 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002386
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002387 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002388 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002389 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002390 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002391}
2392
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002393void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002394 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002395 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002396}
2397
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002398void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002399 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002400 if (intrinsic.TryDispatch(invoke)) {
2401 return;
2402 }
2403
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002404 HandleInvoke(invoke);
2405}
2406
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002407void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002408 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2409 return;
2410 }
2411
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002412 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002413 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002414 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002415}
2416
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002417void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2418 HandleInvoke(invoke);
2419 // Add the hidden argument.
2420 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2421}
2422
2423void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2424 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002425 LocationSummary* locations = invoke->GetLocations();
2426 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2427 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002428 Location receiver = locations->InAt(0);
2429 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2430
Roland Levillain0d5a2812015-11-13 10:07:31 +00002431 // Set the hidden argument. This is safe to do this here, as RAX
2432 // won't be modified thereafter, before the `call` instruction.
2433 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002434 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002435
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002436 if (receiver.IsStackSlot()) {
2437 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002438 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002439 __ movl(temp, Address(temp, class_offset));
2440 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002441 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002442 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002443 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002444 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002445 // Instead of simply (possibly) unpoisoning `temp` here, we should
2446 // emit a read barrier for the previous class reference load.
2447 // However this is not required in practice, as this is an
2448 // intermediate/temporary reference and because the current
2449 // concurrent copying collector keeps the from-space memory
2450 // intact/accessible until the end of the marking phase (the
2451 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002452 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002453 // temp = temp->GetAddressOfIMT()
2454 __ movq(temp,
2455 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2456 // temp = temp->GetImtEntryAt(method_offset);
2457 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002458 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002459 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002460 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002461 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002462 __ call(Address(
2463 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002464
2465 DCHECK(!codegen_->IsLeafMethod());
2466 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2467}
2468
Roland Levillain88cb1752014-10-20 16:36:47 +01002469void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2470 LocationSummary* locations =
2471 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2472 switch (neg->GetResultType()) {
2473 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002474 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002475 locations->SetInAt(0, Location::RequiresRegister());
2476 locations->SetOut(Location::SameAsFirstInput());
2477 break;
2478
Roland Levillain88cb1752014-10-20 16:36:47 +01002479 case Primitive::kPrimFloat:
2480 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002481 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002482 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002483 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002484 break;
2485
2486 default:
2487 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2488 }
2489}
2490
2491void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2492 LocationSummary* locations = neg->GetLocations();
2493 Location out = locations->Out();
2494 Location in = locations->InAt(0);
2495 switch (neg->GetResultType()) {
2496 case Primitive::kPrimInt:
2497 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002498 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002500 break;
2501
2502 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002503 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002504 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002506 break;
2507
Roland Levillain5368c212014-11-27 15:03:41 +00002508 case Primitive::kPrimFloat: {
2509 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002510 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002511 // Implement float negation with an exclusive or with value
2512 // 0x80000000 (mask for bit 31, representing the sign of a
2513 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002514 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002516 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002517 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002518
Roland Levillain5368c212014-11-27 15:03:41 +00002519 case Primitive::kPrimDouble: {
2520 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002521 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002522 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002523 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002524 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002525 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002526 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002527 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002528 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002529
2530 default:
2531 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2532 }
2533}
2534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2536 LocationSummary* locations =
2537 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2538 Primitive::Type result_type = conversion->GetResultType();
2539 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002540 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002541
David Brazdilb2bd1c52015-03-25 11:17:37 +00002542 // The Java language does not allow treating boolean as an integral type but
2543 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002544
Roland Levillaindff1f282014-11-05 14:15:05 +00002545 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002546 case Primitive::kPrimByte:
2547 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002548 case Primitive::kPrimLong:
2549 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002550 case Primitive::kPrimBoolean:
2551 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002552 case Primitive::kPrimShort:
2553 case Primitive::kPrimInt:
2554 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002555 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002556 locations->SetInAt(0, Location::Any());
2557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2558 break;
2559
2560 default:
2561 LOG(FATAL) << "Unexpected type conversion from " << input_type
2562 << " to " << result_type;
2563 }
2564 break;
2565
Roland Levillain01a8d712014-11-14 16:27:39 +00002566 case Primitive::kPrimShort:
2567 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002568 case Primitive::kPrimLong:
2569 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002570 case Primitive::kPrimBoolean:
2571 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002572 case Primitive::kPrimByte:
2573 case Primitive::kPrimInt:
2574 case Primitive::kPrimChar:
2575 // Processing a Dex `int-to-short' instruction.
2576 locations->SetInAt(0, Location::Any());
2577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unexpected type conversion from " << input_type
2582 << " to " << result_type;
2583 }
2584 break;
2585
Roland Levillain946e1432014-11-11 17:35:19 +00002586 case Primitive::kPrimInt:
2587 switch (input_type) {
2588 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002589 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002590 locations->SetInAt(0, Location::Any());
2591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2592 break;
2593
2594 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002595 // Processing a Dex `float-to-int' instruction.
2596 locations->SetInAt(0, Location::RequiresFpuRegister());
2597 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002598 break;
2599
Roland Levillain946e1432014-11-11 17:35:19 +00002600 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002601 // Processing a Dex `double-to-int' instruction.
2602 locations->SetInAt(0, Location::RequiresFpuRegister());
2603 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002604 break;
2605
2606 default:
2607 LOG(FATAL) << "Unexpected type conversion from " << input_type
2608 << " to " << result_type;
2609 }
2610 break;
2611
Roland Levillaindff1f282014-11-05 14:15:05 +00002612 case Primitive::kPrimLong:
2613 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002614 case Primitive::kPrimBoolean:
2615 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002616 case Primitive::kPrimByte:
2617 case Primitive::kPrimShort:
2618 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002619 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002620 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002621 // TODO: We would benefit from a (to-be-implemented)
2622 // Location::RegisterOrStackSlot requirement for this input.
2623 locations->SetInAt(0, Location::RequiresRegister());
2624 locations->SetOut(Location::RequiresRegister());
2625 break;
2626
2627 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002628 // Processing a Dex `float-to-long' instruction.
2629 locations->SetInAt(0, Location::RequiresFpuRegister());
2630 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002631 break;
2632
Roland Levillaindff1f282014-11-05 14:15:05 +00002633 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002634 // Processing a Dex `double-to-long' instruction.
2635 locations->SetInAt(0, Location::RequiresFpuRegister());
2636 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002637 break;
2638
2639 default:
2640 LOG(FATAL) << "Unexpected type conversion from " << input_type
2641 << " to " << result_type;
2642 }
2643 break;
2644
Roland Levillain981e4542014-11-14 11:47:14 +00002645 case Primitive::kPrimChar:
2646 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002647 case Primitive::kPrimLong:
2648 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002649 case Primitive::kPrimBoolean:
2650 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002651 case Primitive::kPrimByte:
2652 case Primitive::kPrimShort:
2653 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002654 // Processing a Dex `int-to-char' instruction.
2655 locations->SetInAt(0, Location::Any());
2656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663 break;
2664
Roland Levillaindff1f282014-11-05 14:15:05 +00002665 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002666 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002667 case Primitive::kPrimBoolean:
2668 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002669 case Primitive::kPrimByte:
2670 case Primitive::kPrimShort:
2671 case Primitive::kPrimInt:
2672 case Primitive::kPrimChar:
2673 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002674 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002675 locations->SetOut(Location::RequiresFpuRegister());
2676 break;
2677
2678 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002679 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002680 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002681 locations->SetOut(Location::RequiresFpuRegister());
2682 break;
2683
Roland Levillaincff13742014-11-17 14:32:17 +00002684 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002685 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002686 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002687 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002688 break;
2689
2690 default:
2691 LOG(FATAL) << "Unexpected type conversion from " << input_type
2692 << " to " << result_type;
2693 };
2694 break;
2695
Roland Levillaindff1f282014-11-05 14:15:05 +00002696 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002697 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002698 case Primitive::kPrimBoolean:
2699 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002700 case Primitive::kPrimByte:
2701 case Primitive::kPrimShort:
2702 case Primitive::kPrimInt:
2703 case Primitive::kPrimChar:
2704 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002705 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002706 locations->SetOut(Location::RequiresFpuRegister());
2707 break;
2708
2709 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002710 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002711 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002712 locations->SetOut(Location::RequiresFpuRegister());
2713 break;
2714
Roland Levillaincff13742014-11-17 14:32:17 +00002715 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002716 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002717 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002718 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002719 break;
2720
2721 default:
2722 LOG(FATAL) << "Unexpected type conversion from " << input_type
2723 << " to " << result_type;
2724 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002725 break;
2726
2727 default:
2728 LOG(FATAL) << "Unexpected type conversion from " << input_type
2729 << " to " << result_type;
2730 }
2731}
2732
2733void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2734 LocationSummary* locations = conversion->GetLocations();
2735 Location out = locations->Out();
2736 Location in = locations->InAt(0);
2737 Primitive::Type result_type = conversion->GetResultType();
2738 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002739 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002740 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002741 case Primitive::kPrimByte:
2742 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002743 case Primitive::kPrimLong:
2744 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002745 case Primitive::kPrimBoolean:
2746 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002747 case Primitive::kPrimShort:
2748 case Primitive::kPrimInt:
2749 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002750 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002751 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002752 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002753 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002754 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002755 Address(CpuRegister(RSP), in.GetStackIndex()));
2756 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002758 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002759 }
2760 break;
2761
2762 default:
2763 LOG(FATAL) << "Unexpected type conversion from " << input_type
2764 << " to " << result_type;
2765 }
2766 break;
2767
Roland Levillain01a8d712014-11-14 16:27:39 +00002768 case Primitive::kPrimShort:
2769 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002770 case Primitive::kPrimLong:
2771 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002772 case Primitive::kPrimBoolean:
2773 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002774 case Primitive::kPrimByte:
2775 case Primitive::kPrimInt:
2776 case Primitive::kPrimChar:
2777 // Processing a Dex `int-to-short' instruction.
2778 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002779 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002780 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002782 Address(CpuRegister(RSP), in.GetStackIndex()));
2783 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002785 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002786 }
2787 break;
2788
2789 default:
2790 LOG(FATAL) << "Unexpected type conversion from " << input_type
2791 << " to " << result_type;
2792 }
2793 break;
2794
Roland Levillain946e1432014-11-11 17:35:19 +00002795 case Primitive::kPrimInt:
2796 switch (input_type) {
2797 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002798 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002799 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002801 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002803 Address(CpuRegister(RSP), in.GetStackIndex()));
2804 } else {
2805 DCHECK(in.IsConstant());
2806 DCHECK(in.GetConstant()->IsLongConstant());
2807 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002809 }
2810 break;
2811
Roland Levillain3f8f9362014-12-02 17:45:01 +00002812 case Primitive::kPrimFloat: {
2813 // Processing a Dex `float-to-int' instruction.
2814 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2815 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002816 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002817
2818 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002819 // if input >= (float)INT_MAX goto done
2820 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002821 __ j(kAboveEqual, &done);
2822 // if input == NaN goto nan
2823 __ j(kUnordered, &nan);
2824 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002825 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002826 __ jmp(&done);
2827 __ Bind(&nan);
2828 // output = 0
2829 __ xorl(output, output);
2830 __ Bind(&done);
2831 break;
2832 }
2833
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002834 case Primitive::kPrimDouble: {
2835 // Processing a Dex `double-to-int' instruction.
2836 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2837 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002838 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002839
2840 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002841 // if input >= (double)INT_MAX goto done
2842 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002843 __ j(kAboveEqual, &done);
2844 // if input == NaN goto nan
2845 __ j(kUnordered, &nan);
2846 // output = double-to-int-truncate(input)
2847 __ cvttsd2si(output, input);
2848 __ jmp(&done);
2849 __ Bind(&nan);
2850 // output = 0
2851 __ xorl(output, output);
2852 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002853 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002854 }
Roland Levillain946e1432014-11-11 17:35:19 +00002855
2856 default:
2857 LOG(FATAL) << "Unexpected type conversion from " << input_type
2858 << " to " << result_type;
2859 }
2860 break;
2861
Roland Levillaindff1f282014-11-05 14:15:05 +00002862 case Primitive::kPrimLong:
2863 switch (input_type) {
2864 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002865 case Primitive::kPrimBoolean:
2866 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002867 case Primitive::kPrimByte:
2868 case Primitive::kPrimShort:
2869 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002870 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002871 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002872 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002873 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002874 break;
2875
Roland Levillain624279f2014-12-04 11:54:28 +00002876 case Primitive::kPrimFloat: {
2877 // Processing a Dex `float-to-long' instruction.
2878 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2879 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002880 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002881
Mark Mendell92e83bf2015-05-07 11:25:03 -04002882 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002883 // if input >= (float)LONG_MAX goto done
2884 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002885 __ j(kAboveEqual, &done);
2886 // if input == NaN goto nan
2887 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002888 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002889 __ cvttss2si(output, input, true);
2890 __ jmp(&done);
2891 __ Bind(&nan);
2892 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002893 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002894 __ Bind(&done);
2895 break;
2896 }
2897
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002898 case Primitive::kPrimDouble: {
2899 // Processing a Dex `double-to-long' instruction.
2900 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2901 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002902 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002903
Mark Mendell92e83bf2015-05-07 11:25:03 -04002904 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002905 // if input >= (double)LONG_MAX goto done
2906 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002907 __ j(kAboveEqual, &done);
2908 // if input == NaN goto nan
2909 __ j(kUnordered, &nan);
2910 // output = double-to-long-truncate(input)
2911 __ cvttsd2si(output, input, true);
2912 __ jmp(&done);
2913 __ Bind(&nan);
2914 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002915 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002916 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002917 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002918 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002919
2920 default:
2921 LOG(FATAL) << "Unexpected type conversion from " << input_type
2922 << " to " << result_type;
2923 }
2924 break;
2925
Roland Levillain981e4542014-11-14 11:47:14 +00002926 case Primitive::kPrimChar:
2927 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002928 case Primitive::kPrimLong:
2929 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002930 case Primitive::kPrimBoolean:
2931 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002932 case Primitive::kPrimByte:
2933 case Primitive::kPrimShort:
2934 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002935 // Processing a Dex `int-to-char' instruction.
2936 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002937 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002938 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002940 Address(CpuRegister(RSP), in.GetStackIndex()));
2941 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002942 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002943 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002944 }
2945 break;
2946
2947 default:
2948 LOG(FATAL) << "Unexpected type conversion from " << input_type
2949 << " to " << result_type;
2950 }
2951 break;
2952
Roland Levillaindff1f282014-11-05 14:15:05 +00002953 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002954 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002955 case Primitive::kPrimBoolean:
2956 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002957 case Primitive::kPrimByte:
2958 case Primitive::kPrimShort:
2959 case Primitive::kPrimInt:
2960 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002961 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002962 if (in.IsRegister()) {
2963 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2964 } else if (in.IsConstant()) {
2965 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2966 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002967 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002968 } else {
2969 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2970 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2971 }
Roland Levillaincff13742014-11-17 14:32:17 +00002972 break;
2973
2974 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002975 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002976 if (in.IsRegister()) {
2977 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2978 } else if (in.IsConstant()) {
2979 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2980 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002981 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002982 } else {
2983 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2984 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2985 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002986 break;
2987
Roland Levillaincff13742014-11-17 14:32:17 +00002988 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002989 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002990 if (in.IsFpuRegister()) {
2991 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2992 } else if (in.IsConstant()) {
2993 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2994 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002995 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002996 } else {
2997 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2998 Address(CpuRegister(RSP), in.GetStackIndex()));
2999 }
Roland Levillaincff13742014-11-17 14:32:17 +00003000 break;
3001
3002 default:
3003 LOG(FATAL) << "Unexpected type conversion from " << input_type
3004 << " to " << result_type;
3005 };
3006 break;
3007
Roland Levillaindff1f282014-11-05 14:15:05 +00003008 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003009 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003010 case Primitive::kPrimBoolean:
3011 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003012 case Primitive::kPrimByte:
3013 case Primitive::kPrimShort:
3014 case Primitive::kPrimInt:
3015 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003016 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003017 if (in.IsRegister()) {
3018 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3019 } else if (in.IsConstant()) {
3020 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3021 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003022 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003023 } else {
3024 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3025 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3026 }
Roland Levillaincff13742014-11-17 14:32:17 +00003027 break;
3028
3029 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003030 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003031 if (in.IsRegister()) {
3032 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3033 } else if (in.IsConstant()) {
3034 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3035 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003036 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003037 } else {
3038 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3039 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3040 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003041 break;
3042
Roland Levillaincff13742014-11-17 14:32:17 +00003043 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003044 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003045 if (in.IsFpuRegister()) {
3046 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3047 } else if (in.IsConstant()) {
3048 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3049 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003050 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003051 } else {
3052 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3053 Address(CpuRegister(RSP), in.GetStackIndex()));
3054 }
Roland Levillaincff13742014-11-17 14:32:17 +00003055 break;
3056
3057 default:
3058 LOG(FATAL) << "Unexpected type conversion from " << input_type
3059 << " to " << result_type;
3060 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003061 break;
3062
3063 default:
3064 LOG(FATAL) << "Unexpected type conversion from " << input_type
3065 << " to " << result_type;
3066 }
3067}
3068
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003069void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003070 LocationSummary* locations =
3071 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003072 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003073 case Primitive::kPrimInt: {
3074 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003075 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3076 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003077 break;
3078 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003079
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003080 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003081 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003082 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003083 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003085 break;
3086 }
3087
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003088 case Primitive::kPrimDouble:
3089 case Primitive::kPrimFloat: {
3090 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003091 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003094 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003095
3096 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003097 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003098 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003099}
3100
3101void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3102 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003103 Location first = locations->InAt(0);
3104 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003105 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003106
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003107 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003108 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003109 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003110 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3111 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003112 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3113 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003114 } else {
3115 __ leal(out.AsRegister<CpuRegister>(), Address(
3116 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3117 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003118 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003119 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3120 __ addl(out.AsRegister<CpuRegister>(),
3121 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3122 } else {
3123 __ leal(out.AsRegister<CpuRegister>(), Address(
3124 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3125 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003126 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003127 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003129 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003130 break;
3131 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003132
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003133 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003134 if (second.IsRegister()) {
3135 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3136 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003137 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3138 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003139 } else {
3140 __ leaq(out.AsRegister<CpuRegister>(), Address(
3141 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3142 }
3143 } else {
3144 DCHECK(second.IsConstant());
3145 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3146 int32_t int32_value = Low32Bits(value);
3147 DCHECK_EQ(int32_value, value);
3148 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3149 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3150 } else {
3151 __ leaq(out.AsRegister<CpuRegister>(), Address(
3152 first.AsRegister<CpuRegister>(), int32_value));
3153 }
3154 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003155 break;
3156 }
3157
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003158 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003159 if (second.IsFpuRegister()) {
3160 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3161 } else if (second.IsConstant()) {
3162 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003163 codegen_->LiteralFloatAddress(
3164 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003165 } else {
3166 DCHECK(second.IsStackSlot());
3167 __ addss(first.AsFpuRegister<XmmRegister>(),
3168 Address(CpuRegister(RSP), second.GetStackIndex()));
3169 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003170 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003171 }
3172
3173 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003174 if (second.IsFpuRegister()) {
3175 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3176 } else if (second.IsConstant()) {
3177 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003178 codegen_->LiteralDoubleAddress(
3179 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003180 } else {
3181 DCHECK(second.IsDoubleStackSlot());
3182 __ addsd(first.AsFpuRegister<XmmRegister>(),
3183 Address(CpuRegister(RSP), second.GetStackIndex()));
3184 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003185 break;
3186 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003187
3188 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003189 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003190 }
3191}
3192
3193void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003194 LocationSummary* locations =
3195 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003196 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003197 case Primitive::kPrimInt: {
3198 locations->SetInAt(0, Location::RequiresRegister());
3199 locations->SetInAt(1, Location::Any());
3200 locations->SetOut(Location::SameAsFirstInput());
3201 break;
3202 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003203 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003204 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003205 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003206 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003207 break;
3208 }
Calin Juravle11351682014-10-23 15:38:15 +01003209 case Primitive::kPrimFloat:
3210 case Primitive::kPrimDouble: {
3211 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003212 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003213 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003214 break;
Calin Juravle11351682014-10-23 15:38:15 +01003215 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003216 default:
Calin Juravle11351682014-10-23 15:38:15 +01003217 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003218 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003219}
3220
3221void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3222 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003223 Location first = locations->InAt(0);
3224 Location second = locations->InAt(1);
3225 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003226 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003227 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003228 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003230 } else if (second.IsConstant()) {
3231 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003232 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003233 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003235 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003236 break;
3237 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003238 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003239 if (second.IsConstant()) {
3240 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3241 DCHECK(IsInt<32>(value));
3242 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3243 } else {
3244 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3245 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003246 break;
3247 }
3248
Calin Juravle11351682014-10-23 15:38:15 +01003249 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003250 if (second.IsFpuRegister()) {
3251 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3252 } else if (second.IsConstant()) {
3253 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003254 codegen_->LiteralFloatAddress(
3255 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003256 } else {
3257 DCHECK(second.IsStackSlot());
3258 __ subss(first.AsFpuRegister<XmmRegister>(),
3259 Address(CpuRegister(RSP), second.GetStackIndex()));
3260 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003261 break;
Calin Juravle11351682014-10-23 15:38:15 +01003262 }
3263
3264 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003265 if (second.IsFpuRegister()) {
3266 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3267 } else if (second.IsConstant()) {
3268 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003269 codegen_->LiteralDoubleAddress(
3270 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003271 } else {
3272 DCHECK(second.IsDoubleStackSlot());
3273 __ subsd(first.AsFpuRegister<XmmRegister>(),
3274 Address(CpuRegister(RSP), second.GetStackIndex()));
3275 }
Calin Juravle11351682014-10-23 15:38:15 +01003276 break;
3277 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003278
3279 default:
Calin Juravle11351682014-10-23 15:38:15 +01003280 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003281 }
3282}
3283
Calin Juravle34bacdf2014-10-07 20:23:36 +01003284void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3285 LocationSummary* locations =
3286 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3287 switch (mul->GetResultType()) {
3288 case Primitive::kPrimInt: {
3289 locations->SetInAt(0, Location::RequiresRegister());
3290 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003291 if (mul->InputAt(1)->IsIntConstant()) {
3292 // Can use 3 operand multiply.
3293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3294 } else {
3295 locations->SetOut(Location::SameAsFirstInput());
3296 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003297 break;
3298 }
3299 case Primitive::kPrimLong: {
3300 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003301 locations->SetInAt(1, Location::Any());
3302 if (mul->InputAt(1)->IsLongConstant() &&
3303 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003304 // Can use 3 operand multiply.
3305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3306 } else {
3307 locations->SetOut(Location::SameAsFirstInput());
3308 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003309 break;
3310 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003311 case Primitive::kPrimFloat:
3312 case Primitive::kPrimDouble: {
3313 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003314 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003315 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003316 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003317 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003318
3319 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003320 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003321 }
3322}
3323
3324void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3325 LocationSummary* locations = mul->GetLocations();
3326 Location first = locations->InAt(0);
3327 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003328 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003330 case Primitive::kPrimInt:
3331 // The constant may have ended up in a register, so test explicitly to avoid
3332 // problems where the output may not be the same as the first operand.
3333 if (mul->InputAt(1)->IsIntConstant()) {
3334 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3335 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3336 } else if (second.IsRegister()) {
3337 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003340 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003341 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003342 __ imull(first.AsRegister<CpuRegister>(),
3343 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003344 }
3345 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003346 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003347 // The constant may have ended up in a register, so test explicitly to avoid
3348 // problems where the output may not be the same as the first operand.
3349 if (mul->InputAt(1)->IsLongConstant()) {
3350 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3351 if (IsInt<32>(value)) {
3352 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3353 Immediate(static_cast<int32_t>(value)));
3354 } else {
3355 // Have to use the constant area.
3356 DCHECK(first.Equals(out));
3357 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3358 }
3359 } else if (second.IsRegister()) {
3360 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003361 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003362 } else {
3363 DCHECK(second.IsDoubleStackSlot());
3364 DCHECK(first.Equals(out));
3365 __ imulq(first.AsRegister<CpuRegister>(),
3366 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003367 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003368 break;
3369 }
3370
Calin Juravleb5bfa962014-10-21 18:02:24 +01003371 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003372 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003373 if (second.IsFpuRegister()) {
3374 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3375 } else if (second.IsConstant()) {
3376 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003377 codegen_->LiteralFloatAddress(
3378 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003379 } else {
3380 DCHECK(second.IsStackSlot());
3381 __ mulss(first.AsFpuRegister<XmmRegister>(),
3382 Address(CpuRegister(RSP), second.GetStackIndex()));
3383 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003384 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003385 }
3386
3387 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003388 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003389 if (second.IsFpuRegister()) {
3390 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3391 } else if (second.IsConstant()) {
3392 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003393 codegen_->LiteralDoubleAddress(
3394 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003395 } else {
3396 DCHECK(second.IsDoubleStackSlot());
3397 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3398 Address(CpuRegister(RSP), second.GetStackIndex()));
3399 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003400 break;
3401 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003402
3403 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003404 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003405 }
3406}
3407
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003408void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3409 uint32_t stack_adjustment, bool is_float) {
3410 if (source.IsStackSlot()) {
3411 DCHECK(is_float);
3412 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3413 } else if (source.IsDoubleStackSlot()) {
3414 DCHECK(!is_float);
3415 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3416 } else {
3417 // Write the value to the temporary location on the stack and load to FP stack.
3418 if (is_float) {
3419 Location stack_temp = Location::StackSlot(temp_offset);
3420 codegen_->Move(stack_temp, source);
3421 __ flds(Address(CpuRegister(RSP), temp_offset));
3422 } else {
3423 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3424 codegen_->Move(stack_temp, source);
3425 __ fldl(Address(CpuRegister(RSP), temp_offset));
3426 }
3427 }
3428}
3429
3430void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3431 Primitive::Type type = rem->GetResultType();
3432 bool is_float = type == Primitive::kPrimFloat;
3433 size_t elem_size = Primitive::ComponentSize(type);
3434 LocationSummary* locations = rem->GetLocations();
3435 Location first = locations->InAt(0);
3436 Location second = locations->InAt(1);
3437 Location out = locations->Out();
3438
3439 // Create stack space for 2 elements.
3440 // TODO: enhance register allocator to ask for stack temporaries.
3441 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3442
3443 // Load the values to the FP stack in reverse order, using temporaries if needed.
3444 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3445 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3446
3447 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003448 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003449 __ Bind(&retry);
3450 __ fprem();
3451
3452 // Move FP status to AX.
3453 __ fstsw();
3454
3455 // And see if the argument reduction is complete. This is signaled by the
3456 // C2 FPU flag bit set to 0.
3457 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3458 __ j(kNotEqual, &retry);
3459
3460 // We have settled on the final value. Retrieve it into an XMM register.
3461 // Store FP top of stack to real stack.
3462 if (is_float) {
3463 __ fsts(Address(CpuRegister(RSP), 0));
3464 } else {
3465 __ fstl(Address(CpuRegister(RSP), 0));
3466 }
3467
3468 // Pop the 2 items from the FP stack.
3469 __ fucompp();
3470
3471 // Load the value from the stack into an XMM register.
3472 DCHECK(out.IsFpuRegister()) << out;
3473 if (is_float) {
3474 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3475 } else {
3476 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3477 }
3478
3479 // And remove the temporary stack space we allocated.
3480 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3481}
3482
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3484 DCHECK(instruction->IsDiv() || instruction->IsRem());
3485
3486 LocationSummary* locations = instruction->GetLocations();
3487 Location second = locations->InAt(1);
3488 DCHECK(second.IsConstant());
3489
3490 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3491 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003492 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493
3494 DCHECK(imm == 1 || imm == -1);
3495
3496 switch (instruction->GetResultType()) {
3497 case Primitive::kPrimInt: {
3498 if (instruction->IsRem()) {
3499 __ xorl(output_register, output_register);
3500 } else {
3501 __ movl(output_register, input_register);
3502 if (imm == -1) {
3503 __ negl(output_register);
3504 }
3505 }
3506 break;
3507 }
3508
3509 case Primitive::kPrimLong: {
3510 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003511 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 } else {
3513 __ movq(output_register, input_register);
3514 if (imm == -1) {
3515 __ negq(output_register);
3516 }
3517 }
3518 break;
3519 }
3520
3521 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003522 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 }
3524}
3525
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003526void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 LocationSummary* locations = instruction->GetLocations();
3528 Location second = locations->InAt(1);
3529
3530 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3531 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3532
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003533 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003534 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3535 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536
3537 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3538
3539 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003540 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 __ testl(numerator, numerator);
3542 __ cmov(kGreaterEqual, tmp, numerator);
3543 int shift = CTZ(imm);
3544 __ sarl(tmp, Immediate(shift));
3545
3546 if (imm < 0) {
3547 __ negl(tmp);
3548 }
3549
3550 __ movl(output_register, tmp);
3551 } else {
3552 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3553 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3554
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003555 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 __ addq(rdx, numerator);
3557 __ testq(numerator, numerator);
3558 __ cmov(kGreaterEqual, rdx, numerator);
3559 int shift = CTZ(imm);
3560 __ sarq(rdx, Immediate(shift));
3561
3562 if (imm < 0) {
3563 __ negq(rdx);
3564 }
3565
3566 __ movq(output_register, rdx);
3567 }
3568}
3569
3570void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3571 DCHECK(instruction->IsDiv() || instruction->IsRem());
3572
3573 LocationSummary* locations = instruction->GetLocations();
3574 Location second = locations->InAt(1);
3575
3576 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3577 : locations->GetTemp(0).AsRegister<CpuRegister>();
3578 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3579 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3580 : locations->Out().AsRegister<CpuRegister>();
3581 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3582
3583 DCHECK_EQ(RAX, eax.AsRegister());
3584 DCHECK_EQ(RDX, edx.AsRegister());
3585 if (instruction->IsDiv()) {
3586 DCHECK_EQ(RAX, out.AsRegister());
3587 } else {
3588 DCHECK_EQ(RDX, out.AsRegister());
3589 }
3590
3591 int64_t magic;
3592 int shift;
3593
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003594 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595 if (instruction->GetResultType() == Primitive::kPrimInt) {
3596 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3597
3598 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3599
3600 __ movl(numerator, eax);
3601
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003602 __ movl(eax, Immediate(magic));
3603 __ imull(numerator);
3604
3605 if (imm > 0 && magic < 0) {
3606 __ addl(edx, numerator);
3607 } else if (imm < 0 && magic > 0) {
3608 __ subl(edx, numerator);
3609 }
3610
3611 if (shift != 0) {
3612 __ sarl(edx, Immediate(shift));
3613 }
3614
3615 __ movl(eax, edx);
3616 __ shrl(edx, Immediate(31));
3617 __ addl(edx, eax);
3618
3619 if (instruction->IsRem()) {
3620 __ movl(eax, numerator);
3621 __ imull(edx, Immediate(imm));
3622 __ subl(eax, edx);
3623 __ movl(edx, eax);
3624 } else {
3625 __ movl(eax, edx);
3626 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003627 } else {
3628 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3629
3630 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3631
3632 CpuRegister rax = eax;
3633 CpuRegister rdx = edx;
3634
3635 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3636
3637 // Save the numerator.
3638 __ movq(numerator, rax);
3639
3640 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003641 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003642
3643 // RDX:RAX = magic * numerator
3644 __ imulq(numerator);
3645
3646 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003647 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 __ addq(rdx, numerator);
3649 } else if (imm < 0 && magic > 0) {
3650 // RDX -= numerator
3651 __ subq(rdx, numerator);
3652 }
3653
3654 // Shift if needed.
3655 if (shift != 0) {
3656 __ sarq(rdx, Immediate(shift));
3657 }
3658
3659 // RDX += 1 if RDX < 0
3660 __ movq(rax, rdx);
3661 __ shrq(rdx, Immediate(63));
3662 __ addq(rdx, rax);
3663
3664 if (instruction->IsRem()) {
3665 __ movq(rax, numerator);
3666
3667 if (IsInt<32>(imm)) {
3668 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3669 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003670 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003671 }
3672
3673 __ subq(rax, rdx);
3674 __ movq(rdx, rax);
3675 } else {
3676 __ movq(rax, rdx);
3677 }
3678 }
3679}
3680
Calin Juravlebacfec32014-11-14 15:54:36 +00003681void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3682 DCHECK(instruction->IsDiv() || instruction->IsRem());
3683 Primitive::Type type = instruction->GetResultType();
3684 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3685
3686 bool is_div = instruction->IsDiv();
3687 LocationSummary* locations = instruction->GetLocations();
3688
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003689 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3690 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003691
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003693 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003694
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003695 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003696 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003697
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003698 if (imm == 0) {
3699 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3700 } else if (imm == 1 || imm == -1) {
3701 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003702 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003703 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003704 } else {
3705 DCHECK(imm <= -2 || imm >= 2);
3706 GenerateDivRemWithAnyConstant(instruction);
3707 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003708 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003709 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003710 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003711 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003712 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003713
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003714 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3715 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3716 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3717 // so it's safe to just use negl instead of more complex comparisons.
3718 if (type == Primitive::kPrimInt) {
3719 __ cmpl(second_reg, Immediate(-1));
3720 __ j(kEqual, slow_path->GetEntryLabel());
3721 // edx:eax <- sign-extended of eax
3722 __ cdq();
3723 // eax = quotient, edx = remainder
3724 __ idivl(second_reg);
3725 } else {
3726 __ cmpq(second_reg, Immediate(-1));
3727 __ j(kEqual, slow_path->GetEntryLabel());
3728 // rdx:rax <- sign-extended of rax
3729 __ cqo();
3730 // rax = quotient, rdx = remainder
3731 __ idivq(second_reg);
3732 }
3733 __ Bind(slow_path->GetExitLabel());
3734 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003735}
3736
Calin Juravle7c4954d2014-10-28 16:57:40 +00003737void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3738 LocationSummary* locations =
3739 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3740 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003741 case Primitive::kPrimInt:
3742 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003743 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003744 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003745 locations->SetOut(Location::SameAsFirstInput());
3746 // Intel uses edx:eax as the dividend.
3747 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003748 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3749 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3750 // output and request another temp.
3751 if (div->InputAt(1)->IsConstant()) {
3752 locations->AddTemp(Location::RequiresRegister());
3753 }
Calin Juravled0d48522014-11-04 16:40:20 +00003754 break;
3755 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003756
Calin Juravle7c4954d2014-10-28 16:57:40 +00003757 case Primitive::kPrimFloat:
3758 case Primitive::kPrimDouble: {
3759 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003760 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003761 locations->SetOut(Location::SameAsFirstInput());
3762 break;
3763 }
3764
3765 default:
3766 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3767 }
3768}
3769
3770void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3771 LocationSummary* locations = div->GetLocations();
3772 Location first = locations->InAt(0);
3773 Location second = locations->InAt(1);
3774 DCHECK(first.Equals(locations->Out()));
3775
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003776 Primitive::Type type = div->GetResultType();
3777 switch (type) {
3778 case Primitive::kPrimInt:
3779 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003781 break;
3782 }
3783
Calin Juravle7c4954d2014-10-28 16:57:40 +00003784 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003785 if (second.IsFpuRegister()) {
3786 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3787 } else if (second.IsConstant()) {
3788 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003789 codegen_->LiteralFloatAddress(
3790 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003791 } else {
3792 DCHECK(second.IsStackSlot());
3793 __ divss(first.AsFpuRegister<XmmRegister>(),
3794 Address(CpuRegister(RSP), second.GetStackIndex()));
3795 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003796 break;
3797 }
3798
3799 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003800 if (second.IsFpuRegister()) {
3801 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3802 } else if (second.IsConstant()) {
3803 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003804 codegen_->LiteralDoubleAddress(
3805 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003806 } else {
3807 DCHECK(second.IsDoubleStackSlot());
3808 __ divsd(first.AsFpuRegister<XmmRegister>(),
3809 Address(CpuRegister(RSP), second.GetStackIndex()));
3810 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003811 break;
3812 }
3813
3814 default:
3815 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3816 }
3817}
3818
Calin Juravlebacfec32014-11-14 15:54:36 +00003819void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003820 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003821 LocationSummary* locations =
3822 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003823
3824 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003825 case Primitive::kPrimInt:
3826 case Primitive::kPrimLong: {
3827 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003828 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003829 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3830 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003831 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3832 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3833 // output and request another temp.
3834 if (rem->InputAt(1)->IsConstant()) {
3835 locations->AddTemp(Location::RequiresRegister());
3836 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003837 break;
3838 }
3839
3840 case Primitive::kPrimFloat:
3841 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003842 locations->SetInAt(0, Location::Any());
3843 locations->SetInAt(1, Location::Any());
3844 locations->SetOut(Location::RequiresFpuRegister());
3845 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003846 break;
3847 }
3848
3849 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003850 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003851 }
3852}
3853
3854void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3855 Primitive::Type type = rem->GetResultType();
3856 switch (type) {
3857 case Primitive::kPrimInt:
3858 case Primitive::kPrimLong: {
3859 GenerateDivRemIntegral(rem);
3860 break;
3861 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003862 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003863 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003864 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003865 break;
3866 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003867 default:
3868 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3869 }
3870}
3871
Calin Juravled0d48522014-11-04 16:40:20 +00003872void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003873 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003874 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003875}
3876
3877void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003878 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003879 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3880 codegen_->AddSlowPath(slow_path);
3881
3882 LocationSummary* locations = instruction->GetLocations();
3883 Location value = locations->InAt(0);
3884
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003885 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003886 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003887 case Primitive::kPrimByte:
3888 case Primitive::kPrimChar:
3889 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003890 case Primitive::kPrimInt: {
3891 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003893 __ j(kEqual, slow_path->GetEntryLabel());
3894 } else if (value.IsStackSlot()) {
3895 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3896 __ j(kEqual, slow_path->GetEntryLabel());
3897 } else {
3898 DCHECK(value.IsConstant()) << value;
3899 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003900 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003901 }
3902 }
3903 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003904 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003905 case Primitive::kPrimLong: {
3906 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003907 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003908 __ j(kEqual, slow_path->GetEntryLabel());
3909 } else if (value.IsDoubleStackSlot()) {
3910 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3911 __ j(kEqual, slow_path->GetEntryLabel());
3912 } else {
3913 DCHECK(value.IsConstant()) << value;
3914 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003915 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003916 }
3917 }
3918 break;
3919 }
3920 default:
3921 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003922 }
Calin Juravled0d48522014-11-04 16:40:20 +00003923}
3924
Calin Juravle9aec02f2014-11-18 23:06:35 +00003925void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3926 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3927
3928 LocationSummary* locations =
3929 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3930
3931 switch (op->GetResultType()) {
3932 case Primitive::kPrimInt:
3933 case Primitive::kPrimLong: {
3934 locations->SetInAt(0, Location::RequiresRegister());
3935 // The shift count needs to be in CL.
3936 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3937 locations->SetOut(Location::SameAsFirstInput());
3938 break;
3939 }
3940 default:
3941 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3942 }
3943}
3944
3945void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3946 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3947
3948 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003950 Location second = locations->InAt(1);
3951
3952 switch (op->GetResultType()) {
3953 case Primitive::kPrimInt: {
3954 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003955 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003956 if (op->IsShl()) {
3957 __ shll(first_reg, second_reg);
3958 } else if (op->IsShr()) {
3959 __ sarl(first_reg, second_reg);
3960 } else {
3961 __ shrl(first_reg, second_reg);
3962 }
3963 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003964 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003965 if (op->IsShl()) {
3966 __ shll(first_reg, imm);
3967 } else if (op->IsShr()) {
3968 __ sarl(first_reg, imm);
3969 } else {
3970 __ shrl(first_reg, imm);
3971 }
3972 }
3973 break;
3974 }
3975 case Primitive::kPrimLong: {
3976 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003978 if (op->IsShl()) {
3979 __ shlq(first_reg, second_reg);
3980 } else if (op->IsShr()) {
3981 __ sarq(first_reg, second_reg);
3982 } else {
3983 __ shrq(first_reg, second_reg);
3984 }
3985 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003986 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003987 if (op->IsShl()) {
3988 __ shlq(first_reg, imm);
3989 } else if (op->IsShr()) {
3990 __ sarq(first_reg, imm);
3991 } else {
3992 __ shrq(first_reg, imm);
3993 }
3994 }
3995 break;
3996 }
3997 default:
3998 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003999 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004000 }
4001}
4002
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004003void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4004 LocationSummary* locations =
4005 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4006
4007 switch (ror->GetResultType()) {
4008 case Primitive::kPrimInt:
4009 case Primitive::kPrimLong: {
4010 locations->SetInAt(0, Location::RequiresRegister());
4011 // The shift count needs to be in CL (unless it is a constant).
4012 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4013 locations->SetOut(Location::SameAsFirstInput());
4014 break;
4015 }
4016 default:
4017 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4018 UNREACHABLE();
4019 }
4020}
4021
4022void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4023 LocationSummary* locations = ror->GetLocations();
4024 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4025 Location second = locations->InAt(1);
4026
4027 switch (ror->GetResultType()) {
4028 case Primitive::kPrimInt:
4029 if (second.IsRegister()) {
4030 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4031 __ rorl(first_reg, second_reg);
4032 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004033 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004034 __ rorl(first_reg, imm);
4035 }
4036 break;
4037 case Primitive::kPrimLong:
4038 if (second.IsRegister()) {
4039 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4040 __ rorq(first_reg, second_reg);
4041 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004042 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004043 __ rorq(first_reg, imm);
4044 }
4045 break;
4046 default:
4047 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4048 UNREACHABLE();
4049 }
4050}
4051
Calin Juravle9aec02f2014-11-18 23:06:35 +00004052void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4053 HandleShift(shl);
4054}
4055
4056void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4057 HandleShift(shl);
4058}
4059
4060void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4061 HandleShift(shr);
4062}
4063
4064void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4065 HandleShift(shr);
4066}
4067
4068void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4069 HandleShift(ushr);
4070}
4071
4072void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4073 HandleShift(ushr);
4074}
4075
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004076void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004077 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004078 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004079 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004080 if (instruction->IsStringAlloc()) {
4081 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4082 } else {
4083 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4084 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4085 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004086 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004087}
4088
4089void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004090 // Note: if heap poisoning is enabled, the entry point takes cares
4091 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004092 if (instruction->IsStringAlloc()) {
4093 // String is allocated through StringFactory. Call NewEmptyString entry point.
4094 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004095 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004096 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4097 __ call(Address(temp, code_offset.SizeValue()));
4098 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4099 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004100 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004101 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4102 DCHECK(!codegen_->IsLeafMethod());
4103 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004104}
4105
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004106void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4107 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004108 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004109 InvokeRuntimeCallingConvention calling_convention;
4110 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004111 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004112 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004113 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004114}
4115
4116void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
4117 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004118 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
Andreas Gampea5b09a62016-11-17 15:21:22 -08004119 instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01004120 // Note: if heap poisoning is enabled, the entry point takes cares
4121 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004122 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004123 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004124
4125 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004126}
4127
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004128void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004129 LocationSummary* locations =
4130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004131 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4132 if (location.IsStackSlot()) {
4133 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4134 } else if (location.IsDoubleStackSlot()) {
4135 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4136 }
4137 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004138}
4139
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004140void InstructionCodeGeneratorX86_64::VisitParameterValue(
4141 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004142 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004143}
4144
4145void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4146 LocationSummary* locations =
4147 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4148 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4149}
4150
4151void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4152 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4153 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004154}
4155
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004156void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4157 LocationSummary* locations =
4158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4159 locations->SetInAt(0, Location::RequiresRegister());
4160 locations->SetOut(Location::RequiresRegister());
4161}
4162
4163void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4164 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004165 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004166 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004167 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004168 __ movq(locations->Out().AsRegister<CpuRegister>(),
4169 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004170 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004171 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004172 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004173 __ movq(locations->Out().AsRegister<CpuRegister>(),
4174 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4175 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004176 __ movq(locations->Out().AsRegister<CpuRegister>(),
4177 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004178 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004179}
4180
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004181void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004182 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004183 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004184 locations->SetInAt(0, Location::RequiresRegister());
4185 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004186}
4187
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004188void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4189 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004190 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4191 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004192 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004193 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004194 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004196 break;
4197
4198 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004199 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004200 break;
4201
4202 default:
4203 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4204 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004205}
4206
David Brazdil66d126e2015-04-03 16:02:44 +01004207void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4208 LocationSummary* locations =
4209 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4210 locations->SetInAt(0, Location::RequiresRegister());
4211 locations->SetOut(Location::SameAsFirstInput());
4212}
4213
4214void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004215 LocationSummary* locations = bool_not->GetLocations();
4216 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4217 locations->Out().AsRegister<CpuRegister>().AsRegister());
4218 Location out = locations->Out();
4219 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4220}
4221
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004222void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004223 LocationSummary* locations =
4224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004225 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004226 locations->SetInAt(i, Location::Any());
4227 }
4228 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004229}
4230
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004231void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004232 LOG(FATAL) << "Unimplemented";
4233}
4234
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004235void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004236 /*
4237 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004238 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004239 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4240 */
4241 switch (kind) {
4242 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004243 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004244 break;
4245 }
4246 case MemBarrierKind::kAnyStore:
4247 case MemBarrierKind::kLoadAny:
4248 case MemBarrierKind::kStoreStore: {
4249 // nop
4250 break;
4251 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004252 case MemBarrierKind::kNTStoreStore:
4253 // Non-Temporal Store/Store needs an explicit fence.
4254 MemoryFence(/* non-temporal */ true);
4255 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004256 }
4257}
4258
4259void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4260 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4261
Roland Levillain0d5a2812015-11-13 10:07:31 +00004262 bool object_field_get_with_read_barrier =
4263 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004264 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004265 new (GetGraph()->GetArena()) LocationSummary(instruction,
4266 object_field_get_with_read_barrier ?
4267 LocationSummary::kCallOnSlowPath :
4268 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004269 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004270 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004271 }
Calin Juravle52c48962014-12-16 17:02:57 +00004272 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004273 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4274 locations->SetOut(Location::RequiresFpuRegister());
4275 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004276 // The output overlaps for an object field get when read barriers
4277 // are enabled: we do not want the move to overwrite the object's
4278 // location, as we need it to emit the read barrier.
4279 locations->SetOut(
4280 Location::RequiresRegister(),
4281 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004282 }
Calin Juravle52c48962014-12-16 17:02:57 +00004283}
4284
4285void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4286 const FieldInfo& field_info) {
4287 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4288
4289 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004290 Location base_loc = locations->InAt(0);
4291 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004292 Location out = locations->Out();
4293 bool is_volatile = field_info.IsVolatile();
4294 Primitive::Type field_type = field_info.GetFieldType();
4295 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4296
4297 switch (field_type) {
4298 case Primitive::kPrimBoolean: {
4299 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4300 break;
4301 }
4302
4303 case Primitive::kPrimByte: {
4304 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4305 break;
4306 }
4307
4308 case Primitive::kPrimShort: {
4309 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4310 break;
4311 }
4312
4313 case Primitive::kPrimChar: {
4314 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4315 break;
4316 }
4317
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004318 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004319 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4320 break;
4321 }
4322
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004323 case Primitive::kPrimNot: {
4324 // /* HeapReference<Object> */ out = *(base + offset)
4325 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004326 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004327 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004328 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004329 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004330 if (is_volatile) {
4331 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4332 }
4333 } else {
4334 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4335 codegen_->MaybeRecordImplicitNullCheck(instruction);
4336 if (is_volatile) {
4337 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4338 }
4339 // If read barriers are enabled, emit read barriers other than
4340 // Baker's using a slow path (and also unpoison the loaded
4341 // reference, if heap poisoning is enabled).
4342 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4343 }
4344 break;
4345 }
4346
Calin Juravle52c48962014-12-16 17:02:57 +00004347 case Primitive::kPrimLong: {
4348 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4349 break;
4350 }
4351
4352 case Primitive::kPrimFloat: {
4353 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4354 break;
4355 }
4356
4357 case Primitive::kPrimDouble: {
4358 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4359 break;
4360 }
4361
4362 case Primitive::kPrimVoid:
4363 LOG(FATAL) << "Unreachable type " << field_type;
4364 UNREACHABLE();
4365 }
4366
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004367 if (field_type == Primitive::kPrimNot) {
4368 // Potential implicit null checks, in the case of reference
4369 // fields, are handled in the previous switch statement.
4370 } else {
4371 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004372 }
Roland Levillain4d027112015-07-01 15:41:14 +01004373
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004374 if (is_volatile) {
4375 if (field_type == Primitive::kPrimNot) {
4376 // Memory barriers, in the case of references, are also handled
4377 // in the previous switch statement.
4378 } else {
4379 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4380 }
Roland Levillain4d027112015-07-01 15:41:14 +01004381 }
Calin Juravle52c48962014-12-16 17:02:57 +00004382}
4383
4384void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4385 const FieldInfo& field_info) {
4386 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4387
4388 LocationSummary* locations =
4389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004390 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004391 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004392 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004393 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004394
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004395 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004396 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004397 if (is_volatile) {
4398 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4399 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4400 } else {
4401 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4402 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004403 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004404 if (is_volatile) {
4405 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4406 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4407 } else {
4408 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4409 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004410 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004411 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004412 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004413 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004414 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004415 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4416 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004417 locations->AddTemp(Location::RequiresRegister());
4418 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419}
4420
Calin Juravle52c48962014-12-16 17:02:57 +00004421void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004422 const FieldInfo& field_info,
4423 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004424 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4425
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004427 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4428 Location value = locations->InAt(1);
4429 bool is_volatile = field_info.IsVolatile();
4430 Primitive::Type field_type = field_info.GetFieldType();
4431 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4432
4433 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004434 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004435 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436
Mark Mendellea5af682015-10-22 17:35:49 -04004437 bool maybe_record_implicit_null_check_done = false;
4438
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439 switch (field_type) {
4440 case Primitive::kPrimBoolean:
4441 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004442 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004443 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004444 __ movb(Address(base, offset), Immediate(v));
4445 } else {
4446 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4447 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448 break;
4449 }
4450
4451 case Primitive::kPrimShort:
4452 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004453 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004454 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004455 __ movw(Address(base, offset), Immediate(v));
4456 } else {
4457 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4458 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004459 break;
4460 }
4461
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004462 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004464 if (value.IsConstant()) {
4465 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004466 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4467 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4468 // Note: if heap poisoning is enabled, no need to poison
4469 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004470 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004471 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004472 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4473 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4474 __ movl(temp, value.AsRegister<CpuRegister>());
4475 __ PoisonHeapReference(temp);
4476 __ movl(Address(base, offset), temp);
4477 } else {
4478 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4479 }
Mark Mendell40741f32015-04-20 22:10:34 -04004480 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481 break;
4482 }
4483
4484 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004485 if (value.IsConstant()) {
4486 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004487 codegen_->MoveInt64ToAddress(Address(base, offset),
4488 Address(base, offset + sizeof(int32_t)),
4489 v,
4490 instruction);
4491 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004492 } else {
4493 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4494 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004495 break;
4496 }
4497
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004498 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004499 if (value.IsConstant()) {
4500 int32_t v =
4501 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4502 __ movl(Address(base, offset), Immediate(v));
4503 } else {
4504 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4505 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004506 break;
4507 }
4508
4509 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004510 if (value.IsConstant()) {
4511 int64_t v =
4512 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4513 codegen_->MoveInt64ToAddress(Address(base, offset),
4514 Address(base, offset + sizeof(int32_t)),
4515 v,
4516 instruction);
4517 maybe_record_implicit_null_check_done = true;
4518 } else {
4519 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4520 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004521 break;
4522 }
4523
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524 case Primitive::kPrimVoid:
4525 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004526 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004527 }
Calin Juravle52c48962014-12-16 17:02:57 +00004528
Mark Mendellea5af682015-10-22 17:35:49 -04004529 if (!maybe_record_implicit_null_check_done) {
4530 codegen_->MaybeRecordImplicitNullCheck(instruction);
4531 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004532
4533 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4534 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4535 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004536 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004537 }
4538
Calin Juravle52c48962014-12-16 17:02:57 +00004539 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004540 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004541 }
4542}
4543
4544void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4545 HandleFieldSet(instruction, instruction->GetFieldInfo());
4546}
4547
4548void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004549 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004550}
4551
4552void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004553 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004554}
4555
4556void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004557 HandleFieldGet(instruction, instruction->GetFieldInfo());
4558}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559
Calin Juravle52c48962014-12-16 17:02:57 +00004560void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4561 HandleFieldGet(instruction);
4562}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563
Calin Juravle52c48962014-12-16 17:02:57 +00004564void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4565 HandleFieldGet(instruction, instruction->GetFieldInfo());
4566}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004567
Calin Juravle52c48962014-12-16 17:02:57 +00004568void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4569 HandleFieldSet(instruction, instruction->GetFieldInfo());
4570}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004571
Calin Juravle52c48962014-12-16 17:02:57 +00004572void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004573 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004574}
4575
Calin Juravlee460d1d2015-09-29 04:52:17 +01004576void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4577 HUnresolvedInstanceFieldGet* instruction) {
4578 FieldAccessCallingConventionX86_64 calling_convention;
4579 codegen_->CreateUnresolvedFieldLocationSummary(
4580 instruction, instruction->GetFieldType(), calling_convention);
4581}
4582
4583void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4584 HUnresolvedInstanceFieldGet* instruction) {
4585 FieldAccessCallingConventionX86_64 calling_convention;
4586 codegen_->GenerateUnresolvedFieldAccess(instruction,
4587 instruction->GetFieldType(),
4588 instruction->GetFieldIndex(),
4589 instruction->GetDexPc(),
4590 calling_convention);
4591}
4592
4593void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4594 HUnresolvedInstanceFieldSet* instruction) {
4595 FieldAccessCallingConventionX86_64 calling_convention;
4596 codegen_->CreateUnresolvedFieldLocationSummary(
4597 instruction, instruction->GetFieldType(), calling_convention);
4598}
4599
4600void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4601 HUnresolvedInstanceFieldSet* instruction) {
4602 FieldAccessCallingConventionX86_64 calling_convention;
4603 codegen_->GenerateUnresolvedFieldAccess(instruction,
4604 instruction->GetFieldType(),
4605 instruction->GetFieldIndex(),
4606 instruction->GetDexPc(),
4607 calling_convention);
4608}
4609
4610void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4611 HUnresolvedStaticFieldGet* instruction) {
4612 FieldAccessCallingConventionX86_64 calling_convention;
4613 codegen_->CreateUnresolvedFieldLocationSummary(
4614 instruction, instruction->GetFieldType(), calling_convention);
4615}
4616
4617void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4618 HUnresolvedStaticFieldGet* instruction) {
4619 FieldAccessCallingConventionX86_64 calling_convention;
4620 codegen_->GenerateUnresolvedFieldAccess(instruction,
4621 instruction->GetFieldType(),
4622 instruction->GetFieldIndex(),
4623 instruction->GetDexPc(),
4624 calling_convention);
4625}
4626
4627void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4628 HUnresolvedStaticFieldSet* instruction) {
4629 FieldAccessCallingConventionX86_64 calling_convention;
4630 codegen_->CreateUnresolvedFieldLocationSummary(
4631 instruction, instruction->GetFieldType(), calling_convention);
4632}
4633
4634void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4635 HUnresolvedStaticFieldSet* instruction) {
4636 FieldAccessCallingConventionX86_64 calling_convention;
4637 codegen_->GenerateUnresolvedFieldAccess(instruction,
4638 instruction->GetFieldType(),
4639 instruction->GetFieldIndex(),
4640 instruction->GetDexPc(),
4641 calling_convention);
4642}
4643
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004644void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004645 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4646 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4647 ? Location::RequiresRegister()
4648 : Location::Any();
4649 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650}
4651
Calin Juravle2ae48182016-03-16 14:05:09 +00004652void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4653 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004654 return;
4655 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004656 LocationSummary* locations = instruction->GetLocations();
4657 Location obj = locations->InAt(0);
4658
4659 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004660 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004661}
4662
Calin Juravle2ae48182016-03-16 14:05:09 +00004663void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004664 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004665 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004666
4667 LocationSummary* locations = instruction->GetLocations();
4668 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004669
4670 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004671 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004672 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004673 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004674 } else {
4675 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004676 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004677 __ jmp(slow_path->GetEntryLabel());
4678 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004679 }
4680 __ j(kEqual, slow_path->GetEntryLabel());
4681}
4682
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004683void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004684 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004685}
4686
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004688 bool object_array_get_with_read_barrier =
4689 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004690 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004691 new (GetGraph()->GetArena()) LocationSummary(instruction,
4692 object_array_get_with_read_barrier ?
4693 LocationSummary::kCallOnSlowPath :
4694 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004695 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004696 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004697 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004698 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004699 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004700 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4702 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004703 // The output overlaps for an object array get when read barriers
4704 // are enabled: we do not want the move to overwrite the array's
4705 // location, as we need it to emit the read barrier.
4706 locations->SetOut(
4707 Location::RequiresRegister(),
4708 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710}
4711
4712void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4713 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004714 Location obj_loc = locations->InAt(0);
4715 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004717 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004718 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004720 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004721 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004722 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004723 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004724 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004725 break;
4726 }
4727
4728 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004729 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004730 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 break;
4732 }
4733
4734 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004735 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004736 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 break;
4738 }
4739
4740 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004741 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004742 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4743 // Branch cases into compressed and uncompressed for each index's type.
4744 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4745 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004746 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004748 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4749 "Expecting 0=compressed, 1=uncompressed");
4750 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004751 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4752 __ jmp(&done);
4753 __ Bind(&not_compressed);
4754 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4755 __ Bind(&done);
4756 } else {
4757 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4758 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 break;
4760 }
4761
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004762 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004763 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004764 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004765 break;
4766 }
4767
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004768 case Primitive::kPrimNot: {
4769 static_assert(
4770 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4771 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004772 // /* HeapReference<Object> */ out =
4773 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4774 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004775 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004776 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004777 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004778 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004779 } else {
4780 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004781 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4782 codegen_->MaybeRecordImplicitNullCheck(instruction);
4783 // If read barriers are enabled, emit read barriers other than
4784 // Baker's using a slow path (and also unpoison the loaded
4785 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004786 if (index.IsConstant()) {
4787 uint32_t offset =
4788 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004789 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4790 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004791 codegen_->MaybeGenerateReadBarrierSlow(
4792 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4793 }
4794 }
4795 break;
4796 }
4797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004799 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004800 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 break;
4802 }
4803
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004804 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004805 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004806 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004807 break;
4808 }
4809
4810 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004811 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004812 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004813 break;
4814 }
4815
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004817 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004818 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819 }
Roland Levillain4d027112015-07-01 15:41:14 +01004820
4821 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004822 // Potential implicit null checks, in the case of reference
4823 // arrays, are handled in the previous switch statement.
4824 } else {
4825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004826 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827}
4828
4829void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004830 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004831
4832 bool needs_write_barrier =
4833 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004834 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004835
Nicolas Geoffray39468442014-09-02 15:17:15 +01004836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004838 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004839 LocationSummary::kCallOnSlowPath :
4840 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004841
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004842 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004843 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4844 if (Primitive::IsFloatingPointType(value_type)) {
4845 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 } else {
4847 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4848 }
4849
4850 if (needs_write_barrier) {
4851 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004852 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004855}
4856
4857void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4858 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004859 Location array_loc = locations->InAt(0);
4860 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004862 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004863 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004864 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004865 bool needs_write_barrier =
4866 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004867 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4868 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4869 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870
4871 switch (value_type) {
4872 case Primitive::kPrimBoolean:
4873 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004874 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004875 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 if (value.IsRegister()) {
4877 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004879 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882 break;
4883 }
4884
4885 case Primitive::kPrimShort:
4886 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004888 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004889 if (value.IsRegister()) {
4890 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004892 DCHECK(value.IsConstant()) << value;
4893 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004895 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896 break;
4897 }
4898
4899 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004900 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004901 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004902
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 if (!value.IsRegister()) {
4904 // Just setting null.
4905 DCHECK(instruction->InputAt(2)->IsNullConstant());
4906 DCHECK(value.IsConstant()) << value;
4907 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004908 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004909 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004910 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004912 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004913
4914 DCHECK(needs_write_barrier);
4915 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004916 // We cannot use a NearLabel for `done`, as its range may be too
4917 // short when Baker read barriers are enabled.
4918 Label done;
4919 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004920 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004921 Location temp_loc = locations->GetTemp(0);
4922 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004923 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4925 codegen_->AddSlowPath(slow_path);
4926 if (instruction->GetValueCanBeNull()) {
4927 __ testl(register_value, register_value);
4928 __ j(kNotEqual, &not_null);
4929 __ movl(address, Immediate(0));
4930 codegen_->MaybeRecordImplicitNullCheck(instruction);
4931 __ jmp(&done);
4932 __ Bind(&not_null);
4933 }
4934
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004935 // Note that when Baker read barriers are enabled, the type
4936 // checks are performed without read barriers. This is fine,
4937 // even in the case where a class object is in the from-space
4938 // after the flip, as a comparison involving such a type would
4939 // not produce a false positive; it may of course produce a
4940 // false negative, in which case we would take the ArraySet
4941 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004942
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004943 // /* HeapReference<Class> */ temp = array->klass_
4944 __ movl(temp, Address(array, class_offset));
4945 codegen_->MaybeRecordImplicitNullCheck(instruction);
4946 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004947
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004948 // /* HeapReference<Class> */ temp = temp->component_type_
4949 __ movl(temp, Address(temp, component_offset));
4950 // If heap poisoning is enabled, no need to unpoison `temp`
4951 // nor the object reference in `register_value->klass`, as
4952 // we are comparing two poisoned references.
4953 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004954
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004955 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4956 __ j(kEqual, &do_put);
4957 // If heap poisoning is enabled, the `temp` reference has
4958 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004959 __ MaybeUnpoisonHeapReference(temp);
4960
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004961 // If heap poisoning is enabled, no need to unpoison the
4962 // heap reference loaded below, as it is only used for a
4963 // comparison with null.
4964 __ cmpl(Address(temp, super_offset), Immediate(0));
4965 __ j(kNotEqual, slow_path->GetEntryLabel());
4966 __ Bind(&do_put);
4967 } else {
4968 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004969 }
4970 }
4971
4972 if (kPoisonHeapReferences) {
4973 __ movl(temp, register_value);
4974 __ PoisonHeapReference(temp);
4975 __ movl(address, temp);
4976 } else {
4977 __ movl(address, register_value);
4978 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004979 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004980 codegen_->MaybeRecordImplicitNullCheck(instruction);
4981 }
4982
4983 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4984 codegen_->MarkGCCard(
4985 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4986 __ Bind(&done);
4987
4988 if (slow_path != nullptr) {
4989 __ Bind(slow_path->GetExitLabel());
4990 }
4991
4992 break;
4993 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004994
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004995 case Primitive::kPrimInt: {
4996 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004997 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004998 if (value.IsRegister()) {
4999 __ movl(address, value.AsRegister<CpuRegister>());
5000 } else {
5001 DCHECK(value.IsConstant()) << value;
5002 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5003 __ movl(address, Immediate(v));
5004 }
5005 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005006 break;
5007 }
5008
5009 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005010 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005011 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005012 if (value.IsRegister()) {
5013 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005014 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005016 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005017 Address address_high =
5018 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005019 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020 }
5021 break;
5022 }
5023
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005024 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005025 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005026 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005027 if (value.IsFpuRegister()) {
5028 __ movss(address, value.AsFpuRegister<XmmRegister>());
5029 } else {
5030 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005031 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005032 __ movl(address, Immediate(v));
5033 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005034 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005035 break;
5036 }
5037
5038 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005039 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005040 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005041 if (value.IsFpuRegister()) {
5042 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5043 codegen_->MaybeRecordImplicitNullCheck(instruction);
5044 } else {
5045 int64_t v =
5046 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005047 Address address_high =
5048 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005049 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5050 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005051 break;
5052 }
5053
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 case Primitive::kPrimVoid:
5055 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005056 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 }
5058}
5059
5060void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005061 LocationSummary* locations =
5062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005063 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005064 if (!instruction->IsEmittedAtUseSite()) {
5065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5066 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067}
5068
5069void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005070 if (instruction->IsEmittedAtUseSite()) {
5071 return;
5072 }
5073
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005075 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005076 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5077 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005079 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005080 // Mask out most significant bit in case the array is String's array of char.
5081 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005082 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084}
5085
5086void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005087 RegisterSet caller_saves = RegisterSet::Empty();
5088 InvokeRuntimeCallingConvention calling_convention;
5089 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5090 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5091 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005092 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005093 HInstruction* length = instruction->InputAt(1);
5094 if (!length->IsEmittedAtUseSite()) {
5095 locations->SetInAt(1, Location::RegisterOrConstant(length));
5096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097}
5098
5099void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5100 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005101 Location index_loc = locations->InAt(0);
5102 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005103 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104
Mark Mendell99dbd682015-04-22 16:18:52 -04005105 if (length_loc.IsConstant()) {
5106 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5107 if (index_loc.IsConstant()) {
5108 // BCE will remove the bounds check if we are guarenteed to pass.
5109 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5110 if (index < 0 || index >= length) {
5111 codegen_->AddSlowPath(slow_path);
5112 __ jmp(slow_path->GetEntryLabel());
5113 } else {
5114 // Some optimization after BCE may have generated this, and we should not
5115 // generate a bounds check if it is a valid range.
5116 }
5117 return;
5118 }
5119
5120 // We have to reverse the jump condition because the length is the constant.
5121 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5122 __ cmpl(index_reg, Immediate(length));
5123 codegen_->AddSlowPath(slow_path);
5124 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005125 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005126 HInstruction* array_length = instruction->InputAt(1);
5127 if (array_length->IsEmittedAtUseSite()) {
5128 // Address the length field in the array.
5129 DCHECK(array_length->IsArrayLength());
5130 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5131 Location array_loc = array_length->GetLocations()->InAt(0);
5132 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005133 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005134 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5135 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005136 CpuRegister length_reg = CpuRegister(TMP);
5137 __ movl(length_reg, array_len);
5138 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005139 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005140 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005141 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005142 // Checking the bound for general case:
5143 // Array of char or String's array when the compression feature off.
5144 if (index_loc.IsConstant()) {
5145 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5146 __ cmpl(array_len, Immediate(value));
5147 } else {
5148 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5149 }
5150 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005151 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005152 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005153 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005154 }
5155 codegen_->AddSlowPath(slow_path);
5156 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005157 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158}
5159
5160void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5161 CpuRegister card,
5162 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005163 CpuRegister value,
5164 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005165 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005166 if (value_can_be_null) {
5167 __ testl(value, value);
5168 __ j(kEqual, &is_null);
5169 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005170 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005171 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 __ movq(temp, object);
5173 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005174 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005175 if (value_can_be_null) {
5176 __ Bind(&is_null);
5177 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178}
5179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005180void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005181 LOG(FATAL) << "Unimplemented";
5182}
5183
5184void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005185 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5186}
5187
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005188void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005189 LocationSummary* locations =
5190 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005191 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005192}
5193
5194void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005195 HBasicBlock* block = instruction->GetBlock();
5196 if (block->GetLoopInformation() != nullptr) {
5197 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5198 // The back edge will generate the suspend check.
5199 return;
5200 }
5201 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5202 // The goto will generate the suspend check.
5203 return;
5204 }
5205 GenerateSuspendCheck(instruction, nullptr);
5206}
5207
5208void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5209 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005210 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005211 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5212 if (slow_path == nullptr) {
5213 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5214 instruction->SetSlowPath(slow_path);
5215 codegen_->AddSlowPath(slow_path);
5216 if (successor != nullptr) {
5217 DCHECK(successor->IsLoopHeader());
5218 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5219 }
5220 } else {
5221 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5222 }
5223
Andreas Gampe542451c2016-07-26 09:02:02 -07005224 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005225 /* no_rip */ true),
5226 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005227 if (successor == nullptr) {
5228 __ j(kNotEqual, slow_path->GetEntryLabel());
5229 __ Bind(slow_path->GetReturnLabel());
5230 } else {
5231 __ j(kEqual, codegen_->GetLabelOf(successor));
5232 __ jmp(slow_path->GetEntryLabel());
5233 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005234}
5235
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005236X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5237 return codegen_->GetAssembler();
5238}
5239
5240void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005241 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005242 Location source = move->GetSource();
5243 Location destination = move->GetDestination();
5244
5245 if (source.IsRegister()) {
5246 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005247 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005248 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005249 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005250 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005251 } else {
5252 DCHECK(destination.IsDoubleStackSlot());
5253 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005254 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005255 }
5256 } else if (source.IsStackSlot()) {
5257 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005258 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005259 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005261 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005263 } else {
5264 DCHECK(destination.IsStackSlot());
5265 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5266 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5267 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005268 } else if (source.IsDoubleStackSlot()) {
5269 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005270 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005271 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005272 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005273 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5274 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005275 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005276 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005277 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5278 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5279 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005280 } else if (source.IsConstant()) {
5281 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005282 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5283 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005284 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005285 if (value == 0) {
5286 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5287 } else {
5288 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5289 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005290 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005292 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005293 }
5294 } else if (constant->IsLongConstant()) {
5295 int64_t value = constant->AsLongConstant()->GetValue();
5296 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005297 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005298 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005299 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005300 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005301 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005303 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005305 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005306 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005307 } else {
5308 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005309 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5311 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005312 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005313 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005314 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005315 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005317 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005318 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005319 } else {
5320 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005321 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005322 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005323 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005324 } else if (source.IsFpuRegister()) {
5325 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005326 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005327 } else if (destination.IsStackSlot()) {
5328 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005329 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005330 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005331 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005332 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005333 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005334 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005335 }
5336}
5337
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005338void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005339 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005340 __ movl(Address(CpuRegister(RSP), mem), reg);
5341 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005342}
5343
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005344void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005345 ScratchRegisterScope ensure_scratch(
5346 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5347
5348 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5349 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5350 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5351 Address(CpuRegister(RSP), mem2 + stack_offset));
5352 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5353 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5354 CpuRegister(ensure_scratch.GetRegister()));
5355}
5356
Mark Mendell8a1c7282015-06-29 15:41:28 -04005357void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5358 __ movq(CpuRegister(TMP), reg1);
5359 __ movq(reg1, reg2);
5360 __ movq(reg2, CpuRegister(TMP));
5361}
5362
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005363void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5364 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5365 __ movq(Address(CpuRegister(RSP), mem), reg);
5366 __ movq(reg, CpuRegister(TMP));
5367}
5368
5369void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5370 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005371 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005372
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005373 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5374 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5375 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5376 Address(CpuRegister(RSP), mem2 + stack_offset));
5377 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5378 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5379 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005380}
5381
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005382void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5383 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5384 __ movss(Address(CpuRegister(RSP), mem), reg);
5385 __ movd(reg, CpuRegister(TMP));
5386}
5387
5388void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5389 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5390 __ movsd(Address(CpuRegister(RSP), mem), reg);
5391 __ movd(reg, CpuRegister(TMP));
5392}
5393
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005394void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005395 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005396 Location source = move->GetSource();
5397 Location destination = move->GetDestination();
5398
5399 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005400 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005401 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005402 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005403 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005404 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005405 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005406 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5407 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005408 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005409 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005410 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005411 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5412 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005413 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005414 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5415 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5416 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005417 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005418 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005419 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005420 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005421 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005422 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005423 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005424 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005425 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005426 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005427 }
5428}
5429
5430
5431void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5432 __ pushq(CpuRegister(reg));
5433}
5434
5435
5436void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5437 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005438}
5439
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005440void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005441 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005442 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5443 Immediate(mirror::Class::kStatusInitialized));
5444 __ j(kLess, slow_path->GetEntryLabel());
5445 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005446 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005447}
5448
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005449HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5450 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005451 switch (desired_class_load_kind) {
5452 case HLoadClass::LoadKind::kReferrersClass:
5453 break;
5454 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5455 DCHECK(!GetCompilerOptions().GetCompilePic());
5456 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5457 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5458 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5459 DCHECK(GetCompilerOptions().GetCompilePic());
5460 break;
5461 case HLoadClass::LoadKind::kBootImageAddress:
5462 break;
5463 case HLoadClass::LoadKind::kDexCacheAddress:
5464 DCHECK(Runtime::Current()->UseJitCompilation());
5465 break;
5466 case HLoadClass::LoadKind::kDexCachePcRelative:
5467 DCHECK(!Runtime::Current()->UseJitCompilation());
5468 break;
5469 case HLoadClass::LoadKind::kDexCacheViaMethod:
5470 break;
5471 }
5472 return desired_class_load_kind;
5473}
5474
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005475void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005476 if (cls->NeedsAccessCheck()) {
5477 InvokeRuntimeCallingConvention calling_convention;
5478 CodeGenerator::CreateLoadClassLocationSummary(
5479 cls,
5480 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5481 Location::RegisterLocation(RAX),
5482 /* code_generator_supports_read_barrier */ true);
5483 return;
5484 }
5485
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005486 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5487 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005488 ? LocationSummary::kCallOnSlowPath
5489 : LocationSummary::kNoCall;
5490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005491 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005492 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005493 }
5494
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005495 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5496 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5497 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5498 locations->SetInAt(0, Location::RequiresRegister());
5499 }
5500 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005501}
5502
5503void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005504 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005505 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005506 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescuba45db02016-07-12 22:53:02 +01005507 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005508 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005509 return;
5510 }
5511
Roland Levillain0d5a2812015-11-13 10:07:31 +00005512 Location out_loc = locations->Out();
5513 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005515 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5516 ? kWithoutReadBarrier
5517 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005518 bool generate_null_check = false;
5519 switch (cls->GetLoadKind()) {
5520 case HLoadClass::LoadKind::kReferrersClass: {
5521 DCHECK(!cls->CanCallRuntime());
5522 DCHECK(!cls->MustGenerateClinitCheck());
5523 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5524 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5525 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005526 cls,
5527 out_loc,
5528 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005529 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005530 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531 break;
5532 }
5533 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005534 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005535 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5536 codegen_->RecordTypePatch(cls);
5537 break;
5538 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005539 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005540 DCHECK_NE(cls->GetAddress(), 0u);
5541 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5542 __ movl(out, Immediate(address)); // Zero-extended.
5543 codegen_->RecordSimplePatch();
5544 break;
5545 }
5546 case HLoadClass::LoadKind::kDexCacheAddress: {
5547 DCHECK_NE(cls->GetAddress(), 0u);
5548 // /* GcRoot<mirror::Class> */ out = *address
5549 if (IsUint<32>(cls->GetAddress())) {
5550 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005551 GenerateGcRootFieldLoad(cls,
5552 out_loc,
5553 address,
Roland Levillain00468f32016-10-27 18:02:48 +01005554 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005555 read_barrier_option);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005556 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5558 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005559 GenerateGcRootFieldLoad(cls,
5560 out_loc,
5561 Address(out, 0),
Roland Levillain00468f32016-10-27 18:02:48 +01005562 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005563 read_barrier_option);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005564 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005565 generate_null_check = !cls->IsInDexCache();
5566 break;
5567 }
5568 case HLoadClass::LoadKind::kDexCachePcRelative: {
5569 uint32_t offset = cls->GetDexCacheElementOffset();
5570 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5571 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5572 /* no_rip */ false);
5573 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005574 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005575 generate_null_check = !cls->IsInDexCache();
5576 break;
5577 }
5578 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5579 // /* GcRoot<mirror::Class>[] */ out =
5580 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5581 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5582 __ movq(out,
5583 Address(current_method,
5584 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5585 // /* GcRoot<mirror::Class> */ out = out[type_index]
5586 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005587 cls,
5588 out_loc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005589 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_)),
Roland Levillain00468f32016-10-27 18:02:48 +01005590 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005591 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005592 generate_null_check = !cls->IsInDexCache();
5593 break;
5594 }
5595 default:
5596 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5597 UNREACHABLE();
5598 }
5599
5600 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5601 DCHECK(cls->CanCallRuntime());
5602 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5603 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5604 codegen_->AddSlowPath(slow_path);
5605 if (generate_null_check) {
5606 __ testl(out, out);
5607 __ j(kEqual, slow_path->GetEntryLabel());
5608 }
5609 if (cls->MustGenerateClinitCheck()) {
5610 GenerateClassInitializationCheck(slow_path, out);
5611 } else {
5612 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005613 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005614 }
5615}
5616
5617void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5618 LocationSummary* locations =
5619 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5620 locations->SetInAt(0, Location::RequiresRegister());
5621 if (check->HasUses()) {
5622 locations->SetOut(Location::SameAsFirstInput());
5623 }
5624}
5625
5626void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005627 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005628 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005629 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005630 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005631 GenerateClassInitializationCheck(slow_path,
5632 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005633}
5634
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005635HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5636 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005637 switch (desired_string_load_kind) {
5638 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5639 DCHECK(!GetCompilerOptions().GetCompilePic());
5640 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5641 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5642 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5643 DCHECK(GetCompilerOptions().GetCompilePic());
5644 break;
5645 case HLoadString::LoadKind::kBootImageAddress:
5646 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005647 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005648 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005649 break;
5650 case HLoadString::LoadKind::kDexCacheViaMethod:
5651 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005652 case HLoadString::LoadKind::kJitTableAddress:
5653 DCHECK(Runtime::Current()->UseJitCompilation());
5654 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005655 }
5656 return desired_string_load_kind;
5657}
5658
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005659void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005660 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005662 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005663 locations->SetOut(Location::RegisterLocation(RAX));
5664 } else {
5665 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005666 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5667 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5668 // Rely on the pResolveString and/or marking to save everything.
5669 // Custom calling convention: RAX serves as both input and output.
5670 RegisterSet caller_saves = RegisterSet::Empty();
5671 caller_saves.Add(Location::RegisterLocation(RAX));
5672 locations->SetCustomSlowPathCallerSaves(caller_saves);
5673 } else {
5674 // For non-Baker read barrier we have a temp-clobbering call.
5675 }
5676 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005677 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005678}
5679
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005680Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
5681 dex::StringIndex dex_index) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005682 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index), /* placeholder */ 0u);
5683 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005684 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005685 PatchInfo<Label>* info = &jit_string_patches_.back();
5686 return &info->label;
5687}
5688
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005689void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005690 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005691 Location out_loc = locations->Out();
5692 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005694 switch (load->GetLoadKind()) {
5695 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005696 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005697 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005698 return; // No dex cache slow path.
5699 }
5700 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005701 DCHECK_NE(load->GetAddress(), 0u);
5702 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5703 __ movl(out, Immediate(address)); // Zero-extended.
5704 codegen_->RecordSimplePatch();
5705 return; // No dex cache slow path.
5706 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005707 case HLoadString::LoadKind::kBssEntry: {
5708 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5709 /* no_rip */ false);
5710 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5711 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005712 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005713 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5714 codegen_->AddSlowPath(slow_path);
5715 __ testl(out, out);
5716 __ j(kEqual, slow_path->GetEntryLabel());
5717 __ Bind(slow_path->GetExitLabel());
5718 return;
5719 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005720 case HLoadString::LoadKind::kJitTableAddress: {
5721 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5722 /* no_rip */ true);
5723 Label* fixup_label =
5724 codegen_->NewJitRootStringPatch(load->GetDexFile(), load->GetStringIndex());
5725 // /* GcRoot<mirror::String> */ out = *address
5726 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5727 return;
5728 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005729 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005730 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005731 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005733 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005734 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005735 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005736 codegen_->InvokeRuntime(kQuickResolveString,
5737 load,
5738 load->GetDexPc());
5739 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005740}
5741
David Brazdilcb1c0552015-08-04 16:22:25 +01005742static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005743 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005744 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005745}
5746
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005747void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5748 LocationSummary* locations =
5749 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5750 locations->SetOut(Location::RequiresRegister());
5751}
5752
5753void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005754 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5755}
5756
5757void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5758 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5759}
5760
5761void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5762 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005763}
5764
5765void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5766 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005767 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005768 InvokeRuntimeCallingConvention calling_convention;
5769 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5770}
5771
5772void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005773 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005774 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005775}
5776
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005777static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5778 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005779 // We need a temporary for holding the iftable length.
5780 return true;
5781 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005782 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005783 !kUseBakerReadBarrier &&
5784 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005785 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5786 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5787}
5788
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005789static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5790 return kEmitCompilerReadBarrier &&
5791 !kUseBakerReadBarrier &&
5792 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5793 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5794 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5795}
5796
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005797void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005798 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005800 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005802 case TypeCheckKind::kExactCheck:
5803 case TypeCheckKind::kAbstractClassCheck:
5804 case TypeCheckKind::kClassHierarchyCheck:
5805 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 call_kind =
5807 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005808 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 break;
5810 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005811 case TypeCheckKind::kUnresolvedCheck:
5812 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005813 call_kind = LocationSummary::kCallOnSlowPath;
5814 break;
5815 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005817 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005818 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005819 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005820 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821 locations->SetInAt(0, Location::RequiresRegister());
5822 locations->SetInAt(1, Location::Any());
5823 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5824 locations->SetOut(Location::RequiresRegister());
5825 // When read barriers are enabled, we need a temporary register for
5826 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005827 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005830}
5831
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005832void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005833 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005834 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835 Location obj_loc = locations->InAt(0);
5836 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005837 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005838 Location out_loc = locations->Out();
5839 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005840 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005841 locations->GetTemp(0) :
5842 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005843 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5845 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5846 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005847 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005848 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005849
5850 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005851 // Avoid null check if we know obj is not null.
5852 if (instruction->MustDoNullCheck()) {
5853 __ testl(obj, obj);
5854 __ j(kEqual, &zero);
5855 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005856
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005857 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005858 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005859 // /* HeapReference<Class> */ out = obj->klass_
5860 GenerateReferenceLoadTwoRegisters(instruction,
5861 out_loc,
5862 obj_loc,
5863 class_offset,
5864 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 if (cls.IsRegister()) {
5866 __ cmpl(out, cls.AsRegister<CpuRegister>());
5867 } else {
5868 DCHECK(cls.IsStackSlot()) << cls;
5869 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5870 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005871 if (zero.IsLinked()) {
5872 // Classes must be equal for the instanceof to succeed.
5873 __ j(kNotEqual, &zero);
5874 __ movl(out, Immediate(1));
5875 __ jmp(&done);
5876 } else {
5877 __ setcc(kEqual, out);
5878 // setcc only sets the low byte.
5879 __ andl(out, Immediate(1));
5880 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 break;
5882 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005885 // /* HeapReference<Class> */ out = obj->klass_
5886 GenerateReferenceLoadTwoRegisters(instruction,
5887 out_loc,
5888 obj_loc,
5889 class_offset,
5890 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 // If the class is abstract, we eagerly fetch the super class of the
5892 // object to avoid doing a comparison we know will fail.
5893 NearLabel loop, success;
5894 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005896 GenerateReferenceLoadOneRegister(instruction,
5897 out_loc,
5898 super_offset,
5899 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005900 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 __ testl(out, out);
5902 // If `out` is null, we use it for the result, and jump to `done`.
5903 __ j(kEqual, &done);
5904 if (cls.IsRegister()) {
5905 __ cmpl(out, cls.AsRegister<CpuRegister>());
5906 } else {
5907 DCHECK(cls.IsStackSlot()) << cls;
5908 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5909 }
5910 __ j(kNotEqual, &loop);
5911 __ movl(out, Immediate(1));
5912 if (zero.IsLinked()) {
5913 __ jmp(&done);
5914 }
5915 break;
5916 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005917
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005919 // /* HeapReference<Class> */ out = obj->klass_
5920 GenerateReferenceLoadTwoRegisters(instruction,
5921 out_loc,
5922 obj_loc,
5923 class_offset,
5924 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 // Walk over the class hierarchy to find a match.
5926 NearLabel loop, success;
5927 __ Bind(&loop);
5928 if (cls.IsRegister()) {
5929 __ cmpl(out, cls.AsRegister<CpuRegister>());
5930 } else {
5931 DCHECK(cls.IsStackSlot()) << cls;
5932 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5933 }
5934 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005936 GenerateReferenceLoadOneRegister(instruction,
5937 out_loc,
5938 super_offset,
5939 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005940 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 __ testl(out, out);
5942 __ j(kNotEqual, &loop);
5943 // If `out` is null, we use it for the result, and jump to `done`.
5944 __ jmp(&done);
5945 __ Bind(&success);
5946 __ movl(out, Immediate(1));
5947 if (zero.IsLinked()) {
5948 __ jmp(&done);
5949 }
5950 break;
5951 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005952
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005954 // /* HeapReference<Class> */ out = obj->klass_
5955 GenerateReferenceLoadTwoRegisters(instruction,
5956 out_loc,
5957 obj_loc,
5958 class_offset,
5959 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005960 // Do an exact check.
5961 NearLabel exact_check;
5962 if (cls.IsRegister()) {
5963 __ cmpl(out, cls.AsRegister<CpuRegister>());
5964 } else {
5965 DCHECK(cls.IsStackSlot()) << cls;
5966 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5967 }
5968 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005969 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005971 GenerateReferenceLoadOneRegister(instruction,
5972 out_loc,
5973 component_offset,
5974 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005975 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976 __ testl(out, out);
5977 // If `out` is null, we use it for the result, and jump to `done`.
5978 __ j(kEqual, &done);
5979 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5980 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005981 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 __ movl(out, Immediate(1));
5983 __ jmp(&done);
5984 break;
5985 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005988 // No read barrier since the slow path will retry upon failure.
5989 // /* HeapReference<Class> */ out = obj->klass_
5990 GenerateReferenceLoadTwoRegisters(instruction,
5991 out_loc,
5992 obj_loc,
5993 class_offset,
5994 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995 if (cls.IsRegister()) {
5996 __ cmpl(out, cls.AsRegister<CpuRegister>());
5997 } else {
5998 DCHECK(cls.IsStackSlot()) << cls;
5999 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6000 }
6001 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006002 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6003 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006004 codegen_->AddSlowPath(slow_path);
6005 __ j(kNotEqual, slow_path->GetEntryLabel());
6006 __ movl(out, Immediate(1));
6007 if (zero.IsLinked()) {
6008 __ jmp(&done);
6009 }
6010 break;
6011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012
Calin Juravle98893e12015-10-02 21:05:03 +01006013 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006014 case TypeCheckKind::kInterfaceCheck: {
6015 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006016 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006017 // cases.
6018 //
6019 // We cannot directly call the InstanceofNonTrivial runtime
6020 // entry point without resorting to a type checking slow path
6021 // here (i.e. by calling InvokeRuntime directly), as it would
6022 // require to assign fixed registers for the inputs of this
6023 // HInstanceOf instruction (following the runtime calling
6024 // convention), which might be cluttered by the potential first
6025 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006026 //
6027 // TODO: Introduce a new runtime entry point taking the object
6028 // to test (instead of its class) as argument, and let it deal
6029 // with the read barrier issues. This will let us refactor this
6030 // case of the `switch` code as it was previously (with a direct
6031 // call to the runtime not using a type checking slow path).
6032 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033 DCHECK(locations->OnlyCallsOnSlowPath());
6034 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6035 /* is_fatal */ false);
6036 codegen_->AddSlowPath(slow_path);
6037 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006038 if (zero.IsLinked()) {
6039 __ jmp(&done);
6040 }
6041 break;
6042 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006043 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006044
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006045 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006046 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 __ xorl(out, out);
6048 }
6049
6050 if (done.IsLinked()) {
6051 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006052 }
6053
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006054 if (slow_path != nullptr) {
6055 __ Bind(slow_path->GetExitLabel());
6056 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006057}
6058
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006059static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006060 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006061 case TypeCheckKind::kExactCheck:
6062 case TypeCheckKind::kAbstractClassCheck:
6063 case TypeCheckKind::kClassHierarchyCheck:
6064 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006065 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006066 case TypeCheckKind::kInterfaceCheck:
6067 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006068 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006070 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006071 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006072 LOG(FATAL) << "Unreachable";
6073 UNREACHABLE();
6074}
6075
6076void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6077 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6078 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6079 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6080 LocationSummary::CallKind call_kind = is_fatal_slow_path
6081 ? LocationSummary::kNoCall
6082 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6084 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006085 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6086 // Require a register for the interface check since there is a loop that compares the class to
6087 // a memory address.
6088 locations->SetInAt(1, Location::RequiresRegister());
6089 } else {
6090 locations->SetInAt(1, Location::Any());
6091 }
6092
Roland Levillain0d5a2812015-11-13 10:07:31 +00006093 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6094 locations->AddTemp(Location::RequiresRegister());
6095 // When read barriers are enabled, we need an additional temporary
6096 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006097 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006100}
6101
6102void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006103 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006104 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006105 Location obj_loc = locations->InAt(0);
6106 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006107 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006108 Location temp_loc = locations->GetTemp(0);
6109 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006110 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006111 locations->GetTemp(1) :
6112 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006113 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6114 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6115 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6116 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6117 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6118 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006119 const uint32_t object_array_data_offset =
6120 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006121
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006122 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6123 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6124 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006125 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006126 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127 SlowPathCode* type_check_slow_path =
6128 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6129 is_type_check_slow_path_fatal);
6130 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006131
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006132
6133 NearLabel done;
6134 // Avoid null check if we know obj is not null.
6135 if (instruction->MustDoNullCheck()) {
6136 __ testl(obj, obj);
6137 __ j(kEqual, &done);
6138 }
6139
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 case TypeCheckKind::kExactCheck:
6142 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006143 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006144 GenerateReferenceLoadTwoRegisters(instruction,
6145 temp_loc,
6146 obj_loc,
6147 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006148 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 if (cls.IsRegister()) {
6150 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6151 } else {
6152 DCHECK(cls.IsStackSlot()) << cls;
6153 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6154 }
6155 // Jump to slow path for throwing the exception or doing a
6156 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 break;
6159 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006160
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006162 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006163 GenerateReferenceLoadTwoRegisters(instruction,
6164 temp_loc,
6165 obj_loc,
6166 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006167 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 // If the class is abstract, we eagerly fetch the super class of the
6169 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006170 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006172 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006173 GenerateReferenceLoadOneRegister(instruction,
6174 temp_loc,
6175 super_offset,
6176 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006177 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006179 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6180 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006182 // Otherwise, compare the classes.
6183 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 if (cls.IsRegister()) {
6185 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6186 } else {
6187 DCHECK(cls.IsStackSlot()) << cls;
6188 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6189 }
6190 __ j(kNotEqual, &loop);
6191 break;
6192 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006193
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006194 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006195 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006196 GenerateReferenceLoadTwoRegisters(instruction,
6197 temp_loc,
6198 obj_loc,
6199 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006200 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006202 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 __ Bind(&loop);
6204 if (cls.IsRegister()) {
6205 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6206 } else {
6207 DCHECK(cls.IsStackSlot()) << cls;
6208 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6209 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006210 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211
Roland Levillain0d5a2812015-11-13 10:07:31 +00006212 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006213 GenerateReferenceLoadOneRegister(instruction,
6214 temp_loc,
6215 super_offset,
6216 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006217 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218
6219 // If the class reference currently in `temp` is not null, jump
6220 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006222 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225 break;
6226 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006229 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006230 GenerateReferenceLoadTwoRegisters(instruction,
6231 temp_loc,
6232 obj_loc,
6233 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006234 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006235 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006237 if (cls.IsRegister()) {
6238 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6239 } else {
6240 DCHECK(cls.IsStackSlot()) << cls;
6241 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6242 }
6243 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244
6245 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006247 GenerateReferenceLoadOneRegister(instruction,
6248 temp_loc,
6249 component_offset,
6250 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006251 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252
6253 // If the component type is not null (i.e. the object is indeed
6254 // an array), jump to label `check_non_primitive_component_type`
6255 // to further check that this component type is not a primitive
6256 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006258 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006259 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006261 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006262 break;
6263 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006265 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006266 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267 //
6268 // We cannot directly call the CheckCast runtime entry point
6269 // without resorting to a type checking slow path here (i.e. by
6270 // calling InvokeRuntime directly), as it would require to
6271 // assign fixed registers for the inputs of this HInstanceOf
6272 // instruction (following the runtime calling convention), which
6273 // might be cluttered by the potential first read barrier
6274 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006275 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006276 break;
6277 }
6278
6279 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006280 // Fast path for the interface check. We always go slow path for heap poisoning since
6281 // unpoisoning cls would require an extra temp.
6282 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006283 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6284 // doing this.
6285 // /* HeapReference<Class> */ temp = obj->klass_
6286 GenerateReferenceLoadTwoRegisters(instruction,
6287 temp_loc,
6288 obj_loc,
6289 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006290 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006291
6292 // /* HeapReference<Class> */ temp = temp->iftable_
6293 GenerateReferenceLoadTwoRegisters(instruction,
6294 temp_loc,
6295 temp_loc,
6296 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006297 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006298 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006299 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006300 // Loop through the iftable and check if any class matches.
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006301 NearLabel start_loop;
6302 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006303 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006304 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006305 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6306 // Go to next interface if the classes do not match.
6307 __ cmpl(cls.AsRegister<CpuRegister>(),
6308 CodeGeneratorX86_64::ArrayAddress(temp,
6309 maybe_temp2_loc,
6310 TIMES_4,
6311 object_array_data_offset));
6312 __ j(kNotEqual, &start_loop); // Return if same class.
6313 } else {
6314 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006315 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 break;
6317 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006319 if (done.IsLinked()) {
6320 __ Bind(&done);
6321 }
6322
Roland Levillain0d5a2812015-11-13 10:07:31 +00006323 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324}
6325
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006326void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6327 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006329 InvokeRuntimeCallingConvention calling_convention;
6330 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6331}
6332
6333void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006334 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006335 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006336 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006337 if (instruction->IsEnter()) {
6338 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6339 } else {
6340 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6341 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006342}
6343
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006344void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6345void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6346void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6347
6348void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6349 LocationSummary* locations =
6350 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6351 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6352 || instruction->GetResultType() == Primitive::kPrimLong);
6353 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006354 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006355 locations->SetOut(Location::SameAsFirstInput());
6356}
6357
6358void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6359 HandleBitwiseOperation(instruction);
6360}
6361
6362void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6363 HandleBitwiseOperation(instruction);
6364}
6365
6366void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6367 HandleBitwiseOperation(instruction);
6368}
6369
6370void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6371 LocationSummary* locations = instruction->GetLocations();
6372 Location first = locations->InAt(0);
6373 Location second = locations->InAt(1);
6374 DCHECK(first.Equals(locations->Out()));
6375
6376 if (instruction->GetResultType() == Primitive::kPrimInt) {
6377 if (second.IsRegister()) {
6378 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006379 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006380 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006381 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006382 } else {
6383 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006384 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006385 }
6386 } else if (second.IsConstant()) {
6387 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6388 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006389 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006390 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006391 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006392 } else {
6393 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006394 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006395 }
6396 } else {
6397 Address address(CpuRegister(RSP), second.GetStackIndex());
6398 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006399 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006400 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006401 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006402 } else {
6403 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006404 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006405 }
6406 }
6407 } else {
6408 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006409 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6410 bool second_is_constant = false;
6411 int64_t value = 0;
6412 if (second.IsConstant()) {
6413 second_is_constant = true;
6414 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006415 }
Mark Mendell40741f32015-04-20 22:10:34 -04006416 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006417
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006418 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006419 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006420 if (is_int32_value) {
6421 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6422 } else {
6423 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6424 }
6425 } else if (second.IsDoubleStackSlot()) {
6426 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006427 } else {
6428 __ andq(first_reg, second.AsRegister<CpuRegister>());
6429 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006430 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006431 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006432 if (is_int32_value) {
6433 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6434 } else {
6435 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6436 }
6437 } else if (second.IsDoubleStackSlot()) {
6438 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006439 } else {
6440 __ orq(first_reg, second.AsRegister<CpuRegister>());
6441 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006442 } else {
6443 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006444 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006445 if (is_int32_value) {
6446 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6447 } else {
6448 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6449 }
6450 } else if (second.IsDoubleStackSlot()) {
6451 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006452 } else {
6453 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6454 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006455 }
6456 }
6457}
6458
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006459void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6460 HInstruction* instruction,
6461 Location out,
6462 uint32_t offset,
6463 Location maybe_temp,
6464 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006465 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006466 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006467 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006468 if (kUseBakerReadBarrier) {
6469 // Load with fast path based Baker's read barrier.
6470 // /* HeapReference<Object> */ out = *(out + offset)
6471 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006472 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006473 } else {
6474 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006475 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006476 // in the following move operation, as we will need it for the
6477 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006478 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006479 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006480 // /* HeapReference<Object> */ out = *(out + offset)
6481 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006482 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006483 }
6484 } else {
6485 // Plain load with no read barrier.
6486 // /* HeapReference<Object> */ out = *(out + offset)
6487 __ movl(out_reg, Address(out_reg, offset));
6488 __ MaybeUnpoisonHeapReference(out_reg);
6489 }
6490}
6491
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006492void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6493 HInstruction* instruction,
6494 Location out,
6495 Location obj,
6496 uint32_t offset,
6497 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006498 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6499 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006500 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006501 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006502 if (kUseBakerReadBarrier) {
6503 // Load with fast path based Baker's read barrier.
6504 // /* HeapReference<Object> */ out = *(obj + offset)
6505 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006506 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006507 } else {
6508 // Load with slow path based read barrier.
6509 // /* HeapReference<Object> */ out = *(obj + offset)
6510 __ movl(out_reg, Address(obj_reg, offset));
6511 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6512 }
6513 } else {
6514 // Plain load with no read barrier.
6515 // /* HeapReference<Object> */ out = *(obj + offset)
6516 __ movl(out_reg, Address(obj_reg, offset));
6517 __ MaybeUnpoisonHeapReference(out_reg);
6518 }
6519}
6520
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006521void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6522 HInstruction* instruction,
6523 Location root,
6524 const Address& address,
6525 Label* fixup_label,
6526 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006527 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006528 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006529 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006530 if (kUseBakerReadBarrier) {
6531 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6532 // Baker's read barrier are used:
6533 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006534 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006535 // if (Thread::Current()->GetIsGcMarking()) {
6536 // root = ReadBarrier::Mark(root)
6537 // }
6538
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006539 // /* GcRoot<mirror::Object> */ root = *address
6540 __ movl(root_reg, address);
6541 if (fixup_label != nullptr) {
6542 __ Bind(fixup_label);
6543 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006544 static_assert(
6545 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6546 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6547 "have different sizes.");
6548 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6549 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6550 "have different sizes.");
6551
Vladimir Marko953437b2016-08-24 08:30:46 +00006552 // Slow path marking the GC root `root`.
6553 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006554 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006555 codegen_->AddSlowPath(slow_path);
6556
Andreas Gampe542451c2016-07-26 09:02:02 -07006557 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006558 /* no_rip */ true),
6559 Immediate(0));
6560 __ j(kNotEqual, slow_path->GetEntryLabel());
6561 __ Bind(slow_path->GetExitLabel());
6562 } else {
6563 // GC root loaded through a slow path for read barriers other
6564 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006565 // /* GcRoot<mirror::Object>* */ root = address
6566 __ leaq(root_reg, address);
6567 if (fixup_label != nullptr) {
6568 __ Bind(fixup_label);
6569 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006570 // /* mirror::Object* */ root = root->Read()
6571 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6572 }
6573 } else {
6574 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006575 // /* GcRoot<mirror::Object> */ root = *address
6576 __ movl(root_reg, address);
6577 if (fixup_label != nullptr) {
6578 __ Bind(fixup_label);
6579 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006580 // Note that GC roots are not affected by heap poisoning, thus we
6581 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006582 }
6583}
6584
6585void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6586 Location ref,
6587 CpuRegister obj,
6588 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006589 bool needs_null_check) {
6590 DCHECK(kEmitCompilerReadBarrier);
6591 DCHECK(kUseBakerReadBarrier);
6592
6593 // /* HeapReference<Object> */ ref = *(obj + offset)
6594 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006595 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006596}
6597
6598void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6599 Location ref,
6600 CpuRegister obj,
6601 uint32_t data_offset,
6602 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006603 bool needs_null_check) {
6604 DCHECK(kEmitCompilerReadBarrier);
6605 DCHECK(kUseBakerReadBarrier);
6606
Roland Levillain3d312422016-06-23 13:53:42 +01006607 static_assert(
6608 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6609 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006610 // /* HeapReference<Object> */ ref =
6611 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006612 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006613 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006614}
6615
6616void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6617 Location ref,
6618 CpuRegister obj,
6619 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006620 bool needs_null_check,
6621 bool always_update_field,
6622 CpuRegister* temp1,
6623 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006624 DCHECK(kEmitCompilerReadBarrier);
6625 DCHECK(kUseBakerReadBarrier);
6626
6627 // In slow path based read barriers, the read barrier call is
6628 // inserted after the original load. However, in fast path based
6629 // Baker's read barriers, we need to perform the load of
6630 // mirror::Object::monitor_ *before* the original reference load.
6631 // This load-load ordering is required by the read barrier.
6632 // The fast path/slow path (for Baker's algorithm) should look like:
6633 //
6634 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6635 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6636 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006637 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006638 // if (is_gray) {
6639 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6640 // }
6641 //
6642 // Note: the original implementation in ReadBarrier::Barrier is
6643 // slightly more complex as:
6644 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006645 // the high-bits of rb_state, which are expected to be all zeroes
6646 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6647 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006648 // - it performs additional checks that we do not do here for
6649 // performance reasons.
6650
6651 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006652 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6653
Vladimir Marko953437b2016-08-24 08:30:46 +00006654 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006655 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6656 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006657 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6658 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6659 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6660
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006661 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006662 // ref = ReadBarrier::Mark(ref);
6663 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6664 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006665 if (needs_null_check) {
6666 MaybeRecordImplicitNullCheck(instruction);
6667 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006668
6669 // Load fence to prevent load-load reordering.
6670 // Note that this is a no-op, thanks to the x86-64 memory model.
6671 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6672
6673 // The actual reference load.
6674 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006675 __ movl(ref_reg, src); // Flags are unaffected.
6676
6677 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6678 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006679 SlowPathCode* slow_path;
6680 if (always_update_field) {
6681 DCHECK(temp1 != nullptr);
6682 DCHECK(temp2 != nullptr);
6683 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6684 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6685 } else {
6686 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6687 instruction, ref, /* unpoison_ref_before_marking */ true);
6688 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006689 AddSlowPath(slow_path);
6690
6691 // We have done the "if" of the gray bit check above, now branch based on the flags.
6692 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006693
6694 // Object* ref = ref_addr->AsMirrorPtr()
6695 __ MaybeUnpoisonHeapReference(ref_reg);
6696
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006697 __ Bind(slow_path->GetExitLabel());
6698}
6699
6700void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6701 Location out,
6702 Location ref,
6703 Location obj,
6704 uint32_t offset,
6705 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006706 DCHECK(kEmitCompilerReadBarrier);
6707
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006708 // Insert a slow path based read barrier *after* the reference load.
6709 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 // If heap poisoning is enabled, the unpoisoning of the loaded
6711 // reference will be carried out by the runtime within the slow
6712 // path.
6713 //
6714 // Note that `ref` currently does not get unpoisoned (when heap
6715 // poisoning is enabled), which is alright as the `ref` argument is
6716 // not used by the artReadBarrierSlow entry point.
6717 //
6718 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6719 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6720 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6721 AddSlowPath(slow_path);
6722
Roland Levillain0d5a2812015-11-13 10:07:31 +00006723 __ jmp(slow_path->GetEntryLabel());
6724 __ Bind(slow_path->GetExitLabel());
6725}
6726
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006727void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6728 Location out,
6729 Location ref,
6730 Location obj,
6731 uint32_t offset,
6732 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006734 // Baker's read barriers shall be handled by the fast path
6735 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6736 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737 // If heap poisoning is enabled, unpoisoning will be taken care of
6738 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006739 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006740 } else if (kPoisonHeapReferences) {
6741 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6742 }
6743}
6744
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006745void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6746 Location out,
6747 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748 DCHECK(kEmitCompilerReadBarrier);
6749
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006750 // Insert a slow path based read barrier *after* the GC root load.
6751 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006752 // Note that GC roots are not affected by heap poisoning, so we do
6753 // not need to do anything special for this here.
6754 SlowPathCode* slow_path =
6755 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6756 AddSlowPath(slow_path);
6757
Roland Levillain0d5a2812015-11-13 10:07:31 +00006758 __ jmp(slow_path->GetEntryLabel());
6759 __ Bind(slow_path->GetExitLabel());
6760}
6761
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006762void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006763 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006764 LOG(FATAL) << "Unreachable";
6765}
6766
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006767void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006768 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006769 LOG(FATAL) << "Unreachable";
6770}
6771
Mark Mendellfe57faa2015-09-18 09:26:15 -04006772// Simple implementation of packed switch - generate cascaded compare/jumps.
6773void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6774 LocationSummary* locations =
6775 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6776 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006777 locations->AddTemp(Location::RequiresRegister());
6778 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006779}
6780
6781void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6782 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006783 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006784 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006785 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6786 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6787 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006788 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6789
6790 // Should we generate smaller inline compare/jumps?
6791 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6792 // Figure out the correct compare values and jump conditions.
6793 // Handle the first compare/branch as a special case because it might
6794 // jump to the default case.
6795 DCHECK_GT(num_entries, 2u);
6796 Condition first_condition;
6797 uint32_t index;
6798 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6799 if (lower_bound != 0) {
6800 first_condition = kLess;
6801 __ cmpl(value_reg_in, Immediate(lower_bound));
6802 __ j(first_condition, codegen_->GetLabelOf(default_block));
6803 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6804
6805 index = 1;
6806 } else {
6807 // Handle all the compare/jumps below.
6808 first_condition = kBelow;
6809 index = 0;
6810 }
6811
6812 // Handle the rest of the compare/jumps.
6813 for (; index + 1 < num_entries; index += 2) {
6814 int32_t compare_to_value = lower_bound + index + 1;
6815 __ cmpl(value_reg_in, Immediate(compare_to_value));
6816 // Jump to successors[index] if value < case_value[index].
6817 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6818 // Jump to successors[index + 1] if value == case_value[index + 1].
6819 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6820 }
6821
6822 if (index != num_entries) {
6823 // There are an odd number of entries. Handle the last one.
6824 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006825 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006826 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6827 }
6828
6829 // And the default for any other value.
6830 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6831 __ jmp(codegen_->GetLabelOf(default_block));
6832 }
6833 return;
6834 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006835
6836 // Remove the bias, if needed.
6837 Register value_reg_out = value_reg_in.AsRegister();
6838 if (lower_bound != 0) {
6839 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6840 value_reg_out = temp_reg.AsRegister();
6841 }
6842 CpuRegister value_reg(value_reg_out);
6843
6844 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006845 __ cmpl(value_reg, Immediate(num_entries - 1));
6846 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006847
Mark Mendell9c86b482015-09-18 13:36:07 -04006848 // We are in the range of the table.
6849 // Load the address of the jump table in the constant area.
6850 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006851
Mark Mendell9c86b482015-09-18 13:36:07 -04006852 // Load the (signed) offset from the jump table.
6853 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6854
6855 // Add the offset to the address of the table base.
6856 __ addq(temp_reg, base_reg);
6857
6858 // And jump.
6859 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006860}
6861
Aart Bikc5d47542016-01-27 17:00:35 -08006862void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6863 if (value == 0) {
6864 __ xorl(dest, dest);
6865 } else {
6866 __ movl(dest, Immediate(value));
6867 }
6868}
6869
Mark Mendell92e83bf2015-05-07 11:25:03 -04006870void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6871 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006872 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006873 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006874 } else if (IsUint<32>(value)) {
6875 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006876 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6877 } else {
6878 __ movq(dest, Immediate(value));
6879 }
6880}
6881
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006882void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6883 if (value == 0) {
6884 __ xorps(dest, dest);
6885 } else {
6886 __ movss(dest, LiteralInt32Address(value));
6887 }
6888}
6889
6890void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6891 if (value == 0) {
6892 __ xorpd(dest, dest);
6893 } else {
6894 __ movsd(dest, LiteralInt64Address(value));
6895 }
6896}
6897
6898void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6899 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6900}
6901
6902void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6903 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6904}
6905
Aart Bika19616e2016-02-01 18:57:58 -08006906void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6907 if (value == 0) {
6908 __ testl(dest, dest);
6909 } else {
6910 __ cmpl(dest, Immediate(value));
6911 }
6912}
6913
6914void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6915 if (IsInt<32>(value)) {
6916 if (value == 0) {
6917 __ testq(dest, dest);
6918 } else {
6919 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6920 }
6921 } else {
6922 // Value won't fit in an int.
6923 __ cmpq(dest, LiteralInt64Address(value));
6924 }
6925}
6926
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006927void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6928 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006929 GenerateIntCompare(lhs_reg, rhs);
6930}
6931
6932void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006933 if (rhs.IsConstant()) {
6934 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006935 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006936 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006937 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006938 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006939 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006940 }
6941}
6942
6943void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6944 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6945 if (rhs.IsConstant()) {
6946 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6947 Compare64BitValue(lhs_reg, value);
6948 } else if (rhs.IsDoubleStackSlot()) {
6949 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6950 } else {
6951 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6952 }
6953}
6954
6955Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6956 Location index,
6957 ScaleFactor scale,
6958 uint32_t data_offset) {
6959 return index.IsConstant() ?
6960 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6961 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6962}
6963
Mark Mendellcfa410b2015-05-25 16:02:44 -04006964void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6965 DCHECK(dest.IsDoubleStackSlot());
6966 if (IsInt<32>(value)) {
6967 // Can move directly as an int32 constant.
6968 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6969 Immediate(static_cast<int32_t>(value)));
6970 } else {
6971 Load64BitValue(CpuRegister(TMP), value);
6972 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6973 }
6974}
6975
Mark Mendell9c86b482015-09-18 13:36:07 -04006976/**
6977 * Class to handle late fixup of offsets into constant area.
6978 */
6979class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6980 public:
6981 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6982 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6983
6984 protected:
6985 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6986
6987 CodeGeneratorX86_64* codegen_;
6988
6989 private:
6990 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6991 // Patch the correct offset for the instruction. We use the address of the
6992 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6993 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6994 int32_t relative_position = constant_offset - pos;
6995
6996 // Patch in the right value.
6997 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6998 }
6999
7000 // Location in constant area that the fixup refers to.
7001 size_t offset_into_constant_area_;
7002};
7003
7004/**
7005 t * Class to handle late fixup of offsets to a jump table that will be created in the
7006 * constant area.
7007 */
7008class JumpTableRIPFixup : public RIPFixup {
7009 public:
7010 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7011 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7012
7013 void CreateJumpTable() {
7014 X86_64Assembler* assembler = codegen_->GetAssembler();
7015
7016 // Ensure that the reference to the jump table has the correct offset.
7017 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7018 SetOffset(offset_in_constant_table);
7019
7020 // Compute the offset from the start of the function to this jump table.
7021 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7022
7023 // Populate the jump table with the correct values for the jump table.
7024 int32_t num_entries = switch_instr_->GetNumEntries();
7025 HBasicBlock* block = switch_instr_->GetBlock();
7026 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7027 // The value that we want is the target offset - the position of the table.
7028 for (int32_t i = 0; i < num_entries; i++) {
7029 HBasicBlock* b = successors[i];
7030 Label* l = codegen_->GetLabelOf(b);
7031 DCHECK(l->IsBound());
7032 int32_t offset_to_block = l->Position() - current_table_offset;
7033 assembler->AppendInt32(offset_to_block);
7034 }
7035 }
7036
7037 private:
7038 const HPackedSwitch* switch_instr_;
7039};
7040
Mark Mendellf55c3e02015-03-26 21:07:46 -04007041void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7042 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007043 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007044 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7045 // 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 -04007046 assembler->Align(4, 0);
7047 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007048
7049 // Populate any jump tables.
7050 for (auto jump_table : fixups_to_jump_tables_) {
7051 jump_table->CreateJumpTable();
7052 }
7053
7054 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007055 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007056 }
7057
7058 // And finish up.
7059 CodeGenerator::Finalize(allocator);
7060}
7061
Mark Mendellf55c3e02015-03-26 21:07:46 -04007062Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7063 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7064 return Address::RIP(fixup);
7065}
7066
7067Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7068 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7069 return Address::RIP(fixup);
7070}
7071
7072Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7073 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7074 return Address::RIP(fixup);
7075}
7076
7077Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7078 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7079 return Address::RIP(fixup);
7080}
7081
Andreas Gampe85b62f22015-09-09 13:15:38 -07007082// TODO: trg as memory.
7083void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7084 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007085 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007086 return;
7087 }
7088
7089 DCHECK_NE(type, Primitive::kPrimVoid);
7090
7091 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7092 if (trg.Equals(return_loc)) {
7093 return;
7094 }
7095
7096 // Let the parallel move resolver take care of all of this.
7097 HParallelMove parallel_move(GetGraph()->GetArena());
7098 parallel_move.AddMove(return_loc, trg, type, nullptr);
7099 GetMoveResolver()->EmitNativeCode(&parallel_move);
7100}
7101
Mark Mendell9c86b482015-09-18 13:36:07 -04007102Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7103 // Create a fixup to be used to create and address the jump table.
7104 JumpTableRIPFixup* table_fixup =
7105 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7106
7107 // We have to populate the jump tables.
7108 fixups_to_jump_tables_.push_back(table_fixup);
7109 return Address::RIP(table_fixup);
7110}
7111
Mark Mendellea5af682015-10-22 17:35:49 -04007112void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7113 const Address& addr_high,
7114 int64_t v,
7115 HInstruction* instruction) {
7116 if (IsInt<32>(v)) {
7117 int32_t v_32 = v;
7118 __ movq(addr_low, Immediate(v_32));
7119 MaybeRecordImplicitNullCheck(instruction);
7120 } else {
7121 // Didn't fit in a register. Do it in pieces.
7122 int32_t low_v = Low32Bits(v);
7123 int32_t high_v = High32Bits(v);
7124 __ movl(addr_low, Immediate(low_v));
7125 MaybeRecordImplicitNullCheck(instruction);
7126 __ movl(addr_high, Immediate(high_v));
7127 }
7128}
7129
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007130void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7131 for (const PatchInfo<Label>& info : jit_string_patches_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007132 const auto& it = jit_string_roots_.find(StringReference(&info.dex_file,
7133 dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007134 DCHECK(it != jit_string_roots_.end());
7135 size_t index_in_table = it->second;
7136 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7137 uintptr_t address =
7138 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7139 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7140 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7141 dchecked_integral_cast<uint32_t>(address);
7142 }
7143}
7144
Roland Levillain4d027112015-07-01 15:41:14 +01007145#undef __
7146
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007147} // namespace x86_64
7148} // namespace art