blob: 665d028338c24612e3d1fac5269a5c19fbec602c [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) {
202 __ andl(length_loc.AsRegister<CpuRegister>(), Immediate(INT32_MAX));
203 }
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;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100250 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000251 at_,
252 dex_pc_,
253 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000254 if (do_clinit_) {
255 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
256 } else {
257 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
258 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100259
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262 if (out.IsValid()) {
263 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000264 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 }
266
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000267 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100268 __ jmp(GetExitLabel());
269 }
270
Alexandre Rames9931f312015-06-19 14:47:01 +0100271 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
272
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100273 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 // The class this slow path will load.
275 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100276
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 // The instruction where this slow path is happening.
278 // (Might be the load class or an initialization check).
279 HInstruction* const at_;
280
281 // The dex PC of `at_`.
282 const uint32_t dex_pc_;
283
284 // Whether to initialize the class.
285 const bool do_clinit_;
286
287 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288};
289
Vladimir Markoaad75c62016-10-03 08:46:48 +0000290class LoadStringSlowPathX86_64 : public SlowPathCode {
291 public:
292 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
295 LocationSummary* locations = instruction_->GetLocations();
296 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
297
298 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
299 __ Bind(GetEntryLabel());
300 SaveLiveRegisters(codegen, locations);
301
302 InvokeRuntimeCallingConvention calling_convention;
303 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
304 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
305 x86_64_codegen->InvokeRuntime(kQuickResolveString,
306 instruction_,
307 instruction_->GetDexPc(),
308 this);
309 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
310 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
311 RestoreLiveRegisters(codegen, locations);
312
313 // Store the resolved String to the BSS entry.
314 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
315 locations->Out().AsRegister<CpuRegister>());
316 Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
317 __ Bind(fixup_label);
318
319 __ jmp(GetExitLabel());
320 }
321
322 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
323
324 private:
325 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
326};
327
Andreas Gampe85b62f22015-09-09 13:15:38 -0700328class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000331 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
336 : locations->Out();
337 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 DCHECK(instruction_->IsCheckCast()
339 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
Roland Levillain0d5a2812015-11-13 10:07:31 +0000341 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 if (!is_fatal_) {
345 SaveLiveRegisters(codegen, locations);
346 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347
348 // We're moving two locations to locations that could overlap, so we need a parallel
349 // move resolver.
350 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000351 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100352 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000353 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100354 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100355 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100356 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
357 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100360 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000361 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700362 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 } else {
364 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100365 x86_64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000366 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000367 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 if (!is_fatal_) {
370 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000371 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000373
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 RestoreLiveRegisters(codegen, locations);
375 __ jmp(GetExitLabel());
376 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377 }
378
Alexandre Rames9931f312015-06-19 14:47:01 +0100379 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
380
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000381 bool IsFatal() const OVERRIDE { return is_fatal_; }
382
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000383 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000385
386 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
387};
388
Andreas Gampe85b62f22015-09-09 13:15:38 -0700389class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 public:
Aart Bik42249c32016-01-07 15:33:50 -0800391 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000392 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393
394 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000395 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100397 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000398 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 }
400
Alexandre Rames9931f312015-06-19 14:47:01 +0100401 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
402
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
405};
406
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407class ArraySetSlowPathX86_64 : public SlowPathCode {
408 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000409 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100410
411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
412 LocationSummary* locations = instruction_->GetLocations();
413 __ Bind(GetEntryLabel());
414 SaveLiveRegisters(codegen, locations);
415
416 InvokeRuntimeCallingConvention calling_convention;
417 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
418 parallel_move.AddMove(
419 locations->InAt(0),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
421 Primitive::kPrimNot,
422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(1),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
426 Primitive::kPrimInt,
427 nullptr);
428 parallel_move.AddMove(
429 locations->InAt(2),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
431 Primitive::kPrimNot,
432 nullptr);
433 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
434
Roland Levillain0d5a2812015-11-13 10:07:31 +0000435 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100436 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000437 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 RestoreLiveRegisters(codegen, locations);
439 __ jmp(GetExitLabel());
440 }
441
442 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
443
444 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
446};
447
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000448// Slow path marking an object during a read barrier.
449class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
450 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000451 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
452 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000453 DCHECK(kEmitCompilerReadBarrier);
454 }
455
456 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
457
458 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
459 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100460 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000461 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100462 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000463 DCHECK(instruction_->IsInstanceFieldGet() ||
464 instruction_->IsStaticFieldGet() ||
465 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100466 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000467 instruction_->IsLoadClass() ||
468 instruction_->IsLoadString() ||
469 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100470 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100471 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
472 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000473 << "Unexpected instruction in read barrier marking slow path: "
474 << instruction_->DebugName();
475
476 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000477 if (unpoison_) {
478 // Object* ref = ref_addr->AsMirrorPtr()
479 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
480 }
Roland Levillain4359e612016-07-20 11:32:19 +0100481 // No need to save live registers; it's taken care of by the
482 // entrypoint. Also, there is no need to update the stack mask,
483 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000484 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100485 DCHECK_NE(reg, RSP);
486 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
487 // "Compact" slow path, saving two moves.
488 //
489 // Instead of using the standard runtime calling convention (input
490 // and output in R0):
491 //
492 // RDI <- obj
493 // RAX <- ReadBarrierMark(RDI)
494 // obj <- RAX
495 //
496 // we just use rX (the register holding `obj`) as input and output
497 // of a dedicated entrypoint:
498 //
499 // rX <- ReadBarrierMarkRegX(rX)
500 //
501 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700502 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100503 // This runtime call does not require a stack map.
504 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000505 __ jmp(GetExitLabel());
506 }
507
508 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000509 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000510 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000511
512 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
513};
514
Roland Levillain0d5a2812015-11-13 10:07:31 +0000515// Slow path generating a read barrier for a heap reference.
516class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
517 public:
518 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
519 Location out,
520 Location ref,
521 Location obj,
522 uint32_t offset,
523 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000524 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525 out_(out),
526 ref_(ref),
527 obj_(obj),
528 offset_(offset),
529 index_(index) {
530 DCHECK(kEmitCompilerReadBarrier);
531 // If `obj` is equal to `out` or `ref`, it means the initial
532 // object has been overwritten by (or after) the heap object
533 // reference load to be instrumented, e.g.:
534 //
535 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000536 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000537 //
538 // In that case, we have lost the information about the original
539 // object, and the emitted read barrier cannot work properly.
540 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
541 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
542}
543
544 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
545 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
546 LocationSummary* locations = instruction_->GetLocations();
547 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
548 DCHECK(locations->CanCall());
549 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100550 DCHECK(instruction_->IsInstanceFieldGet() ||
551 instruction_->IsStaticFieldGet() ||
552 instruction_->IsArrayGet() ||
553 instruction_->IsInstanceOf() ||
554 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100555 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000556 << "Unexpected instruction in read barrier for heap reference slow path: "
557 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000558
559 __ Bind(GetEntryLabel());
560 SaveLiveRegisters(codegen, locations);
561
562 // We may have to change the index's value, but as `index_` is a
563 // constant member (like other "inputs" of this slow path),
564 // introduce a copy of it, `index`.
565 Location index = index_;
566 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100567 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000568 if (instruction_->IsArrayGet()) {
569 // Compute real offset and store it in index_.
570 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
571 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
572 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
573 // We are about to change the value of `index_reg` (see the
574 // calls to art::x86_64::X86_64Assembler::shll and
575 // art::x86_64::X86_64Assembler::AddImmediate below), but it
576 // has not been saved by the previous call to
577 // art::SlowPathCode::SaveLiveRegisters, as it is a
578 // callee-save register --
579 // art::SlowPathCode::SaveLiveRegisters does not consider
580 // callee-save registers, as it has been designed with the
581 // assumption that callee-save registers are supposed to be
582 // handled by the called function. So, as a callee-save
583 // register, `index_reg` _would_ eventually be saved onto
584 // the stack, but it would be too late: we would have
585 // changed its value earlier. Therefore, we manually save
586 // it here into another freely available register,
587 // `free_reg`, chosen of course among the caller-save
588 // registers (as a callee-save `free_reg` register would
589 // exhibit the same problem).
590 //
591 // Note we could have requested a temporary register from
592 // the register allocator instead; but we prefer not to, as
593 // this is a slow path, and we know we can find a
594 // caller-save register that is available.
595 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
596 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
597 index_reg = free_reg;
598 index = Location::RegisterLocation(index_reg);
599 } else {
600 // The initial register stored in `index_` has already been
601 // saved in the call to art::SlowPathCode::SaveLiveRegisters
602 // (as it is not a callee-save register), so we can freely
603 // use it.
604 }
605 // Shifting the index value contained in `index_reg` by the
606 // scale factor (2) cannot overflow in practice, as the
607 // runtime is unable to allocate object arrays with a size
608 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
609 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
610 static_assert(
611 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
612 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
613 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
614 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100615 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
616 // intrinsics, `index_` is not shifted by a scale factor of 2
617 // (as in the case of ArrayGet), as it is actually an offset
618 // to an object field within an object.
619 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000620 DCHECK(instruction_->GetLocations()->Intrinsified());
621 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
622 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
623 << instruction_->AsInvoke()->GetIntrinsic();
624 DCHECK_EQ(offset_, 0U);
625 DCHECK(index_.IsRegister());
626 }
627 }
628
629 // We're moving two or three locations to locations that could
630 // overlap, so we need a parallel move resolver.
631 InvokeRuntimeCallingConvention calling_convention;
632 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
633 parallel_move.AddMove(ref_,
634 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
635 Primitive::kPrimNot,
636 nullptr);
637 parallel_move.AddMove(obj_,
638 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
639 Primitive::kPrimNot,
640 nullptr);
641 if (index.IsValid()) {
642 parallel_move.AddMove(index,
643 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
644 Primitive::kPrimInt,
645 nullptr);
646 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
647 } else {
648 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
649 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
650 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100651 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000652 instruction_,
653 instruction_->GetDexPc(),
654 this);
655 CheckEntrypointTypes<
656 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
657 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
658
659 RestoreLiveRegisters(codegen, locations);
660 __ jmp(GetExitLabel());
661 }
662
663 const char* GetDescription() const OVERRIDE {
664 return "ReadBarrierForHeapReferenceSlowPathX86_64";
665 }
666
667 private:
668 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
669 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
670 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
671 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
672 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
673 return static_cast<CpuRegister>(i);
674 }
675 }
676 // We shall never fail to find a free caller-save register, as
677 // there are more than two core caller-save registers on x86-64
678 // (meaning it is possible to find one which is different from
679 // `ref` and `obj`).
680 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
681 LOG(FATAL) << "Could not find a free caller-save register";
682 UNREACHABLE();
683 }
684
Roland Levillain0d5a2812015-11-13 10:07:31 +0000685 const Location out_;
686 const Location ref_;
687 const Location obj_;
688 const uint32_t offset_;
689 // An additional location containing an index to an array.
690 // Only used for HArrayGet and the UnsafeGetObject &
691 // UnsafeGetObjectVolatile intrinsics.
692 const Location index_;
693
694 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
695};
696
697// Slow path generating a read barrier for a GC root.
698class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
699 public:
700 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000701 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000702 DCHECK(kEmitCompilerReadBarrier);
703 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000704
705 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
706 LocationSummary* locations = instruction_->GetLocations();
707 DCHECK(locations->CanCall());
708 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000709 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
710 << "Unexpected instruction in read barrier for GC root slow path: "
711 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000712
713 __ Bind(GetEntryLabel());
714 SaveLiveRegisters(codegen, locations);
715
716 InvokeRuntimeCallingConvention calling_convention;
717 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
718 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100719 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000720 instruction_,
721 instruction_->GetDexPc(),
722 this);
723 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
724 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
725
726 RestoreLiveRegisters(codegen, locations);
727 __ jmp(GetExitLabel());
728 }
729
730 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
731
732 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000733 const Location out_;
734 const Location root_;
735
736 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
737};
738
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100739#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100740// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
741#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100742
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700744 switch (cond) {
745 case kCondEQ: return kEqual;
746 case kCondNE: return kNotEqual;
747 case kCondLT: return kLess;
748 case kCondLE: return kLessEqual;
749 case kCondGT: return kGreater;
750 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700751 case kCondB: return kBelow;
752 case kCondBE: return kBelowEqual;
753 case kCondA: return kAbove;
754 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700755 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100756 LOG(FATAL) << "Unreachable";
757 UNREACHABLE();
758}
759
Aart Bike9f37602015-10-09 11:15:55 -0700760// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100761inline Condition X86_64FPCondition(IfCondition cond) {
762 switch (cond) {
763 case kCondEQ: return kEqual;
764 case kCondNE: return kNotEqual;
765 case kCondLT: return kBelow;
766 case kCondLE: return kBelowEqual;
767 case kCondGT: return kAbove;
768 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700769 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100770 };
771 LOG(FATAL) << "Unreachable";
772 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700773}
774
Vladimir Markodc151b22015-10-15 18:02:30 +0100775HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
776 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100777 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100778 switch (desired_dispatch_info.code_ptr_location) {
779 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
780 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
781 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
782 return HInvokeStaticOrDirect::DispatchInfo {
783 desired_dispatch_info.method_load_kind,
784 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
785 desired_dispatch_info.method_load_data,
786 0u
787 };
788 default:
789 return desired_dispatch_info;
790 }
791}
792
Serguei Katkov288c7a82016-05-16 11:53:15 +0600793Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
794 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800795 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000796 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
797 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100798 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000799 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100800 uint32_t offset =
801 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
802 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000803 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100804 }
Vladimir Marko58155012015-08-19 12:49:41 +0000805 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000806 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000807 break;
808 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
809 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
810 break;
811 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
812 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000813 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
814 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000815 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
816 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000817 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000818 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000819 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000820 // Bind a new fixup label at the end of the "movl" insn.
821 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100822 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000823 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000824 }
Vladimir Marko58155012015-08-19 12:49:41 +0000825 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000826 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000827 Register method_reg;
828 CpuRegister reg = temp.AsRegister<CpuRegister>();
829 if (current_method.IsRegister()) {
830 method_reg = current_method.AsRegister<Register>();
831 } else {
832 DCHECK(invoke->GetLocations()->Intrinsified());
833 DCHECK(!current_method.IsValid());
834 method_reg = reg.AsRegister();
835 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000837 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100838 __ movq(reg,
839 Address(CpuRegister(method_reg),
840 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100841 // temp = temp[index_in_cache];
842 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
843 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000844 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
845 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100846 }
Vladimir Marko58155012015-08-19 12:49:41 +0000847 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600848 return callee_method;
849}
850
851void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
852 Location temp) {
853 // All registers are assumed to be correctly set up.
854 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000855
856 switch (invoke->GetCodePtrLocation()) {
857 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
858 __ call(&frame_entry_label_);
859 break;
860 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000861 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
862 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000863 Label* label = &relative_call_patches_.back().label;
864 __ call(label); // Bind to the patch label, override at link time.
865 __ Bind(label); // Bind the label at the end of the "call" insn.
866 break;
867 }
868 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
869 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100870 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
871 LOG(FATAL) << "Unsupported";
872 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000873 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
874 // (callee_method + offset_of_quick_compiled_code)()
875 __ call(Address(callee_method.AsRegister<CpuRegister>(),
876 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700877 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000878 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000879 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800880
881 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800882}
883
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000884void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
885 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
886 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
887 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000888
889 // Use the calling convention instead of the location of the receiver, as
890 // intrinsics may have put the receiver in a different register. In the intrinsics
891 // slow path, the arguments have been moved to the right place, so here we are
892 // guaranteed that the receiver is the first register of the calling convention.
893 InvokeDexCallingConvention calling_convention;
894 Register receiver = calling_convention.GetRegisterAt(0);
895
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000896 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000898 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000899 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000900 // Instead of simply (possibly) unpoisoning `temp` here, we should
901 // emit a read barrier for the previous class reference load.
902 // However this is not required in practice, as this is an
903 // intermediate/temporary reference and because the current
904 // concurrent copying collector keeps the from-space memory
905 // intact/accessible until the end of the marking phase (the
906 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000907 __ MaybeUnpoisonHeapReference(temp);
908 // temp = temp->GetMethodAt(method_offset);
909 __ movq(temp, Address(temp, method_offset));
910 // call temp->GetEntryPoint();
911 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700912 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000913}
914
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000915void CodeGeneratorX86_64::RecordSimplePatch() {
916 if (GetCompilerOptions().GetIncludePatchInformation()) {
917 simple_patches_.emplace_back();
918 __ Bind(&simple_patches_.back());
919 }
920}
921
Vladimir Markoaad75c62016-10-03 08:46:48 +0000922void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
923 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000924 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
925 __ Bind(&string_patches_.back().label);
926}
927
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100928void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
929 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
930 __ Bind(&type_patches_.back().label);
931}
932
Vladimir Markoaad75c62016-10-03 08:46:48 +0000933Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
934 DCHECK(!GetCompilerOptions().IsBootImage());
935 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
936 return &string_patches_.back().label;
937}
938
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
940 uint32_t element_offset) {
941 // Add a patch entry and return the label.
942 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
943 return &pc_relative_dex_cache_patches_.back().label;
944}
945
Vladimir Markoaad75c62016-10-03 08:46:48 +0000946// The label points to the end of the "movl" or another instruction but the literal offset
947// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
948constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
949
950template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
951inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
952 const ArenaDeque<PatchInfo<Label>>& infos,
953 ArenaVector<LinkerPatch>* linker_patches) {
954 for (const PatchInfo<Label>& info : infos) {
955 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
956 linker_patches->push_back(
957 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
958 }
959}
960
Vladimir Marko58155012015-08-19 12:49:41 +0000961void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
962 DCHECK(linker_patches->empty());
963 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000964 method_patches_.size() +
965 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000966 pc_relative_dex_cache_patches_.size() +
967 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100968 string_patches_.size() +
969 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000970 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000971 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000972 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000973 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +0000974 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000975 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000976 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000977 linker_patches->push_back(
978 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +0000979 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000980 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
981 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000982 for (const Label& label : simple_patches_) {
983 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
984 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
985 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000986 if (!GetCompilerOptions().IsBootImage()) {
987 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
988 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000989 // These are always PC-relative, see GetSupportedLoadStringKind().
Vladimir Markoaad75c62016-10-03 08:46:48 +0000990 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000991 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000992 // These are always PC-relative, see GetSupportedLoadClassKind().
993 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +0000994}
995
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100996void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100997 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998}
999
1000void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001001 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002}
1003
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001004size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1005 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1006 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001007}
1008
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001009size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1010 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1011 return kX86_64WordSize;
1012}
1013
1014size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1015 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1016 return kX86_64WordSize;
1017}
1018
1019size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1020 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1021 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001022}
1023
Calin Juravle175dc732015-08-25 15:42:32 +01001024void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1025 HInstruction* instruction,
1026 uint32_t dex_pc,
1027 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001028 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001029 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1030 if (EntrypointRequiresStackMap(entrypoint)) {
1031 RecordPcInfo(instruction, dex_pc, slow_path);
1032 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001033}
1034
Roland Levillaindec8f632016-07-22 17:10:06 +01001035void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1036 HInstruction* instruction,
1037 SlowPathCode* slow_path) {
1038 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001039 GenerateInvokeRuntime(entry_point_offset);
1040}
1041
1042void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001043 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1044}
1045
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001046static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001047// Use a fake return address register to mimic Quick.
1048static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001049CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001050 const X86_64InstructionSetFeatures& isa_features,
1051 const CompilerOptions& compiler_options,
1052 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001053 : CodeGenerator(graph,
1054 kNumberOfCpuRegisters,
1055 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001056 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001057 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1058 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001059 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001060 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1061 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001062 compiler_options,
1063 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001064 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001066 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001067 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001068 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001069 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001070 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001071 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1072 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001073 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001074 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1075 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001076 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001077 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1079}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001081InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1082 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001083 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084 assembler_(codegen->GetAssembler()),
1085 codegen_(codegen) {}
1086
David Brazdil58282f42016-01-14 12:45:10 +00001087void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001089 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001091 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001092 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093}
1094
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001095static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001096 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001097}
David Srbecky9d8606d2015-04-12 09:35:32 +01001098
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001099static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001100 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001101}
1102
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001104 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001105 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001106 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001107 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001108 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001109
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001110 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001111 __ testq(CpuRegister(RAX), Address(
1112 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001113 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001114 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001115
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001116 if (HasEmptyFrame()) {
1117 return;
1118 }
1119
Nicolas Geoffray98893962015-01-21 12:32:32 +00001120 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001121 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001122 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001123 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001124 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1125 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001126 }
1127 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001128
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001129 int adjust = GetFrameSize() - GetCoreSpillSize();
1130 __ subq(CpuRegister(RSP), Immediate(adjust));
1131 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001132 uint32_t xmm_spill_location = GetFpuSpillStart();
1133 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001134
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001135 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1136 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001137 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1138 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1139 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001140 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001141 }
1142
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001143 // Save the current method if we need it. Note that we do not
1144 // do this in HCurrentMethod, as the instruction might have been removed
1145 // in the SSA graph.
1146 if (RequiresCurrentMethod()) {
1147 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1148 CpuRegister(kMethodRegisterArgument));
1149 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150}
1151
1152void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001153 __ cfi().RememberState();
1154 if (!HasEmptyFrame()) {
1155 uint32_t xmm_spill_location = GetFpuSpillStart();
1156 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1157 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1158 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1159 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1160 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1161 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1162 }
1163 }
1164
1165 int adjust = GetFrameSize() - GetCoreSpillSize();
1166 __ addq(CpuRegister(RSP), Immediate(adjust));
1167 __ cfi().AdjustCFAOffset(-adjust);
1168
1169 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1170 Register reg = kCoreCalleeSaves[i];
1171 if (allocated_registers_.ContainsCoreRegister(reg)) {
1172 __ popq(CpuRegister(reg));
1173 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1174 __ cfi().Restore(DWARFReg(reg));
1175 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001176 }
1177 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001178 __ ret();
1179 __ cfi().RestoreState();
1180 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001181}
1182
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001183void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1184 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185}
1186
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187void CodeGeneratorX86_64::Move(Location destination, Location source) {
1188 if (source.Equals(destination)) {
1189 return;
1190 }
1191 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001192 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001193 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001194 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001196 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001198 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1199 } else if (source.IsConstant()) {
1200 HConstant* constant = source.GetConstant();
1201 if (constant->IsLongConstant()) {
1202 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1203 } else {
1204 Load32BitValue(dest, GetInt32ValueOf(constant));
1205 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 } else {
1207 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001208 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 }
1210 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001211 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001213 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001215 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1216 } else if (source.IsConstant()) {
1217 HConstant* constant = source.GetConstant();
1218 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1219 if (constant->IsFloatConstant()) {
1220 Load32BitValue(dest, static_cast<int32_t>(value));
1221 } else {
1222 Load64BitValue(dest, value);
1223 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001225 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001226 } else {
1227 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001228 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001229 }
1230 } else if (destination.IsStackSlot()) {
1231 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (source.IsFpuRegister()) {
1235 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001237 } else if (source.IsConstant()) {
1238 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001239 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001240 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001242 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001243 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1244 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 }
1246 } else {
1247 DCHECK(destination.IsDoubleStackSlot());
1248 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001249 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001250 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001251 } else if (source.IsFpuRegister()) {
1252 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001253 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001254 } else if (source.IsConstant()) {
1255 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001256 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1257 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001258 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001259 } else {
1260 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001261 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1262 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001263 }
1264 }
1265}
1266
Calin Juravle175dc732015-08-25 15:42:32 +01001267void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1268 DCHECK(location.IsRegister());
1269 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1270}
1271
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272void CodeGeneratorX86_64::MoveLocation(
1273 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1274 Move(dst, src);
1275}
1276
1277void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1278 if (location.IsRegister()) {
1279 locations->AddTemp(location);
1280 } else {
1281 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1282 }
1283}
1284
David Brazdilfc6a86a2015-06-26 10:33:45 +00001285void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001286 DCHECK(!successor->IsExitBlock());
1287
1288 HBasicBlock* block = got->GetBlock();
1289 HInstruction* previous = got->GetPrevious();
1290
1291 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001292 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001293 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1294 return;
1295 }
1296
1297 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1298 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1299 }
1300 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001301 __ jmp(codegen_->GetLabelOf(successor));
1302 }
1303}
1304
David Brazdilfc6a86a2015-06-26 10:33:45 +00001305void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1306 got->SetLocations(nullptr);
1307}
1308
1309void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1310 HandleGoto(got, got->GetSuccessor());
1311}
1312
1313void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1314 try_boundary->SetLocations(nullptr);
1315}
1316
1317void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1318 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1319 if (!successor->IsExitBlock()) {
1320 HandleGoto(try_boundary, successor);
1321 }
1322}
1323
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1325 exit->SetLocations(nullptr);
1326}
1327
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001328void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001329}
1330
Mark Mendell152408f2015-12-31 12:28:50 -05001331template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001332void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001333 LabelType* true_label,
1334 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001335 if (cond->IsFPConditionTrueIfNaN()) {
1336 __ j(kUnordered, true_label);
1337 } else if (cond->IsFPConditionFalseIfNaN()) {
1338 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001339 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001340 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001341}
1342
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001343void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001344 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001345
Mark Mendellc4701932015-04-10 13:18:51 -04001346 Location left = locations->InAt(0);
1347 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001348 Primitive::Type type = condition->InputAt(0)->GetType();
1349 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001350 case Primitive::kPrimBoolean:
1351 case Primitive::kPrimByte:
1352 case Primitive::kPrimChar:
1353 case Primitive::kPrimShort:
1354 case Primitive::kPrimInt:
1355 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001356 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001357 break;
1358 }
Mark Mendellc4701932015-04-10 13:18:51 -04001359 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001360 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001361 break;
1362 }
1363 case Primitive::kPrimFloat: {
1364 if (right.IsFpuRegister()) {
1365 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1366 } else if (right.IsConstant()) {
1367 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1368 codegen_->LiteralFloatAddress(
1369 right.GetConstant()->AsFloatConstant()->GetValue()));
1370 } else {
1371 DCHECK(right.IsStackSlot());
1372 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1373 Address(CpuRegister(RSP), right.GetStackIndex()));
1374 }
Mark Mendellc4701932015-04-10 13:18:51 -04001375 break;
1376 }
1377 case Primitive::kPrimDouble: {
1378 if (right.IsFpuRegister()) {
1379 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1380 } else if (right.IsConstant()) {
1381 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1382 codegen_->LiteralDoubleAddress(
1383 right.GetConstant()->AsDoubleConstant()->GetValue()));
1384 } else {
1385 DCHECK(right.IsDoubleStackSlot());
1386 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1387 Address(CpuRegister(RSP), right.GetStackIndex()));
1388 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001389 break;
1390 }
1391 default:
1392 LOG(FATAL) << "Unexpected condition type " << type;
1393 }
1394}
1395
1396template<class LabelType>
1397void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1398 LabelType* true_target_in,
1399 LabelType* false_target_in) {
1400 // Generated branching requires both targets to be explicit. If either of the
1401 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1402 LabelType fallthrough_target;
1403 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1404 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1405
1406 // Generate the comparison to set the CC.
1407 GenerateCompareTest(condition);
1408
1409 // Now generate the correct jump(s).
1410 Primitive::Type type = condition->InputAt(0)->GetType();
1411 switch (type) {
1412 case Primitive::kPrimLong: {
1413 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1414 break;
1415 }
1416 case Primitive::kPrimFloat: {
1417 GenerateFPJumps(condition, true_target, false_target);
1418 break;
1419 }
1420 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001421 GenerateFPJumps(condition, true_target, false_target);
1422 break;
1423 }
1424 default:
1425 LOG(FATAL) << "Unexpected condition type " << type;
1426 }
1427
David Brazdil0debae72015-11-12 18:37:00 +00001428 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001429 __ jmp(false_target);
1430 }
David Brazdil0debae72015-11-12 18:37:00 +00001431
1432 if (fallthrough_target.IsLinked()) {
1433 __ Bind(&fallthrough_target);
1434 }
Mark Mendellc4701932015-04-10 13:18:51 -04001435}
1436
David Brazdil0debae72015-11-12 18:37:00 +00001437static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1438 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1439 // are set only strictly before `branch`. We can't use the eflags on long
1440 // conditions if they are materialized due to the complex branching.
1441 return cond->IsCondition() &&
1442 cond->GetNext() == branch &&
1443 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1444}
1445
Mark Mendell152408f2015-12-31 12:28:50 -05001446template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001447void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001448 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001449 LabelType* true_target,
1450 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001451 HInstruction* cond = instruction->InputAt(condition_input_index);
1452
1453 if (true_target == nullptr && false_target == nullptr) {
1454 // Nothing to do. The code always falls through.
1455 return;
1456 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001457 // Constant condition, statically compared against "true" (integer value 1).
1458 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001459 if (true_target != nullptr) {
1460 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001461 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001462 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001463 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001464 if (false_target != nullptr) {
1465 __ jmp(false_target);
1466 }
1467 }
1468 return;
1469 }
1470
1471 // The following code generates these patterns:
1472 // (1) true_target == nullptr && false_target != nullptr
1473 // - opposite condition true => branch to false_target
1474 // (2) true_target != nullptr && false_target == nullptr
1475 // - condition true => branch to true_target
1476 // (3) true_target != nullptr && false_target != nullptr
1477 // - condition true => branch to true_target
1478 // - branch to false_target
1479 if (IsBooleanValueOrMaterializedCondition(cond)) {
1480 if (AreEflagsSetFrom(cond, instruction)) {
1481 if (true_target == nullptr) {
1482 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1483 } else {
1484 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1485 }
1486 } else {
1487 // Materialized condition, compare against 0.
1488 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1489 if (lhs.IsRegister()) {
1490 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1491 } else {
1492 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1493 }
1494 if (true_target == nullptr) {
1495 __ j(kEqual, false_target);
1496 } else {
1497 __ j(kNotEqual, true_target);
1498 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001499 }
1500 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001501 // Condition has not been materialized, use its inputs as the
1502 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001503 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001504
David Brazdil0debae72015-11-12 18:37:00 +00001505 // If this is a long or FP comparison that has been folded into
1506 // the HCondition, generate the comparison directly.
1507 Primitive::Type type = condition->InputAt(0)->GetType();
1508 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1509 GenerateCompareTestAndBranch(condition, true_target, false_target);
1510 return;
1511 }
1512
1513 Location lhs = condition->GetLocations()->InAt(0);
1514 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001515 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001516 if (true_target == nullptr) {
1517 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1518 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001519 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001520 }
Dave Allison20dfc792014-06-16 20:44:29 -07001521 }
David Brazdil0debae72015-11-12 18:37:00 +00001522
1523 // If neither branch falls through (case 3), the conditional branch to `true_target`
1524 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1525 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001526 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001527 }
1528}
1529
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001531 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1532 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001533 locations->SetInAt(0, Location::Any());
1534 }
1535}
1536
1537void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001538 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1539 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1540 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1541 nullptr : codegen_->GetLabelOf(true_successor);
1542 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1543 nullptr : codegen_->GetLabelOf(false_successor);
1544 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001545}
1546
1547void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1548 LocationSummary* locations = new (GetGraph()->GetArena())
1549 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001550 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001551 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001552 locations->SetInAt(0, Location::Any());
1553 }
1554}
1555
1556void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001557 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001558 GenerateTestAndBranch<Label>(deoptimize,
1559 /* condition_input_index */ 0,
1560 slow_path->GetEntryLabel(),
1561 /* false_target */ nullptr);
1562}
1563
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001564static bool SelectCanUseCMOV(HSelect* select) {
1565 // There are no conditional move instructions for XMMs.
1566 if (Primitive::IsFloatingPointType(select->GetType())) {
1567 return false;
1568 }
1569
1570 // A FP condition doesn't generate the single CC that we need.
1571 HInstruction* condition = select->GetCondition();
1572 if (condition->IsCondition() &&
1573 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1574 return false;
1575 }
1576
1577 // We can generate a CMOV for this Select.
1578 return true;
1579}
1580
David Brazdil74eb1b22015-12-14 11:44:01 +00001581void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1583 if (Primitive::IsFloatingPointType(select->GetType())) {
1584 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001585 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001586 } else {
1587 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001588 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001589 if (select->InputAt(1)->IsConstant()) {
1590 locations->SetInAt(1, Location::RequiresRegister());
1591 } else {
1592 locations->SetInAt(1, Location::Any());
1593 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001594 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001595 locations->SetInAt(1, Location::Any());
1596 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001597 }
1598 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1599 locations->SetInAt(2, Location::RequiresRegister());
1600 }
1601 locations->SetOut(Location::SameAsFirstInput());
1602}
1603
1604void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1605 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001606 if (SelectCanUseCMOV(select)) {
1607 // If both the condition and the source types are integer, we can generate
1608 // a CMOV to implement Select.
1609 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001610 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001611 DCHECK(locations->InAt(0).Equals(locations->Out()));
1612
1613 HInstruction* select_condition = select->GetCondition();
1614 Condition cond = kNotEqual;
1615
1616 // Figure out how to test the 'condition'.
1617 if (select_condition->IsCondition()) {
1618 HCondition* condition = select_condition->AsCondition();
1619 if (!condition->IsEmittedAtUseSite()) {
1620 // This was a previously materialized condition.
1621 // Can we use the existing condition code?
1622 if (AreEflagsSetFrom(condition, select)) {
1623 // Materialization was the previous instruction. Condition codes are right.
1624 cond = X86_64IntegerCondition(condition->GetCondition());
1625 } else {
1626 // No, we have to recreate the condition code.
1627 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1628 __ testl(cond_reg, cond_reg);
1629 }
1630 } else {
1631 GenerateCompareTest(condition);
1632 cond = X86_64IntegerCondition(condition->GetCondition());
1633 }
1634 } else {
1635 // Must be a boolean condition, which needs to be compared to 0.
1636 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1637 __ testl(cond_reg, cond_reg);
1638 }
1639
1640 // If the condition is true, overwrite the output, which already contains false.
1641 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001642 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1643 if (value_true_loc.IsRegister()) {
1644 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1645 } else {
1646 __ cmov(cond,
1647 value_false,
1648 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1649 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001650 } else {
1651 NearLabel false_target;
1652 GenerateTestAndBranch<NearLabel>(select,
1653 /* condition_input_index */ 2,
1654 /* true_target */ nullptr,
1655 &false_target);
1656 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1657 __ Bind(&false_target);
1658 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001659}
1660
David Srbecky0cf44932015-12-09 14:09:59 +00001661void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1662 new (GetGraph()->GetArena()) LocationSummary(info);
1663}
1664
David Srbeckyd28f4a02016-03-14 17:14:24 +00001665void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1666 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001667}
1668
1669void CodeGeneratorX86_64::GenerateNop() {
1670 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001671}
1672
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001674 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001675 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001676 // Handle the long/FP comparisons made in instruction simplification.
1677 switch (cond->InputAt(0)->GetType()) {
1678 case Primitive::kPrimLong:
1679 locations->SetInAt(0, Location::RequiresRegister());
1680 locations->SetInAt(1, Location::Any());
1681 break;
1682 case Primitive::kPrimFloat:
1683 case Primitive::kPrimDouble:
1684 locations->SetInAt(0, Location::RequiresFpuRegister());
1685 locations->SetInAt(1, Location::Any());
1686 break;
1687 default:
1688 locations->SetInAt(0, Location::RequiresRegister());
1689 locations->SetInAt(1, Location::Any());
1690 break;
1691 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001692 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001693 locations->SetOut(Location::RequiresRegister());
1694 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001695}
1696
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001697void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001698 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001699 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001700 }
Mark Mendellc4701932015-04-10 13:18:51 -04001701
1702 LocationSummary* locations = cond->GetLocations();
1703 Location lhs = locations->InAt(0);
1704 Location rhs = locations->InAt(1);
1705 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001706 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001707
1708 switch (cond->InputAt(0)->GetType()) {
1709 default:
1710 // Integer case.
1711
1712 // Clear output register: setcc only sets the low byte.
1713 __ xorl(reg, reg);
1714
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001715 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001716 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001717 return;
1718 case Primitive::kPrimLong:
1719 // Clear output register: setcc only sets the low byte.
1720 __ xorl(reg, reg);
1721
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001722 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001723 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001724 return;
1725 case Primitive::kPrimFloat: {
1726 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1727 if (rhs.IsConstant()) {
1728 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1729 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1730 } else if (rhs.IsStackSlot()) {
1731 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1732 } else {
1733 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1734 }
1735 GenerateFPJumps(cond, &true_label, &false_label);
1736 break;
1737 }
1738 case Primitive::kPrimDouble: {
1739 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1740 if (rhs.IsConstant()) {
1741 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1742 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1743 } else if (rhs.IsDoubleStackSlot()) {
1744 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1745 } else {
1746 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1747 }
1748 GenerateFPJumps(cond, &true_label, &false_label);
1749 break;
1750 }
1751 }
1752
1753 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001754 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001755
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ Bind(&false_label);
1758 __ xorl(reg, reg);
1759 __ jmp(&done_label);
1760
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001762 __ Bind(&true_label);
1763 __ movl(reg, Immediate(1));
1764 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001813}
1814
Aart Bike9f37602015-10-09 11:15:55 -07001815void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001847void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001850 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001851 case Primitive::kPrimBoolean:
1852 case Primitive::kPrimByte:
1853 case Primitive::kPrimShort:
1854 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001855 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001856 case Primitive::kPrimLong: {
1857 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001858 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1860 break;
1861 }
1862 case Primitive::kPrimFloat:
1863 case Primitive::kPrimDouble: {
1864 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001865 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001866 locations->SetOut(Location::RequiresRegister());
1867 break;
1868 }
1869 default:
1870 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1871 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001872}
1873
1874void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001875 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001877 Location left = locations->InAt(0);
1878 Location right = locations->InAt(1);
1879
Mark Mendell0c9497d2015-08-21 09:30:05 -04001880 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001881 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001882 Condition less_cond = kLess;
1883
Calin Juravleddb7df22014-11-25 20:56:51 +00001884 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001885 case Primitive::kPrimBoolean:
1886 case Primitive::kPrimByte:
1887 case Primitive::kPrimShort:
1888 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001889 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001890 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08001891 break;
1892 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001893 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001894 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001895 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001896 }
1897 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001898 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1899 if (right.IsConstant()) {
1900 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1901 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1902 } else if (right.IsStackSlot()) {
1903 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1904 } else {
1905 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1906 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001907 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001908 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001909 break;
1910 }
1911 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001912 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1913 if (right.IsConstant()) {
1914 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1915 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1916 } else if (right.IsDoubleStackSlot()) {
1917 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1918 } else {
1919 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1920 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001922 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001923 break;
1924 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001925 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001926 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001927 }
Aart Bika19616e2016-02-01 18:57:58 -08001928
Calin Juravleddb7df22014-11-25 20:56:51 +00001929 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001930 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001931 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001932
Calin Juravle91debbc2014-11-26 19:01:09 +00001933 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001934 __ movl(out, Immediate(1));
1935 __ jmp(&done);
1936
1937 __ Bind(&less);
1938 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001939
1940 __ Bind(&done);
1941}
1942
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001943void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001946 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947}
1948
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001950 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951}
1952
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001953void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1956 locations->SetOut(Location::ConstantLocation(constant));
1957}
1958
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001959void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001960 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001961}
1962
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001963void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001964 LocationSummary* locations =
1965 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001966 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967}
1968
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001969void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001970 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971}
1972
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001973void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1976 locations->SetOut(Location::ConstantLocation(constant));
1977}
1978
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001979void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001980 // Will be generated at use site.
1981}
1982
1983void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1984 LocationSummary* locations =
1985 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1986 locations->SetOut(Location::ConstantLocation(constant));
1987}
1988
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001989void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1990 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001991 // Will be generated at use site.
1992}
1993
Calin Juravle27df7582015-04-17 19:12:31 +01001994void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1995 memory_barrier->SetLocations(nullptr);
1996}
1997
1998void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001999 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002000}
2001
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2003 ret->SetLocations(nullptr);
2004}
2005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002007 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008}
2009
2010void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002013 switch (ret->InputAt(0)->GetType()) {
2014 case Primitive::kPrimBoolean:
2015 case Primitive::kPrimByte:
2016 case Primitive::kPrimChar:
2017 case Primitive::kPrimShort:
2018 case Primitive::kPrimInt:
2019 case Primitive::kPrimNot:
2020 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002021 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022 break;
2023
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002024 case Primitive::kPrimFloat:
2025 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002026 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002027 break;
2028
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002029 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002030 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002031 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002032}
2033
2034void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2035 if (kIsDebugBuild) {
2036 switch (ret->InputAt(0)->GetType()) {
2037 case Primitive::kPrimBoolean:
2038 case Primitive::kPrimByte:
2039 case Primitive::kPrimChar:
2040 case Primitive::kPrimShort:
2041 case Primitive::kPrimInt:
2042 case Primitive::kPrimNot:
2043 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002045 break;
2046
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002047 case Primitive::kPrimFloat:
2048 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002050 XMM0);
2051 break;
2052
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002053 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002054 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002055 }
2056 }
2057 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002058}
2059
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002060Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2061 switch (type) {
2062 case Primitive::kPrimBoolean:
2063 case Primitive::kPrimByte:
2064 case Primitive::kPrimChar:
2065 case Primitive::kPrimShort:
2066 case Primitive::kPrimInt:
2067 case Primitive::kPrimNot:
2068 case Primitive::kPrimLong:
2069 return Location::RegisterLocation(RAX);
2070
2071 case Primitive::kPrimVoid:
2072 return Location::NoLocation();
2073
2074 case Primitive::kPrimDouble:
2075 case Primitive::kPrimFloat:
2076 return Location::FpuRegisterLocation(XMM0);
2077 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002078
2079 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002080}
2081
2082Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2083 return Location::RegisterLocation(kMethodRegisterArgument);
2084}
2085
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002086Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002087 switch (type) {
2088 case Primitive::kPrimBoolean:
2089 case Primitive::kPrimByte:
2090 case Primitive::kPrimChar:
2091 case Primitive::kPrimShort:
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimNot: {
2094 uint32_t index = gp_index_++;
2095 stack_index_++;
2096 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002097 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098 } else {
2099 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2100 }
2101 }
2102
2103 case Primitive::kPrimLong: {
2104 uint32_t index = gp_index_;
2105 stack_index_ += 2;
2106 if (index < calling_convention.GetNumberOfRegisters()) {
2107 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002108 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002109 } else {
2110 gp_index_ += 2;
2111 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2112 }
2113 }
2114
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002115 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002116 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002117 stack_index_++;
2118 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002119 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002120 } else {
2121 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2122 }
2123 }
2124
2125 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002126 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 stack_index_ += 2;
2128 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002130 } else {
2131 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2132 }
2133 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002134
2135 case Primitive::kPrimVoid:
2136 LOG(FATAL) << "Unexpected parameter type " << type;
2137 break;
2138 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002139 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002140}
2141
Calin Juravle175dc732015-08-25 15:42:32 +01002142void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2143 // The trampoline uses the same calling convention as dex calling conventions,
2144 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2145 // the method_idx.
2146 HandleInvoke(invoke);
2147}
2148
2149void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2150 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2151}
2152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002153void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002154 // Explicit clinit checks triggered by static invokes must have been pruned by
2155 // art::PrepareForRegisterAllocation.
2156 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002157
Mark Mendellfb8d2792015-03-31 22:16:59 -04002158 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002159 if (intrinsic.TryDispatch(invoke)) {
2160 return;
2161 }
2162
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002163 HandleInvoke(invoke);
2164}
2165
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002166static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2167 if (invoke->GetLocations()->Intrinsified()) {
2168 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2169 intrinsic.Dispatch(invoke);
2170 return true;
2171 }
2172 return false;
2173}
2174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002175void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002176 // Explicit clinit checks triggered by static invokes must have been pruned by
2177 // art::PrepareForRegisterAllocation.
2178 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002179
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002180 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2181 return;
2182 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002184 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002185 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002186 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002188}
2189
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002190void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002191 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002192 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002193}
2194
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002195void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002196 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002197 if (intrinsic.TryDispatch(invoke)) {
2198 return;
2199 }
2200
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002201 HandleInvoke(invoke);
2202}
2203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2206 return;
2207 }
2208
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002209 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002210 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212}
2213
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2215 HandleInvoke(invoke);
2216 // Add the hidden argument.
2217 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2218}
2219
2220void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2221 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002222 LocationSummary* locations = invoke->GetLocations();
2223 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2224 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002225 Location receiver = locations->InAt(0);
2226 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2227
Roland Levillain0d5a2812015-11-13 10:07:31 +00002228 // Set the hidden argument. This is safe to do this here, as RAX
2229 // won't be modified thereafter, before the `call` instruction.
2230 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002231 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233 if (receiver.IsStackSlot()) {
2234 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002235 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 __ movl(temp, Address(temp, class_offset));
2237 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002238 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002241 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002242 // Instead of simply (possibly) unpoisoning `temp` here, we should
2243 // emit a read barrier for the previous class reference load.
2244 // However this is not required in practice, as this is an
2245 // intermediate/temporary reference and because the current
2246 // concurrent copying collector keeps the from-space memory
2247 // intact/accessible until the end of the marking phase (the
2248 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002249 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002250 // temp = temp->GetAddressOfIMT()
2251 __ movq(temp,
2252 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2253 // temp = temp->GetImtEntryAt(method_offset);
2254 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002255 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002257 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002259 __ call(Address(
2260 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261
2262 DCHECK(!codegen_->IsLeafMethod());
2263 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2264}
2265
Roland Levillain88cb1752014-10-20 16:36:47 +01002266void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2269 switch (neg->GetResultType()) {
2270 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002271 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002272 locations->SetInAt(0, Location::RequiresRegister());
2273 locations->SetOut(Location::SameAsFirstInput());
2274 break;
2275
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 case Primitive::kPrimFloat:
2277 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002278 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002279 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002280 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002281 break;
2282
2283 default:
2284 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2285 }
2286}
2287
2288void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2289 LocationSummary* locations = neg->GetLocations();
2290 Location out = locations->Out();
2291 Location in = locations->InAt(0);
2292 switch (neg->GetResultType()) {
2293 case Primitive::kPrimInt:
2294 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002295 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002297 break;
2298
2299 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002300 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002303 break;
2304
Roland Levillain5368c212014-11-27 15:03:41 +00002305 case Primitive::kPrimFloat: {
2306 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002307 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002308 // Implement float negation with an exclusive or with value
2309 // 0x80000000 (mask for bit 31, representing the sign of a
2310 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002311 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002314 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002315
Roland Levillain5368c212014-11-27 15:03:41 +00002316 case Primitive::kPrimDouble: {
2317 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002318 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002319 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002320 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002321 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002322 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002324 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002325 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002326
2327 default:
2328 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2329 }
2330}
2331
Roland Levillaindff1f282014-11-05 14:15:05 +00002332void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2333 LocationSummary* locations =
2334 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2335 Primitive::Type result_type = conversion->GetResultType();
2336 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002337 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002338
David Brazdilb2bd1c52015-03-25 11:17:37 +00002339 // The Java language does not allow treating boolean as an integral type but
2340 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002341
Roland Levillaindff1f282014-11-05 14:15:05 +00002342 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002343 case Primitive::kPrimByte:
2344 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002345 case Primitive::kPrimLong:
2346 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002347 case Primitive::kPrimBoolean:
2348 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002349 case Primitive::kPrimShort:
2350 case Primitive::kPrimInt:
2351 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002352 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002353 locations->SetInAt(0, Location::Any());
2354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361 break;
2362
Roland Levillain01a8d712014-11-14 16:27:39 +00002363 case Primitive::kPrimShort:
2364 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002365 case Primitive::kPrimLong:
2366 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002367 case Primitive::kPrimBoolean:
2368 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002369 case Primitive::kPrimByte:
2370 case Primitive::kPrimInt:
2371 case Primitive::kPrimChar:
2372 // Processing a Dex `int-to-short' instruction.
2373 locations->SetInAt(0, Location::Any());
2374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2375 break;
2376
2377 default:
2378 LOG(FATAL) << "Unexpected type conversion from " << input_type
2379 << " to " << result_type;
2380 }
2381 break;
2382
Roland Levillain946e1432014-11-11 17:35:19 +00002383 case Primitive::kPrimInt:
2384 switch (input_type) {
2385 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002386 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002387 locations->SetInAt(0, Location::Any());
2388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2389 break;
2390
2391 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002392 // Processing a Dex `float-to-int' instruction.
2393 locations->SetInAt(0, Location::RequiresFpuRegister());
2394 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002395 break;
2396
Roland Levillain946e1432014-11-11 17:35:19 +00002397 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002398 // Processing a Dex `double-to-int' instruction.
2399 locations->SetInAt(0, Location::RequiresFpuRegister());
2400 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002401 break;
2402
2403 default:
2404 LOG(FATAL) << "Unexpected type conversion from " << input_type
2405 << " to " << result_type;
2406 }
2407 break;
2408
Roland Levillaindff1f282014-11-05 14:15:05 +00002409 case Primitive::kPrimLong:
2410 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002411 case Primitive::kPrimBoolean:
2412 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002413 case Primitive::kPrimByte:
2414 case Primitive::kPrimShort:
2415 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002416 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002417 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002418 // TODO: We would benefit from a (to-be-implemented)
2419 // Location::RegisterOrStackSlot requirement for this input.
2420 locations->SetInAt(0, Location::RequiresRegister());
2421 locations->SetOut(Location::RequiresRegister());
2422 break;
2423
2424 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002425 // Processing a Dex `float-to-long' instruction.
2426 locations->SetInAt(0, Location::RequiresFpuRegister());
2427 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002428 break;
2429
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002431 // Processing a Dex `double-to-long' instruction.
2432 locations->SetInAt(0, Location::RequiresFpuRegister());
2433 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 break;
2435
2436 default:
2437 LOG(FATAL) << "Unexpected type conversion from " << input_type
2438 << " to " << result_type;
2439 }
2440 break;
2441
Roland Levillain981e4542014-11-14 11:47:14 +00002442 case Primitive::kPrimChar:
2443 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002444 case Primitive::kPrimLong:
2445 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002451 // Processing a Dex `int-to-char' instruction.
2452 locations->SetInAt(0, Location::Any());
2453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2454 break;
2455
2456 default:
2457 LOG(FATAL) << "Unexpected type conversion from " << input_type
2458 << " to " << result_type;
2459 }
2460 break;
2461
Roland Levillaindff1f282014-11-05 14:15:05 +00002462 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002463 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002464 case Primitive::kPrimBoolean:
2465 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002466 case Primitive::kPrimByte:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimInt:
2469 case Primitive::kPrimChar:
2470 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002471 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002472 locations->SetOut(Location::RequiresFpuRegister());
2473 break;
2474
2475 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002476 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002477 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002478 locations->SetOut(Location::RequiresFpuRegister());
2479 break;
2480
Roland Levillaincff13742014-11-17 14:32:17 +00002481 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002482 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002483 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected type conversion from " << input_type
2489 << " to " << result_type;
2490 };
2491 break;
2492
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002494 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002495 case Primitive::kPrimBoolean:
2496 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002497 case Primitive::kPrimByte:
2498 case Primitive::kPrimShort:
2499 case Primitive::kPrimInt:
2500 case Primitive::kPrimChar:
2501 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002502 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002503 locations->SetOut(Location::RequiresFpuRegister());
2504 break;
2505
2506 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002507 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002508 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002509 locations->SetOut(Location::RequiresFpuRegister());
2510 break;
2511
Roland Levillaincff13742014-11-17 14:32:17 +00002512 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002513 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002514 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002515 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002516 break;
2517
2518 default:
2519 LOG(FATAL) << "Unexpected type conversion from " << input_type
2520 << " to " << result_type;
2521 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected type conversion from " << input_type
2526 << " to " << result_type;
2527 }
2528}
2529
2530void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2531 LocationSummary* locations = conversion->GetLocations();
2532 Location out = locations->Out();
2533 Location in = locations->InAt(0);
2534 Primitive::Type result_type = conversion->GetResultType();
2535 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002536 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002538 case Primitive::kPrimByte:
2539 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002540 case Primitive::kPrimLong:
2541 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002542 case Primitive::kPrimBoolean:
2543 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002544 case Primitive::kPrimShort:
2545 case Primitive::kPrimInt:
2546 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002547 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002548 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002550 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002552 Address(CpuRegister(RSP), in.GetStackIndex()));
2553 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002555 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002556 }
2557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563 break;
2564
Roland Levillain01a8d712014-11-14 16:27:39 +00002565 case Primitive::kPrimShort:
2566 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002567 case Primitive::kPrimLong:
2568 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002569 case Primitive::kPrimBoolean:
2570 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002571 case Primitive::kPrimByte:
2572 case Primitive::kPrimInt:
2573 case Primitive::kPrimChar:
2574 // Processing a Dex `int-to-short' instruction.
2575 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002577 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002578 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002579 Address(CpuRegister(RSP), in.GetStackIndex()));
2580 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002581 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002582 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002583 }
2584 break;
2585
2586 default:
2587 LOG(FATAL) << "Unexpected type conversion from " << input_type
2588 << " to " << result_type;
2589 }
2590 break;
2591
Roland Levillain946e1432014-11-11 17:35:19 +00002592 case Primitive::kPrimInt:
2593 switch (input_type) {
2594 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002595 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002596 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002598 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002600 Address(CpuRegister(RSP), in.GetStackIndex()));
2601 } else {
2602 DCHECK(in.IsConstant());
2603 DCHECK(in.GetConstant()->IsLongConstant());
2604 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002606 }
2607 break;
2608
Roland Levillain3f8f9362014-12-02 17:45:01 +00002609 case Primitive::kPrimFloat: {
2610 // Processing a Dex `float-to-int' instruction.
2611 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2612 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002613 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002614
2615 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002616 // if input >= (float)INT_MAX goto done
2617 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002618 __ j(kAboveEqual, &done);
2619 // if input == NaN goto nan
2620 __ j(kUnordered, &nan);
2621 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002622 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002623 __ jmp(&done);
2624 __ Bind(&nan);
2625 // output = 0
2626 __ xorl(output, output);
2627 __ Bind(&done);
2628 break;
2629 }
2630
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631 case Primitive::kPrimDouble: {
2632 // Processing a Dex `double-to-int' instruction.
2633 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2634 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002635 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002636
2637 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002638 // if input >= (double)INT_MAX goto done
2639 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002640 __ j(kAboveEqual, &done);
2641 // if input == NaN goto nan
2642 __ j(kUnordered, &nan);
2643 // output = double-to-int-truncate(input)
2644 __ cvttsd2si(output, input);
2645 __ jmp(&done);
2646 __ Bind(&nan);
2647 // output = 0
2648 __ xorl(output, output);
2649 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002650 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002651 }
Roland Levillain946e1432014-11-11 17:35:19 +00002652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 }
2657 break;
2658
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimLong:
2660 switch (input_type) {
2661 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002662 case Primitive::kPrimBoolean:
2663 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002664 case Primitive::kPrimByte:
2665 case Primitive::kPrimShort:
2666 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002667 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002668 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002669 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002671 break;
2672
Roland Levillain624279f2014-12-04 11:54:28 +00002673 case Primitive::kPrimFloat: {
2674 // Processing a Dex `float-to-long' instruction.
2675 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2676 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002677 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002678
Mark Mendell92e83bf2015-05-07 11:25:03 -04002679 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002680 // if input >= (float)LONG_MAX goto done
2681 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002682 __ j(kAboveEqual, &done);
2683 // if input == NaN goto nan
2684 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002685 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002686 __ cvttss2si(output, input, true);
2687 __ jmp(&done);
2688 __ Bind(&nan);
2689 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002690 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002691 __ Bind(&done);
2692 break;
2693 }
2694
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002695 case Primitive::kPrimDouble: {
2696 // Processing a Dex `double-to-long' instruction.
2697 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2698 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002699 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002700
Mark Mendell92e83bf2015-05-07 11:25:03 -04002701 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002702 // if input >= (double)LONG_MAX goto done
2703 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002704 __ j(kAboveEqual, &done);
2705 // if input == NaN goto nan
2706 __ j(kUnordered, &nan);
2707 // output = double-to-long-truncate(input)
2708 __ cvttsd2si(output, input, true);
2709 __ jmp(&done);
2710 __ Bind(&nan);
2711 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002712 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002713 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002714 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002715 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 }
2721 break;
2722
Roland Levillain981e4542014-11-14 11:47:14 +00002723 case Primitive::kPrimChar:
2724 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002725 case Primitive::kPrimLong:
2726 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002727 case Primitive::kPrimBoolean:
2728 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002729 case Primitive::kPrimByte:
2730 case Primitive::kPrimShort:
2731 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002732 // Processing a Dex `int-to-char' instruction.
2733 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002735 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002737 Address(CpuRegister(RSP), in.GetStackIndex()));
2738 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002739 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002740 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002741 }
2742 break;
2743
2744 default:
2745 LOG(FATAL) << "Unexpected type conversion from " << input_type
2746 << " to " << result_type;
2747 }
2748 break;
2749
Roland Levillaindff1f282014-11-05 14:15:05 +00002750 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002751 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002752 case Primitive::kPrimBoolean:
2753 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002754 case Primitive::kPrimByte:
2755 case Primitive::kPrimShort:
2756 case Primitive::kPrimInt:
2757 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002758 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002759 if (in.IsRegister()) {
2760 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2761 } else if (in.IsConstant()) {
2762 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2763 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002764 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002765 } else {
2766 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2767 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2768 }
Roland Levillaincff13742014-11-17 14:32:17 +00002769 break;
2770
2771 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002772 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002773 if (in.IsRegister()) {
2774 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2775 } else if (in.IsConstant()) {
2776 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2777 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002778 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002779 } else {
2780 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2781 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2782 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002783 break;
2784
Roland Levillaincff13742014-11-17 14:32:17 +00002785 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002786 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002787 if (in.IsFpuRegister()) {
2788 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2789 } else if (in.IsConstant()) {
2790 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2791 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002792 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002793 } else {
2794 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2795 Address(CpuRegister(RSP), in.GetStackIndex()));
2796 }
Roland Levillaincff13742014-11-17 14:32:17 +00002797 break;
2798
2799 default:
2800 LOG(FATAL) << "Unexpected type conversion from " << input_type
2801 << " to " << result_type;
2802 };
2803 break;
2804
Roland Levillaindff1f282014-11-05 14:15:05 +00002805 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002806 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002807 case Primitive::kPrimBoolean:
2808 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002809 case Primitive::kPrimByte:
2810 case Primitive::kPrimShort:
2811 case Primitive::kPrimInt:
2812 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002813 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002814 if (in.IsRegister()) {
2815 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2816 } else if (in.IsConstant()) {
2817 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2818 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002819 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002820 } else {
2821 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2822 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2823 }
Roland Levillaincff13742014-11-17 14:32:17 +00002824 break;
2825
2826 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002827 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002828 if (in.IsRegister()) {
2829 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2830 } else if (in.IsConstant()) {
2831 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2832 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002833 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002834 } else {
2835 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2836 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2837 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002838 break;
2839
Roland Levillaincff13742014-11-17 14:32:17 +00002840 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002841 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002842 if (in.IsFpuRegister()) {
2843 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2844 } else if (in.IsConstant()) {
2845 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2846 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002847 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002848 } else {
2849 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2850 Address(CpuRegister(RSP), in.GetStackIndex()));
2851 }
Roland Levillaincff13742014-11-17 14:32:17 +00002852 break;
2853
2854 default:
2855 LOG(FATAL) << "Unexpected type conversion from " << input_type
2856 << " to " << result_type;
2857 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002858 break;
2859
2860 default:
2861 LOG(FATAL) << "Unexpected type conversion from " << input_type
2862 << " to " << result_type;
2863 }
2864}
2865
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002866void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002867 LocationSummary* locations =
2868 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002869 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002870 case Primitive::kPrimInt: {
2871 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002872 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002874 break;
2875 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002876
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002877 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002878 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002879 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002880 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002882 break;
2883 }
2884
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002885 case Primitive::kPrimDouble:
2886 case Primitive::kPrimFloat: {
2887 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002888 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002889 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002890 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002891 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002892
2893 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002894 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002895 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896}
2897
2898void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2899 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002900 Location first = locations->InAt(0);
2901 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002902 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002903
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002905 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002906 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002907 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2908 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002909 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2910 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002911 } else {
2912 __ leal(out.AsRegister<CpuRegister>(), Address(
2913 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2914 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002915 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002916 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2917 __ addl(out.AsRegister<CpuRegister>(),
2918 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2919 } else {
2920 __ leal(out.AsRegister<CpuRegister>(), Address(
2921 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2922 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002923 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002924 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002925 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002926 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002927 break;
2928 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002930 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002931 if (second.IsRegister()) {
2932 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2933 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002934 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2935 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002936 } else {
2937 __ leaq(out.AsRegister<CpuRegister>(), Address(
2938 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2939 }
2940 } else {
2941 DCHECK(second.IsConstant());
2942 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2943 int32_t int32_value = Low32Bits(value);
2944 DCHECK_EQ(int32_value, value);
2945 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2946 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2947 } else {
2948 __ leaq(out.AsRegister<CpuRegister>(), Address(
2949 first.AsRegister<CpuRegister>(), int32_value));
2950 }
2951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002952 break;
2953 }
2954
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002956 if (second.IsFpuRegister()) {
2957 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2958 } else if (second.IsConstant()) {
2959 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002960 codegen_->LiteralFloatAddress(
2961 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002962 } else {
2963 DCHECK(second.IsStackSlot());
2964 __ addss(first.AsFpuRegister<XmmRegister>(),
2965 Address(CpuRegister(RSP), second.GetStackIndex()));
2966 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002967 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 }
2969
2970 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002971 if (second.IsFpuRegister()) {
2972 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2973 } else if (second.IsConstant()) {
2974 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002975 codegen_->LiteralDoubleAddress(
2976 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002977 } else {
2978 DCHECK(second.IsDoubleStackSlot());
2979 __ addsd(first.AsFpuRegister<XmmRegister>(),
2980 Address(CpuRegister(RSP), second.GetStackIndex()));
2981 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 break;
2983 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002984
2985 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 }
2988}
2989
2990void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002991 LocationSummary* locations =
2992 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002994 case Primitive::kPrimInt: {
2995 locations->SetInAt(0, Location::RequiresRegister());
2996 locations->SetInAt(1, Location::Any());
2997 locations->SetOut(Location::SameAsFirstInput());
2998 break;
2999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003000 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003001 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003002 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003003 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003004 break;
3005 }
Calin Juravle11351682014-10-23 15:38:15 +01003006 case Primitive::kPrimFloat:
3007 case Primitive::kPrimDouble: {
3008 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003009 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003010 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011 break;
Calin Juravle11351682014-10-23 15:38:15 +01003012 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003013 default:
Calin Juravle11351682014-10-23 15:38:15 +01003014 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003015 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003016}
3017
3018void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3019 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003020 Location first = locations->InAt(0);
3021 Location second = locations->InAt(1);
3022 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003024 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003025 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003027 } else if (second.IsConstant()) {
3028 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003030 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003032 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003033 break;
3034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003036 if (second.IsConstant()) {
3037 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3038 DCHECK(IsInt<32>(value));
3039 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3040 } else {
3041 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3042 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003043 break;
3044 }
3045
Calin Juravle11351682014-10-23 15:38:15 +01003046 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003047 if (second.IsFpuRegister()) {
3048 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3049 } else if (second.IsConstant()) {
3050 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003051 codegen_->LiteralFloatAddress(
3052 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003053 } else {
3054 DCHECK(second.IsStackSlot());
3055 __ subss(first.AsFpuRegister<XmmRegister>(),
3056 Address(CpuRegister(RSP), second.GetStackIndex()));
3057 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 break;
Calin Juravle11351682014-10-23 15:38:15 +01003059 }
3060
3061 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003062 if (second.IsFpuRegister()) {
3063 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3064 } else if (second.IsConstant()) {
3065 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003066 codegen_->LiteralDoubleAddress(
3067 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003068 } else {
3069 DCHECK(second.IsDoubleStackSlot());
3070 __ subsd(first.AsFpuRegister<XmmRegister>(),
3071 Address(CpuRegister(RSP), second.GetStackIndex()));
3072 }
Calin Juravle11351682014-10-23 15:38:15 +01003073 break;
3074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003075
3076 default:
Calin Juravle11351682014-10-23 15:38:15 +01003077 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 }
3079}
3080
Calin Juravle34bacdf2014-10-07 20:23:36 +01003081void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3082 LocationSummary* locations =
3083 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3084 switch (mul->GetResultType()) {
3085 case Primitive::kPrimInt: {
3086 locations->SetInAt(0, Location::RequiresRegister());
3087 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003088 if (mul->InputAt(1)->IsIntConstant()) {
3089 // Can use 3 operand multiply.
3090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3091 } else {
3092 locations->SetOut(Location::SameAsFirstInput());
3093 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003094 break;
3095 }
3096 case Primitive::kPrimLong: {
3097 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003098 locations->SetInAt(1, Location::Any());
3099 if (mul->InputAt(1)->IsLongConstant() &&
3100 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003101 // Can use 3 operand multiply.
3102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3103 } else {
3104 locations->SetOut(Location::SameAsFirstInput());
3105 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003106 break;
3107 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003108 case Primitive::kPrimFloat:
3109 case Primitive::kPrimDouble: {
3110 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003111 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003113 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003114 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003115
3116 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003117 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003118 }
3119}
3120
3121void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3122 LocationSummary* locations = mul->GetLocations();
3123 Location first = locations->InAt(0);
3124 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003125 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003127 case Primitive::kPrimInt:
3128 // The constant may have ended up in a register, so test explicitly to avoid
3129 // problems where the output may not be the same as the first operand.
3130 if (mul->InputAt(1)->IsIntConstant()) {
3131 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3132 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3133 } else if (second.IsRegister()) {
3134 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003136 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003137 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003139 __ imull(first.AsRegister<CpuRegister>(),
3140 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141 }
3142 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003143 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003144 // The constant may have ended up in a register, so test explicitly to avoid
3145 // problems where the output may not be the same as the first operand.
3146 if (mul->InputAt(1)->IsLongConstant()) {
3147 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3148 if (IsInt<32>(value)) {
3149 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3150 Immediate(static_cast<int32_t>(value)));
3151 } else {
3152 // Have to use the constant area.
3153 DCHECK(first.Equals(out));
3154 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3155 }
3156 } else if (second.IsRegister()) {
3157 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003158 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003159 } else {
3160 DCHECK(second.IsDoubleStackSlot());
3161 DCHECK(first.Equals(out));
3162 __ imulq(first.AsRegister<CpuRegister>(),
3163 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003164 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 break;
3166 }
3167
Calin Juravleb5bfa962014-10-21 18:02:24 +01003168 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003169 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003170 if (second.IsFpuRegister()) {
3171 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3172 } else if (second.IsConstant()) {
3173 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003174 codegen_->LiteralFloatAddress(
3175 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003176 } else {
3177 DCHECK(second.IsStackSlot());
3178 __ mulss(first.AsFpuRegister<XmmRegister>(),
3179 Address(CpuRegister(RSP), second.GetStackIndex()));
3180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003182 }
3183
3184 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003185 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003186 if (second.IsFpuRegister()) {
3187 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3188 } else if (second.IsConstant()) {
3189 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003190 codegen_->LiteralDoubleAddress(
3191 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003192 } else {
3193 DCHECK(second.IsDoubleStackSlot());
3194 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3195 Address(CpuRegister(RSP), second.GetStackIndex()));
3196 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003197 break;
3198 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199
3200 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 }
3203}
3204
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003205void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3206 uint32_t stack_adjustment, bool is_float) {
3207 if (source.IsStackSlot()) {
3208 DCHECK(is_float);
3209 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3210 } else if (source.IsDoubleStackSlot()) {
3211 DCHECK(!is_float);
3212 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3213 } else {
3214 // Write the value to the temporary location on the stack and load to FP stack.
3215 if (is_float) {
3216 Location stack_temp = Location::StackSlot(temp_offset);
3217 codegen_->Move(stack_temp, source);
3218 __ flds(Address(CpuRegister(RSP), temp_offset));
3219 } else {
3220 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3221 codegen_->Move(stack_temp, source);
3222 __ fldl(Address(CpuRegister(RSP), temp_offset));
3223 }
3224 }
3225}
3226
3227void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3228 Primitive::Type type = rem->GetResultType();
3229 bool is_float = type == Primitive::kPrimFloat;
3230 size_t elem_size = Primitive::ComponentSize(type);
3231 LocationSummary* locations = rem->GetLocations();
3232 Location first = locations->InAt(0);
3233 Location second = locations->InAt(1);
3234 Location out = locations->Out();
3235
3236 // Create stack space for 2 elements.
3237 // TODO: enhance register allocator to ask for stack temporaries.
3238 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3239
3240 // Load the values to the FP stack in reverse order, using temporaries if needed.
3241 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3242 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3243
3244 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003245 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003246 __ Bind(&retry);
3247 __ fprem();
3248
3249 // Move FP status to AX.
3250 __ fstsw();
3251
3252 // And see if the argument reduction is complete. This is signaled by the
3253 // C2 FPU flag bit set to 0.
3254 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3255 __ j(kNotEqual, &retry);
3256
3257 // We have settled on the final value. Retrieve it into an XMM register.
3258 // Store FP top of stack to real stack.
3259 if (is_float) {
3260 __ fsts(Address(CpuRegister(RSP), 0));
3261 } else {
3262 __ fstl(Address(CpuRegister(RSP), 0));
3263 }
3264
3265 // Pop the 2 items from the FP stack.
3266 __ fucompp();
3267
3268 // Load the value from the stack into an XMM register.
3269 DCHECK(out.IsFpuRegister()) << out;
3270 if (is_float) {
3271 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3272 } else {
3273 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3274 }
3275
3276 // And remove the temporary stack space we allocated.
3277 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3278}
3279
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3281 DCHECK(instruction->IsDiv() || instruction->IsRem());
3282
3283 LocationSummary* locations = instruction->GetLocations();
3284 Location second = locations->InAt(1);
3285 DCHECK(second.IsConstant());
3286
3287 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3288 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003289 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003290
3291 DCHECK(imm == 1 || imm == -1);
3292
3293 switch (instruction->GetResultType()) {
3294 case Primitive::kPrimInt: {
3295 if (instruction->IsRem()) {
3296 __ xorl(output_register, output_register);
3297 } else {
3298 __ movl(output_register, input_register);
3299 if (imm == -1) {
3300 __ negl(output_register);
3301 }
3302 }
3303 break;
3304 }
3305
3306 case Primitive::kPrimLong: {
3307 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003308 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003309 } else {
3310 __ movq(output_register, input_register);
3311 if (imm == -1) {
3312 __ negq(output_register);
3313 }
3314 }
3315 break;
3316 }
3317
3318 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003319 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003320 }
3321}
3322
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003323void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003324 LocationSummary* locations = instruction->GetLocations();
3325 Location second = locations->InAt(1);
3326
3327 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3328 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3329
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003330 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003331 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3332 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333
3334 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3335
3336 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003337 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003338 __ testl(numerator, numerator);
3339 __ cmov(kGreaterEqual, tmp, numerator);
3340 int shift = CTZ(imm);
3341 __ sarl(tmp, Immediate(shift));
3342
3343 if (imm < 0) {
3344 __ negl(tmp);
3345 }
3346
3347 __ movl(output_register, tmp);
3348 } else {
3349 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3350 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3351
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003352 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003353 __ addq(rdx, numerator);
3354 __ testq(numerator, numerator);
3355 __ cmov(kGreaterEqual, rdx, numerator);
3356 int shift = CTZ(imm);
3357 __ sarq(rdx, Immediate(shift));
3358
3359 if (imm < 0) {
3360 __ negq(rdx);
3361 }
3362
3363 __ movq(output_register, rdx);
3364 }
3365}
3366
3367void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3368 DCHECK(instruction->IsDiv() || instruction->IsRem());
3369
3370 LocationSummary* locations = instruction->GetLocations();
3371 Location second = locations->InAt(1);
3372
3373 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3374 : locations->GetTemp(0).AsRegister<CpuRegister>();
3375 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3376 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3377 : locations->Out().AsRegister<CpuRegister>();
3378 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3379
3380 DCHECK_EQ(RAX, eax.AsRegister());
3381 DCHECK_EQ(RDX, edx.AsRegister());
3382 if (instruction->IsDiv()) {
3383 DCHECK_EQ(RAX, out.AsRegister());
3384 } else {
3385 DCHECK_EQ(RDX, out.AsRegister());
3386 }
3387
3388 int64_t magic;
3389 int shift;
3390
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003391 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003392 if (instruction->GetResultType() == Primitive::kPrimInt) {
3393 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3394
3395 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3396
3397 __ movl(numerator, eax);
3398
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 __ movl(eax, Immediate(magic));
3400 __ imull(numerator);
3401
3402 if (imm > 0 && magic < 0) {
3403 __ addl(edx, numerator);
3404 } else if (imm < 0 && magic > 0) {
3405 __ subl(edx, numerator);
3406 }
3407
3408 if (shift != 0) {
3409 __ sarl(edx, Immediate(shift));
3410 }
3411
3412 __ movl(eax, edx);
3413 __ shrl(edx, Immediate(31));
3414 __ addl(edx, eax);
3415
3416 if (instruction->IsRem()) {
3417 __ movl(eax, numerator);
3418 __ imull(edx, Immediate(imm));
3419 __ subl(eax, edx);
3420 __ movl(edx, eax);
3421 } else {
3422 __ movl(eax, edx);
3423 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424 } else {
3425 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3426
3427 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3428
3429 CpuRegister rax = eax;
3430 CpuRegister rdx = edx;
3431
3432 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3433
3434 // Save the numerator.
3435 __ movq(numerator, rax);
3436
3437 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003438 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439
3440 // RDX:RAX = magic * numerator
3441 __ imulq(numerator);
3442
3443 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003444 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 __ addq(rdx, numerator);
3446 } else if (imm < 0 && magic > 0) {
3447 // RDX -= numerator
3448 __ subq(rdx, numerator);
3449 }
3450
3451 // Shift if needed.
3452 if (shift != 0) {
3453 __ sarq(rdx, Immediate(shift));
3454 }
3455
3456 // RDX += 1 if RDX < 0
3457 __ movq(rax, rdx);
3458 __ shrq(rdx, Immediate(63));
3459 __ addq(rdx, rax);
3460
3461 if (instruction->IsRem()) {
3462 __ movq(rax, numerator);
3463
3464 if (IsInt<32>(imm)) {
3465 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3466 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003467 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003468 }
3469
3470 __ subq(rax, rdx);
3471 __ movq(rdx, rax);
3472 } else {
3473 __ movq(rax, rdx);
3474 }
3475 }
3476}
3477
Calin Juravlebacfec32014-11-14 15:54:36 +00003478void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3479 DCHECK(instruction->IsDiv() || instruction->IsRem());
3480 Primitive::Type type = instruction->GetResultType();
3481 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3482
3483 bool is_div = instruction->IsDiv();
3484 LocationSummary* locations = instruction->GetLocations();
3485
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3487 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003488
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003491
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003492 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003493 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003494
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495 if (imm == 0) {
3496 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3497 } else if (imm == 1 || imm == -1) {
3498 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003499 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003500 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 } else {
3502 DCHECK(imm <= -2 || imm >= 2);
3503 GenerateDivRemWithAnyConstant(instruction);
3504 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003505 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003506 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003508 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003510
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3512 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3513 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3514 // so it's safe to just use negl instead of more complex comparisons.
3515 if (type == Primitive::kPrimInt) {
3516 __ cmpl(second_reg, Immediate(-1));
3517 __ j(kEqual, slow_path->GetEntryLabel());
3518 // edx:eax <- sign-extended of eax
3519 __ cdq();
3520 // eax = quotient, edx = remainder
3521 __ idivl(second_reg);
3522 } else {
3523 __ cmpq(second_reg, Immediate(-1));
3524 __ j(kEqual, slow_path->GetEntryLabel());
3525 // rdx:rax <- sign-extended of rax
3526 __ cqo();
3527 // rax = quotient, rdx = remainder
3528 __ idivq(second_reg);
3529 }
3530 __ Bind(slow_path->GetExitLabel());
3531 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003532}
3533
Calin Juravle7c4954d2014-10-28 16:57:40 +00003534void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3535 LocationSummary* locations =
3536 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3537 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003538 case Primitive::kPrimInt:
3539 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003540 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003542 locations->SetOut(Location::SameAsFirstInput());
3543 // Intel uses edx:eax as the dividend.
3544 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003545 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3546 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3547 // output and request another temp.
3548 if (div->InputAt(1)->IsConstant()) {
3549 locations->AddTemp(Location::RequiresRegister());
3550 }
Calin Juravled0d48522014-11-04 16:40:20 +00003551 break;
3552 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003553
Calin Juravle7c4954d2014-10-28 16:57:40 +00003554 case Primitive::kPrimFloat:
3555 case Primitive::kPrimDouble: {
3556 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003557 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003558 locations->SetOut(Location::SameAsFirstInput());
3559 break;
3560 }
3561
3562 default:
3563 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3564 }
3565}
3566
3567void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3568 LocationSummary* locations = div->GetLocations();
3569 Location first = locations->InAt(0);
3570 Location second = locations->InAt(1);
3571 DCHECK(first.Equals(locations->Out()));
3572
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 Primitive::Type type = div->GetResultType();
3574 switch (type) {
3575 case Primitive::kPrimInt:
3576 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003577 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003578 break;
3579 }
3580
Calin Juravle7c4954d2014-10-28 16:57:40 +00003581 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003582 if (second.IsFpuRegister()) {
3583 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3584 } else if (second.IsConstant()) {
3585 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003586 codegen_->LiteralFloatAddress(
3587 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003588 } else {
3589 DCHECK(second.IsStackSlot());
3590 __ divss(first.AsFpuRegister<XmmRegister>(),
3591 Address(CpuRegister(RSP), second.GetStackIndex()));
3592 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003593 break;
3594 }
3595
3596 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003597 if (second.IsFpuRegister()) {
3598 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3599 } else if (second.IsConstant()) {
3600 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003601 codegen_->LiteralDoubleAddress(
3602 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003603 } else {
3604 DCHECK(second.IsDoubleStackSlot());
3605 __ divsd(first.AsFpuRegister<XmmRegister>(),
3606 Address(CpuRegister(RSP), second.GetStackIndex()));
3607 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003608 break;
3609 }
3610
3611 default:
3612 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3613 }
3614}
3615
Calin Juravlebacfec32014-11-14 15:54:36 +00003616void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003617 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003618 LocationSummary* locations =
3619 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003620
3621 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 case Primitive::kPrimInt:
3623 case Primitive::kPrimLong: {
3624 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003625 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3627 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3629 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3630 // output and request another temp.
3631 if (rem->InputAt(1)->IsConstant()) {
3632 locations->AddTemp(Location::RequiresRegister());
3633 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636
3637 case Primitive::kPrimFloat:
3638 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003639 locations->SetInAt(0, Location::Any());
3640 locations->SetInAt(1, Location::Any());
3641 locations->SetOut(Location::RequiresFpuRegister());
3642 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003643 break;
3644 }
3645
3646 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003647 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 }
3649}
3650
3651void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3652 Primitive::Type type = rem->GetResultType();
3653 switch (type) {
3654 case Primitive::kPrimInt:
3655 case Primitive::kPrimLong: {
3656 GenerateDivRemIntegral(rem);
3657 break;
3658 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003659 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003660 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003661 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003662 break;
3663 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003664 default:
3665 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3666 }
3667}
3668
Calin Juravled0d48522014-11-04 16:40:20 +00003669void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003670 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003671 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003672}
3673
3674void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003675 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003676 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3677 codegen_->AddSlowPath(slow_path);
3678
3679 LocationSummary* locations = instruction->GetLocations();
3680 Location value = locations->InAt(0);
3681
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003682 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003683 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003684 case Primitive::kPrimByte:
3685 case Primitive::kPrimChar:
3686 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003687 case Primitive::kPrimInt: {
3688 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003690 __ j(kEqual, slow_path->GetEntryLabel());
3691 } else if (value.IsStackSlot()) {
3692 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3693 __ j(kEqual, slow_path->GetEntryLabel());
3694 } else {
3695 DCHECK(value.IsConstant()) << value;
3696 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003697 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003698 }
3699 }
3700 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003701 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003702 case Primitive::kPrimLong: {
3703 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003705 __ j(kEqual, slow_path->GetEntryLabel());
3706 } else if (value.IsDoubleStackSlot()) {
3707 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3708 __ j(kEqual, slow_path->GetEntryLabel());
3709 } else {
3710 DCHECK(value.IsConstant()) << value;
3711 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003712 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003713 }
3714 }
3715 break;
3716 }
3717 default:
3718 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003719 }
Calin Juravled0d48522014-11-04 16:40:20 +00003720}
3721
Calin Juravle9aec02f2014-11-18 23:06:35 +00003722void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3723 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3724
3725 LocationSummary* locations =
3726 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3727
3728 switch (op->GetResultType()) {
3729 case Primitive::kPrimInt:
3730 case Primitive::kPrimLong: {
3731 locations->SetInAt(0, Location::RequiresRegister());
3732 // The shift count needs to be in CL.
3733 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3734 locations->SetOut(Location::SameAsFirstInput());
3735 break;
3736 }
3737 default:
3738 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3739 }
3740}
3741
3742void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3743 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3744
3745 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003747 Location second = locations->InAt(1);
3748
3749 switch (op->GetResultType()) {
3750 case Primitive::kPrimInt: {
3751 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003752 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 if (op->IsShl()) {
3754 __ shll(first_reg, second_reg);
3755 } else if (op->IsShr()) {
3756 __ sarl(first_reg, second_reg);
3757 } else {
3758 __ shrl(first_reg, second_reg);
3759 }
3760 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003761 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003762 if (op->IsShl()) {
3763 __ shll(first_reg, imm);
3764 } else if (op->IsShr()) {
3765 __ sarl(first_reg, imm);
3766 } else {
3767 __ shrl(first_reg, imm);
3768 }
3769 }
3770 break;
3771 }
3772 case Primitive::kPrimLong: {
3773 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775 if (op->IsShl()) {
3776 __ shlq(first_reg, second_reg);
3777 } else if (op->IsShr()) {
3778 __ sarq(first_reg, second_reg);
3779 } else {
3780 __ shrq(first_reg, second_reg);
3781 }
3782 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003783 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003784 if (op->IsShl()) {
3785 __ shlq(first_reg, imm);
3786 } else if (op->IsShr()) {
3787 __ sarq(first_reg, imm);
3788 } else {
3789 __ shrq(first_reg, imm);
3790 }
3791 }
3792 break;
3793 }
3794 default:
3795 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003796 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 }
3798}
3799
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003800void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3801 LocationSummary* locations =
3802 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3803
3804 switch (ror->GetResultType()) {
3805 case Primitive::kPrimInt:
3806 case Primitive::kPrimLong: {
3807 locations->SetInAt(0, Location::RequiresRegister());
3808 // The shift count needs to be in CL (unless it is a constant).
3809 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3810 locations->SetOut(Location::SameAsFirstInput());
3811 break;
3812 }
3813 default:
3814 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3815 UNREACHABLE();
3816 }
3817}
3818
3819void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3820 LocationSummary* locations = ror->GetLocations();
3821 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3822 Location second = locations->InAt(1);
3823
3824 switch (ror->GetResultType()) {
3825 case Primitive::kPrimInt:
3826 if (second.IsRegister()) {
3827 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3828 __ rorl(first_reg, second_reg);
3829 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003830 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003831 __ rorl(first_reg, imm);
3832 }
3833 break;
3834 case Primitive::kPrimLong:
3835 if (second.IsRegister()) {
3836 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3837 __ rorq(first_reg, second_reg);
3838 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003839 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003840 __ rorq(first_reg, imm);
3841 }
3842 break;
3843 default:
3844 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3845 UNREACHABLE();
3846 }
3847}
3848
Calin Juravle9aec02f2014-11-18 23:06:35 +00003849void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3850 HandleShift(shl);
3851}
3852
3853void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3854 HandleShift(shl);
3855}
3856
3857void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3858 HandleShift(shr);
3859}
3860
3861void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3862 HandleShift(shr);
3863}
3864
3865void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3866 HandleShift(ushr);
3867}
3868
3869void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3870 HandleShift(ushr);
3871}
3872
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003873void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003874 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003876 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003877 if (instruction->IsStringAlloc()) {
3878 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3879 } else {
3880 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3881 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3882 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003883 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003884}
3885
3886void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003887 // Note: if heap poisoning is enabled, the entry point takes cares
3888 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003889 if (instruction->IsStringAlloc()) {
3890 // String is allocated through StringFactory. Call NewEmptyString entry point.
3891 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003892 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003893 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3894 __ call(Address(temp, code_offset.SizeValue()));
3895 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3896 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003897 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003898 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3899 DCHECK(!codegen_->IsLeafMethod());
3900 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003901}
3902
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003903void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3904 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003906 InvokeRuntimeCallingConvention calling_convention;
3907 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003908 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003909 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003910 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003911}
3912
3913void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3914 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003915 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3916 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003917 // Note: if heap poisoning is enabled, the entry point takes cares
3918 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003919 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003920 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003921
3922 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003923}
3924
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003925void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003926 LocationSummary* locations =
3927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003928 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3929 if (location.IsStackSlot()) {
3930 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3931 } else if (location.IsDoubleStackSlot()) {
3932 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3933 }
3934 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003935}
3936
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003937void InstructionCodeGeneratorX86_64::VisitParameterValue(
3938 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003939 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003940}
3941
3942void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3943 LocationSummary* locations =
3944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3945 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3946}
3947
3948void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3949 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3950 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003951}
3952
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003953void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3954 LocationSummary* locations =
3955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3956 locations->SetInAt(0, Location::RequiresRegister());
3957 locations->SetOut(Location::RequiresRegister());
3958}
3959
3960void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3961 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003963 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003964 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003965 __ movq(locations->Out().AsRegister<CpuRegister>(),
3966 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003967 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003968 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003969 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003970 __ movq(locations->Out().AsRegister<CpuRegister>(),
3971 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3972 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003973 __ movq(locations->Out().AsRegister<CpuRegister>(),
3974 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003975 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003976}
3977
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003978void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003979 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003980 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003981 locations->SetInAt(0, Location::RequiresRegister());
3982 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003983}
3984
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003985void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3986 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003987 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3988 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003989 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003990 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003991 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003992 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003993 break;
3994
3995 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003997 break;
3998
3999 default:
4000 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4001 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004002}
4003
David Brazdil66d126e2015-04-03 16:02:44 +01004004void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4005 LocationSummary* locations =
4006 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4007 locations->SetInAt(0, Location::RequiresRegister());
4008 locations->SetOut(Location::SameAsFirstInput());
4009}
4010
4011void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004012 LocationSummary* locations = bool_not->GetLocations();
4013 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4014 locations->Out().AsRegister<CpuRegister>().AsRegister());
4015 Location out = locations->Out();
4016 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4017}
4018
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004019void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004020 LocationSummary* locations =
4021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004022 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004023 locations->SetInAt(i, Location::Any());
4024 }
4025 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004026}
4027
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004028void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004029 LOG(FATAL) << "Unimplemented";
4030}
4031
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004032void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004033 /*
4034 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004035 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004036 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4037 */
4038 switch (kind) {
4039 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004040 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004041 break;
4042 }
4043 case MemBarrierKind::kAnyStore:
4044 case MemBarrierKind::kLoadAny:
4045 case MemBarrierKind::kStoreStore: {
4046 // nop
4047 break;
4048 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004049 case MemBarrierKind::kNTStoreStore:
4050 // Non-Temporal Store/Store needs an explicit fence.
4051 MemoryFence(/* non-temporal */ true);
4052 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004053 }
4054}
4055
4056void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4057 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4058
Roland Levillain0d5a2812015-11-13 10:07:31 +00004059 bool object_field_get_with_read_barrier =
4060 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004061 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004062 new (GetGraph()->GetArena()) LocationSummary(instruction,
4063 object_field_get_with_read_barrier ?
4064 LocationSummary::kCallOnSlowPath :
4065 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004066 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004067 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004068 }
Calin Juravle52c48962014-12-16 17:02:57 +00004069 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004070 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4071 locations->SetOut(Location::RequiresFpuRegister());
4072 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004073 // The output overlaps for an object field get when read barriers
4074 // are enabled: we do not want the move to overwrite the object's
4075 // location, as we need it to emit the read barrier.
4076 locations->SetOut(
4077 Location::RequiresRegister(),
4078 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004079 }
Calin Juravle52c48962014-12-16 17:02:57 +00004080}
4081
4082void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4083 const FieldInfo& field_info) {
4084 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4085
4086 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004087 Location base_loc = locations->InAt(0);
4088 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004089 Location out = locations->Out();
4090 bool is_volatile = field_info.IsVolatile();
4091 Primitive::Type field_type = field_info.GetFieldType();
4092 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4093
4094 switch (field_type) {
4095 case Primitive::kPrimBoolean: {
4096 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4097 break;
4098 }
4099
4100 case Primitive::kPrimByte: {
4101 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4102 break;
4103 }
4104
4105 case Primitive::kPrimShort: {
4106 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4107 break;
4108 }
4109
4110 case Primitive::kPrimChar: {
4111 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4112 break;
4113 }
4114
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004115 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004116 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4117 break;
4118 }
4119
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004120 case Primitive::kPrimNot: {
4121 // /* HeapReference<Object> */ out = *(base + offset)
4122 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004123 // Note that a potential implicit null check is handled in this
4124 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4125 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004126 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004127 if (is_volatile) {
4128 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4129 }
4130 } else {
4131 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4132 codegen_->MaybeRecordImplicitNullCheck(instruction);
4133 if (is_volatile) {
4134 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4135 }
4136 // If read barriers are enabled, emit read barriers other than
4137 // Baker's using a slow path (and also unpoison the loaded
4138 // reference, if heap poisoning is enabled).
4139 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4140 }
4141 break;
4142 }
4143
Calin Juravle52c48962014-12-16 17:02:57 +00004144 case Primitive::kPrimLong: {
4145 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4146 break;
4147 }
4148
4149 case Primitive::kPrimFloat: {
4150 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4151 break;
4152 }
4153
4154 case Primitive::kPrimDouble: {
4155 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4156 break;
4157 }
4158
4159 case Primitive::kPrimVoid:
4160 LOG(FATAL) << "Unreachable type " << field_type;
4161 UNREACHABLE();
4162 }
4163
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004164 if (field_type == Primitive::kPrimNot) {
4165 // Potential implicit null checks, in the case of reference
4166 // fields, are handled in the previous switch statement.
4167 } else {
4168 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004169 }
Roland Levillain4d027112015-07-01 15:41:14 +01004170
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004171 if (is_volatile) {
4172 if (field_type == Primitive::kPrimNot) {
4173 // Memory barriers, in the case of references, are also handled
4174 // in the previous switch statement.
4175 } else {
4176 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4177 }
Roland Levillain4d027112015-07-01 15:41:14 +01004178 }
Calin Juravle52c48962014-12-16 17:02:57 +00004179}
4180
4181void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4182 const FieldInfo& field_info) {
4183 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4184
4185 LocationSummary* locations =
4186 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004187 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004188 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004189 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004190 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004191
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004192 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004193 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004194 if (is_volatile) {
4195 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4196 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4197 } else {
4198 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4199 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004200 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004201 if (is_volatile) {
4202 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4203 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4204 } else {
4205 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4206 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004207 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004208 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004209 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004210 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004211 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004212 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4213 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004214 locations->AddTemp(Location::RequiresRegister());
4215 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004216}
4217
Calin Juravle52c48962014-12-16 17:02:57 +00004218void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004219 const FieldInfo& field_info,
4220 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004221 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4222
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004223 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004224 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4225 Location value = locations->InAt(1);
4226 bool is_volatile = field_info.IsVolatile();
4227 Primitive::Type field_type = field_info.GetFieldType();
4228 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4229
4230 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004231 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004232 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233
Mark Mendellea5af682015-10-22 17:35:49 -04004234 bool maybe_record_implicit_null_check_done = false;
4235
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236 switch (field_type) {
4237 case Primitive::kPrimBoolean:
4238 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004239 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004240 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004241 __ movb(Address(base, offset), Immediate(v));
4242 } else {
4243 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4244 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245 break;
4246 }
4247
4248 case Primitive::kPrimShort:
4249 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004250 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004251 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004252 __ movw(Address(base, offset), Immediate(v));
4253 } else {
4254 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4255 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004256 break;
4257 }
4258
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004261 if (value.IsConstant()) {
4262 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004263 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4264 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4265 // Note: if heap poisoning is enabled, no need to poison
4266 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004267 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004268 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004269 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4270 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4271 __ movl(temp, value.AsRegister<CpuRegister>());
4272 __ PoisonHeapReference(temp);
4273 __ movl(Address(base, offset), temp);
4274 } else {
4275 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4276 }
Mark Mendell40741f32015-04-20 22:10:34 -04004277 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278 break;
4279 }
4280
4281 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004282 if (value.IsConstant()) {
4283 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004284 codegen_->MoveInt64ToAddress(Address(base, offset),
4285 Address(base, offset + sizeof(int32_t)),
4286 v,
4287 instruction);
4288 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004289 } else {
4290 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292 break;
4293 }
4294
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004295 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004296 if (value.IsConstant()) {
4297 int32_t v =
4298 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4299 __ movl(Address(base, offset), Immediate(v));
4300 } else {
4301 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4302 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004303 break;
4304 }
4305
4306 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004307 if (value.IsConstant()) {
4308 int64_t v =
4309 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4310 codegen_->MoveInt64ToAddress(Address(base, offset),
4311 Address(base, offset + sizeof(int32_t)),
4312 v,
4313 instruction);
4314 maybe_record_implicit_null_check_done = true;
4315 } else {
4316 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4317 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004318 break;
4319 }
4320
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004321 case Primitive::kPrimVoid:
4322 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004323 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324 }
Calin Juravle52c48962014-12-16 17:02:57 +00004325
Mark Mendellea5af682015-10-22 17:35:49 -04004326 if (!maybe_record_implicit_null_check_done) {
4327 codegen_->MaybeRecordImplicitNullCheck(instruction);
4328 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004329
4330 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4331 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4332 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004333 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004334 }
4335
Calin Juravle52c48962014-12-16 17:02:57 +00004336 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004337 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004338 }
4339}
4340
4341void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4342 HandleFieldSet(instruction, instruction->GetFieldInfo());
4343}
4344
4345void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004346 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004347}
4348
4349void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004350 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004351}
4352
4353void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004354 HandleFieldGet(instruction, instruction->GetFieldInfo());
4355}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004356
Calin Juravle52c48962014-12-16 17:02:57 +00004357void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4358 HandleFieldGet(instruction);
4359}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360
Calin Juravle52c48962014-12-16 17:02:57 +00004361void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4362 HandleFieldGet(instruction, instruction->GetFieldInfo());
4363}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364
Calin Juravle52c48962014-12-16 17:02:57 +00004365void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4366 HandleFieldSet(instruction, instruction->GetFieldInfo());
4367}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004368
Calin Juravle52c48962014-12-16 17:02:57 +00004369void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004370 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371}
4372
Calin Juravlee460d1d2015-09-29 04:52:17 +01004373void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4374 HUnresolvedInstanceFieldGet* instruction) {
4375 FieldAccessCallingConventionX86_64 calling_convention;
4376 codegen_->CreateUnresolvedFieldLocationSummary(
4377 instruction, instruction->GetFieldType(), calling_convention);
4378}
4379
4380void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4381 HUnresolvedInstanceFieldGet* instruction) {
4382 FieldAccessCallingConventionX86_64 calling_convention;
4383 codegen_->GenerateUnresolvedFieldAccess(instruction,
4384 instruction->GetFieldType(),
4385 instruction->GetFieldIndex(),
4386 instruction->GetDexPc(),
4387 calling_convention);
4388}
4389
4390void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4391 HUnresolvedInstanceFieldSet* instruction) {
4392 FieldAccessCallingConventionX86_64 calling_convention;
4393 codegen_->CreateUnresolvedFieldLocationSummary(
4394 instruction, instruction->GetFieldType(), calling_convention);
4395}
4396
4397void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4398 HUnresolvedInstanceFieldSet* instruction) {
4399 FieldAccessCallingConventionX86_64 calling_convention;
4400 codegen_->GenerateUnresolvedFieldAccess(instruction,
4401 instruction->GetFieldType(),
4402 instruction->GetFieldIndex(),
4403 instruction->GetDexPc(),
4404 calling_convention);
4405}
4406
4407void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4408 HUnresolvedStaticFieldGet* instruction) {
4409 FieldAccessCallingConventionX86_64 calling_convention;
4410 codegen_->CreateUnresolvedFieldLocationSummary(
4411 instruction, instruction->GetFieldType(), calling_convention);
4412}
4413
4414void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4415 HUnresolvedStaticFieldGet* instruction) {
4416 FieldAccessCallingConventionX86_64 calling_convention;
4417 codegen_->GenerateUnresolvedFieldAccess(instruction,
4418 instruction->GetFieldType(),
4419 instruction->GetFieldIndex(),
4420 instruction->GetDexPc(),
4421 calling_convention);
4422}
4423
4424void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4425 HUnresolvedStaticFieldSet* instruction) {
4426 FieldAccessCallingConventionX86_64 calling_convention;
4427 codegen_->CreateUnresolvedFieldLocationSummary(
4428 instruction, instruction->GetFieldType(), calling_convention);
4429}
4430
4431void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4432 HUnresolvedStaticFieldSet* instruction) {
4433 FieldAccessCallingConventionX86_64 calling_convention;
4434 codegen_->GenerateUnresolvedFieldAccess(instruction,
4435 instruction->GetFieldType(),
4436 instruction->GetFieldIndex(),
4437 instruction->GetDexPc(),
4438 calling_convention);
4439}
4440
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004442 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4443 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4444 ? Location::RequiresRegister()
4445 : Location::Any();
4446 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447}
4448
Calin Juravle2ae48182016-03-16 14:05:09 +00004449void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4450 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004451 return;
4452 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004453 LocationSummary* locations = instruction->GetLocations();
4454 Location obj = locations->InAt(0);
4455
4456 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004457 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004458}
4459
Calin Juravle2ae48182016-03-16 14:05:09 +00004460void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004461 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004462 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463
4464 LocationSummary* locations = instruction->GetLocations();
4465 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466
4467 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004468 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004469 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004471 } else {
4472 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004473 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004474 __ jmp(slow_path->GetEntryLabel());
4475 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476 }
4477 __ j(kEqual, slow_path->GetEntryLabel());
4478}
4479
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004480void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004481 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004482}
4483
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004484void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004485 bool object_array_get_with_read_barrier =
4486 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004487 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004488 new (GetGraph()->GetArena()) LocationSummary(instruction,
4489 object_array_get_with_read_barrier ?
4490 LocationSummary::kCallOnSlowPath :
4491 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004492 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004493 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004494 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004495 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004496 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004497 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4498 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4499 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004500 // The output overlaps for an object array get when read barriers
4501 // are enabled: we do not want the move to overwrite the array's
4502 // location, as we need it to emit the read barrier.
4503 locations->SetOut(
4504 Location::RequiresRegister(),
4505 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004506 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004507}
4508
4509void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4510 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004511 Location obj_loc = locations->InAt(0);
4512 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004513 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004514 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004515 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004516
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004517 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004518 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004519 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004520 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004521 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522 break;
4523 }
4524
4525 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004526 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004527 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 break;
4529 }
4530
4531 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004532 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004533 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534 break;
4535 }
4536
4537 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004538 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004539 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4540 // Branch cases into compressed and uncompressed for each index's type.
4541 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4542 NearLabel done, not_compressed;
4543 __ cmpl(Address(obj, count_offset), Immediate(0));
4544 codegen_->MaybeRecordImplicitNullCheck(instruction);
4545 __ j(kGreaterEqual, &not_compressed);
4546 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4547 __ jmp(&done);
4548 __ Bind(&not_compressed);
4549 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4550 __ Bind(&done);
4551 } else {
4552 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4553 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004554 break;
4555 }
4556
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004557 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004558 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004559 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004560 break;
4561 }
4562
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004563 case Primitive::kPrimNot: {
4564 static_assert(
4565 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4566 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004567 // /* HeapReference<Object> */ out =
4568 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4569 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004570 // Note that a potential implicit null check is handled in this
4571 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4572 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004573 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004574 } else {
4575 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004576 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4577 codegen_->MaybeRecordImplicitNullCheck(instruction);
4578 // If read barriers are enabled, emit read barriers other than
4579 // Baker's using a slow path (and also unpoison the loaded
4580 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004581 if (index.IsConstant()) {
4582 uint32_t offset =
4583 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004584 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4585 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004586 codegen_->MaybeGenerateReadBarrierSlow(
4587 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4588 }
4589 }
4590 break;
4591 }
4592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004593 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004594 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004595 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 break;
4597 }
4598
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004599 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004600 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004601 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004602 break;
4603 }
4604
4605 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004606 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004607 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004608 break;
4609 }
4610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004612 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004613 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004614 }
Roland Levillain4d027112015-07-01 15:41:14 +01004615
4616 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004617 // Potential implicit null checks, in the case of reference
4618 // arrays, are handled in the previous switch statement.
4619 } else {
4620 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622}
4623
4624void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004625 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004626
4627 bool needs_write_barrier =
4628 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004629 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004630
Nicolas Geoffray39468442014-09-02 15:17:15 +01004631 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004633 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004634 LocationSummary::kCallOnSlowPath :
4635 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004636
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004638 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4639 if (Primitive::IsFloatingPointType(value_type)) {
4640 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004641 } else {
4642 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4643 }
4644
4645 if (needs_write_barrier) {
4646 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004647 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004648 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004650}
4651
4652void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4653 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004654 Location array_loc = locations->InAt(0);
4655 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004657 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004658 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004659 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004660 bool needs_write_barrier =
4661 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004662 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4663 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4664 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665
4666 switch (value_type) {
4667 case Primitive::kPrimBoolean:
4668 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004669 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004670 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 if (value.IsRegister()) {
4672 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004674 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677 break;
4678 }
4679
4680 case Primitive::kPrimShort:
4681 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004683 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004684 if (value.IsRegister()) {
4685 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004686 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004687 DCHECK(value.IsConstant()) << value;
4688 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004690 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 break;
4692 }
4693
4694 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004695 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004696 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004698 if (!value.IsRegister()) {
4699 // Just setting null.
4700 DCHECK(instruction->InputAt(2)->IsNullConstant());
4701 DCHECK(value.IsConstant()) << value;
4702 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004703 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004704 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004705 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004706 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004707 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004708
4709 DCHECK(needs_write_barrier);
4710 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004711 // We cannot use a NearLabel for `done`, as its range may be too
4712 // short when Baker read barriers are enabled.
4713 Label done;
4714 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004715 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004716 Location temp_loc = locations->GetTemp(0);
4717 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004718 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004719 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4720 codegen_->AddSlowPath(slow_path);
4721 if (instruction->GetValueCanBeNull()) {
4722 __ testl(register_value, register_value);
4723 __ j(kNotEqual, &not_null);
4724 __ movl(address, Immediate(0));
4725 codegen_->MaybeRecordImplicitNullCheck(instruction);
4726 __ jmp(&done);
4727 __ Bind(&not_null);
4728 }
4729
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004730 // Note that when Baker read barriers are enabled, the type
4731 // checks are performed without read barriers. This is fine,
4732 // even in the case where a class object is in the from-space
4733 // after the flip, as a comparison involving such a type would
4734 // not produce a false positive; it may of course produce a
4735 // false negative, in which case we would take the ArraySet
4736 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004737
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004738 // /* HeapReference<Class> */ temp = array->klass_
4739 __ movl(temp, Address(array, class_offset));
4740 codegen_->MaybeRecordImplicitNullCheck(instruction);
4741 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004742
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004743 // /* HeapReference<Class> */ temp = temp->component_type_
4744 __ movl(temp, Address(temp, component_offset));
4745 // If heap poisoning is enabled, no need to unpoison `temp`
4746 // nor the object reference in `register_value->klass`, as
4747 // we are comparing two poisoned references.
4748 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004749
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004750 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4751 __ j(kEqual, &do_put);
4752 // If heap poisoning is enabled, the `temp` reference has
4753 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004754 __ MaybeUnpoisonHeapReference(temp);
4755
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004756 // If heap poisoning is enabled, no need to unpoison the
4757 // heap reference loaded below, as it is only used for a
4758 // comparison with null.
4759 __ cmpl(Address(temp, super_offset), Immediate(0));
4760 __ j(kNotEqual, slow_path->GetEntryLabel());
4761 __ Bind(&do_put);
4762 } else {
4763 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 }
4765 }
4766
4767 if (kPoisonHeapReferences) {
4768 __ movl(temp, register_value);
4769 __ PoisonHeapReference(temp);
4770 __ movl(address, temp);
4771 } else {
4772 __ movl(address, register_value);
4773 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004774 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004775 codegen_->MaybeRecordImplicitNullCheck(instruction);
4776 }
4777
4778 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4779 codegen_->MarkGCCard(
4780 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4781 __ Bind(&done);
4782
4783 if (slow_path != nullptr) {
4784 __ Bind(slow_path->GetExitLabel());
4785 }
4786
4787 break;
4788 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004789
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004790 case Primitive::kPrimInt: {
4791 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004792 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 if (value.IsRegister()) {
4794 __ movl(address, value.AsRegister<CpuRegister>());
4795 } else {
4796 DCHECK(value.IsConstant()) << value;
4797 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4798 __ movl(address, Immediate(v));
4799 }
4800 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 break;
4802 }
4803
4804 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004805 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004806 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004807 if (value.IsRegister()) {
4808 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004809 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004811 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004812 Address address_high =
4813 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004814 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815 }
4816 break;
4817 }
4818
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004819 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004820 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004821 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004822 if (value.IsFpuRegister()) {
4823 __ movss(address, value.AsFpuRegister<XmmRegister>());
4824 } else {
4825 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004826 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004827 __ movl(address, Immediate(v));
4828 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004829 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004830 break;
4831 }
4832
4833 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004834 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004835 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004836 if (value.IsFpuRegister()) {
4837 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4838 codegen_->MaybeRecordImplicitNullCheck(instruction);
4839 } else {
4840 int64_t v =
4841 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004842 Address address_high =
4843 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004844 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4845 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004846 break;
4847 }
4848
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004849 case Primitive::kPrimVoid:
4850 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004851 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 }
4853}
4854
4855void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004856 LocationSummary* locations =
4857 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004858 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04004859 if (!instruction->IsEmittedAtUseSite()) {
4860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4861 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862}
4863
4864void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04004865 if (instruction->IsEmittedAtUseSite()) {
4866 return;
4867 }
4868
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004870 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004871 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4872 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004873 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07004875 // Mask out most significant bit in case the array is String's array of char.
4876 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
4877 __ andl(out, Immediate(INT32_MAX));
4878 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879}
4880
4881void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004882 RegisterSet caller_saves = RegisterSet::Empty();
4883 InvokeRuntimeCallingConvention calling_convention;
4884 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4885 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4886 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004887 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04004888 HInstruction* length = instruction->InputAt(1);
4889 if (!length->IsEmittedAtUseSite()) {
4890 locations->SetInAt(1, Location::RegisterOrConstant(length));
4891 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892}
4893
4894void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4895 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004896 Location index_loc = locations->InAt(0);
4897 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04004898 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899
Mark Mendell99dbd682015-04-22 16:18:52 -04004900 if (length_loc.IsConstant()) {
4901 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4902 if (index_loc.IsConstant()) {
4903 // BCE will remove the bounds check if we are guarenteed to pass.
4904 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4905 if (index < 0 || index >= length) {
4906 codegen_->AddSlowPath(slow_path);
4907 __ jmp(slow_path->GetEntryLabel());
4908 } else {
4909 // Some optimization after BCE may have generated this, and we should not
4910 // generate a bounds check if it is a valid range.
4911 }
4912 return;
4913 }
4914
4915 // We have to reverse the jump condition because the length is the constant.
4916 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4917 __ cmpl(index_reg, Immediate(length));
4918 codegen_->AddSlowPath(slow_path);
4919 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004920 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04004921 HInstruction* array_length = instruction->InputAt(1);
4922 if (array_length->IsEmittedAtUseSite()) {
4923 // Address the length field in the array.
4924 DCHECK(array_length->IsArrayLength());
4925 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
4926 Location array_loc = array_length->GetLocations()->InAt(0);
4927 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07004928 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4929 CpuRegister length_reg = CpuRegister(TMP);
4930 __ movl(length_reg, array_len);
4931 codegen_->MaybeRecordImplicitNullCheck(array_length);
4932 __ andl(length_reg, Immediate(INT32_MAX));
4933 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04004934 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07004935 // Checking the bound for general case:
4936 // Array of char or String's array when the compression feature off.
4937 if (index_loc.IsConstant()) {
4938 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4939 __ cmpl(array_len, Immediate(value));
4940 } else {
4941 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
4942 }
4943 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04004944 }
Mark Mendell99dbd682015-04-22 16:18:52 -04004945 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004946 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04004947 }
4948 codegen_->AddSlowPath(slow_path);
4949 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004950 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951}
4952
4953void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4954 CpuRegister card,
4955 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004956 CpuRegister value,
4957 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004958 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004959 if (value_can_be_null) {
4960 __ testl(value, value);
4961 __ j(kEqual, &is_null);
4962 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004963 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004964 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004965 __ movq(temp, object);
4966 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004967 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004968 if (value_can_be_null) {
4969 __ Bind(&is_null);
4970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971}
4972
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004973void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004974 LOG(FATAL) << "Unimplemented";
4975}
4976
4977void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004978 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4979}
4980
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004981void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004982 LocationSummary* locations =
4983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004984 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004985}
4986
4987void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004988 HBasicBlock* block = instruction->GetBlock();
4989 if (block->GetLoopInformation() != nullptr) {
4990 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4991 // The back edge will generate the suspend check.
4992 return;
4993 }
4994 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4995 // The goto will generate the suspend check.
4996 return;
4997 }
4998 GenerateSuspendCheck(instruction, nullptr);
4999}
5000
5001void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5002 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005003 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005004 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5005 if (slow_path == nullptr) {
5006 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5007 instruction->SetSlowPath(slow_path);
5008 codegen_->AddSlowPath(slow_path);
5009 if (successor != nullptr) {
5010 DCHECK(successor->IsLoopHeader());
5011 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5012 }
5013 } else {
5014 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5015 }
5016
Andreas Gampe542451c2016-07-26 09:02:02 -07005017 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005018 /* no_rip */ true),
5019 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005020 if (successor == nullptr) {
5021 __ j(kNotEqual, slow_path->GetEntryLabel());
5022 __ Bind(slow_path->GetReturnLabel());
5023 } else {
5024 __ j(kEqual, codegen_->GetLabelOf(successor));
5025 __ jmp(slow_path->GetEntryLabel());
5026 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005027}
5028
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005029X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5030 return codegen_->GetAssembler();
5031}
5032
5033void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005034 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005035 Location source = move->GetSource();
5036 Location destination = move->GetDestination();
5037
5038 if (source.IsRegister()) {
5039 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005040 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005041 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005042 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005043 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005044 } else {
5045 DCHECK(destination.IsDoubleStackSlot());
5046 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005047 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005048 }
5049 } else if (source.IsStackSlot()) {
5050 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005051 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005052 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005053 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005054 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005055 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005056 } else {
5057 DCHECK(destination.IsStackSlot());
5058 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5059 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5060 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005061 } else if (source.IsDoubleStackSlot()) {
5062 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005063 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005064 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005065 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005066 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5067 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005068 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005069 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005070 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5071 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5072 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005073 } else if (source.IsConstant()) {
5074 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005075 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5076 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005077 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005078 if (value == 0) {
5079 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5080 } else {
5081 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5082 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005083 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005084 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005085 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005086 }
5087 } else if (constant->IsLongConstant()) {
5088 int64_t value = constant->AsLongConstant()->GetValue();
5089 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005090 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005091 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005092 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005093 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005094 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005095 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005096 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005097 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005098 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005099 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005100 } else {
5101 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005102 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005103 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5104 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005105 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005106 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005107 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005108 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005109 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005110 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005111 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005112 } else {
5113 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005114 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005115 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005116 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005117 } else if (source.IsFpuRegister()) {
5118 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005119 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005120 } else if (destination.IsStackSlot()) {
5121 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005122 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005123 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005124 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005125 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005126 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005127 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005128 }
5129}
5130
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005131void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005132 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005133 __ movl(Address(CpuRegister(RSP), mem), reg);
5134 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005135}
5136
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005137void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005138 ScratchRegisterScope ensure_scratch(
5139 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5140
5141 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5142 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5143 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5144 Address(CpuRegister(RSP), mem2 + stack_offset));
5145 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5146 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5147 CpuRegister(ensure_scratch.GetRegister()));
5148}
5149
Mark Mendell8a1c7282015-06-29 15:41:28 -04005150void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5151 __ movq(CpuRegister(TMP), reg1);
5152 __ movq(reg1, reg2);
5153 __ movq(reg2, CpuRegister(TMP));
5154}
5155
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005156void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5157 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5158 __ movq(Address(CpuRegister(RSP), mem), reg);
5159 __ movq(reg, CpuRegister(TMP));
5160}
5161
5162void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5163 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005164 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005165
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005166 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5167 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5168 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5169 Address(CpuRegister(RSP), mem2 + stack_offset));
5170 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5171 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5172 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005173}
5174
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005175void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5176 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5177 __ movss(Address(CpuRegister(RSP), mem), reg);
5178 __ movd(reg, CpuRegister(TMP));
5179}
5180
5181void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5182 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5183 __ movsd(Address(CpuRegister(RSP), mem), reg);
5184 __ movd(reg, CpuRegister(TMP));
5185}
5186
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005187void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005188 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005189 Location source = move->GetSource();
5190 Location destination = move->GetDestination();
5191
5192 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005193 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005194 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005195 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005196 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005197 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005198 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005199 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5200 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005201 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005202 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005203 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005204 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5205 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005206 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5208 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5209 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005210 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005211 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005212 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005213 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005215 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005216 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005218 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220 }
5221}
5222
5223
5224void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5225 __ pushq(CpuRegister(reg));
5226}
5227
5228
5229void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5230 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005231}
5232
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005233void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005234 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005235 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5236 Immediate(mirror::Class::kStatusInitialized));
5237 __ j(kLess, slow_path->GetEntryLabel());
5238 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005239 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005240}
5241
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005242HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5243 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005244 switch (desired_class_load_kind) {
5245 case HLoadClass::LoadKind::kReferrersClass:
5246 break;
5247 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5248 DCHECK(!GetCompilerOptions().GetCompilePic());
5249 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5250 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5251 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5252 DCHECK(GetCompilerOptions().GetCompilePic());
5253 break;
5254 case HLoadClass::LoadKind::kBootImageAddress:
5255 break;
5256 case HLoadClass::LoadKind::kDexCacheAddress:
5257 DCHECK(Runtime::Current()->UseJitCompilation());
5258 break;
5259 case HLoadClass::LoadKind::kDexCachePcRelative:
5260 DCHECK(!Runtime::Current()->UseJitCompilation());
5261 break;
5262 case HLoadClass::LoadKind::kDexCacheViaMethod:
5263 break;
5264 }
5265 return desired_class_load_kind;
5266}
5267
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005268void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005269 if (cls->NeedsAccessCheck()) {
5270 InvokeRuntimeCallingConvention calling_convention;
5271 CodeGenerator::CreateLoadClassLocationSummary(
5272 cls,
5273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5274 Location::RegisterLocation(RAX),
5275 /* code_generator_supports_read_barrier */ true);
5276 return;
5277 }
5278
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005279 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5280 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005281 ? LocationSummary::kCallOnSlowPath
5282 : LocationSummary::kNoCall;
5283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005284 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005285 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005286 }
5287
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005288 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5289 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5290 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5291 locations->SetInAt(0, Location::RequiresRegister());
5292 }
5293 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005294}
5295
5296void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005297 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005298 if (cls->NeedsAccessCheck()) {
5299 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005300 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005301 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005302 return;
5303 }
5304
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305 Location out_loc = locations->Out();
5306 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005307
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005308 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005309 bool generate_null_check = false;
5310 switch (cls->GetLoadKind()) {
5311 case HLoadClass::LoadKind::kReferrersClass: {
5312 DCHECK(!cls->CanCallRuntime());
5313 DCHECK(!cls->MustGenerateClinitCheck());
5314 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5315 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5316 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005317 cls,
5318 out_loc,
5319 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5320 /*fixup_label*/nullptr,
5321 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005322 break;
5323 }
5324 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005325 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005326 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5327 codegen_->RecordTypePatch(cls);
5328 break;
5329 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005330 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005331 DCHECK_NE(cls->GetAddress(), 0u);
5332 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5333 __ movl(out, Immediate(address)); // Zero-extended.
5334 codegen_->RecordSimplePatch();
5335 break;
5336 }
5337 case HLoadClass::LoadKind::kDexCacheAddress: {
5338 DCHECK_NE(cls->GetAddress(), 0u);
5339 // /* GcRoot<mirror::Class> */ out = *address
5340 if (IsUint<32>(cls->GetAddress())) {
5341 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005342 GenerateGcRootFieldLoad(cls,
5343 out_loc,
5344 address,
5345 /*fixup_label*/nullptr,
5346 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005347 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005348 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5349 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005350 GenerateGcRootFieldLoad(cls,
5351 out_loc,
5352 Address(out, 0),
5353 /*fixup_label*/nullptr,
5354 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005355 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005356 generate_null_check = !cls->IsInDexCache();
5357 break;
5358 }
5359 case HLoadClass::LoadKind::kDexCachePcRelative: {
5360 uint32_t offset = cls->GetDexCacheElementOffset();
5361 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5362 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5363 /* no_rip */ false);
5364 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005365 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005366 generate_null_check = !cls->IsInDexCache();
5367 break;
5368 }
5369 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5370 // /* GcRoot<mirror::Class>[] */ out =
5371 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5372 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5373 __ movq(out,
5374 Address(current_method,
5375 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5376 // /* GcRoot<mirror::Class> */ out = out[type_index]
5377 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005378 cls,
5379 out_loc,
5380 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5381 /*fixup_label*/nullptr,
5382 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005383 generate_null_check = !cls->IsInDexCache();
5384 break;
5385 }
5386 default:
5387 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5388 UNREACHABLE();
5389 }
5390
5391 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5392 DCHECK(cls->CanCallRuntime());
5393 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5394 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5395 codegen_->AddSlowPath(slow_path);
5396 if (generate_null_check) {
5397 __ testl(out, out);
5398 __ j(kEqual, slow_path->GetEntryLabel());
5399 }
5400 if (cls->MustGenerateClinitCheck()) {
5401 GenerateClassInitializationCheck(slow_path, out);
5402 } else {
5403 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005404 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005405 }
5406}
5407
5408void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5409 LocationSummary* locations =
5410 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5411 locations->SetInAt(0, Location::RequiresRegister());
5412 if (check->HasUses()) {
5413 locations->SetOut(Location::SameAsFirstInput());
5414 }
5415}
5416
5417void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005418 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005419 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005420 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005422 GenerateClassInitializationCheck(slow_path,
5423 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424}
5425
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005426HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5427 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005428 switch (desired_string_load_kind) {
5429 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5430 DCHECK(!GetCompilerOptions().GetCompilePic());
5431 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5432 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5433 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5434 DCHECK(GetCompilerOptions().GetCompilePic());
5435 break;
5436 case HLoadString::LoadKind::kBootImageAddress:
5437 break;
5438 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005439 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005440 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005441 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005442 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005443 break;
5444 case HLoadString::LoadKind::kDexCacheViaMethod:
5445 break;
5446 }
5447 return desired_string_load_kind;
5448}
5449
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005450void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005451 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005452 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5453 ? LocationSummary::kCallOnMainOnly
5454 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005455 : LocationSummary::kNoCall;
5456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005457 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5458 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005459 locations->SetOut(Location::RegisterLocation(RAX));
5460 } else {
5461 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005462 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005463}
5464
5465void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005466 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005467 Location out_loc = locations->Out();
5468 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005469
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 switch (load->GetLoadKind()) {
5471 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005473 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005474 return; // No dex cache slow path.
5475 }
5476 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 DCHECK_NE(load->GetAddress(), 0u);
5478 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5479 __ movl(out, Immediate(address)); // Zero-extended.
5480 codegen_->RecordSimplePatch();
5481 return; // No dex cache slow path.
5482 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005483 case HLoadString::LoadKind::kBssEntry: {
5484 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5485 /* no_rip */ false);
5486 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5487 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5488 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5489 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5490 codegen_->AddSlowPath(slow_path);
5491 __ testl(out, out);
5492 __ j(kEqual, slow_path->GetEntryLabel());
5493 __ Bind(slow_path->GetExitLabel());
5494 return;
5495 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005496 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005497 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005498 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005500 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005501 InvokeRuntimeCallingConvention calling_convention;
5502 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
5503 Immediate(load->GetStringIndex()));
5504 codegen_->InvokeRuntime(kQuickResolveString,
5505 load,
5506 load->GetDexPc());
5507 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005508}
5509
David Brazdilcb1c0552015-08-04 16:22:25 +01005510static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005511 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005512 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005513}
5514
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005515void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5516 LocationSummary* locations =
5517 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5518 locations->SetOut(Location::RequiresRegister());
5519}
5520
5521void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005522 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5523}
5524
5525void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5526 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5527}
5528
5529void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5530 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005531}
5532
5533void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5534 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005536 InvokeRuntimeCallingConvention calling_convention;
5537 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5538}
5539
5540void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005541 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005542 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005543}
5544
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005545static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5546 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005547 !kUseBakerReadBarrier &&
5548 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005549 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5550 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5551}
5552
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005553void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005554 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005555 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005556 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005557 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 case TypeCheckKind::kExactCheck:
5559 case TypeCheckKind::kAbstractClassCheck:
5560 case TypeCheckKind::kClassHierarchyCheck:
5561 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005562 call_kind =
5563 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005564 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565 break;
5566 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567 case TypeCheckKind::kUnresolvedCheck:
5568 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 call_kind = LocationSummary::kCallOnSlowPath;
5570 break;
5571 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005572
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005574 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005575 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005576 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005577 locations->SetInAt(0, Location::RequiresRegister());
5578 locations->SetInAt(1, Location::Any());
5579 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5580 locations->SetOut(Location::RequiresRegister());
5581 // When read barriers are enabled, we need a temporary register for
5582 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005583 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005584 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005585 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005586}
5587
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005588void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005589 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005590 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005591 Location obj_loc = locations->InAt(0);
5592 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005593 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005594 Location out_loc = locations->Out();
5595 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005596 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005597 locations->GetTemp(0) :
5598 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005599 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005600 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5601 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5602 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005603 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005605
5606 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005607 // Avoid null check if we know obj is not null.
5608 if (instruction->MustDoNullCheck()) {
5609 __ testl(obj, obj);
5610 __ j(kEqual, &zero);
5611 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612
Roland Levillain0d5a2812015-11-13 10:07:31 +00005613 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005614 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005615
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005616 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005617 case TypeCheckKind::kExactCheck: {
5618 if (cls.IsRegister()) {
5619 __ cmpl(out, cls.AsRegister<CpuRegister>());
5620 } else {
5621 DCHECK(cls.IsStackSlot()) << cls;
5622 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5623 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005624 if (zero.IsLinked()) {
5625 // Classes must be equal for the instanceof to succeed.
5626 __ j(kNotEqual, &zero);
5627 __ movl(out, Immediate(1));
5628 __ jmp(&done);
5629 } else {
5630 __ setcc(kEqual, out);
5631 // setcc only sets the low byte.
5632 __ andl(out, Immediate(1));
5633 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005634 break;
5635 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005636
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005637 case TypeCheckKind::kAbstractClassCheck: {
5638 // If the class is abstract, we eagerly fetch the super class of the
5639 // object to avoid doing a comparison we know will fail.
5640 NearLabel loop, success;
5641 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005642 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005643 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 __ testl(out, out);
5645 // If `out` is null, we use it for the result, and jump to `done`.
5646 __ j(kEqual, &done);
5647 if (cls.IsRegister()) {
5648 __ cmpl(out, cls.AsRegister<CpuRegister>());
5649 } else {
5650 DCHECK(cls.IsStackSlot()) << cls;
5651 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5652 }
5653 __ j(kNotEqual, &loop);
5654 __ movl(out, Immediate(1));
5655 if (zero.IsLinked()) {
5656 __ jmp(&done);
5657 }
5658 break;
5659 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005660
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005661 case TypeCheckKind::kClassHierarchyCheck: {
5662 // Walk over the class hierarchy to find a match.
5663 NearLabel loop, success;
5664 __ Bind(&loop);
5665 if (cls.IsRegister()) {
5666 __ cmpl(out, cls.AsRegister<CpuRegister>());
5667 } else {
5668 DCHECK(cls.IsStackSlot()) << cls;
5669 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5670 }
5671 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005672 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005673 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005674 __ testl(out, out);
5675 __ j(kNotEqual, &loop);
5676 // If `out` is null, we use it for the result, and jump to `done`.
5677 __ jmp(&done);
5678 __ Bind(&success);
5679 __ movl(out, Immediate(1));
5680 if (zero.IsLinked()) {
5681 __ jmp(&done);
5682 }
5683 break;
5684 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005685
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005686 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005687 // Do an exact check.
5688 NearLabel exact_check;
5689 if (cls.IsRegister()) {
5690 __ cmpl(out, cls.AsRegister<CpuRegister>());
5691 } else {
5692 DCHECK(cls.IsStackSlot()) << cls;
5693 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5694 }
5695 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005696 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005697 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005698 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005699 __ testl(out, out);
5700 // If `out` is null, we use it for the result, and jump to `done`.
5701 __ j(kEqual, &done);
5702 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5703 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005704 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 __ movl(out, Immediate(1));
5706 __ jmp(&done);
5707 break;
5708 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005709
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005710 case TypeCheckKind::kArrayCheck: {
5711 if (cls.IsRegister()) {
5712 __ cmpl(out, cls.AsRegister<CpuRegister>());
5713 } else {
5714 DCHECK(cls.IsStackSlot()) << cls;
5715 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5716 }
5717 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005718 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5719 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 codegen_->AddSlowPath(slow_path);
5721 __ j(kNotEqual, slow_path->GetEntryLabel());
5722 __ movl(out, Immediate(1));
5723 if (zero.IsLinked()) {
5724 __ jmp(&done);
5725 }
5726 break;
5727 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728
Calin Juravle98893e12015-10-02 21:05:03 +01005729 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005730 case TypeCheckKind::kInterfaceCheck: {
5731 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005732 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 // cases.
5734 //
5735 // We cannot directly call the InstanceofNonTrivial runtime
5736 // entry point without resorting to a type checking slow path
5737 // here (i.e. by calling InvokeRuntime directly), as it would
5738 // require to assign fixed registers for the inputs of this
5739 // HInstanceOf instruction (following the runtime calling
5740 // convention), which might be cluttered by the potential first
5741 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005742 //
5743 // TODO: Introduce a new runtime entry point taking the object
5744 // to test (instead of its class) as argument, and let it deal
5745 // with the read barrier issues. This will let us refactor this
5746 // case of the `switch` code as it was previously (with a direct
5747 // call to the runtime not using a type checking slow path).
5748 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749 DCHECK(locations->OnlyCallsOnSlowPath());
5750 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5751 /* is_fatal */ false);
5752 codegen_->AddSlowPath(slow_path);
5753 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 if (zero.IsLinked()) {
5755 __ jmp(&done);
5756 }
5757 break;
5758 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005759 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005760
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005762 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005763 __ xorl(out, out);
5764 }
5765
5766 if (done.IsLinked()) {
5767 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005768 }
5769
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005770 if (slow_path != nullptr) {
5771 __ Bind(slow_path->GetExitLabel());
5772 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005773}
5774
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005775void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005776 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5777 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5779 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005780 case TypeCheckKind::kExactCheck:
5781 case TypeCheckKind::kAbstractClassCheck:
5782 case TypeCheckKind::kClassHierarchyCheck:
5783 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005784 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5785 LocationSummary::kCallOnSlowPath :
5786 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 break;
5788 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005789 case TypeCheckKind::kUnresolvedCheck:
5790 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005791 call_kind = LocationSummary::kCallOnSlowPath;
5792 break;
5793 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5795 locations->SetInAt(0, Location::RequiresRegister());
5796 locations->SetInAt(1, Location::Any());
5797 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5798 locations->AddTemp(Location::RequiresRegister());
5799 // When read barriers are enabled, we need an additional temporary
5800 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005801 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005802 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005803 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005804}
5805
5806void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005807 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005808 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809 Location obj_loc = locations->InAt(0);
5810 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005811 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005812 Location temp_loc = locations->GetTemp(0);
5813 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005814 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005815 locations->GetTemp(1) :
5816 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005817 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5818 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5819 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5820 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005821
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 bool is_type_check_slow_path_fatal =
5823 (type_check_kind == TypeCheckKind::kExactCheck ||
5824 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5825 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5826 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5827 !instruction->CanThrowIntoCatchBlock();
5828 SlowPathCode* type_check_slow_path =
5829 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5830 is_type_check_slow_path_fatal);
5831 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005832
Roland Levillain0d5a2812015-11-13 10:07:31 +00005833 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005834 case TypeCheckKind::kExactCheck:
5835 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005836 NearLabel done;
5837 // Avoid null check if we know obj is not null.
5838 if (instruction->MustDoNullCheck()) {
5839 __ testl(obj, obj);
5840 __ j(kEqual, &done);
5841 }
5842
5843 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005844 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005845
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005846 if (cls.IsRegister()) {
5847 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5848 } else {
5849 DCHECK(cls.IsStackSlot()) << cls;
5850 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5851 }
5852 // Jump to slow path for throwing the exception or doing a
5853 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005854 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005855 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005856 break;
5857 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005859 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005860 NearLabel done;
5861 // Avoid null check if we know obj is not null.
5862 if (instruction->MustDoNullCheck()) {
5863 __ testl(obj, obj);
5864 __ j(kEqual, &done);
5865 }
5866
5867 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005868 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005869
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 // If the class is abstract, we eagerly fetch the super class of the
5871 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005873 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005874 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005875 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876
5877 // If the class reference currently in `temp` is not null, jump
5878 // to the `compare_classes` label to compare it with the checked
5879 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881 __ j(kNotEqual, &compare_classes);
5882 // Otherwise, jump to the slow path to throw the exception.
5883 //
5884 // But before, move back the object's class into `temp` before
5885 // going into the slow path, as it has been overwritten in the
5886 // meantime.
5887 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005888 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005889 __ jmp(type_check_slow_path->GetEntryLabel());
5890
5891 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005892 if (cls.IsRegister()) {
5893 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5894 } else {
5895 DCHECK(cls.IsStackSlot()) << cls;
5896 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5897 }
5898 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005899 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 break;
5901 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005903 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005904 NearLabel done;
5905 // Avoid null check if we know obj is not null.
5906 if (instruction->MustDoNullCheck()) {
5907 __ testl(obj, obj);
5908 __ j(kEqual, &done);
5909 }
5910
5911 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005912 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005913
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005915 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 __ Bind(&loop);
5917 if (cls.IsRegister()) {
5918 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5919 } else {
5920 DCHECK(cls.IsStackSlot()) << cls;
5921 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5922 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005923 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005924
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005926 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005927
5928 // If the class reference currently in `temp` is not null, jump
5929 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 __ testl(temp, temp);
5931 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932 // Otherwise, jump to the slow path to throw the exception.
5933 //
5934 // But before, move back the object's class into `temp` before
5935 // going into the slow path, as it has been overwritten in the
5936 // meantime.
5937 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005938 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005939 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005940 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 break;
5942 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005943
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005944 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005945 // We cannot use a NearLabel here, as its range might be too
5946 // short in some cases when read barriers are enabled. This has
5947 // been observed for instance when the code emitted for this
5948 // case uses high x86-64 registers (R8-R15).
5949 Label done;
5950 // Avoid null check if we know obj is not null.
5951 if (instruction->MustDoNullCheck()) {
5952 __ testl(obj, obj);
5953 __ j(kEqual, &done);
5954 }
5955
5956 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005957 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005958
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005959 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005960 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005961 if (cls.IsRegister()) {
5962 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5963 } else {
5964 DCHECK(cls.IsStackSlot()) << cls;
5965 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5966 }
5967 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968
5969 // 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> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005971 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972
5973 // If the component type is not null (i.e. the object is indeed
5974 // an array), jump to label `check_non_primitive_component_type`
5975 // to further check that this component type is not a primitive
5976 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 __ j(kNotEqual, &check_non_primitive_component_type);
5979 // Otherwise, jump to the slow path to throw the exception.
5980 //
5981 // But before, move back the object's class into `temp` before
5982 // going into the slow path, as it has been overwritten in the
5983 // meantime.
5984 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005985 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 __ jmp(type_check_slow_path->GetEntryLabel());
5987
5988 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 __ j(kEqual, &done);
5991 // Same comment as above regarding `temp` and the slow path.
5992 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005993 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005995 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 break;
5997 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998
Calin Juravle98893e12015-10-02 21:05:03 +01005999 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006001 NearLabel done;
6002 // Avoid null check if we know obj is not null.
6003 if (instruction->MustDoNullCheck()) {
6004 __ testl(obj, obj);
6005 __ j(kEqual, &done);
6006 }
6007
6008 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006009 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006010
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006011 // We always go into the type check slow path for the unresolved
6012 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013 //
6014 // We cannot directly call the CheckCast runtime entry point
6015 // without resorting to a type checking slow path here (i.e. by
6016 // calling InvokeRuntime directly), as it would require to
6017 // assign fixed registers for the inputs of this HInstanceOf
6018 // instruction (following the runtime calling convention), which
6019 // might be cluttered by the potential first read barrier
6020 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006021 //
6022 // TODO: Introduce a new runtime entry point taking the object
6023 // to test (instead of its class) as argument, and let it deal
6024 // with the read barrier issues. This will let us refactor this
6025 // case of the `switch` code as it was previously (with a direct
6026 // call to the runtime not using a type checking slow path).
6027 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006028 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006029 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030 break;
6031 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006032
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006034}
6035
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006036void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6037 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006039 InvokeRuntimeCallingConvention calling_convention;
6040 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6041}
6042
6043void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006044 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006045 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006046 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006047 if (instruction->IsEnter()) {
6048 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6049 } else {
6050 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6051 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006052}
6053
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006054void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6055void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6056void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6057
6058void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6059 LocationSummary* locations =
6060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6061 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6062 || instruction->GetResultType() == Primitive::kPrimLong);
6063 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006064 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006065 locations->SetOut(Location::SameAsFirstInput());
6066}
6067
6068void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6069 HandleBitwiseOperation(instruction);
6070}
6071
6072void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6073 HandleBitwiseOperation(instruction);
6074}
6075
6076void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6077 HandleBitwiseOperation(instruction);
6078}
6079
6080void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6081 LocationSummary* locations = instruction->GetLocations();
6082 Location first = locations->InAt(0);
6083 Location second = locations->InAt(1);
6084 DCHECK(first.Equals(locations->Out()));
6085
6086 if (instruction->GetResultType() == Primitive::kPrimInt) {
6087 if (second.IsRegister()) {
6088 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006089 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006090 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006091 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006092 } else {
6093 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006094 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006095 }
6096 } else if (second.IsConstant()) {
6097 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6098 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006099 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006100 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006101 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006102 } else {
6103 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006104 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006105 }
6106 } else {
6107 Address address(CpuRegister(RSP), second.GetStackIndex());
6108 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006109 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006110 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006111 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006112 } else {
6113 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006114 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006115 }
6116 }
6117 } else {
6118 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006119 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6120 bool second_is_constant = false;
6121 int64_t value = 0;
6122 if (second.IsConstant()) {
6123 second_is_constant = true;
6124 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006125 }
Mark Mendell40741f32015-04-20 22:10:34 -04006126 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006127
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006128 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006129 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006130 if (is_int32_value) {
6131 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6132 } else {
6133 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6134 }
6135 } else if (second.IsDoubleStackSlot()) {
6136 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006137 } else {
6138 __ andq(first_reg, second.AsRegister<CpuRegister>());
6139 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006140 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006141 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006142 if (is_int32_value) {
6143 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6144 } else {
6145 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6146 }
6147 } else if (second.IsDoubleStackSlot()) {
6148 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006149 } else {
6150 __ orq(first_reg, second.AsRegister<CpuRegister>());
6151 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006152 } else {
6153 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006154 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006155 if (is_int32_value) {
6156 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6157 } else {
6158 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6159 }
6160 } else if (second.IsDoubleStackSlot()) {
6161 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006162 } else {
6163 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6164 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006165 }
6166 }
6167}
6168
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006169void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6170 Location out,
6171 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006172 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006173 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6174 if (kEmitCompilerReadBarrier) {
6175 if (kUseBakerReadBarrier) {
6176 // Load with fast path based Baker's read barrier.
6177 // /* HeapReference<Object> */ out = *(out + offset)
6178 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006179 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006180 } else {
6181 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006182 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006183 // in the following move operation, as we will need it for the
6184 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006185 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006186 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006187 // /* HeapReference<Object> */ out = *(out + offset)
6188 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006189 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006190 }
6191 } else {
6192 // Plain load with no read barrier.
6193 // /* HeapReference<Object> */ out = *(out + offset)
6194 __ movl(out_reg, Address(out_reg, offset));
6195 __ MaybeUnpoisonHeapReference(out_reg);
6196 }
6197}
6198
6199void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6200 Location out,
6201 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006202 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006203 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6204 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6205 if (kEmitCompilerReadBarrier) {
6206 if (kUseBakerReadBarrier) {
6207 // Load with fast path based Baker's read barrier.
6208 // /* HeapReference<Object> */ out = *(obj + offset)
6209 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006210 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006211 } else {
6212 // Load with slow path based read barrier.
6213 // /* HeapReference<Object> */ out = *(obj + offset)
6214 __ movl(out_reg, Address(obj_reg, offset));
6215 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6216 }
6217 } else {
6218 // Plain load with no read barrier.
6219 // /* HeapReference<Object> */ out = *(obj + offset)
6220 __ movl(out_reg, Address(obj_reg, offset));
6221 __ MaybeUnpoisonHeapReference(out_reg);
6222 }
6223}
6224
6225void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6226 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006227 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006228 Label* fixup_label,
6229 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006230 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006231 if (requires_read_barrier) {
6232 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006233 if (kUseBakerReadBarrier) {
6234 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6235 // Baker's read barrier are used:
6236 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006238 // if (Thread::Current()->GetIsGcMarking()) {
6239 // root = ReadBarrier::Mark(root)
6240 // }
6241
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 // /* GcRoot<mirror::Object> */ root = *address
6243 __ movl(root_reg, address);
6244 if (fixup_label != nullptr) {
6245 __ Bind(fixup_label);
6246 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006247 static_assert(
6248 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6249 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6250 "have different sizes.");
6251 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6252 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6253 "have different sizes.");
6254
Vladimir Marko953437b2016-08-24 08:30:46 +00006255 // Slow path marking the GC root `root`.
6256 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6257 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006258 codegen_->AddSlowPath(slow_path);
6259
Andreas Gampe542451c2016-07-26 09:02:02 -07006260 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006261 /* no_rip */ true),
6262 Immediate(0));
6263 __ j(kNotEqual, slow_path->GetEntryLabel());
6264 __ Bind(slow_path->GetExitLabel());
6265 } else {
6266 // GC root loaded through a slow path for read barriers other
6267 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006268 // /* GcRoot<mirror::Object>* */ root = address
6269 __ leaq(root_reg, address);
6270 if (fixup_label != nullptr) {
6271 __ Bind(fixup_label);
6272 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006273 // /* mirror::Object* */ root = root->Read()
6274 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6275 }
6276 } else {
6277 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006278 // /* GcRoot<mirror::Object> */ root = *address
6279 __ movl(root_reg, address);
6280 if (fixup_label != nullptr) {
6281 __ Bind(fixup_label);
6282 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006283 // Note that GC roots are not affected by heap poisoning, thus we
6284 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006285 }
6286}
6287
6288void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6289 Location ref,
6290 CpuRegister obj,
6291 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006292 bool needs_null_check) {
6293 DCHECK(kEmitCompilerReadBarrier);
6294 DCHECK(kUseBakerReadBarrier);
6295
6296 // /* HeapReference<Object> */ ref = *(obj + offset)
6297 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006298 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006299}
6300
6301void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6302 Location ref,
6303 CpuRegister obj,
6304 uint32_t data_offset,
6305 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006306 bool needs_null_check) {
6307 DCHECK(kEmitCompilerReadBarrier);
6308 DCHECK(kUseBakerReadBarrier);
6309
Roland Levillain3d312422016-06-23 13:53:42 +01006310 static_assert(
6311 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6312 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006313 // /* HeapReference<Object> */ ref =
6314 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006315 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006316 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006317}
6318
6319void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6320 Location ref,
6321 CpuRegister obj,
6322 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006323 bool needs_null_check) {
6324 DCHECK(kEmitCompilerReadBarrier);
6325 DCHECK(kUseBakerReadBarrier);
6326
6327 // In slow path based read barriers, the read barrier call is
6328 // inserted after the original load. However, in fast path based
6329 // Baker's read barriers, we need to perform the load of
6330 // mirror::Object::monitor_ *before* the original reference load.
6331 // This load-load ordering is required by the read barrier.
6332 // The fast path/slow path (for Baker's algorithm) should look like:
6333 //
6334 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6335 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6336 // HeapReference<Object> ref = *src; // Original reference load.
6337 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6338 // if (is_gray) {
6339 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6340 // }
6341 //
6342 // Note: the original implementation in ReadBarrier::Barrier is
6343 // slightly more complex as:
6344 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006345 // the high-bits of rb_state, which are expected to be all zeroes
6346 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6347 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006348 // - it performs additional checks that we do not do here for
6349 // performance reasons.
6350
6351 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006352 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6353
Vladimir Marko953437b2016-08-24 08:30:46 +00006354 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6355 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6356 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6357 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6358 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6359 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6360 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6361
6362 // if (rb_state == ReadBarrier::gray_ptr_)
6363 // ref = ReadBarrier::Mark(ref);
6364 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6365 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006366 if (needs_null_check) {
6367 MaybeRecordImplicitNullCheck(instruction);
6368 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006369
6370 // Load fence to prevent load-load reordering.
6371 // Note that this is a no-op, thanks to the x86-64 memory model.
6372 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6373
6374 // The actual reference load.
6375 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006376 __ movl(ref_reg, src); // Flags are unaffected.
6377
6378 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6379 // Slow path marking the object `ref` when it is gray.
6380 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6381 instruction, ref, /* unpoison */ true);
6382 AddSlowPath(slow_path);
6383
6384 // We have done the "if" of the gray bit check above, now branch based on the flags.
6385 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006386
6387 // Object* ref = ref_addr->AsMirrorPtr()
6388 __ MaybeUnpoisonHeapReference(ref_reg);
6389
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006390 __ Bind(slow_path->GetExitLabel());
6391}
6392
6393void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6394 Location out,
6395 Location ref,
6396 Location obj,
6397 uint32_t offset,
6398 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 DCHECK(kEmitCompilerReadBarrier);
6400
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006401 // Insert a slow path based read barrier *after* the reference load.
6402 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 // If heap poisoning is enabled, the unpoisoning of the loaded
6404 // reference will be carried out by the runtime within the slow
6405 // path.
6406 //
6407 // Note that `ref` currently does not get unpoisoned (when heap
6408 // poisoning is enabled), which is alright as the `ref` argument is
6409 // not used by the artReadBarrierSlow entry point.
6410 //
6411 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6412 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6413 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6414 AddSlowPath(slow_path);
6415
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 __ jmp(slow_path->GetEntryLabel());
6417 __ Bind(slow_path->GetExitLabel());
6418}
6419
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006420void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6421 Location out,
6422 Location ref,
6423 Location obj,
6424 uint32_t offset,
6425 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006427 // Baker's read barriers shall be handled by the fast path
6428 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6429 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 // If heap poisoning is enabled, unpoisoning will be taken care of
6431 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006432 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433 } else if (kPoisonHeapReferences) {
6434 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6435 }
6436}
6437
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006438void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6439 Location out,
6440 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441 DCHECK(kEmitCompilerReadBarrier);
6442
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006443 // Insert a slow path based read barrier *after* the GC root load.
6444 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 // Note that GC roots are not affected by heap poisoning, so we do
6446 // not need to do anything special for this here.
6447 SlowPathCode* slow_path =
6448 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6449 AddSlowPath(slow_path);
6450
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 __ jmp(slow_path->GetEntryLabel());
6452 __ Bind(slow_path->GetExitLabel());
6453}
6454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006455void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006456 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006457 LOG(FATAL) << "Unreachable";
6458}
6459
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006460void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006461 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006462 LOG(FATAL) << "Unreachable";
6463}
6464
Mark Mendellfe57faa2015-09-18 09:26:15 -04006465// Simple implementation of packed switch - generate cascaded compare/jumps.
6466void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6467 LocationSummary* locations =
6468 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6469 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006470 locations->AddTemp(Location::RequiresRegister());
6471 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006472}
6473
6474void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6475 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006476 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006477 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006478 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6479 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6480 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006481 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6482
6483 // Should we generate smaller inline compare/jumps?
6484 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6485 // Figure out the correct compare values and jump conditions.
6486 // Handle the first compare/branch as a special case because it might
6487 // jump to the default case.
6488 DCHECK_GT(num_entries, 2u);
6489 Condition first_condition;
6490 uint32_t index;
6491 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6492 if (lower_bound != 0) {
6493 first_condition = kLess;
6494 __ cmpl(value_reg_in, Immediate(lower_bound));
6495 __ j(first_condition, codegen_->GetLabelOf(default_block));
6496 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6497
6498 index = 1;
6499 } else {
6500 // Handle all the compare/jumps below.
6501 first_condition = kBelow;
6502 index = 0;
6503 }
6504
6505 // Handle the rest of the compare/jumps.
6506 for (; index + 1 < num_entries; index += 2) {
6507 int32_t compare_to_value = lower_bound + index + 1;
6508 __ cmpl(value_reg_in, Immediate(compare_to_value));
6509 // Jump to successors[index] if value < case_value[index].
6510 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6511 // Jump to successors[index + 1] if value == case_value[index + 1].
6512 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6513 }
6514
6515 if (index != num_entries) {
6516 // There are an odd number of entries. Handle the last one.
6517 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006518 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006519 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6520 }
6521
6522 // And the default for any other value.
6523 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6524 __ jmp(codegen_->GetLabelOf(default_block));
6525 }
6526 return;
6527 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006528
6529 // Remove the bias, if needed.
6530 Register value_reg_out = value_reg_in.AsRegister();
6531 if (lower_bound != 0) {
6532 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6533 value_reg_out = temp_reg.AsRegister();
6534 }
6535 CpuRegister value_reg(value_reg_out);
6536
6537 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006538 __ cmpl(value_reg, Immediate(num_entries - 1));
6539 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006540
Mark Mendell9c86b482015-09-18 13:36:07 -04006541 // We are in the range of the table.
6542 // Load the address of the jump table in the constant area.
6543 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006544
Mark Mendell9c86b482015-09-18 13:36:07 -04006545 // Load the (signed) offset from the jump table.
6546 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6547
6548 // Add the offset to the address of the table base.
6549 __ addq(temp_reg, base_reg);
6550
6551 // And jump.
6552 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006553}
6554
Aart Bikc5d47542016-01-27 17:00:35 -08006555void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6556 if (value == 0) {
6557 __ xorl(dest, dest);
6558 } else {
6559 __ movl(dest, Immediate(value));
6560 }
6561}
6562
Mark Mendell92e83bf2015-05-07 11:25:03 -04006563void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6564 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006565 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006566 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006567 } else if (IsUint<32>(value)) {
6568 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006569 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6570 } else {
6571 __ movq(dest, Immediate(value));
6572 }
6573}
6574
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006575void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6576 if (value == 0) {
6577 __ xorps(dest, dest);
6578 } else {
6579 __ movss(dest, LiteralInt32Address(value));
6580 }
6581}
6582
6583void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6584 if (value == 0) {
6585 __ xorpd(dest, dest);
6586 } else {
6587 __ movsd(dest, LiteralInt64Address(value));
6588 }
6589}
6590
6591void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6592 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6593}
6594
6595void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6596 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6597}
6598
Aart Bika19616e2016-02-01 18:57:58 -08006599void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6600 if (value == 0) {
6601 __ testl(dest, dest);
6602 } else {
6603 __ cmpl(dest, Immediate(value));
6604 }
6605}
6606
6607void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6608 if (IsInt<32>(value)) {
6609 if (value == 0) {
6610 __ testq(dest, dest);
6611 } else {
6612 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6613 }
6614 } else {
6615 // Value won't fit in an int.
6616 __ cmpq(dest, LiteralInt64Address(value));
6617 }
6618}
6619
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006620void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6621 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006622 GenerateIntCompare(lhs_reg, rhs);
6623}
6624
6625void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006626 if (rhs.IsConstant()) {
6627 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006628 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006629 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006630 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006631 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006632 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006633 }
6634}
6635
6636void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6637 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6638 if (rhs.IsConstant()) {
6639 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6640 Compare64BitValue(lhs_reg, value);
6641 } else if (rhs.IsDoubleStackSlot()) {
6642 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6643 } else {
6644 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6645 }
6646}
6647
6648Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6649 Location index,
6650 ScaleFactor scale,
6651 uint32_t data_offset) {
6652 return index.IsConstant() ?
6653 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6654 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6655}
6656
Mark Mendellcfa410b2015-05-25 16:02:44 -04006657void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6658 DCHECK(dest.IsDoubleStackSlot());
6659 if (IsInt<32>(value)) {
6660 // Can move directly as an int32 constant.
6661 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6662 Immediate(static_cast<int32_t>(value)));
6663 } else {
6664 Load64BitValue(CpuRegister(TMP), value);
6665 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6666 }
6667}
6668
Mark Mendell9c86b482015-09-18 13:36:07 -04006669/**
6670 * Class to handle late fixup of offsets into constant area.
6671 */
6672class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6673 public:
6674 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6675 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6676
6677 protected:
6678 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6679
6680 CodeGeneratorX86_64* codegen_;
6681
6682 private:
6683 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6684 // Patch the correct offset for the instruction. We use the address of the
6685 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6686 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6687 int32_t relative_position = constant_offset - pos;
6688
6689 // Patch in the right value.
6690 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6691 }
6692
6693 // Location in constant area that the fixup refers to.
6694 size_t offset_into_constant_area_;
6695};
6696
6697/**
6698 t * Class to handle late fixup of offsets to a jump table that will be created in the
6699 * constant area.
6700 */
6701class JumpTableRIPFixup : public RIPFixup {
6702 public:
6703 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6704 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6705
6706 void CreateJumpTable() {
6707 X86_64Assembler* assembler = codegen_->GetAssembler();
6708
6709 // Ensure that the reference to the jump table has the correct offset.
6710 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6711 SetOffset(offset_in_constant_table);
6712
6713 // Compute the offset from the start of the function to this jump table.
6714 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6715
6716 // Populate the jump table with the correct values for the jump table.
6717 int32_t num_entries = switch_instr_->GetNumEntries();
6718 HBasicBlock* block = switch_instr_->GetBlock();
6719 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6720 // The value that we want is the target offset - the position of the table.
6721 for (int32_t i = 0; i < num_entries; i++) {
6722 HBasicBlock* b = successors[i];
6723 Label* l = codegen_->GetLabelOf(b);
6724 DCHECK(l->IsBound());
6725 int32_t offset_to_block = l->Position() - current_table_offset;
6726 assembler->AppendInt32(offset_to_block);
6727 }
6728 }
6729
6730 private:
6731 const HPackedSwitch* switch_instr_;
6732};
6733
Mark Mendellf55c3e02015-03-26 21:07:46 -04006734void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6735 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006736 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006737 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6738 // 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 -04006739 assembler->Align(4, 0);
6740 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006741
6742 // Populate any jump tables.
6743 for (auto jump_table : fixups_to_jump_tables_) {
6744 jump_table->CreateJumpTable();
6745 }
6746
6747 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006748 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006749 }
6750
6751 // And finish up.
6752 CodeGenerator::Finalize(allocator);
6753}
6754
Mark Mendellf55c3e02015-03-26 21:07:46 -04006755Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6756 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6757 return Address::RIP(fixup);
6758}
6759
6760Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6761 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6762 return Address::RIP(fixup);
6763}
6764
6765Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6766 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6767 return Address::RIP(fixup);
6768}
6769
6770Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6771 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6772 return Address::RIP(fixup);
6773}
6774
Andreas Gampe85b62f22015-09-09 13:15:38 -07006775// TODO: trg as memory.
6776void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6777 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006778 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006779 return;
6780 }
6781
6782 DCHECK_NE(type, Primitive::kPrimVoid);
6783
6784 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6785 if (trg.Equals(return_loc)) {
6786 return;
6787 }
6788
6789 // Let the parallel move resolver take care of all of this.
6790 HParallelMove parallel_move(GetGraph()->GetArena());
6791 parallel_move.AddMove(return_loc, trg, type, nullptr);
6792 GetMoveResolver()->EmitNativeCode(&parallel_move);
6793}
6794
Mark Mendell9c86b482015-09-18 13:36:07 -04006795Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6796 // Create a fixup to be used to create and address the jump table.
6797 JumpTableRIPFixup* table_fixup =
6798 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6799
6800 // We have to populate the jump tables.
6801 fixups_to_jump_tables_.push_back(table_fixup);
6802 return Address::RIP(table_fixup);
6803}
6804
Mark Mendellea5af682015-10-22 17:35:49 -04006805void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6806 const Address& addr_high,
6807 int64_t v,
6808 HInstruction* instruction) {
6809 if (IsInt<32>(v)) {
6810 int32_t v_32 = v;
6811 __ movq(addr_low, Immediate(v_32));
6812 MaybeRecordImplicitNullCheck(instruction);
6813 } else {
6814 // Didn't fit in a register. Do it in pieces.
6815 int32_t low_v = Low32Bits(v);
6816 int32_t high_v = High32Bits(v);
6817 __ movl(addr_low, Immediate(low_v));
6818 MaybeRecordImplicitNullCheck(instruction);
6819 __ movl(addr_high, Immediate(high_v));
6820 }
6821}
6822
Roland Levillain4d027112015-07-01 15:41:14 +01006823#undef __
6824
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006825} // namespace x86_64
6826} // namespace art