blob: 1f0d648438718f7a302a291c71eff3c8968b2454 [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
Vladimir Markoaad75c62016-10-03 08:46:48 +0000302 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100303 // Custom calling convention: RAX serves as both input and output.
304 __ movl(CpuRegister(RAX), Immediate(string_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000305 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 Levillain6b577cf2016-10-13 16:51:08 +0100460 CpuRegister cpu_reg = obj_.AsRegister<CpuRegister>();
461 Register reg = cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000462 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100463 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000464 DCHECK(instruction_->IsInstanceFieldGet() ||
465 instruction_->IsStaticFieldGet() ||
466 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100467 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000468 instruction_->IsLoadClass() ||
469 instruction_->IsLoadString() ||
470 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100471 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100472 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
473 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000474 << "Unexpected instruction in read barrier marking slow path: "
475 << instruction_->DebugName();
476
477 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000478 if (unpoison_) {
479 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillain6b577cf2016-10-13 16:51:08 +0100480 __ MaybeUnpoisonHeapReference(cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000481 }
Roland Levillain4359e612016-07-20 11:32:19 +0100482 // No need to save live registers; it's taken care of by the
483 // entrypoint. Also, there is no need to update the stack mask,
484 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000485 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100486 DCHECK_NE(reg, RSP);
487 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
488 // "Compact" slow path, saving two moves.
489 //
490 // Instead of using the standard runtime calling convention (input
491 // and output in R0):
492 //
493 // RDI <- obj
494 // RAX <- ReadBarrierMark(RDI)
495 // obj <- RAX
496 //
497 // we just use rX (the register holding `obj`) as input and output
498 // of a dedicated entrypoint:
499 //
500 // rX <- ReadBarrierMarkRegX(rX)
501 //
502 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700503 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100504 // This runtime call does not require a stack map.
505 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000506 __ jmp(GetExitLabel());
507 }
508
509 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000510 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000511 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000512
513 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
514};
515
Roland Levillain0d5a2812015-11-13 10:07:31 +0000516// Slow path generating a read barrier for a heap reference.
517class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
518 public:
519 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
520 Location out,
521 Location ref,
522 Location obj,
523 uint32_t offset,
524 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000525 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000526 out_(out),
527 ref_(ref),
528 obj_(obj),
529 offset_(offset),
530 index_(index) {
531 DCHECK(kEmitCompilerReadBarrier);
532 // If `obj` is equal to `out` or `ref`, it means the initial
533 // object has been overwritten by (or after) the heap object
534 // reference load to be instrumented, e.g.:
535 //
536 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000537 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000538 //
539 // In that case, we have lost the information about the original
540 // object, and the emitted read barrier cannot work properly.
541 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
542 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
543}
544
545 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
546 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
547 LocationSummary* locations = instruction_->GetLocations();
548 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
549 DCHECK(locations->CanCall());
550 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100551 DCHECK(instruction_->IsInstanceFieldGet() ||
552 instruction_->IsStaticFieldGet() ||
553 instruction_->IsArrayGet() ||
554 instruction_->IsInstanceOf() ||
555 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100556 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000557 << "Unexpected instruction in read barrier for heap reference slow path: "
558 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000559
560 __ Bind(GetEntryLabel());
561 SaveLiveRegisters(codegen, locations);
562
563 // We may have to change the index's value, but as `index_` is a
564 // constant member (like other "inputs" of this slow path),
565 // introduce a copy of it, `index`.
566 Location index = index_;
567 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100568 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000569 if (instruction_->IsArrayGet()) {
570 // Compute real offset and store it in index_.
571 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
572 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
573 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
574 // We are about to change the value of `index_reg` (see the
575 // calls to art::x86_64::X86_64Assembler::shll and
576 // art::x86_64::X86_64Assembler::AddImmediate below), but it
577 // has not been saved by the previous call to
578 // art::SlowPathCode::SaveLiveRegisters, as it is a
579 // callee-save register --
580 // art::SlowPathCode::SaveLiveRegisters does not consider
581 // callee-save registers, as it has been designed with the
582 // assumption that callee-save registers are supposed to be
583 // handled by the called function. So, as a callee-save
584 // register, `index_reg` _would_ eventually be saved onto
585 // the stack, but it would be too late: we would have
586 // changed its value earlier. Therefore, we manually save
587 // it here into another freely available register,
588 // `free_reg`, chosen of course among the caller-save
589 // registers (as a callee-save `free_reg` register would
590 // exhibit the same problem).
591 //
592 // Note we could have requested a temporary register from
593 // the register allocator instead; but we prefer not to, as
594 // this is a slow path, and we know we can find a
595 // caller-save register that is available.
596 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
597 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
598 index_reg = free_reg;
599 index = Location::RegisterLocation(index_reg);
600 } else {
601 // The initial register stored in `index_` has already been
602 // saved in the call to art::SlowPathCode::SaveLiveRegisters
603 // (as it is not a callee-save register), so we can freely
604 // use it.
605 }
606 // Shifting the index value contained in `index_reg` by the
607 // scale factor (2) cannot overflow in practice, as the
608 // runtime is unable to allocate object arrays with a size
609 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
610 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
611 static_assert(
612 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
613 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
614 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
615 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100616 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
617 // intrinsics, `index_` is not shifted by a scale factor of 2
618 // (as in the case of ArrayGet), as it is actually an offset
619 // to an object field within an object.
620 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000621 DCHECK(instruction_->GetLocations()->Intrinsified());
622 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
623 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
624 << instruction_->AsInvoke()->GetIntrinsic();
625 DCHECK_EQ(offset_, 0U);
626 DCHECK(index_.IsRegister());
627 }
628 }
629
630 // We're moving two or three locations to locations that could
631 // overlap, so we need a parallel move resolver.
632 InvokeRuntimeCallingConvention calling_convention;
633 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
634 parallel_move.AddMove(ref_,
635 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
636 Primitive::kPrimNot,
637 nullptr);
638 parallel_move.AddMove(obj_,
639 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
640 Primitive::kPrimNot,
641 nullptr);
642 if (index.IsValid()) {
643 parallel_move.AddMove(index,
644 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
645 Primitive::kPrimInt,
646 nullptr);
647 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
648 } else {
649 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
650 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
651 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100652 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000653 instruction_,
654 instruction_->GetDexPc(),
655 this);
656 CheckEntrypointTypes<
657 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
658 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
659
660 RestoreLiveRegisters(codegen, locations);
661 __ jmp(GetExitLabel());
662 }
663
664 const char* GetDescription() const OVERRIDE {
665 return "ReadBarrierForHeapReferenceSlowPathX86_64";
666 }
667
668 private:
669 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
670 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
671 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
672 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
673 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
674 return static_cast<CpuRegister>(i);
675 }
676 }
677 // We shall never fail to find a free caller-save register, as
678 // there are more than two core caller-save registers on x86-64
679 // (meaning it is possible to find one which is different from
680 // `ref` and `obj`).
681 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
682 LOG(FATAL) << "Could not find a free caller-save register";
683 UNREACHABLE();
684 }
685
Roland Levillain0d5a2812015-11-13 10:07:31 +0000686 const Location out_;
687 const Location ref_;
688 const Location obj_;
689 const uint32_t offset_;
690 // An additional location containing an index to an array.
691 // Only used for HArrayGet and the UnsafeGetObject &
692 // UnsafeGetObjectVolatile intrinsics.
693 const Location index_;
694
695 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
696};
697
698// Slow path generating a read barrier for a GC root.
699class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
700 public:
701 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000702 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000703 DCHECK(kEmitCompilerReadBarrier);
704 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000705
706 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
707 LocationSummary* locations = instruction_->GetLocations();
708 DCHECK(locations->CanCall());
709 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000710 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
711 << "Unexpected instruction in read barrier for GC root slow path: "
712 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000713
714 __ Bind(GetEntryLabel());
715 SaveLiveRegisters(codegen, locations);
716
717 InvokeRuntimeCallingConvention calling_convention;
718 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
719 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100720 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721 instruction_,
722 instruction_->GetDexPc(),
723 this);
724 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
725 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
726
727 RestoreLiveRegisters(codegen, locations);
728 __ jmp(GetExitLabel());
729 }
730
731 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
732
733 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000734 const Location out_;
735 const Location root_;
736
737 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
738};
739
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100740#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100741// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
742#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100743
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700745 switch (cond) {
746 case kCondEQ: return kEqual;
747 case kCondNE: return kNotEqual;
748 case kCondLT: return kLess;
749 case kCondLE: return kLessEqual;
750 case kCondGT: return kGreater;
751 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700752 case kCondB: return kBelow;
753 case kCondBE: return kBelowEqual;
754 case kCondA: return kAbove;
755 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700756 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100757 LOG(FATAL) << "Unreachable";
758 UNREACHABLE();
759}
760
Aart Bike9f37602015-10-09 11:15:55 -0700761// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100762inline Condition X86_64FPCondition(IfCondition cond) {
763 switch (cond) {
764 case kCondEQ: return kEqual;
765 case kCondNE: return kNotEqual;
766 case kCondLT: return kBelow;
767 case kCondLE: return kBelowEqual;
768 case kCondGT: return kAbove;
769 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700770 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100771 };
772 LOG(FATAL) << "Unreachable";
773 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700774}
775
Vladimir Markodc151b22015-10-15 18:02:30 +0100776HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
777 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100778 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100779 switch (desired_dispatch_info.code_ptr_location) {
780 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
781 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
782 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
783 return HInvokeStaticOrDirect::DispatchInfo {
784 desired_dispatch_info.method_load_kind,
785 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
786 desired_dispatch_info.method_load_data,
787 0u
788 };
789 default:
790 return desired_dispatch_info;
791 }
792}
793
Serguei Katkov288c7a82016-05-16 11:53:15 +0600794Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
795 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800796 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000797 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
798 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100799 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000800 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100801 uint32_t offset =
802 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
803 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000804 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100805 }
Vladimir Marko58155012015-08-19 12:49:41 +0000806 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000807 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000808 break;
809 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
810 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
811 break;
812 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
813 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000814 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
815 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000816 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
817 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000818 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000819 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000820 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000821 // Bind a new fixup label at the end of the "movl" insn.
822 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100823 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000824 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000825 }
Vladimir Marko58155012015-08-19 12:49:41 +0000826 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000827 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000828 Register method_reg;
829 CpuRegister reg = temp.AsRegister<CpuRegister>();
830 if (current_method.IsRegister()) {
831 method_reg = current_method.AsRegister<Register>();
832 } else {
833 DCHECK(invoke->GetLocations()->Intrinsified());
834 DCHECK(!current_method.IsValid());
835 method_reg = reg.AsRegister();
836 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
837 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000838 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100839 __ movq(reg,
840 Address(CpuRegister(method_reg),
841 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100842 // temp = temp[index_in_cache];
843 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
844 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000845 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
846 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100847 }
Vladimir Marko58155012015-08-19 12:49:41 +0000848 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600849 return callee_method;
850}
851
852void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
853 Location temp) {
854 // All registers are assumed to be correctly set up.
855 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000856
857 switch (invoke->GetCodePtrLocation()) {
858 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
859 __ call(&frame_entry_label_);
860 break;
861 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000862 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
863 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000864 Label* label = &relative_call_patches_.back().label;
865 __ call(label); // Bind to the patch label, override at link time.
866 __ Bind(label); // Bind the label at the end of the "call" insn.
867 break;
868 }
869 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
870 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100871 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
872 LOG(FATAL) << "Unsupported";
873 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000874 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
875 // (callee_method + offset_of_quick_compiled_code)()
876 __ call(Address(callee_method.AsRegister<CpuRegister>(),
877 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700878 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000879 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000880 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800881
882 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800883}
884
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000885void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
886 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
887 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
888 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000889
890 // Use the calling convention instead of the location of the receiver, as
891 // intrinsics may have put the receiver in a different register. In the intrinsics
892 // slow path, the arguments have been moved to the right place, so here we are
893 // guaranteed that the receiver is the first register of the calling convention.
894 InvokeDexCallingConvention calling_convention;
895 Register receiver = calling_convention.GetRegisterAt(0);
896
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000897 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000898 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000899 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000900 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000901 // Instead of simply (possibly) unpoisoning `temp` here, we should
902 // emit a read barrier for the previous class reference load.
903 // However this is not required in practice, as this is an
904 // intermediate/temporary reference and because the current
905 // concurrent copying collector keeps the from-space memory
906 // intact/accessible until the end of the marking phase (the
907 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000908 __ MaybeUnpoisonHeapReference(temp);
909 // temp = temp->GetMethodAt(method_offset);
910 __ movq(temp, Address(temp, method_offset));
911 // call temp->GetEntryPoint();
912 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700913 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000914}
915
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000916void CodeGeneratorX86_64::RecordSimplePatch() {
917 if (GetCompilerOptions().GetIncludePatchInformation()) {
918 simple_patches_.emplace_back();
919 __ Bind(&simple_patches_.back());
920 }
921}
922
Vladimir Markoaad75c62016-10-03 08:46:48 +0000923void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
924 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000925 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
926 __ Bind(&string_patches_.back().label);
927}
928
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100929void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
930 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
931 __ Bind(&type_patches_.back().label);
932}
933
Vladimir Markoaad75c62016-10-03 08:46:48 +0000934Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
935 DCHECK(!GetCompilerOptions().IsBootImage());
936 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
937 return &string_patches_.back().label;
938}
939
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000940Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
941 uint32_t element_offset) {
942 // Add a patch entry and return the label.
943 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
944 return &pc_relative_dex_cache_patches_.back().label;
945}
946
Vladimir Markoaad75c62016-10-03 08:46:48 +0000947// The label points to the end of the "movl" or another instruction but the literal offset
948// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
949constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
950
951template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
952inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
953 const ArenaDeque<PatchInfo<Label>>& infos,
954 ArenaVector<LinkerPatch>* linker_patches) {
955 for (const PatchInfo<Label>& info : infos) {
956 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
957 linker_patches->push_back(
958 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
959 }
960}
961
Vladimir Marko58155012015-08-19 12:49:41 +0000962void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
963 DCHECK(linker_patches->empty());
964 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000965 method_patches_.size() +
966 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000967 pc_relative_dex_cache_patches_.size() +
968 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100969 string_patches_.size() +
970 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000971 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000972 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000973 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000974 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +0000975 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000976 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000977 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000978 linker_patches->push_back(
979 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +0000980 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000981 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
982 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000983 for (const Label& label : simple_patches_) {
984 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
985 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
986 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000987 if (!GetCompilerOptions().IsBootImage()) {
988 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
989 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000990 // These are always PC-relative, see GetSupportedLoadStringKind().
Vladimir Markoaad75c62016-10-03 08:46:48 +0000991 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000992 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000993 // These are always PC-relative, see GetSupportedLoadClassKind().
994 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +0000995}
996
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100997void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100998 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100999}
1000
1001void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001002 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001003}
1004
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001005size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1006 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1007 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001008}
1009
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001010size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1011 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1012 return kX86_64WordSize;
1013}
1014
1015size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1016 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1017 return kX86_64WordSize;
1018}
1019
1020size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1021 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1022 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001023}
1024
Calin Juravle175dc732015-08-25 15:42:32 +01001025void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1026 HInstruction* instruction,
1027 uint32_t dex_pc,
1028 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001029 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001030 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1031 if (EntrypointRequiresStackMap(entrypoint)) {
1032 RecordPcInfo(instruction, dex_pc, slow_path);
1033 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001034}
1035
Roland Levillaindec8f632016-07-22 17:10:06 +01001036void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1037 HInstruction* instruction,
1038 SlowPathCode* slow_path) {
1039 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001040 GenerateInvokeRuntime(entry_point_offset);
1041}
1042
1043void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001044 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1045}
1046
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001047static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001048// Use a fake return address register to mimic Quick.
1049static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001050CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001051 const X86_64InstructionSetFeatures& isa_features,
1052 const CompilerOptions& compiler_options,
1053 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001054 : CodeGenerator(graph,
1055 kNumberOfCpuRegisters,
1056 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001058 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1059 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001060 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001061 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1062 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001063 compiler_options,
1064 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001065 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001066 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001067 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001068 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001069 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001070 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001071 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001072 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1073 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001074 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001075 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1076 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001077 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001078 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001079 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1080}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001082InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1083 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001084 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001085 assembler_(codegen->GetAssembler()),
1086 codegen_(codegen) {}
1087
David Brazdil58282f42016-01-14 12:45:10 +00001088void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001089 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001090 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001092 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001093 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094}
1095
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001096static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001097 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001098}
David Srbecky9d8606d2015-04-12 09:35:32 +01001099
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001100static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001101 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001102}
1103
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001105 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001106 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001107 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001108 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001109 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001110
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001111 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001112 __ testq(CpuRegister(RAX), Address(
1113 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001115 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001116
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001117 if (HasEmptyFrame()) {
1118 return;
1119 }
1120
Nicolas Geoffray98893962015-01-21 12:32:32 +00001121 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001122 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001123 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001124 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001125 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1126 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001127 }
1128 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001129
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001130 int adjust = GetFrameSize() - GetCoreSpillSize();
1131 __ subq(CpuRegister(RSP), Immediate(adjust));
1132 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001133 uint32_t xmm_spill_location = GetFpuSpillStart();
1134 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001135
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001136 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1137 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001138 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1139 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1140 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001141 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001142 }
1143
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001144 // Save the current method if we need it. Note that we do not
1145 // do this in HCurrentMethod, as the instruction might have been removed
1146 // in the SSA graph.
1147 if (RequiresCurrentMethod()) {
1148 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1149 CpuRegister(kMethodRegisterArgument));
1150 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151}
1152
1153void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001154 __ cfi().RememberState();
1155 if (!HasEmptyFrame()) {
1156 uint32_t xmm_spill_location = GetFpuSpillStart();
1157 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1158 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1159 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1160 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1161 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1162 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1163 }
1164 }
1165
1166 int adjust = GetFrameSize() - GetCoreSpillSize();
1167 __ addq(CpuRegister(RSP), Immediate(adjust));
1168 __ cfi().AdjustCFAOffset(-adjust);
1169
1170 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1171 Register reg = kCoreCalleeSaves[i];
1172 if (allocated_registers_.ContainsCoreRegister(reg)) {
1173 __ popq(CpuRegister(reg));
1174 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1175 __ cfi().Restore(DWARFReg(reg));
1176 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001177 }
1178 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001179 __ ret();
1180 __ cfi().RestoreState();
1181 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182}
1183
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001184void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1185 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001186}
1187
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188void CodeGeneratorX86_64::Move(Location destination, Location source) {
1189 if (source.Equals(destination)) {
1190 return;
1191 }
1192 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001193 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001194 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001195 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001197 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001198 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001199 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1200 } else if (source.IsConstant()) {
1201 HConstant* constant = source.GetConstant();
1202 if (constant->IsLongConstant()) {
1203 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1204 } else {
1205 Load32BitValue(dest, GetInt32ValueOf(constant));
1206 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207 } else {
1208 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001209 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 }
1211 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001212 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001214 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001216 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1217 } else if (source.IsConstant()) {
1218 HConstant* constant = source.GetConstant();
1219 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1220 if (constant->IsFloatConstant()) {
1221 Load32BitValue(dest, static_cast<int32_t>(value));
1222 } else {
1223 Load64BitValue(dest, value);
1224 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001226 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else {
1228 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001229 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230 }
1231 } else if (destination.IsStackSlot()) {
1232 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 } else if (source.IsFpuRegister()) {
1236 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001238 } else if (source.IsConstant()) {
1239 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001240 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001241 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001243 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001244 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1245 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001246 }
1247 } else {
1248 DCHECK(destination.IsDoubleStackSlot());
1249 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001250 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 } else if (source.IsFpuRegister()) {
1253 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001254 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001255 } else if (source.IsConstant()) {
1256 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001257 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1258 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001259 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 } else {
1261 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001262 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1263 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264 }
1265 }
1266}
1267
Calin Juravle175dc732015-08-25 15:42:32 +01001268void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1269 DCHECK(location.IsRegister());
1270 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1271}
1272
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273void CodeGeneratorX86_64::MoveLocation(
1274 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1275 Move(dst, src);
1276}
1277
1278void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1279 if (location.IsRegister()) {
1280 locations->AddTemp(location);
1281 } else {
1282 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1283 }
1284}
1285
David Brazdilfc6a86a2015-06-26 10:33:45 +00001286void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001287 DCHECK(!successor->IsExitBlock());
1288
1289 HBasicBlock* block = got->GetBlock();
1290 HInstruction* previous = got->GetPrevious();
1291
1292 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001293 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001294 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1295 return;
1296 }
1297
1298 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1299 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1300 }
1301 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001302 __ jmp(codegen_->GetLabelOf(successor));
1303 }
1304}
1305
David Brazdilfc6a86a2015-06-26 10:33:45 +00001306void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1307 got->SetLocations(nullptr);
1308}
1309
1310void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1311 HandleGoto(got, got->GetSuccessor());
1312}
1313
1314void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1315 try_boundary->SetLocations(nullptr);
1316}
1317
1318void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1319 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1320 if (!successor->IsExitBlock()) {
1321 HandleGoto(try_boundary, successor);
1322 }
1323}
1324
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001325void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1326 exit->SetLocations(nullptr);
1327}
1328
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001329void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001330}
1331
Mark Mendell152408f2015-12-31 12:28:50 -05001332template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001333void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001334 LabelType* true_label,
1335 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001336 if (cond->IsFPConditionTrueIfNaN()) {
1337 __ j(kUnordered, true_label);
1338 } else if (cond->IsFPConditionFalseIfNaN()) {
1339 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001340 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001342}
1343
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001344void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001345 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001346
Mark Mendellc4701932015-04-10 13:18:51 -04001347 Location left = locations->InAt(0);
1348 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001349 Primitive::Type type = condition->InputAt(0)->GetType();
1350 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001351 case Primitive::kPrimBoolean:
1352 case Primitive::kPrimByte:
1353 case Primitive::kPrimChar:
1354 case Primitive::kPrimShort:
1355 case Primitive::kPrimInt:
1356 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001357 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001358 break;
1359 }
Mark Mendellc4701932015-04-10 13:18:51 -04001360 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001361 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001362 break;
1363 }
1364 case Primitive::kPrimFloat: {
1365 if (right.IsFpuRegister()) {
1366 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1367 } else if (right.IsConstant()) {
1368 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1369 codegen_->LiteralFloatAddress(
1370 right.GetConstant()->AsFloatConstant()->GetValue()));
1371 } else {
1372 DCHECK(right.IsStackSlot());
1373 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1374 Address(CpuRegister(RSP), right.GetStackIndex()));
1375 }
Mark Mendellc4701932015-04-10 13:18:51 -04001376 break;
1377 }
1378 case Primitive::kPrimDouble: {
1379 if (right.IsFpuRegister()) {
1380 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1381 } else if (right.IsConstant()) {
1382 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1383 codegen_->LiteralDoubleAddress(
1384 right.GetConstant()->AsDoubleConstant()->GetValue()));
1385 } else {
1386 DCHECK(right.IsDoubleStackSlot());
1387 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1388 Address(CpuRegister(RSP), right.GetStackIndex()));
1389 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001390 break;
1391 }
1392 default:
1393 LOG(FATAL) << "Unexpected condition type " << type;
1394 }
1395}
1396
1397template<class LabelType>
1398void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1399 LabelType* true_target_in,
1400 LabelType* false_target_in) {
1401 // Generated branching requires both targets to be explicit. If either of the
1402 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1403 LabelType fallthrough_target;
1404 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1405 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1406
1407 // Generate the comparison to set the CC.
1408 GenerateCompareTest(condition);
1409
1410 // Now generate the correct jump(s).
1411 Primitive::Type type = condition->InputAt(0)->GetType();
1412 switch (type) {
1413 case Primitive::kPrimLong: {
1414 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1415 break;
1416 }
1417 case Primitive::kPrimFloat: {
1418 GenerateFPJumps(condition, true_target, false_target);
1419 break;
1420 }
1421 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001422 GenerateFPJumps(condition, true_target, false_target);
1423 break;
1424 }
1425 default:
1426 LOG(FATAL) << "Unexpected condition type " << type;
1427 }
1428
David Brazdil0debae72015-11-12 18:37:00 +00001429 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001430 __ jmp(false_target);
1431 }
David Brazdil0debae72015-11-12 18:37:00 +00001432
1433 if (fallthrough_target.IsLinked()) {
1434 __ Bind(&fallthrough_target);
1435 }
Mark Mendellc4701932015-04-10 13:18:51 -04001436}
1437
David Brazdil0debae72015-11-12 18:37:00 +00001438static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1439 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1440 // are set only strictly before `branch`. We can't use the eflags on long
1441 // conditions if they are materialized due to the complex branching.
1442 return cond->IsCondition() &&
1443 cond->GetNext() == branch &&
1444 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1445}
1446
Mark Mendell152408f2015-12-31 12:28:50 -05001447template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001448void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001449 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001450 LabelType* true_target,
1451 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001452 HInstruction* cond = instruction->InputAt(condition_input_index);
1453
1454 if (true_target == nullptr && false_target == nullptr) {
1455 // Nothing to do. The code always falls through.
1456 return;
1457 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001458 // Constant condition, statically compared against "true" (integer value 1).
1459 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001460 if (true_target != nullptr) {
1461 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001462 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001463 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001464 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001465 if (false_target != nullptr) {
1466 __ jmp(false_target);
1467 }
1468 }
1469 return;
1470 }
1471
1472 // The following code generates these patterns:
1473 // (1) true_target == nullptr && false_target != nullptr
1474 // - opposite condition true => branch to false_target
1475 // (2) true_target != nullptr && false_target == nullptr
1476 // - condition true => branch to true_target
1477 // (3) true_target != nullptr && false_target != nullptr
1478 // - condition true => branch to true_target
1479 // - branch to false_target
1480 if (IsBooleanValueOrMaterializedCondition(cond)) {
1481 if (AreEflagsSetFrom(cond, instruction)) {
1482 if (true_target == nullptr) {
1483 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1484 } else {
1485 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1486 }
1487 } else {
1488 // Materialized condition, compare against 0.
1489 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1490 if (lhs.IsRegister()) {
1491 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1492 } else {
1493 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1494 }
1495 if (true_target == nullptr) {
1496 __ j(kEqual, false_target);
1497 } else {
1498 __ j(kNotEqual, true_target);
1499 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001500 }
1501 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001502 // Condition has not been materialized, use its inputs as the
1503 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001504 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001505
David Brazdil0debae72015-11-12 18:37:00 +00001506 // If this is a long or FP comparison that has been folded into
1507 // the HCondition, generate the comparison directly.
1508 Primitive::Type type = condition->InputAt(0)->GetType();
1509 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1510 GenerateCompareTestAndBranch(condition, true_target, false_target);
1511 return;
1512 }
1513
1514 Location lhs = condition->GetLocations()->InAt(0);
1515 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001516 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001517 if (true_target == nullptr) {
1518 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1519 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001520 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001521 }
Dave Allison20dfc792014-06-16 20:44:29 -07001522 }
David Brazdil0debae72015-11-12 18:37:00 +00001523
1524 // If neither branch falls through (case 3), the conditional branch to `true_target`
1525 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1526 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001527 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001528 }
1529}
1530
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001531void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1533 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001534 locations->SetInAt(0, Location::Any());
1535 }
1536}
1537
1538void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001539 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1540 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1541 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1542 nullptr : codegen_->GetLabelOf(true_successor);
1543 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1544 nullptr : codegen_->GetLabelOf(false_successor);
1545 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001546}
1547
1548void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1549 LocationSummary* locations = new (GetGraph()->GetArena())
1550 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001551 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001552 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001553 locations->SetInAt(0, Location::Any());
1554 }
1555}
1556
1557void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001558 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001559 GenerateTestAndBranch<Label>(deoptimize,
1560 /* condition_input_index */ 0,
1561 slow_path->GetEntryLabel(),
1562 /* false_target */ nullptr);
1563}
1564
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001565static bool SelectCanUseCMOV(HSelect* select) {
1566 // There are no conditional move instructions for XMMs.
1567 if (Primitive::IsFloatingPointType(select->GetType())) {
1568 return false;
1569 }
1570
1571 // A FP condition doesn't generate the single CC that we need.
1572 HInstruction* condition = select->GetCondition();
1573 if (condition->IsCondition() &&
1574 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1575 return false;
1576 }
1577
1578 // We can generate a CMOV for this Select.
1579 return true;
1580}
1581
David Brazdil74eb1b22015-12-14 11:44:01 +00001582void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1584 if (Primitive::IsFloatingPointType(select->GetType())) {
1585 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001586 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001587 } else {
1588 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001589 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001590 if (select->InputAt(1)->IsConstant()) {
1591 locations->SetInAt(1, Location::RequiresRegister());
1592 } else {
1593 locations->SetInAt(1, Location::Any());
1594 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001595 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001596 locations->SetInAt(1, Location::Any());
1597 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001598 }
1599 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1600 locations->SetInAt(2, Location::RequiresRegister());
1601 }
1602 locations->SetOut(Location::SameAsFirstInput());
1603}
1604
1605void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1606 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001607 if (SelectCanUseCMOV(select)) {
1608 // If both the condition and the source types are integer, we can generate
1609 // a CMOV to implement Select.
1610 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001611 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001612 DCHECK(locations->InAt(0).Equals(locations->Out()));
1613
1614 HInstruction* select_condition = select->GetCondition();
1615 Condition cond = kNotEqual;
1616
1617 // Figure out how to test the 'condition'.
1618 if (select_condition->IsCondition()) {
1619 HCondition* condition = select_condition->AsCondition();
1620 if (!condition->IsEmittedAtUseSite()) {
1621 // This was a previously materialized condition.
1622 // Can we use the existing condition code?
1623 if (AreEflagsSetFrom(condition, select)) {
1624 // Materialization was the previous instruction. Condition codes are right.
1625 cond = X86_64IntegerCondition(condition->GetCondition());
1626 } else {
1627 // No, we have to recreate the condition code.
1628 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1629 __ testl(cond_reg, cond_reg);
1630 }
1631 } else {
1632 GenerateCompareTest(condition);
1633 cond = X86_64IntegerCondition(condition->GetCondition());
1634 }
1635 } else {
1636 // Must be a boolean condition, which needs to be compared to 0.
1637 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1638 __ testl(cond_reg, cond_reg);
1639 }
1640
1641 // If the condition is true, overwrite the output, which already contains false.
1642 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001643 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1644 if (value_true_loc.IsRegister()) {
1645 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1646 } else {
1647 __ cmov(cond,
1648 value_false,
1649 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1650 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001651 } else {
1652 NearLabel false_target;
1653 GenerateTestAndBranch<NearLabel>(select,
1654 /* condition_input_index */ 2,
1655 /* true_target */ nullptr,
1656 &false_target);
1657 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1658 __ Bind(&false_target);
1659 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001660}
1661
David Srbecky0cf44932015-12-09 14:09:59 +00001662void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1663 new (GetGraph()->GetArena()) LocationSummary(info);
1664}
1665
David Srbeckyd28f4a02016-03-14 17:14:24 +00001666void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1667 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001668}
1669
1670void CodeGeneratorX86_64::GenerateNop() {
1671 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001672}
1673
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001675 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001676 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001677 // Handle the long/FP comparisons made in instruction simplification.
1678 switch (cond->InputAt(0)->GetType()) {
1679 case Primitive::kPrimLong:
1680 locations->SetInAt(0, Location::RequiresRegister());
1681 locations->SetInAt(1, Location::Any());
1682 break;
1683 case Primitive::kPrimFloat:
1684 case Primitive::kPrimDouble:
1685 locations->SetInAt(0, Location::RequiresFpuRegister());
1686 locations->SetInAt(1, Location::Any());
1687 break;
1688 default:
1689 locations->SetInAt(0, Location::RequiresRegister());
1690 locations->SetInAt(1, Location::Any());
1691 break;
1692 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001693 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001694 locations->SetOut(Location::RequiresRegister());
1695 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001696}
1697
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001699 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001700 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001701 }
Mark Mendellc4701932015-04-10 13:18:51 -04001702
1703 LocationSummary* locations = cond->GetLocations();
1704 Location lhs = locations->InAt(0);
1705 Location rhs = locations->InAt(1);
1706 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001707 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001708
1709 switch (cond->InputAt(0)->GetType()) {
1710 default:
1711 // Integer case.
1712
1713 // Clear output register: setcc only sets the low byte.
1714 __ xorl(reg, reg);
1715
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001716 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001717 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001718 return;
1719 case Primitive::kPrimLong:
1720 // Clear output register: setcc only sets the low byte.
1721 __ xorl(reg, reg);
1722
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001723 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001724 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001725 return;
1726 case Primitive::kPrimFloat: {
1727 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1728 if (rhs.IsConstant()) {
1729 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1730 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1731 } else if (rhs.IsStackSlot()) {
1732 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1733 } else {
1734 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1735 }
1736 GenerateFPJumps(cond, &true_label, &false_label);
1737 break;
1738 }
1739 case Primitive::kPrimDouble: {
1740 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1741 if (rhs.IsConstant()) {
1742 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1743 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1744 } else if (rhs.IsDoubleStackSlot()) {
1745 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1746 } else {
1747 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1748 }
1749 GenerateFPJumps(cond, &true_label, &false_label);
1750 break;
1751 }
1752 }
1753
1754 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001755 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001756
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001758 __ Bind(&false_label);
1759 __ xorl(reg, reg);
1760 __ jmp(&done_label);
1761
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001763 __ Bind(&true_label);
1764 __ movl(reg, Immediate(1));
1765 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001814}
1815
Aart Bike9f37602015-10-09 11:15:55 -07001816void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001818}
1819
1820void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
1844void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001848void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001851 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001852 case Primitive::kPrimBoolean:
1853 case Primitive::kPrimByte:
1854 case Primitive::kPrimShort:
1855 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001856 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001857 case Primitive::kPrimLong: {
1858 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001859 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1861 break;
1862 }
1863 case Primitive::kPrimFloat:
1864 case Primitive::kPrimDouble: {
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001866 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001867 locations->SetOut(Location::RequiresRegister());
1868 break;
1869 }
1870 default:
1871 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1872 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001873}
1874
1875void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001876 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001877 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001878 Location left = locations->InAt(0);
1879 Location right = locations->InAt(1);
1880
Mark Mendell0c9497d2015-08-21 09:30:05 -04001881 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001882 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001883 Condition less_cond = kLess;
1884
Calin Juravleddb7df22014-11-25 20:56:51 +00001885 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001886 case Primitive::kPrimBoolean:
1887 case Primitive::kPrimByte:
1888 case Primitive::kPrimShort:
1889 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001890 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001891 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08001892 break;
1893 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001894 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001895 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001896 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001897 }
1898 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001899 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1900 if (right.IsConstant()) {
1901 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1902 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1903 } else if (right.IsStackSlot()) {
1904 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1905 } else {
1906 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1907 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001908 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001909 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001910 break;
1911 }
1912 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001913 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1914 if (right.IsConstant()) {
1915 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1916 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1917 } else if (right.IsDoubleStackSlot()) {
1918 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1919 } else {
1920 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1921 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001922 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001923 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 break;
1925 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001926 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001927 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001928 }
Aart Bika19616e2016-02-01 18:57:58 -08001929
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001931 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001932 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001933
Calin Juravle91debbc2014-11-26 19:01:09 +00001934 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001935 __ movl(out, Immediate(1));
1936 __ jmp(&done);
1937
1938 __ Bind(&less);
1939 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001940
1941 __ Bind(&done);
1942}
1943
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001944void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001945 LocationSummary* locations =
1946 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001947 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001948}
1949
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001950void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001951 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001952}
1953
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001954void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1957 locations->SetOut(Location::ConstantLocation(constant));
1958}
1959
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001960void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001961 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001962}
1963
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 LocationSummary* locations =
1966 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001967 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001968}
1969
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001970void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001971 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972}
1973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001974void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1975 LocationSummary* locations =
1976 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1977 locations->SetOut(Location::ConstantLocation(constant));
1978}
1979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001980void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001981 // Will be generated at use site.
1982}
1983
1984void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1985 LocationSummary* locations =
1986 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1987 locations->SetOut(Location::ConstantLocation(constant));
1988}
1989
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001990void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1991 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001992 // Will be generated at use site.
1993}
1994
Calin Juravle27df7582015-04-17 19:12:31 +01001995void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1996 memory_barrier->SetLocations(nullptr);
1997}
1998
1999void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002000 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002001}
2002
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002003void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2004 ret->SetLocations(nullptr);
2005}
2006
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002007void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002009}
2010
2011void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002012 LocationSummary* locations =
2013 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014 switch (ret->InputAt(0)->GetType()) {
2015 case Primitive::kPrimBoolean:
2016 case Primitive::kPrimByte:
2017 case Primitive::kPrimChar:
2018 case Primitive::kPrimShort:
2019 case Primitive::kPrimInt:
2020 case Primitive::kPrimNot:
2021 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002022 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002023 break;
2024
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002025 case Primitive::kPrimFloat:
2026 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002027 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002028 break;
2029
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002031 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002032 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002033}
2034
2035void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2036 if (kIsDebugBuild) {
2037 switch (ret->InputAt(0)->GetType()) {
2038 case Primitive::kPrimBoolean:
2039 case Primitive::kPrimByte:
2040 case Primitive::kPrimChar:
2041 case Primitive::kPrimShort:
2042 case Primitive::kPrimInt:
2043 case Primitive::kPrimNot:
2044 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002046 break;
2047
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002048 case Primitive::kPrimFloat:
2049 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002051 XMM0);
2052 break;
2053
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002054 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002055 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002056 }
2057 }
2058 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059}
2060
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002061Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2062 switch (type) {
2063 case Primitive::kPrimBoolean:
2064 case Primitive::kPrimByte:
2065 case Primitive::kPrimChar:
2066 case Primitive::kPrimShort:
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimNot:
2069 case Primitive::kPrimLong:
2070 return Location::RegisterLocation(RAX);
2071
2072 case Primitive::kPrimVoid:
2073 return Location::NoLocation();
2074
2075 case Primitive::kPrimDouble:
2076 case Primitive::kPrimFloat:
2077 return Location::FpuRegisterLocation(XMM0);
2078 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002079
2080 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002081}
2082
2083Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2084 return Location::RegisterLocation(kMethodRegisterArgument);
2085}
2086
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002087Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002088 switch (type) {
2089 case Primitive::kPrimBoolean:
2090 case Primitive::kPrimByte:
2091 case Primitive::kPrimChar:
2092 case Primitive::kPrimShort:
2093 case Primitive::kPrimInt:
2094 case Primitive::kPrimNot: {
2095 uint32_t index = gp_index_++;
2096 stack_index_++;
2097 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002099 } else {
2100 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2101 }
2102 }
2103
2104 case Primitive::kPrimLong: {
2105 uint32_t index = gp_index_;
2106 stack_index_ += 2;
2107 if (index < calling_convention.GetNumberOfRegisters()) {
2108 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002109 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002110 } else {
2111 gp_index_ += 2;
2112 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2113 }
2114 }
2115
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002116 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002117 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 stack_index_++;
2119 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002120 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 } else {
2122 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2123 }
2124 }
2125
2126 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002127 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002128 stack_index_ += 2;
2129 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 } else {
2132 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2133 }
2134 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002135
2136 case Primitive::kPrimVoid:
2137 LOG(FATAL) << "Unexpected parameter type " << type;
2138 break;
2139 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002140 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002141}
2142
Calin Juravle175dc732015-08-25 15:42:32 +01002143void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2144 // The trampoline uses the same calling convention as dex calling conventions,
2145 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2146 // the method_idx.
2147 HandleInvoke(invoke);
2148}
2149
2150void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2151 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2152}
2153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002154void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002155 // Explicit clinit checks triggered by static invokes must have been pruned by
2156 // art::PrepareForRegisterAllocation.
2157 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002158
Mark Mendellfb8d2792015-03-31 22:16:59 -04002159 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002160 if (intrinsic.TryDispatch(invoke)) {
2161 return;
2162 }
2163
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002164 HandleInvoke(invoke);
2165}
2166
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002167static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2168 if (invoke->GetLocations()->Intrinsified()) {
2169 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2170 intrinsic.Dispatch(invoke);
2171 return true;
2172 }
2173 return false;
2174}
2175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002176void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002177 // Explicit clinit checks triggered by static invokes must have been pruned by
2178 // art::PrepareForRegisterAllocation.
2179 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002180
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002181 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2182 return;
2183 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002184
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002185 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002186 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002187 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002188 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002189}
2190
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002191void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002192 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002193 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194}
2195
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002196void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002197 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002198 if (intrinsic.TryDispatch(invoke)) {
2199 return;
2200 }
2201
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002202 HandleInvoke(invoke);
2203}
2204
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002205void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002206 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2207 return;
2208 }
2209
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002210 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002211 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002212 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213}
2214
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2216 HandleInvoke(invoke);
2217 // Add the hidden argument.
2218 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2219}
2220
2221void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2222 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002223 LocationSummary* locations = invoke->GetLocations();
2224 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2225 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002226 Location receiver = locations->InAt(0);
2227 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2228
Roland Levillain0d5a2812015-11-13 10:07:31 +00002229 // Set the hidden argument. This is safe to do this here, as RAX
2230 // won't be modified thereafter, before the `call` instruction.
2231 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002232 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234 if (receiver.IsStackSlot()) {
2235 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002236 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 __ movl(temp, Address(temp, class_offset));
2238 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002239 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002241 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002242 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002243 // Instead of simply (possibly) unpoisoning `temp` here, we should
2244 // emit a read barrier for the previous class reference load.
2245 // However this is not required in practice, as this is an
2246 // intermediate/temporary reference and because the current
2247 // concurrent copying collector keeps the from-space memory
2248 // intact/accessible until the end of the marking phase (the
2249 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002250 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002251 // temp = temp->GetAddressOfIMT()
2252 __ movq(temp,
2253 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2254 // temp = temp->GetImtEntryAt(method_offset);
2255 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002256 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002258 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002260 __ call(Address(
2261 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262
2263 DCHECK(!codegen_->IsLeafMethod());
2264 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2265}
2266
Roland Levillain88cb1752014-10-20 16:36:47 +01002267void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2270 switch (neg->GetResultType()) {
2271 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002272 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002273 locations->SetInAt(0, Location::RequiresRegister());
2274 locations->SetOut(Location::SameAsFirstInput());
2275 break;
2276
Roland Levillain88cb1752014-10-20 16:36:47 +01002277 case Primitive::kPrimFloat:
2278 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002279 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002280 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002281 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002282 break;
2283
2284 default:
2285 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2286 }
2287}
2288
2289void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2290 LocationSummary* locations = neg->GetLocations();
2291 Location out = locations->Out();
2292 Location in = locations->InAt(0);
2293 switch (neg->GetResultType()) {
2294 case Primitive::kPrimInt:
2295 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002296 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002298 break;
2299
2300 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002301 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002302 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002304 break;
2305
Roland Levillain5368c212014-11-27 15:03:41 +00002306 case Primitive::kPrimFloat: {
2307 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002308 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002309 // Implement float negation with an exclusive or with value
2310 // 0x80000000 (mask for bit 31, representing the sign of a
2311 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002312 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002314 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002315 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002316
Roland Levillain5368c212014-11-27 15:03:41 +00002317 case Primitive::kPrimDouble: {
2318 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002319 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002320 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002321 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002322 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002323 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002324 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002325 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002326 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002327
2328 default:
2329 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2330 }
2331}
2332
Roland Levillaindff1f282014-11-05 14:15:05 +00002333void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2334 LocationSummary* locations =
2335 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2336 Primitive::Type result_type = conversion->GetResultType();
2337 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002338 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002339
David Brazdilb2bd1c52015-03-25 11:17:37 +00002340 // The Java language does not allow treating boolean as an integral type but
2341 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002342
Roland Levillaindff1f282014-11-05 14:15:05 +00002343 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002344 case Primitive::kPrimByte:
2345 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002346 case Primitive::kPrimLong:
2347 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002348 case Primitive::kPrimBoolean:
2349 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002350 case Primitive::kPrimShort:
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002353 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002354 locations->SetInAt(0, Location::Any());
2355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 }
2362 break;
2363
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimShort:
2365 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002366 case Primitive::kPrimLong:
2367 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002368 case Primitive::kPrimBoolean:
2369 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002370 case Primitive::kPrimByte:
2371 case Primitive::kPrimInt:
2372 case Primitive::kPrimChar:
2373 // Processing a Dex `int-to-short' instruction.
2374 locations->SetInAt(0, Location::Any());
2375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2376 break;
2377
2378 default:
2379 LOG(FATAL) << "Unexpected type conversion from " << input_type
2380 << " to " << result_type;
2381 }
2382 break;
2383
Roland Levillain946e1432014-11-11 17:35:19 +00002384 case Primitive::kPrimInt:
2385 switch (input_type) {
2386 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002387 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002388 locations->SetInAt(0, Location::Any());
2389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2390 break;
2391
2392 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002393 // Processing a Dex `float-to-int' instruction.
2394 locations->SetInAt(0, Location::RequiresFpuRegister());
2395 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002396 break;
2397
Roland Levillain946e1432014-11-11 17:35:19 +00002398 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002399 // Processing a Dex `double-to-int' instruction.
2400 locations->SetInAt(0, Location::RequiresFpuRegister());
2401 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002402 break;
2403
2404 default:
2405 LOG(FATAL) << "Unexpected type conversion from " << input_type
2406 << " to " << result_type;
2407 }
2408 break;
2409
Roland Levillaindff1f282014-11-05 14:15:05 +00002410 case Primitive::kPrimLong:
2411 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002412 case Primitive::kPrimBoolean:
2413 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002414 case Primitive::kPrimByte:
2415 case Primitive::kPrimShort:
2416 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002417 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002418 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002419 // TODO: We would benefit from a (to-be-implemented)
2420 // Location::RegisterOrStackSlot requirement for this input.
2421 locations->SetInAt(0, Location::RequiresRegister());
2422 locations->SetOut(Location::RequiresRegister());
2423 break;
2424
2425 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002426 // Processing a Dex `float-to-long' instruction.
2427 locations->SetInAt(0, Location::RequiresFpuRegister());
2428 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002429 break;
2430
Roland Levillaindff1f282014-11-05 14:15:05 +00002431 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002432 // Processing a Dex `double-to-long' instruction.
2433 locations->SetInAt(0, Location::RequiresFpuRegister());
2434 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002435 break;
2436
2437 default:
2438 LOG(FATAL) << "Unexpected type conversion from " << input_type
2439 << " to " << result_type;
2440 }
2441 break;
2442
Roland Levillain981e4542014-11-14 11:47:14 +00002443 case Primitive::kPrimChar:
2444 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002445 case Primitive::kPrimLong:
2446 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002447 case Primitive::kPrimBoolean:
2448 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002449 case Primitive::kPrimByte:
2450 case Primitive::kPrimShort:
2451 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `int-to-char' instruction.
2453 locations->SetInAt(0, Location::Any());
2454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2455 break;
2456
2457 default:
2458 LOG(FATAL) << "Unexpected type conversion from " << input_type
2459 << " to " << result_type;
2460 }
2461 break;
2462
Roland Levillaindff1f282014-11-05 14:15:05 +00002463 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002464 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002465 case Primitive::kPrimBoolean:
2466 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002467 case Primitive::kPrimByte:
2468 case Primitive::kPrimShort:
2469 case Primitive::kPrimInt:
2470 case Primitive::kPrimChar:
2471 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002472 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002473 locations->SetOut(Location::RequiresFpuRegister());
2474 break;
2475
2476 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002477 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002478 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002479 locations->SetOut(Location::RequiresFpuRegister());
2480 break;
2481
Roland Levillaincff13742014-11-17 14:32:17 +00002482 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002483 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002484 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002485 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002486 break;
2487
2488 default:
2489 LOG(FATAL) << "Unexpected type conversion from " << input_type
2490 << " to " << result_type;
2491 };
2492 break;
2493
Roland Levillaindff1f282014-11-05 14:15:05 +00002494 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002495 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002496 case Primitive::kPrimBoolean:
2497 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002498 case Primitive::kPrimByte:
2499 case Primitive::kPrimShort:
2500 case Primitive::kPrimInt:
2501 case Primitive::kPrimChar:
2502 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002503 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002504 locations->SetOut(Location::RequiresFpuRegister());
2505 break;
2506
2507 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002508 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002509 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002510 locations->SetOut(Location::RequiresFpuRegister());
2511 break;
2512
Roland Levillaincff13742014-11-17 14:32:17 +00002513 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002514 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002515 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002516 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002517 break;
2518
2519 default:
2520 LOG(FATAL) << "Unexpected type conversion from " << input_type
2521 << " to " << result_type;
2522 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 break;
2524
2525 default:
2526 LOG(FATAL) << "Unexpected type conversion from " << input_type
2527 << " to " << result_type;
2528 }
2529}
2530
2531void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2532 LocationSummary* locations = conversion->GetLocations();
2533 Location out = locations->Out();
2534 Location in = locations->InAt(0);
2535 Primitive::Type result_type = conversion->GetResultType();
2536 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002537 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002539 case Primitive::kPrimByte:
2540 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002541 case Primitive::kPrimLong:
2542 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002543 case Primitive::kPrimBoolean:
2544 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002545 case Primitive::kPrimShort:
2546 case Primitive::kPrimInt:
2547 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002548 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002549 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002550 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002551 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002552 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002553 Address(CpuRegister(RSP), in.GetStackIndex()));
2554 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002555 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002556 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002557 }
2558 break;
2559
2560 default:
2561 LOG(FATAL) << "Unexpected type conversion from " << input_type
2562 << " to " << result_type;
2563 }
2564 break;
2565
Roland Levillain01a8d712014-11-14 16:27:39 +00002566 case Primitive::kPrimShort:
2567 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002568 case Primitive::kPrimLong:
2569 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002570 case Primitive::kPrimBoolean:
2571 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002572 case Primitive::kPrimByte:
2573 case Primitive::kPrimInt:
2574 case Primitive::kPrimChar:
2575 // Processing a Dex `int-to-short' instruction.
2576 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002577 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002578 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002580 Address(CpuRegister(RSP), in.GetStackIndex()));
2581 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002583 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002584 }
2585 break;
2586
2587 default:
2588 LOG(FATAL) << "Unexpected type conversion from " << input_type
2589 << " to " << result_type;
2590 }
2591 break;
2592
Roland Levillain946e1432014-11-11 17:35:19 +00002593 case Primitive::kPrimInt:
2594 switch (input_type) {
2595 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002596 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002597 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002598 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002599 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002601 Address(CpuRegister(RSP), in.GetStackIndex()));
2602 } else {
2603 DCHECK(in.IsConstant());
2604 DCHECK(in.GetConstant()->IsLongConstant());
2605 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002606 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002607 }
2608 break;
2609
Roland Levillain3f8f9362014-12-02 17:45:01 +00002610 case Primitive::kPrimFloat: {
2611 // Processing a Dex `float-to-int' instruction.
2612 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2613 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002614 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002615
2616 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002617 // if input >= (float)INT_MAX goto done
2618 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002619 __ j(kAboveEqual, &done);
2620 // if input == NaN goto nan
2621 __ j(kUnordered, &nan);
2622 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002623 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002624 __ jmp(&done);
2625 __ Bind(&nan);
2626 // output = 0
2627 __ xorl(output, output);
2628 __ Bind(&done);
2629 break;
2630 }
2631
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002632 case Primitive::kPrimDouble: {
2633 // Processing a Dex `double-to-int' instruction.
2634 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2635 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002636 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002637
2638 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002639 // if input >= (double)INT_MAX goto done
2640 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002641 __ j(kAboveEqual, &done);
2642 // if input == NaN goto nan
2643 __ j(kUnordered, &nan);
2644 // output = double-to-int-truncate(input)
2645 __ cvttsd2si(output, input);
2646 __ jmp(&done);
2647 __ Bind(&nan);
2648 // output = 0
2649 __ xorl(output, output);
2650 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002651 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002652 }
Roland Levillain946e1432014-11-11 17:35:19 +00002653
2654 default:
2655 LOG(FATAL) << "Unexpected type conversion from " << input_type
2656 << " to " << result_type;
2657 }
2658 break;
2659
Roland Levillaindff1f282014-11-05 14:15:05 +00002660 case Primitive::kPrimLong:
2661 switch (input_type) {
2662 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002663 case Primitive::kPrimBoolean:
2664 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002665 case Primitive::kPrimByte:
2666 case Primitive::kPrimShort:
2667 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002668 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002669 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002670 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002671 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002672 break;
2673
Roland Levillain624279f2014-12-04 11:54:28 +00002674 case Primitive::kPrimFloat: {
2675 // Processing a Dex `float-to-long' instruction.
2676 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2677 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002678 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002679
Mark Mendell92e83bf2015-05-07 11:25:03 -04002680 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002681 // if input >= (float)LONG_MAX goto done
2682 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002683 __ j(kAboveEqual, &done);
2684 // if input == NaN goto nan
2685 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002686 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002687 __ cvttss2si(output, input, true);
2688 __ jmp(&done);
2689 __ Bind(&nan);
2690 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002691 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002692 __ Bind(&done);
2693 break;
2694 }
2695
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002696 case Primitive::kPrimDouble: {
2697 // Processing a Dex `double-to-long' instruction.
2698 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2699 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002700 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002701
Mark Mendell92e83bf2015-05-07 11:25:03 -04002702 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002703 // if input >= (double)LONG_MAX goto done
2704 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002705 __ j(kAboveEqual, &done);
2706 // if input == NaN goto nan
2707 __ j(kUnordered, &nan);
2708 // output = double-to-long-truncate(input)
2709 __ cvttsd2si(output, input, true);
2710 __ jmp(&done);
2711 __ Bind(&nan);
2712 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002713 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002714 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002715 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002716 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002717
2718 default:
2719 LOG(FATAL) << "Unexpected type conversion from " << input_type
2720 << " to " << result_type;
2721 }
2722 break;
2723
Roland Levillain981e4542014-11-14 11:47:14 +00002724 case Primitive::kPrimChar:
2725 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002726 case Primitive::kPrimLong:
2727 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002728 case Primitive::kPrimBoolean:
2729 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002730 case Primitive::kPrimByte:
2731 case Primitive::kPrimShort:
2732 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002733 // Processing a Dex `int-to-char' instruction.
2734 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002735 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002736 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002737 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002738 Address(CpuRegister(RSP), in.GetStackIndex()));
2739 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002740 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002741 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002742 }
2743 break;
2744
2745 default:
2746 LOG(FATAL) << "Unexpected type conversion from " << input_type
2747 << " to " << result_type;
2748 }
2749 break;
2750
Roland Levillaindff1f282014-11-05 14:15:05 +00002751 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002752 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002753 case Primitive::kPrimBoolean:
2754 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002755 case Primitive::kPrimByte:
2756 case Primitive::kPrimShort:
2757 case Primitive::kPrimInt:
2758 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002759 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002760 if (in.IsRegister()) {
2761 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2762 } else if (in.IsConstant()) {
2763 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2764 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002765 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002766 } else {
2767 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2768 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2769 }
Roland Levillaincff13742014-11-17 14:32:17 +00002770 break;
2771
2772 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002773 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002774 if (in.IsRegister()) {
2775 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2776 } else if (in.IsConstant()) {
2777 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2778 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002779 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002780 } else {
2781 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2782 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2783 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002784 break;
2785
Roland Levillaincff13742014-11-17 14:32:17 +00002786 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002787 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002788 if (in.IsFpuRegister()) {
2789 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2790 } else if (in.IsConstant()) {
2791 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2792 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002793 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002794 } else {
2795 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2796 Address(CpuRegister(RSP), in.GetStackIndex()));
2797 }
Roland Levillaincff13742014-11-17 14:32:17 +00002798 break;
2799
2800 default:
2801 LOG(FATAL) << "Unexpected type conversion from " << input_type
2802 << " to " << result_type;
2803 };
2804 break;
2805
Roland Levillaindff1f282014-11-05 14:15:05 +00002806 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002807 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002808 case Primitive::kPrimBoolean:
2809 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002810 case Primitive::kPrimByte:
2811 case Primitive::kPrimShort:
2812 case Primitive::kPrimInt:
2813 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002814 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002815 if (in.IsRegister()) {
2816 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2817 } else if (in.IsConstant()) {
2818 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2819 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002820 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002821 } else {
2822 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2823 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2824 }
Roland Levillaincff13742014-11-17 14:32:17 +00002825 break;
2826
2827 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002828 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002829 if (in.IsRegister()) {
2830 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2831 } else if (in.IsConstant()) {
2832 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2833 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002834 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002835 } else {
2836 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2837 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2838 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002839 break;
2840
Roland Levillaincff13742014-11-17 14:32:17 +00002841 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002842 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002843 if (in.IsFpuRegister()) {
2844 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2845 } else if (in.IsConstant()) {
2846 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2847 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002848 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002849 } else {
2850 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2851 Address(CpuRegister(RSP), in.GetStackIndex()));
2852 }
Roland Levillaincff13742014-11-17 14:32:17 +00002853 break;
2854
2855 default:
2856 LOG(FATAL) << "Unexpected type conversion from " << input_type
2857 << " to " << result_type;
2858 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002859 break;
2860
2861 default:
2862 LOG(FATAL) << "Unexpected type conversion from " << input_type
2863 << " to " << result_type;
2864 }
2865}
2866
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002867void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002868 LocationSummary* locations =
2869 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002870 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002871 case Primitive::kPrimInt: {
2872 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002873 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2874 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002875 break;
2876 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002877
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002878 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002879 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002880 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002881 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002883 break;
2884 }
2885
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002886 case Primitive::kPrimDouble:
2887 case Primitive::kPrimFloat: {
2888 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002889 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002890 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002891 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002892 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002893
2894 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002895 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002897}
2898
2899void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2900 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002901 Location first = locations->InAt(0);
2902 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002903 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002904
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002905 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002906 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002908 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2909 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002910 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2911 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002912 } else {
2913 __ leal(out.AsRegister<CpuRegister>(), Address(
2914 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2915 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002916 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002917 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2918 __ addl(out.AsRegister<CpuRegister>(),
2919 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2920 } else {
2921 __ leal(out.AsRegister<CpuRegister>(), Address(
2922 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2923 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002924 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002925 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002927 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002928 break;
2929 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002931 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002932 if (second.IsRegister()) {
2933 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2934 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002935 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2936 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002937 } else {
2938 __ leaq(out.AsRegister<CpuRegister>(), Address(
2939 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2940 }
2941 } else {
2942 DCHECK(second.IsConstant());
2943 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2944 int32_t int32_value = Low32Bits(value);
2945 DCHECK_EQ(int32_value, value);
2946 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2947 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2948 } else {
2949 __ leaq(out.AsRegister<CpuRegister>(), Address(
2950 first.AsRegister<CpuRegister>(), int32_value));
2951 }
2952 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002953 break;
2954 }
2955
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002957 if (second.IsFpuRegister()) {
2958 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2959 } else if (second.IsConstant()) {
2960 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002961 codegen_->LiteralFloatAddress(
2962 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002963 } else {
2964 DCHECK(second.IsStackSlot());
2965 __ addss(first.AsFpuRegister<XmmRegister>(),
2966 Address(CpuRegister(RSP), second.GetStackIndex()));
2967 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002968 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002969 }
2970
2971 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002972 if (second.IsFpuRegister()) {
2973 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2974 } else if (second.IsConstant()) {
2975 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002976 codegen_->LiteralDoubleAddress(
2977 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002978 } else {
2979 DCHECK(second.IsDoubleStackSlot());
2980 __ addsd(first.AsFpuRegister<XmmRegister>(),
2981 Address(CpuRegister(RSP), second.GetStackIndex()));
2982 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 break;
2984 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002985
2986 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002988 }
2989}
2990
2991void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002992 LocationSummary* locations =
2993 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002994 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002995 case Primitive::kPrimInt: {
2996 locations->SetInAt(0, Location::RequiresRegister());
2997 locations->SetInAt(1, Location::Any());
2998 locations->SetOut(Location::SameAsFirstInput());
2999 break;
3000 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003001 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003002 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003003 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003004 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003005 break;
3006 }
Calin Juravle11351682014-10-23 15:38:15 +01003007 case Primitive::kPrimFloat:
3008 case Primitive::kPrimDouble: {
3009 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003010 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003011 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003012 break;
Calin Juravle11351682014-10-23 15:38:15 +01003013 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003014 default:
Calin Juravle11351682014-10-23 15:38:15 +01003015 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003016 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003017}
3018
3019void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3020 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003021 Location first = locations->InAt(0);
3022 Location second = locations->InAt(1);
3023 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003024 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003025 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003026 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003027 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003028 } else if (second.IsConstant()) {
3029 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003031 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003032 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003033 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003034 break;
3035 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003036 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003037 if (second.IsConstant()) {
3038 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3039 DCHECK(IsInt<32>(value));
3040 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3041 } else {
3042 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3043 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003044 break;
3045 }
3046
Calin Juravle11351682014-10-23 15:38:15 +01003047 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003048 if (second.IsFpuRegister()) {
3049 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3050 } else if (second.IsConstant()) {
3051 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003052 codegen_->LiteralFloatAddress(
3053 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003054 } else {
3055 DCHECK(second.IsStackSlot());
3056 __ subss(first.AsFpuRegister<XmmRegister>(),
3057 Address(CpuRegister(RSP), second.GetStackIndex()));
3058 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059 break;
Calin Juravle11351682014-10-23 15:38:15 +01003060 }
3061
3062 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003063 if (second.IsFpuRegister()) {
3064 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3065 } else if (second.IsConstant()) {
3066 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003067 codegen_->LiteralDoubleAddress(
3068 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003069 } else {
3070 DCHECK(second.IsDoubleStackSlot());
3071 __ subsd(first.AsFpuRegister<XmmRegister>(),
3072 Address(CpuRegister(RSP), second.GetStackIndex()));
3073 }
Calin Juravle11351682014-10-23 15:38:15 +01003074 break;
3075 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003076
3077 default:
Calin Juravle11351682014-10-23 15:38:15 +01003078 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003079 }
3080}
3081
Calin Juravle34bacdf2014-10-07 20:23:36 +01003082void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3085 switch (mul->GetResultType()) {
3086 case Primitive::kPrimInt: {
3087 locations->SetInAt(0, Location::RequiresRegister());
3088 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003089 if (mul->InputAt(1)->IsIntConstant()) {
3090 // Can use 3 operand multiply.
3091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3092 } else {
3093 locations->SetOut(Location::SameAsFirstInput());
3094 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003095 break;
3096 }
3097 case Primitive::kPrimLong: {
3098 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003099 locations->SetInAt(1, Location::Any());
3100 if (mul->InputAt(1)->IsLongConstant() &&
3101 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003102 // Can use 3 operand multiply.
3103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3104 } else {
3105 locations->SetOut(Location::SameAsFirstInput());
3106 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107 break;
3108 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003109 case Primitive::kPrimFloat:
3110 case Primitive::kPrimDouble: {
3111 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003112 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003113 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003114 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116
3117 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003119 }
3120}
3121
3122void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3123 LocationSummary* locations = mul->GetLocations();
3124 Location first = locations->InAt(0);
3125 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003126 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003127 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003128 case Primitive::kPrimInt:
3129 // The constant may have ended up in a register, so test explicitly to avoid
3130 // problems where the output may not be the same as the first operand.
3131 if (mul->InputAt(1)->IsIntConstant()) {
3132 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3133 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3134 } else if (second.IsRegister()) {
3135 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003136 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003138 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003140 __ imull(first.AsRegister<CpuRegister>(),
3141 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142 }
3143 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003144 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003145 // The constant may have ended up in a register, so test explicitly to avoid
3146 // problems where the output may not be the same as the first operand.
3147 if (mul->InputAt(1)->IsLongConstant()) {
3148 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3149 if (IsInt<32>(value)) {
3150 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3151 Immediate(static_cast<int32_t>(value)));
3152 } else {
3153 // Have to use the constant area.
3154 DCHECK(first.Equals(out));
3155 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3156 }
3157 } else if (second.IsRegister()) {
3158 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003159 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003160 } else {
3161 DCHECK(second.IsDoubleStackSlot());
3162 DCHECK(first.Equals(out));
3163 __ imulq(first.AsRegister<CpuRegister>(),
3164 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003165 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003166 break;
3167 }
3168
Calin Juravleb5bfa962014-10-21 18:02:24 +01003169 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003170 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003171 if (second.IsFpuRegister()) {
3172 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3173 } else if (second.IsConstant()) {
3174 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003175 codegen_->LiteralFloatAddress(
3176 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003177 } else {
3178 DCHECK(second.IsStackSlot());
3179 __ mulss(first.AsFpuRegister<XmmRegister>(),
3180 Address(CpuRegister(RSP), second.GetStackIndex()));
3181 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183 }
3184
3185 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003186 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003187 if (second.IsFpuRegister()) {
3188 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3189 } else if (second.IsConstant()) {
3190 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003191 codegen_->LiteralDoubleAddress(
3192 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003193 } else {
3194 DCHECK(second.IsDoubleStackSlot());
3195 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3196 Address(CpuRegister(RSP), second.GetStackIndex()));
3197 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003198 break;
3199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200
3201 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003202 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203 }
3204}
3205
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003206void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3207 uint32_t stack_adjustment, bool is_float) {
3208 if (source.IsStackSlot()) {
3209 DCHECK(is_float);
3210 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3211 } else if (source.IsDoubleStackSlot()) {
3212 DCHECK(!is_float);
3213 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3214 } else {
3215 // Write the value to the temporary location on the stack and load to FP stack.
3216 if (is_float) {
3217 Location stack_temp = Location::StackSlot(temp_offset);
3218 codegen_->Move(stack_temp, source);
3219 __ flds(Address(CpuRegister(RSP), temp_offset));
3220 } else {
3221 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3222 codegen_->Move(stack_temp, source);
3223 __ fldl(Address(CpuRegister(RSP), temp_offset));
3224 }
3225 }
3226}
3227
3228void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3229 Primitive::Type type = rem->GetResultType();
3230 bool is_float = type == Primitive::kPrimFloat;
3231 size_t elem_size = Primitive::ComponentSize(type);
3232 LocationSummary* locations = rem->GetLocations();
3233 Location first = locations->InAt(0);
3234 Location second = locations->InAt(1);
3235 Location out = locations->Out();
3236
3237 // Create stack space for 2 elements.
3238 // TODO: enhance register allocator to ask for stack temporaries.
3239 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3240
3241 // Load the values to the FP stack in reverse order, using temporaries if needed.
3242 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3243 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3244
3245 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003246 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003247 __ Bind(&retry);
3248 __ fprem();
3249
3250 // Move FP status to AX.
3251 __ fstsw();
3252
3253 // And see if the argument reduction is complete. This is signaled by the
3254 // C2 FPU flag bit set to 0.
3255 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3256 __ j(kNotEqual, &retry);
3257
3258 // We have settled on the final value. Retrieve it into an XMM register.
3259 // Store FP top of stack to real stack.
3260 if (is_float) {
3261 __ fsts(Address(CpuRegister(RSP), 0));
3262 } else {
3263 __ fstl(Address(CpuRegister(RSP), 0));
3264 }
3265
3266 // Pop the 2 items from the FP stack.
3267 __ fucompp();
3268
3269 // Load the value from the stack into an XMM register.
3270 DCHECK(out.IsFpuRegister()) << out;
3271 if (is_float) {
3272 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3273 } else {
3274 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3275 }
3276
3277 // And remove the temporary stack space we allocated.
3278 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3279}
3280
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003281void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3282 DCHECK(instruction->IsDiv() || instruction->IsRem());
3283
3284 LocationSummary* locations = instruction->GetLocations();
3285 Location second = locations->InAt(1);
3286 DCHECK(second.IsConstant());
3287
3288 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3289 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003290 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003291
3292 DCHECK(imm == 1 || imm == -1);
3293
3294 switch (instruction->GetResultType()) {
3295 case Primitive::kPrimInt: {
3296 if (instruction->IsRem()) {
3297 __ xorl(output_register, output_register);
3298 } else {
3299 __ movl(output_register, input_register);
3300 if (imm == -1) {
3301 __ negl(output_register);
3302 }
3303 }
3304 break;
3305 }
3306
3307 case Primitive::kPrimLong: {
3308 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003309 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003310 } else {
3311 __ movq(output_register, input_register);
3312 if (imm == -1) {
3313 __ negq(output_register);
3314 }
3315 }
3316 break;
3317 }
3318
3319 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003320 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003321 }
3322}
3323
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003324void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003325 LocationSummary* locations = instruction->GetLocations();
3326 Location second = locations->InAt(1);
3327
3328 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3329 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3330
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003331 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003332 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3333 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334
3335 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3336
3337 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003338 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003339 __ testl(numerator, numerator);
3340 __ cmov(kGreaterEqual, tmp, numerator);
3341 int shift = CTZ(imm);
3342 __ sarl(tmp, Immediate(shift));
3343
3344 if (imm < 0) {
3345 __ negl(tmp);
3346 }
3347
3348 __ movl(output_register, tmp);
3349 } else {
3350 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3351 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3352
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003353 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003354 __ addq(rdx, numerator);
3355 __ testq(numerator, numerator);
3356 __ cmov(kGreaterEqual, rdx, numerator);
3357 int shift = CTZ(imm);
3358 __ sarq(rdx, Immediate(shift));
3359
3360 if (imm < 0) {
3361 __ negq(rdx);
3362 }
3363
3364 __ movq(output_register, rdx);
3365 }
3366}
3367
3368void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3369 DCHECK(instruction->IsDiv() || instruction->IsRem());
3370
3371 LocationSummary* locations = instruction->GetLocations();
3372 Location second = locations->InAt(1);
3373
3374 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3375 : locations->GetTemp(0).AsRegister<CpuRegister>();
3376 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3377 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3378 : locations->Out().AsRegister<CpuRegister>();
3379 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3380
3381 DCHECK_EQ(RAX, eax.AsRegister());
3382 DCHECK_EQ(RDX, edx.AsRegister());
3383 if (instruction->IsDiv()) {
3384 DCHECK_EQ(RAX, out.AsRegister());
3385 } else {
3386 DCHECK_EQ(RDX, out.AsRegister());
3387 }
3388
3389 int64_t magic;
3390 int shift;
3391
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003392 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003393 if (instruction->GetResultType() == Primitive::kPrimInt) {
3394 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3395
3396 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3397
3398 __ movl(numerator, eax);
3399
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003400 __ movl(eax, Immediate(magic));
3401 __ imull(numerator);
3402
3403 if (imm > 0 && magic < 0) {
3404 __ addl(edx, numerator);
3405 } else if (imm < 0 && magic > 0) {
3406 __ subl(edx, numerator);
3407 }
3408
3409 if (shift != 0) {
3410 __ sarl(edx, Immediate(shift));
3411 }
3412
3413 __ movl(eax, edx);
3414 __ shrl(edx, Immediate(31));
3415 __ addl(edx, eax);
3416
3417 if (instruction->IsRem()) {
3418 __ movl(eax, numerator);
3419 __ imull(edx, Immediate(imm));
3420 __ subl(eax, edx);
3421 __ movl(edx, eax);
3422 } else {
3423 __ movl(eax, edx);
3424 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425 } else {
3426 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3427
3428 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3429
3430 CpuRegister rax = eax;
3431 CpuRegister rdx = edx;
3432
3433 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3434
3435 // Save the numerator.
3436 __ movq(numerator, rax);
3437
3438 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003439 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440
3441 // RDX:RAX = magic * numerator
3442 __ imulq(numerator);
3443
3444 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003445 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446 __ addq(rdx, numerator);
3447 } else if (imm < 0 && magic > 0) {
3448 // RDX -= numerator
3449 __ subq(rdx, numerator);
3450 }
3451
3452 // Shift if needed.
3453 if (shift != 0) {
3454 __ sarq(rdx, Immediate(shift));
3455 }
3456
3457 // RDX += 1 if RDX < 0
3458 __ movq(rax, rdx);
3459 __ shrq(rdx, Immediate(63));
3460 __ addq(rdx, rax);
3461
3462 if (instruction->IsRem()) {
3463 __ movq(rax, numerator);
3464
3465 if (IsInt<32>(imm)) {
3466 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3467 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003468 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 }
3470
3471 __ subq(rax, rdx);
3472 __ movq(rdx, rax);
3473 } else {
3474 __ movq(rax, rdx);
3475 }
3476 }
3477}
3478
Calin Juravlebacfec32014-11-14 15:54:36 +00003479void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3480 DCHECK(instruction->IsDiv() || instruction->IsRem());
3481 Primitive::Type type = instruction->GetResultType();
3482 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3483
3484 bool is_div = instruction->IsDiv();
3485 LocationSummary* locations = instruction->GetLocations();
3486
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3488 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003489
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003491 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003492
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003494 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003495
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496 if (imm == 0) {
3497 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3498 } else if (imm == 1 || imm == -1) {
3499 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003500 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003501 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 } else {
3503 DCHECK(imm <= -2 || imm >= 2);
3504 GenerateDivRemWithAnyConstant(instruction);
3505 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003506 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003507 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003509 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003511
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3513 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3514 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3515 // so it's safe to just use negl instead of more complex comparisons.
3516 if (type == Primitive::kPrimInt) {
3517 __ cmpl(second_reg, Immediate(-1));
3518 __ j(kEqual, slow_path->GetEntryLabel());
3519 // edx:eax <- sign-extended of eax
3520 __ cdq();
3521 // eax = quotient, edx = remainder
3522 __ idivl(second_reg);
3523 } else {
3524 __ cmpq(second_reg, Immediate(-1));
3525 __ j(kEqual, slow_path->GetEntryLabel());
3526 // rdx:rax <- sign-extended of rax
3527 __ cqo();
3528 // rax = quotient, rdx = remainder
3529 __ idivq(second_reg);
3530 }
3531 __ Bind(slow_path->GetExitLabel());
3532 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003533}
3534
Calin Juravle7c4954d2014-10-28 16:57:40 +00003535void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3536 LocationSummary* locations =
3537 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3538 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003539 case Primitive::kPrimInt:
3540 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003541 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003543 locations->SetOut(Location::SameAsFirstInput());
3544 // Intel uses edx:eax as the dividend.
3545 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3547 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3548 // output and request another temp.
3549 if (div->InputAt(1)->IsConstant()) {
3550 locations->AddTemp(Location::RequiresRegister());
3551 }
Calin Juravled0d48522014-11-04 16:40:20 +00003552 break;
3553 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003554
Calin Juravle7c4954d2014-10-28 16:57:40 +00003555 case Primitive::kPrimFloat:
3556 case Primitive::kPrimDouble: {
3557 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003558 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003559 locations->SetOut(Location::SameAsFirstInput());
3560 break;
3561 }
3562
3563 default:
3564 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3565 }
3566}
3567
3568void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3569 LocationSummary* locations = div->GetLocations();
3570 Location first = locations->InAt(0);
3571 Location second = locations->InAt(1);
3572 DCHECK(first.Equals(locations->Out()));
3573
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003574 Primitive::Type type = div->GetResultType();
3575 switch (type) {
3576 case Primitive::kPrimInt:
3577 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003579 break;
3580 }
3581
Calin Juravle7c4954d2014-10-28 16:57:40 +00003582 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003583 if (second.IsFpuRegister()) {
3584 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3585 } else if (second.IsConstant()) {
3586 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003587 codegen_->LiteralFloatAddress(
3588 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003589 } else {
3590 DCHECK(second.IsStackSlot());
3591 __ divss(first.AsFpuRegister<XmmRegister>(),
3592 Address(CpuRegister(RSP), second.GetStackIndex()));
3593 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003594 break;
3595 }
3596
3597 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003598 if (second.IsFpuRegister()) {
3599 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3600 } else if (second.IsConstant()) {
3601 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003602 codegen_->LiteralDoubleAddress(
3603 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003604 } else {
3605 DCHECK(second.IsDoubleStackSlot());
3606 __ divsd(first.AsFpuRegister<XmmRegister>(),
3607 Address(CpuRegister(RSP), second.GetStackIndex()));
3608 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003609 break;
3610 }
3611
3612 default:
3613 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3614 }
3615}
3616
Calin Juravlebacfec32014-11-14 15:54:36 +00003617void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003618 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003619 LocationSummary* locations =
3620 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003621
3622 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003623 case Primitive::kPrimInt:
3624 case Primitive::kPrimLong: {
3625 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003626 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003627 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3628 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3630 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3631 // output and request another temp.
3632 if (rem->InputAt(1)->IsConstant()) {
3633 locations->AddTemp(Location::RequiresRegister());
3634 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003635 break;
3636 }
3637
3638 case Primitive::kPrimFloat:
3639 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003640 locations->SetInAt(0, Location::Any());
3641 locations->SetInAt(1, Location::Any());
3642 locations->SetOut(Location::RequiresFpuRegister());
3643 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003644 break;
3645 }
3646
3647 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003648 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003649 }
3650}
3651
3652void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3653 Primitive::Type type = rem->GetResultType();
3654 switch (type) {
3655 case Primitive::kPrimInt:
3656 case Primitive::kPrimLong: {
3657 GenerateDivRemIntegral(rem);
3658 break;
3659 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003660 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003661 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003662 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003663 break;
3664 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003665 default:
3666 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3667 }
3668}
3669
Calin Juravled0d48522014-11-04 16:40:20 +00003670void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003671 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003672 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003673}
3674
3675void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003676 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003677 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3678 codegen_->AddSlowPath(slow_path);
3679
3680 LocationSummary* locations = instruction->GetLocations();
3681 Location value = locations->InAt(0);
3682
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003683 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003684 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003685 case Primitive::kPrimByte:
3686 case Primitive::kPrimChar:
3687 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003688 case Primitive::kPrimInt: {
3689 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003690 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003691 __ j(kEqual, slow_path->GetEntryLabel());
3692 } else if (value.IsStackSlot()) {
3693 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3694 __ j(kEqual, slow_path->GetEntryLabel());
3695 } else {
3696 DCHECK(value.IsConstant()) << value;
3697 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003698 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003699 }
3700 }
3701 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003702 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003703 case Primitive::kPrimLong: {
3704 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003706 __ j(kEqual, slow_path->GetEntryLabel());
3707 } else if (value.IsDoubleStackSlot()) {
3708 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3709 __ j(kEqual, slow_path->GetEntryLabel());
3710 } else {
3711 DCHECK(value.IsConstant()) << value;
3712 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003713 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003714 }
3715 }
3716 break;
3717 }
3718 default:
3719 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003720 }
Calin Juravled0d48522014-11-04 16:40:20 +00003721}
3722
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3724 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3725
3726 LocationSummary* locations =
3727 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3728
3729 switch (op->GetResultType()) {
3730 case Primitive::kPrimInt:
3731 case Primitive::kPrimLong: {
3732 locations->SetInAt(0, Location::RequiresRegister());
3733 // The shift count needs to be in CL.
3734 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3735 locations->SetOut(Location::SameAsFirstInput());
3736 break;
3737 }
3738 default:
3739 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3740 }
3741}
3742
3743void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3744 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3745
3746 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 Location second = locations->InAt(1);
3749
3750 switch (op->GetResultType()) {
3751 case Primitive::kPrimInt: {
3752 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 if (op->IsShl()) {
3755 __ shll(first_reg, second_reg);
3756 } else if (op->IsShr()) {
3757 __ sarl(first_reg, second_reg);
3758 } else {
3759 __ shrl(first_reg, second_reg);
3760 }
3761 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003762 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763 if (op->IsShl()) {
3764 __ shll(first_reg, imm);
3765 } else if (op->IsShr()) {
3766 __ sarl(first_reg, imm);
3767 } else {
3768 __ shrl(first_reg, imm);
3769 }
3770 }
3771 break;
3772 }
3773 case Primitive::kPrimLong: {
3774 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776 if (op->IsShl()) {
3777 __ shlq(first_reg, second_reg);
3778 } else if (op->IsShr()) {
3779 __ sarq(first_reg, second_reg);
3780 } else {
3781 __ shrq(first_reg, second_reg);
3782 }
3783 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003784 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003785 if (op->IsShl()) {
3786 __ shlq(first_reg, imm);
3787 } else if (op->IsShr()) {
3788 __ sarq(first_reg, imm);
3789 } else {
3790 __ shrq(first_reg, imm);
3791 }
3792 }
3793 break;
3794 }
3795 default:
3796 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003797 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003798 }
3799}
3800
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003801void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3802 LocationSummary* locations =
3803 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3804
3805 switch (ror->GetResultType()) {
3806 case Primitive::kPrimInt:
3807 case Primitive::kPrimLong: {
3808 locations->SetInAt(0, Location::RequiresRegister());
3809 // The shift count needs to be in CL (unless it is a constant).
3810 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3811 locations->SetOut(Location::SameAsFirstInput());
3812 break;
3813 }
3814 default:
3815 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3816 UNREACHABLE();
3817 }
3818}
3819
3820void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3821 LocationSummary* locations = ror->GetLocations();
3822 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3823 Location second = locations->InAt(1);
3824
3825 switch (ror->GetResultType()) {
3826 case Primitive::kPrimInt:
3827 if (second.IsRegister()) {
3828 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3829 __ rorl(first_reg, second_reg);
3830 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003831 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003832 __ rorl(first_reg, imm);
3833 }
3834 break;
3835 case Primitive::kPrimLong:
3836 if (second.IsRegister()) {
3837 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3838 __ rorq(first_reg, second_reg);
3839 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003840 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003841 __ rorq(first_reg, imm);
3842 }
3843 break;
3844 default:
3845 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3846 UNREACHABLE();
3847 }
3848}
3849
Calin Juravle9aec02f2014-11-18 23:06:35 +00003850void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3851 HandleShift(shl);
3852}
3853
3854void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3855 HandleShift(shl);
3856}
3857
3858void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3859 HandleShift(shr);
3860}
3861
3862void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3863 HandleShift(shr);
3864}
3865
3866void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3867 HandleShift(ushr);
3868}
3869
3870void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3871 HandleShift(ushr);
3872}
3873
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003874void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003875 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003876 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003877 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003878 if (instruction->IsStringAlloc()) {
3879 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3880 } else {
3881 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3882 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3883 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003884 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003885}
3886
3887void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003888 // Note: if heap poisoning is enabled, the entry point takes cares
3889 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003890 if (instruction->IsStringAlloc()) {
3891 // String is allocated through StringFactory. Call NewEmptyString entry point.
3892 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003893 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003894 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3895 __ call(Address(temp, code_offset.SizeValue()));
3896 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3897 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003898 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003899 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3900 DCHECK(!codegen_->IsLeafMethod());
3901 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003902}
3903
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003904void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3905 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003907 InvokeRuntimeCallingConvention calling_convention;
3908 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003909 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003910 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003911 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003912}
3913
3914void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3915 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003916 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3917 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003918 // Note: if heap poisoning is enabled, the entry point takes cares
3919 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003920 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003921 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003922
3923 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003924}
3925
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003926void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003927 LocationSummary* locations =
3928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003929 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3930 if (location.IsStackSlot()) {
3931 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3932 } else if (location.IsDoubleStackSlot()) {
3933 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3934 }
3935 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003936}
3937
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003938void InstructionCodeGeneratorX86_64::VisitParameterValue(
3939 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003940 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003941}
3942
3943void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3944 LocationSummary* locations =
3945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3946 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3947}
3948
3949void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3950 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3951 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003952}
3953
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003954void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3955 LocationSummary* locations =
3956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3957 locations->SetInAt(0, Location::RequiresRegister());
3958 locations->SetOut(Location::RequiresRegister());
3959}
3960
3961void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3962 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003963 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003964 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003965 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003966 __ movq(locations->Out().AsRegister<CpuRegister>(),
3967 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003968 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003969 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003970 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003971 __ movq(locations->Out().AsRegister<CpuRegister>(),
3972 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3973 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003974 __ movq(locations->Out().AsRegister<CpuRegister>(),
3975 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003976 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003977}
3978
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003979void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003981 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003982 locations->SetInAt(0, Location::RequiresRegister());
3983 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003984}
3985
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003986void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3987 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003988 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3989 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003990 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003991 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003992 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003993 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003994 break;
3995
3996 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003998 break;
3999
4000 default:
4001 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4002 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004003}
4004
David Brazdil66d126e2015-04-03 16:02:44 +01004005void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4006 LocationSummary* locations =
4007 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4008 locations->SetInAt(0, Location::RequiresRegister());
4009 locations->SetOut(Location::SameAsFirstInput());
4010}
4011
4012void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004013 LocationSummary* locations = bool_not->GetLocations();
4014 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4015 locations->Out().AsRegister<CpuRegister>().AsRegister());
4016 Location out = locations->Out();
4017 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4018}
4019
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004020void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004021 LocationSummary* locations =
4022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004023 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004024 locations->SetInAt(i, Location::Any());
4025 }
4026 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004027}
4028
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004029void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030 LOG(FATAL) << "Unimplemented";
4031}
4032
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004033void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004034 /*
4035 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004036 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004037 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4038 */
4039 switch (kind) {
4040 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004041 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004042 break;
4043 }
4044 case MemBarrierKind::kAnyStore:
4045 case MemBarrierKind::kLoadAny:
4046 case MemBarrierKind::kStoreStore: {
4047 // nop
4048 break;
4049 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004050 case MemBarrierKind::kNTStoreStore:
4051 // Non-Temporal Store/Store needs an explicit fence.
4052 MemoryFence(/* non-temporal */ true);
4053 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004054 }
4055}
4056
4057void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4058 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4059
Roland Levillain0d5a2812015-11-13 10:07:31 +00004060 bool object_field_get_with_read_barrier =
4061 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004062 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004063 new (GetGraph()->GetArena()) LocationSummary(instruction,
4064 object_field_get_with_read_barrier ?
4065 LocationSummary::kCallOnSlowPath :
4066 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004067 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004068 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004069 }
Calin Juravle52c48962014-12-16 17:02:57 +00004070 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004071 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4072 locations->SetOut(Location::RequiresFpuRegister());
4073 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004074 // The output overlaps for an object field get when read barriers
4075 // are enabled: we do not want the move to overwrite the object's
4076 // location, as we need it to emit the read barrier.
4077 locations->SetOut(
4078 Location::RequiresRegister(),
4079 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004080 }
Calin Juravle52c48962014-12-16 17:02:57 +00004081}
4082
4083void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4084 const FieldInfo& field_info) {
4085 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4086
4087 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004088 Location base_loc = locations->InAt(0);
4089 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004090 Location out = locations->Out();
4091 bool is_volatile = field_info.IsVolatile();
4092 Primitive::Type field_type = field_info.GetFieldType();
4093 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4094
4095 switch (field_type) {
4096 case Primitive::kPrimBoolean: {
4097 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4098 break;
4099 }
4100
4101 case Primitive::kPrimByte: {
4102 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4103 break;
4104 }
4105
4106 case Primitive::kPrimShort: {
4107 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4108 break;
4109 }
4110
4111 case Primitive::kPrimChar: {
4112 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4113 break;
4114 }
4115
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004116 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004117 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4118 break;
4119 }
4120
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004121 case Primitive::kPrimNot: {
4122 // /* HeapReference<Object> */ out = *(base + offset)
4123 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004124 // Note that a potential implicit null check is handled in this
4125 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4126 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004127 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004128 if (is_volatile) {
4129 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4130 }
4131 } else {
4132 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4133 codegen_->MaybeRecordImplicitNullCheck(instruction);
4134 if (is_volatile) {
4135 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4136 }
4137 // If read barriers are enabled, emit read barriers other than
4138 // Baker's using a slow path (and also unpoison the loaded
4139 // reference, if heap poisoning is enabled).
4140 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4141 }
4142 break;
4143 }
4144
Calin Juravle52c48962014-12-16 17:02:57 +00004145 case Primitive::kPrimLong: {
4146 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4147 break;
4148 }
4149
4150 case Primitive::kPrimFloat: {
4151 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4152 break;
4153 }
4154
4155 case Primitive::kPrimDouble: {
4156 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4157 break;
4158 }
4159
4160 case Primitive::kPrimVoid:
4161 LOG(FATAL) << "Unreachable type " << field_type;
4162 UNREACHABLE();
4163 }
4164
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004165 if (field_type == Primitive::kPrimNot) {
4166 // Potential implicit null checks, in the case of reference
4167 // fields, are handled in the previous switch statement.
4168 } else {
4169 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004170 }
Roland Levillain4d027112015-07-01 15:41:14 +01004171
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004172 if (is_volatile) {
4173 if (field_type == Primitive::kPrimNot) {
4174 // Memory barriers, in the case of references, are also handled
4175 // in the previous switch statement.
4176 } else {
4177 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4178 }
Roland Levillain4d027112015-07-01 15:41:14 +01004179 }
Calin Juravle52c48962014-12-16 17:02:57 +00004180}
4181
4182void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4183 const FieldInfo& field_info) {
4184 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4185
4186 LocationSummary* locations =
4187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004188 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004189 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004190 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004191 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004192
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004193 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004194 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004195 if (is_volatile) {
4196 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4197 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4198 } else {
4199 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4200 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004201 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004202 if (is_volatile) {
4203 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4204 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4205 } else {
4206 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4207 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004208 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004210 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004211 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004212 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004213 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4214 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004215 locations->AddTemp(Location::RequiresRegister());
4216 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004217}
4218
Calin Juravle52c48962014-12-16 17:02:57 +00004219void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004220 const FieldInfo& field_info,
4221 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004222 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4223
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004225 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4226 Location value = locations->InAt(1);
4227 bool is_volatile = field_info.IsVolatile();
4228 Primitive::Type field_type = field_info.GetFieldType();
4229 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4230
4231 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004232 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004233 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004234
Mark Mendellea5af682015-10-22 17:35:49 -04004235 bool maybe_record_implicit_null_check_done = false;
4236
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004237 switch (field_type) {
4238 case Primitive::kPrimBoolean:
4239 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004240 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004241 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004242 __ movb(Address(base, offset), Immediate(v));
4243 } else {
4244 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4245 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246 break;
4247 }
4248
4249 case Primitive::kPrimShort:
4250 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004251 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004252 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004253 __ movw(Address(base, offset), Immediate(v));
4254 } else {
4255 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4256 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004257 break;
4258 }
4259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004260 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004261 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004262 if (value.IsConstant()) {
4263 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004264 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4265 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4266 // Note: if heap poisoning is enabled, no need to poison
4267 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004268 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004269 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004270 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4271 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4272 __ movl(temp, value.AsRegister<CpuRegister>());
4273 __ PoisonHeapReference(temp);
4274 __ movl(Address(base, offset), temp);
4275 } else {
4276 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4277 }
Mark Mendell40741f32015-04-20 22:10:34 -04004278 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279 break;
4280 }
4281
4282 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004283 if (value.IsConstant()) {
4284 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004285 codegen_->MoveInt64ToAddress(Address(base, offset),
4286 Address(base, offset + sizeof(int32_t)),
4287 v,
4288 instruction);
4289 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004290 } else {
4291 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4292 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293 break;
4294 }
4295
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004296 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004297 if (value.IsConstant()) {
4298 int32_t v =
4299 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4300 __ movl(Address(base, offset), Immediate(v));
4301 } else {
4302 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4303 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004304 break;
4305 }
4306
4307 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004308 if (value.IsConstant()) {
4309 int64_t v =
4310 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4311 codegen_->MoveInt64ToAddress(Address(base, offset),
4312 Address(base, offset + sizeof(int32_t)),
4313 v,
4314 instruction);
4315 maybe_record_implicit_null_check_done = true;
4316 } else {
4317 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4318 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004319 break;
4320 }
4321
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 case Primitive::kPrimVoid:
4323 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004324 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004325 }
Calin Juravle52c48962014-12-16 17:02:57 +00004326
Mark Mendellea5af682015-10-22 17:35:49 -04004327 if (!maybe_record_implicit_null_check_done) {
4328 codegen_->MaybeRecordImplicitNullCheck(instruction);
4329 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004330
4331 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4332 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4333 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004334 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004335 }
4336
Calin Juravle52c48962014-12-16 17:02:57 +00004337 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004338 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004339 }
4340}
4341
4342void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4343 HandleFieldSet(instruction, instruction->GetFieldInfo());
4344}
4345
4346void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004347 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004348}
4349
4350void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004351 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004352}
4353
4354void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004355 HandleFieldGet(instruction, instruction->GetFieldInfo());
4356}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004357
Calin Juravle52c48962014-12-16 17:02:57 +00004358void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4359 HandleFieldGet(instruction);
4360}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361
Calin Juravle52c48962014-12-16 17:02:57 +00004362void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4363 HandleFieldGet(instruction, instruction->GetFieldInfo());
4364}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365
Calin Juravle52c48962014-12-16 17:02:57 +00004366void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4367 HandleFieldSet(instruction, instruction->GetFieldInfo());
4368}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004369
Calin Juravle52c48962014-12-16 17:02:57 +00004370void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004371 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372}
4373
Calin Juravlee460d1d2015-09-29 04:52:17 +01004374void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4375 HUnresolvedInstanceFieldGet* instruction) {
4376 FieldAccessCallingConventionX86_64 calling_convention;
4377 codegen_->CreateUnresolvedFieldLocationSummary(
4378 instruction, instruction->GetFieldType(), calling_convention);
4379}
4380
4381void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4382 HUnresolvedInstanceFieldGet* instruction) {
4383 FieldAccessCallingConventionX86_64 calling_convention;
4384 codegen_->GenerateUnresolvedFieldAccess(instruction,
4385 instruction->GetFieldType(),
4386 instruction->GetFieldIndex(),
4387 instruction->GetDexPc(),
4388 calling_convention);
4389}
4390
4391void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4392 HUnresolvedInstanceFieldSet* instruction) {
4393 FieldAccessCallingConventionX86_64 calling_convention;
4394 codegen_->CreateUnresolvedFieldLocationSummary(
4395 instruction, instruction->GetFieldType(), calling_convention);
4396}
4397
4398void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4399 HUnresolvedInstanceFieldSet* instruction) {
4400 FieldAccessCallingConventionX86_64 calling_convention;
4401 codegen_->GenerateUnresolvedFieldAccess(instruction,
4402 instruction->GetFieldType(),
4403 instruction->GetFieldIndex(),
4404 instruction->GetDexPc(),
4405 calling_convention);
4406}
4407
4408void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4409 HUnresolvedStaticFieldGet* instruction) {
4410 FieldAccessCallingConventionX86_64 calling_convention;
4411 codegen_->CreateUnresolvedFieldLocationSummary(
4412 instruction, instruction->GetFieldType(), calling_convention);
4413}
4414
4415void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4416 HUnresolvedStaticFieldGet* instruction) {
4417 FieldAccessCallingConventionX86_64 calling_convention;
4418 codegen_->GenerateUnresolvedFieldAccess(instruction,
4419 instruction->GetFieldType(),
4420 instruction->GetFieldIndex(),
4421 instruction->GetDexPc(),
4422 calling_convention);
4423}
4424
4425void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4426 HUnresolvedStaticFieldSet* instruction) {
4427 FieldAccessCallingConventionX86_64 calling_convention;
4428 codegen_->CreateUnresolvedFieldLocationSummary(
4429 instruction, instruction->GetFieldType(), calling_convention);
4430}
4431
4432void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4433 HUnresolvedStaticFieldSet* instruction) {
4434 FieldAccessCallingConventionX86_64 calling_convention;
4435 codegen_->GenerateUnresolvedFieldAccess(instruction,
4436 instruction->GetFieldType(),
4437 instruction->GetFieldIndex(),
4438 instruction->GetDexPc(),
4439 calling_convention);
4440}
4441
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004443 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4444 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4445 ? Location::RequiresRegister()
4446 : Location::Any();
4447 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448}
4449
Calin Juravle2ae48182016-03-16 14:05:09 +00004450void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4451 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004452 return;
4453 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004454 LocationSummary* locations = instruction->GetLocations();
4455 Location obj = locations->InAt(0);
4456
4457 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004458 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004459}
4460
Calin Juravle2ae48182016-03-16 14:05:09 +00004461void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004462 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004463 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464
4465 LocationSummary* locations = instruction->GetLocations();
4466 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004467
4468 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004469 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004470 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004472 } else {
4473 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004474 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004475 __ jmp(slow_path->GetEntryLabel());
4476 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 }
4478 __ j(kEqual, slow_path->GetEntryLabel());
4479}
4480
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004481void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004482 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004483}
4484
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004485void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004486 bool object_array_get_with_read_barrier =
4487 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004488 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004489 new (GetGraph()->GetArena()) LocationSummary(instruction,
4490 object_array_get_with_read_barrier ?
4491 LocationSummary::kCallOnSlowPath :
4492 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004493 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004494 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004495 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004496 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004497 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004498 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4499 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4500 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004501 // The output overlaps for an object array get when read barriers
4502 // are enabled: we do not want the move to overwrite the array's
4503 // location, as we need it to emit the read barrier.
4504 locations->SetOut(
4505 Location::RequiresRegister(),
4506 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004507 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004508}
4509
4510void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4511 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004512 Location obj_loc = locations->InAt(0);
4513 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004515 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004516 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004518 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004519 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004520 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004521 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004522 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 break;
4524 }
4525
4526 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004527 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004528 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 break;
4530 }
4531
4532 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004533 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004534 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 break;
4536 }
4537
4538 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004539 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004540 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4541 // Branch cases into compressed and uncompressed for each index's type.
4542 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4543 NearLabel done, not_compressed;
4544 __ cmpl(Address(obj, count_offset), Immediate(0));
4545 codegen_->MaybeRecordImplicitNullCheck(instruction);
4546 __ j(kGreaterEqual, &not_compressed);
4547 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4548 __ jmp(&done);
4549 __ Bind(&not_compressed);
4550 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4551 __ Bind(&done);
4552 } else {
4553 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004555 break;
4556 }
4557
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004558 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004559 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004560 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004561 break;
4562 }
4563
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004564 case Primitive::kPrimNot: {
4565 static_assert(
4566 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4567 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004568 // /* HeapReference<Object> */ out =
4569 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4570 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004571 // Note that a potential implicit null check is handled in this
4572 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4573 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004574 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004575 } else {
4576 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004577 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4578 codegen_->MaybeRecordImplicitNullCheck(instruction);
4579 // If read barriers are enabled, emit read barriers other than
4580 // Baker's using a slow path (and also unpoison the loaded
4581 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004582 if (index.IsConstant()) {
4583 uint32_t offset =
4584 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004585 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4586 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004587 codegen_->MaybeGenerateReadBarrierSlow(
4588 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4589 }
4590 }
4591 break;
4592 }
4593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004595 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004596 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 break;
4598 }
4599
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004600 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004601 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004602 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004603 break;
4604 }
4605
4606 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004607 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004608 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004609 break;
4610 }
4611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004612 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004613 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004614 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615 }
Roland Levillain4d027112015-07-01 15:41:14 +01004616
4617 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004618 // Potential implicit null checks, in the case of reference
4619 // arrays, are handled in the previous switch statement.
4620 } else {
4621 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004622 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623}
4624
4625void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004626 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004627
4628 bool needs_write_barrier =
4629 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004630 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004631
Nicolas Geoffray39468442014-09-02 15:17:15 +01004632 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004633 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004634 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004635 LocationSummary::kCallOnSlowPath :
4636 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004637
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004638 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004639 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4640 if (Primitive::IsFloatingPointType(value_type)) {
4641 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004642 } else {
4643 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4644 }
4645
4646 if (needs_write_barrier) {
4647 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004648 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004649 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004650 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651}
4652
4653void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4654 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004655 Location array_loc = locations->InAt(0);
4656 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004657 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004658 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004659 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004660 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004661 bool needs_write_barrier =
4662 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004663 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4664 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4665 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666
4667 switch (value_type) {
4668 case Primitive::kPrimBoolean:
4669 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004670 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004671 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004672 if (value.IsRegister()) {
4673 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004674 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004675 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004676 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004678 break;
4679 }
4680
4681 case Primitive::kPrimShort:
4682 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004683 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004684 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004685 if (value.IsRegister()) {
4686 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004688 DCHECK(value.IsConstant()) << value;
4689 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004690 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004691 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692 break;
4693 }
4694
4695 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004696 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004697 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004698
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004699 if (!value.IsRegister()) {
4700 // Just setting null.
4701 DCHECK(instruction->InputAt(2)->IsNullConstant());
4702 DCHECK(value.IsConstant()) << value;
4703 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004704 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004705 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004706 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004707 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004708 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004709
4710 DCHECK(needs_write_barrier);
4711 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004712 // We cannot use a NearLabel for `done`, as its range may be too
4713 // short when Baker read barriers are enabled.
4714 Label done;
4715 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004716 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004717 Location temp_loc = locations->GetTemp(0);
4718 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004719 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004720 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4721 codegen_->AddSlowPath(slow_path);
4722 if (instruction->GetValueCanBeNull()) {
4723 __ testl(register_value, register_value);
4724 __ j(kNotEqual, &not_null);
4725 __ movl(address, Immediate(0));
4726 codegen_->MaybeRecordImplicitNullCheck(instruction);
4727 __ jmp(&done);
4728 __ Bind(&not_null);
4729 }
4730
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004731 // Note that when Baker read barriers are enabled, the type
4732 // checks are performed without read barriers. This is fine,
4733 // even in the case where a class object is in the from-space
4734 // after the flip, as a comparison involving such a type would
4735 // not produce a false positive; it may of course produce a
4736 // false negative, in which case we would take the ArraySet
4737 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004738
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004739 // /* HeapReference<Class> */ temp = array->klass_
4740 __ movl(temp, Address(array, class_offset));
4741 codegen_->MaybeRecordImplicitNullCheck(instruction);
4742 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004743
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004744 // /* HeapReference<Class> */ temp = temp->component_type_
4745 __ movl(temp, Address(temp, component_offset));
4746 // If heap poisoning is enabled, no need to unpoison `temp`
4747 // nor the object reference in `register_value->klass`, as
4748 // we are comparing two poisoned references.
4749 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004750
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004751 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4752 __ j(kEqual, &do_put);
4753 // If heap poisoning is enabled, the `temp` reference has
4754 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004755 __ MaybeUnpoisonHeapReference(temp);
4756
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004757 // If heap poisoning is enabled, no need to unpoison the
4758 // heap reference loaded below, as it is only used for a
4759 // comparison with null.
4760 __ cmpl(Address(temp, super_offset), Immediate(0));
4761 __ j(kNotEqual, slow_path->GetEntryLabel());
4762 __ Bind(&do_put);
4763 } else {
4764 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004765 }
4766 }
4767
4768 if (kPoisonHeapReferences) {
4769 __ movl(temp, register_value);
4770 __ PoisonHeapReference(temp);
4771 __ movl(address, temp);
4772 } else {
4773 __ movl(address, register_value);
4774 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004775 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004776 codegen_->MaybeRecordImplicitNullCheck(instruction);
4777 }
4778
4779 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4780 codegen_->MarkGCCard(
4781 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4782 __ Bind(&done);
4783
4784 if (slow_path != nullptr) {
4785 __ Bind(slow_path->GetExitLabel());
4786 }
4787
4788 break;
4789 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004790
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004791 case Primitive::kPrimInt: {
4792 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004793 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 if (value.IsRegister()) {
4795 __ movl(address, value.AsRegister<CpuRegister>());
4796 } else {
4797 DCHECK(value.IsConstant()) << value;
4798 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4799 __ movl(address, Immediate(v));
4800 }
4801 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004802 break;
4803 }
4804
4805 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004807 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 if (value.IsRegister()) {
4809 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004810 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004811 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004812 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004813 Address address_high =
4814 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004815 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816 }
4817 break;
4818 }
4819
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004820 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004821 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004822 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004823 if (value.IsFpuRegister()) {
4824 __ movss(address, value.AsFpuRegister<XmmRegister>());
4825 } else {
4826 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004827 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004828 __ movl(address, Immediate(v));
4829 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004830 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004831 break;
4832 }
4833
4834 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004836 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004837 if (value.IsFpuRegister()) {
4838 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4839 codegen_->MaybeRecordImplicitNullCheck(instruction);
4840 } else {
4841 int64_t v =
4842 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004843 Address address_high =
4844 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004845 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4846 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004847 break;
4848 }
4849
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850 case Primitive::kPrimVoid:
4851 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004852 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004853 }
4854}
4855
4856void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004857 LocationSummary* locations =
4858 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004859 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04004860 if (!instruction->IsEmittedAtUseSite()) {
4861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863}
4864
4865void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04004866 if (instruction->IsEmittedAtUseSite()) {
4867 return;
4868 }
4869
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004871 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004872 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4873 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004874 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004875 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07004876 // Mask out most significant bit in case the array is String's array of char.
4877 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
4878 __ andl(out, Immediate(INT32_MAX));
4879 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880}
4881
4882void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004883 RegisterSet caller_saves = RegisterSet::Empty();
4884 InvokeRuntimeCallingConvention calling_convention;
4885 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4886 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4887 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004888 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04004889 HInstruction* length = instruction->InputAt(1);
4890 if (!length->IsEmittedAtUseSite()) {
4891 locations->SetInAt(1, Location::RegisterOrConstant(length));
4892 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893}
4894
4895void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4896 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004897 Location index_loc = locations->InAt(0);
4898 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04004899 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900
Mark Mendell99dbd682015-04-22 16:18:52 -04004901 if (length_loc.IsConstant()) {
4902 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4903 if (index_loc.IsConstant()) {
4904 // BCE will remove the bounds check if we are guarenteed to pass.
4905 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4906 if (index < 0 || index >= length) {
4907 codegen_->AddSlowPath(slow_path);
4908 __ jmp(slow_path->GetEntryLabel());
4909 } else {
4910 // Some optimization after BCE may have generated this, and we should not
4911 // generate a bounds check if it is a valid range.
4912 }
4913 return;
4914 }
4915
4916 // We have to reverse the jump condition because the length is the constant.
4917 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4918 __ cmpl(index_reg, Immediate(length));
4919 codegen_->AddSlowPath(slow_path);
4920 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004921 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04004922 HInstruction* array_length = instruction->InputAt(1);
4923 if (array_length->IsEmittedAtUseSite()) {
4924 // Address the length field in the array.
4925 DCHECK(array_length->IsArrayLength());
4926 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
4927 Location array_loc = array_length->GetLocations()->InAt(0);
4928 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07004929 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4930 CpuRegister length_reg = CpuRegister(TMP);
4931 __ movl(length_reg, array_len);
4932 codegen_->MaybeRecordImplicitNullCheck(array_length);
4933 __ andl(length_reg, Immediate(INT32_MAX));
4934 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04004935 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07004936 // Checking the bound for general case:
4937 // Array of char or String's array when the compression feature off.
4938 if (index_loc.IsConstant()) {
4939 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4940 __ cmpl(array_len, Immediate(value));
4941 } else {
4942 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
4943 }
4944 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04004945 }
Mark Mendell99dbd682015-04-22 16:18:52 -04004946 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004947 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04004948 }
4949 codegen_->AddSlowPath(slow_path);
4950 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004951 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004952}
4953
4954void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4955 CpuRegister card,
4956 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004957 CpuRegister value,
4958 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004959 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004960 if (value_can_be_null) {
4961 __ testl(value, value);
4962 __ j(kEqual, &is_null);
4963 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004964 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004965 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966 __ movq(temp, object);
4967 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004968 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004969 if (value_can_be_null) {
4970 __ Bind(&is_null);
4971 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004972}
4973
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004974void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004975 LOG(FATAL) << "Unimplemented";
4976}
4977
4978void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004979 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4980}
4981
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004982void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004983 LocationSummary* locations =
4984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004985 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004986}
4987
4988void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004989 HBasicBlock* block = instruction->GetBlock();
4990 if (block->GetLoopInformation() != nullptr) {
4991 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4992 // The back edge will generate the suspend check.
4993 return;
4994 }
4995 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4996 // The goto will generate the suspend check.
4997 return;
4998 }
4999 GenerateSuspendCheck(instruction, nullptr);
5000}
5001
5002void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5003 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005004 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005005 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5006 if (slow_path == nullptr) {
5007 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5008 instruction->SetSlowPath(slow_path);
5009 codegen_->AddSlowPath(slow_path);
5010 if (successor != nullptr) {
5011 DCHECK(successor->IsLoopHeader());
5012 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5013 }
5014 } else {
5015 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5016 }
5017
Andreas Gampe542451c2016-07-26 09:02:02 -07005018 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005019 /* no_rip */ true),
5020 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005021 if (successor == nullptr) {
5022 __ j(kNotEqual, slow_path->GetEntryLabel());
5023 __ Bind(slow_path->GetReturnLabel());
5024 } else {
5025 __ j(kEqual, codegen_->GetLabelOf(successor));
5026 __ jmp(slow_path->GetEntryLabel());
5027 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005028}
5029
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005030X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5031 return codegen_->GetAssembler();
5032}
5033
5034void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005035 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005036 Location source = move->GetSource();
5037 Location destination = move->GetDestination();
5038
5039 if (source.IsRegister()) {
5040 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005041 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005042 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005043 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005044 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005045 } else {
5046 DCHECK(destination.IsDoubleStackSlot());
5047 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005048 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005049 }
5050 } else if (source.IsStackSlot()) {
5051 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005052 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005053 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005054 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005055 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005056 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005057 } else {
5058 DCHECK(destination.IsStackSlot());
5059 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5060 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5061 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005062 } else if (source.IsDoubleStackSlot()) {
5063 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005064 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005065 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005066 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005067 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5068 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005069 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005070 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005071 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5072 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5073 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005074 } else if (source.IsConstant()) {
5075 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005076 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5077 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005078 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005079 if (value == 0) {
5080 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5081 } else {
5082 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5083 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005084 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005085 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005086 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005087 }
5088 } else if (constant->IsLongConstant()) {
5089 int64_t value = constant->AsLongConstant()->GetValue();
5090 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005091 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005092 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005093 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005094 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005095 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005096 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005097 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005098 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005099 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005100 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005101 } else {
5102 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005103 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005104 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5105 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005106 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005107 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005108 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005109 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005110 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005111 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005112 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005113 } else {
5114 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005115 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005116 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005117 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005118 } else if (source.IsFpuRegister()) {
5119 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005121 } else if (destination.IsStackSlot()) {
5122 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005123 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005124 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005125 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005126 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005127 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005128 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005129 }
5130}
5131
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005132void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005133 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005134 __ movl(Address(CpuRegister(RSP), mem), reg);
5135 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005136}
5137
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005138void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005139 ScratchRegisterScope ensure_scratch(
5140 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5141
5142 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5143 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5144 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5145 Address(CpuRegister(RSP), mem2 + stack_offset));
5146 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5147 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5148 CpuRegister(ensure_scratch.GetRegister()));
5149}
5150
Mark Mendell8a1c7282015-06-29 15:41:28 -04005151void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5152 __ movq(CpuRegister(TMP), reg1);
5153 __ movq(reg1, reg2);
5154 __ movq(reg2, CpuRegister(TMP));
5155}
5156
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005157void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5158 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5159 __ movq(Address(CpuRegister(RSP), mem), reg);
5160 __ movq(reg, CpuRegister(TMP));
5161}
5162
5163void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5164 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005165 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005166
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005167 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5168 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5169 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5170 Address(CpuRegister(RSP), mem2 + stack_offset));
5171 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5172 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5173 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005174}
5175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005176void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5177 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5178 __ movss(Address(CpuRegister(RSP), mem), reg);
5179 __ movd(reg, CpuRegister(TMP));
5180}
5181
5182void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5183 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5184 __ movsd(Address(CpuRegister(RSP), mem), reg);
5185 __ movd(reg, CpuRegister(TMP));
5186}
5187
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005188void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005189 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005190 Location source = move->GetSource();
5191 Location destination = move->GetDestination();
5192
5193 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005194 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005195 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005196 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005197 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005198 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005199 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005200 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5201 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005202 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005203 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005204 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005205 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5206 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005208 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5209 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5210 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005212 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005213 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005216 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005219 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005220 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005221 }
5222}
5223
5224
5225void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5226 __ pushq(CpuRegister(reg));
5227}
5228
5229
5230void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5231 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005232}
5233
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005234void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005235 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005236 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5237 Immediate(mirror::Class::kStatusInitialized));
5238 __ j(kLess, slow_path->GetEntryLabel());
5239 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005240 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005241}
5242
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005243HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5244 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005245 switch (desired_class_load_kind) {
5246 case HLoadClass::LoadKind::kReferrersClass:
5247 break;
5248 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5249 DCHECK(!GetCompilerOptions().GetCompilePic());
5250 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5251 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5252 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5253 DCHECK(GetCompilerOptions().GetCompilePic());
5254 break;
5255 case HLoadClass::LoadKind::kBootImageAddress:
5256 break;
5257 case HLoadClass::LoadKind::kDexCacheAddress:
5258 DCHECK(Runtime::Current()->UseJitCompilation());
5259 break;
5260 case HLoadClass::LoadKind::kDexCachePcRelative:
5261 DCHECK(!Runtime::Current()->UseJitCompilation());
5262 break;
5263 case HLoadClass::LoadKind::kDexCacheViaMethod:
5264 break;
5265 }
5266 return desired_class_load_kind;
5267}
5268
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005269void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005270 if (cls->NeedsAccessCheck()) {
5271 InvokeRuntimeCallingConvention calling_convention;
5272 CodeGenerator::CreateLoadClassLocationSummary(
5273 cls,
5274 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5275 Location::RegisterLocation(RAX),
5276 /* code_generator_supports_read_barrier */ true);
5277 return;
5278 }
5279
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005280 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5281 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005282 ? LocationSummary::kCallOnSlowPath
5283 : LocationSummary::kNoCall;
5284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005285 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005286 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005287 }
5288
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005289 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5290 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5291 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5292 locations->SetInAt(0, Location::RequiresRegister());
5293 }
5294 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005295}
5296
5297void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005298 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005299 if (cls->NeedsAccessCheck()) {
5300 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005301 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005302 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005303 return;
5304 }
5305
Roland Levillain0d5a2812015-11-13 10:07:31 +00005306 Location out_loc = locations->Out();
5307 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005308
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005309 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005310 bool generate_null_check = false;
5311 switch (cls->GetLoadKind()) {
5312 case HLoadClass::LoadKind::kReferrersClass: {
5313 DCHECK(!cls->CanCallRuntime());
5314 DCHECK(!cls->MustGenerateClinitCheck());
5315 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5316 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5317 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005318 cls,
5319 out_loc,
5320 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005321 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005322 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005323 break;
5324 }
5325 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005326 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005327 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5328 codegen_->RecordTypePatch(cls);
5329 break;
5330 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005331 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005332 DCHECK_NE(cls->GetAddress(), 0u);
5333 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5334 __ movl(out, Immediate(address)); // Zero-extended.
5335 codegen_->RecordSimplePatch();
5336 break;
5337 }
5338 case HLoadClass::LoadKind::kDexCacheAddress: {
5339 DCHECK_NE(cls->GetAddress(), 0u);
5340 // /* GcRoot<mirror::Class> */ out = *address
5341 if (IsUint<32>(cls->GetAddress())) {
5342 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005343 GenerateGcRootFieldLoad(cls,
5344 out_loc,
5345 address,
Roland Levillain00468f32016-10-27 18:02:48 +01005346 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005347 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005348 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005349 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5350 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005351 GenerateGcRootFieldLoad(cls,
5352 out_loc,
5353 Address(out, 0),
Roland Levillain00468f32016-10-27 18:02:48 +01005354 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005355 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005356 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005357 generate_null_check = !cls->IsInDexCache();
5358 break;
5359 }
5360 case HLoadClass::LoadKind::kDexCachePcRelative: {
5361 uint32_t offset = cls->GetDexCacheElementOffset();
5362 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5363 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5364 /* no_rip */ false);
5365 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005366 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005367 generate_null_check = !cls->IsInDexCache();
5368 break;
5369 }
5370 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5371 // /* GcRoot<mirror::Class>[] */ out =
5372 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5373 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5374 __ movq(out,
5375 Address(current_method,
5376 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5377 // /* GcRoot<mirror::Class> */ out = out[type_index]
5378 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005379 cls,
5380 out_loc,
5381 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01005382 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005383 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005384 generate_null_check = !cls->IsInDexCache();
5385 break;
5386 }
5387 default:
5388 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5389 UNREACHABLE();
5390 }
5391
5392 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5393 DCHECK(cls->CanCallRuntime());
5394 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5395 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5396 codegen_->AddSlowPath(slow_path);
5397 if (generate_null_check) {
5398 __ testl(out, out);
5399 __ j(kEqual, slow_path->GetEntryLabel());
5400 }
5401 if (cls->MustGenerateClinitCheck()) {
5402 GenerateClassInitializationCheck(slow_path, out);
5403 } else {
5404 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005405 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005406 }
5407}
5408
5409void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5410 LocationSummary* locations =
5411 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5412 locations->SetInAt(0, Location::RequiresRegister());
5413 if (check->HasUses()) {
5414 locations->SetOut(Location::SameAsFirstInput());
5415 }
5416}
5417
5418void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005419 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005420 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005421 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005422 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005423 GenerateClassInitializationCheck(slow_path,
5424 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425}
5426
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005427HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5428 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005429 switch (desired_string_load_kind) {
5430 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5431 DCHECK(!GetCompilerOptions().GetCompilePic());
5432 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5433 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5434 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5435 DCHECK(GetCompilerOptions().GetCompilePic());
5436 break;
5437 case HLoadString::LoadKind::kBootImageAddress:
5438 break;
5439 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005440 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005441 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005442 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005443 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005444 break;
5445 case HLoadString::LoadKind::kDexCacheViaMethod:
5446 break;
5447 }
5448 return desired_string_load_kind;
5449}
5450
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005451void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005452 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005453 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5454 ? LocationSummary::kCallOnMainOnly
5455 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005456 : LocationSummary::kNoCall;
5457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005458 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005459 locations->SetOut(Location::RegisterLocation(RAX));
5460 } else {
5461 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005462 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5463 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5464 // Rely on the pResolveString and/or marking to save everything.
5465 // Custom calling convention: RAX serves as both input and output.
5466 RegisterSet caller_saves = RegisterSet::Empty();
5467 caller_saves.Add(Location::RegisterLocation(RAX));
5468 locations->SetCustomSlowPathCallerSaves(caller_saves);
5469 } else {
5470 // For non-Baker read barrier we have a temp-clobbering call.
5471 }
5472 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005474}
5475
5476void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005477 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005478 Location out_loc = locations->Out();
5479 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005480
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005481 switch (load->GetLoadKind()) {
5482 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005483 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005484 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005485 return; // No dex cache slow path.
5486 }
5487 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005488 DCHECK_NE(load->GetAddress(), 0u);
5489 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5490 __ movl(out, Immediate(address)); // Zero-extended.
5491 codegen_->RecordSimplePatch();
5492 return; // No dex cache slow path.
5493 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005494 case HLoadString::LoadKind::kBssEntry: {
5495 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5496 /* no_rip */ false);
5497 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5498 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Roland Levillain00468f32016-10-27 18:02:48 +01005499 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005500 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5501 codegen_->AddSlowPath(slow_path);
5502 __ testl(out, out);
5503 __ j(kEqual, slow_path->GetEntryLabel());
5504 __ Bind(slow_path->GetExitLabel());
5505 return;
5506 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005507 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005508 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005509 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005510
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005511 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005512 // Custom calling convention: RAX serves as both input and output.
5513 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex()));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005514 codegen_->InvokeRuntime(kQuickResolveString,
5515 load,
5516 load->GetDexPc());
5517 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005518}
5519
David Brazdilcb1c0552015-08-04 16:22:25 +01005520static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005521 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005522 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005523}
5524
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005525void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5526 LocationSummary* locations =
5527 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5528 locations->SetOut(Location::RequiresRegister());
5529}
5530
5531void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005532 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5533}
5534
5535void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5536 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5537}
5538
5539void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5540 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005541}
5542
5543void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5544 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005546 InvokeRuntimeCallingConvention calling_convention;
5547 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5548}
5549
5550void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005551 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005552 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005553}
5554
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005555static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5556 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005557 !kUseBakerReadBarrier &&
5558 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005559 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5560 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5561}
5562
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005563void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005564 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005565 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005566 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568 case TypeCheckKind::kExactCheck:
5569 case TypeCheckKind::kAbstractClassCheck:
5570 case TypeCheckKind::kClassHierarchyCheck:
5571 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005572 call_kind =
5573 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005574 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005575 break;
5576 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005577 case TypeCheckKind::kUnresolvedCheck:
5578 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005579 call_kind = LocationSummary::kCallOnSlowPath;
5580 break;
5581 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005582
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005584 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005585 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005586 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005587 locations->SetInAt(0, Location::RequiresRegister());
5588 locations->SetInAt(1, Location::Any());
5589 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5590 locations->SetOut(Location::RequiresRegister());
5591 // When read barriers are enabled, we need a temporary register for
5592 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005593 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005594 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005596}
5597
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005598void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005599 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005600 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005601 Location obj_loc = locations->InAt(0);
5602 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005603 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005604 Location out_loc = locations->Out();
5605 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005606 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005607 locations->GetTemp(0) :
5608 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005609 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005610 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5611 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5612 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005613 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005614 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005615
5616 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005617 // Avoid null check if we know obj is not null.
5618 if (instruction->MustDoNullCheck()) {
5619 __ testl(obj, obj);
5620 __ j(kEqual, &zero);
5621 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622
Roland Levillain0d5a2812015-11-13 10:07:31 +00005623 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005624 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005626 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 case TypeCheckKind::kExactCheck: {
5628 if (cls.IsRegister()) {
5629 __ cmpl(out, cls.AsRegister<CpuRegister>());
5630 } else {
5631 DCHECK(cls.IsStackSlot()) << cls;
5632 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5633 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005634 if (zero.IsLinked()) {
5635 // Classes must be equal for the instanceof to succeed.
5636 __ j(kNotEqual, &zero);
5637 __ movl(out, Immediate(1));
5638 __ jmp(&done);
5639 } else {
5640 __ setcc(kEqual, out);
5641 // setcc only sets the low byte.
5642 __ andl(out, Immediate(1));
5643 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 break;
5645 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005646
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005647 case TypeCheckKind::kAbstractClassCheck: {
5648 // If the class is abstract, we eagerly fetch the super class of the
5649 // object to avoid doing a comparison we know will fail.
5650 NearLabel loop, success;
5651 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005652 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005653 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 __ testl(out, out);
5655 // If `out` is null, we use it for the result, and jump to `done`.
5656 __ j(kEqual, &done);
5657 if (cls.IsRegister()) {
5658 __ cmpl(out, cls.AsRegister<CpuRegister>());
5659 } else {
5660 DCHECK(cls.IsStackSlot()) << cls;
5661 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5662 }
5663 __ j(kNotEqual, &loop);
5664 __ movl(out, Immediate(1));
5665 if (zero.IsLinked()) {
5666 __ jmp(&done);
5667 }
5668 break;
5669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 case TypeCheckKind::kClassHierarchyCheck: {
5672 // Walk over the class hierarchy to find a match.
5673 NearLabel loop, success;
5674 __ Bind(&loop);
5675 if (cls.IsRegister()) {
5676 __ cmpl(out, cls.AsRegister<CpuRegister>());
5677 } else {
5678 DCHECK(cls.IsStackSlot()) << cls;
5679 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5680 }
5681 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005682 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005683 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 __ testl(out, out);
5685 __ j(kNotEqual, &loop);
5686 // If `out` is null, we use it for the result, and jump to `done`.
5687 __ jmp(&done);
5688 __ Bind(&success);
5689 __ movl(out, Immediate(1));
5690 if (zero.IsLinked()) {
5691 __ jmp(&done);
5692 }
5693 break;
5694 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005695
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005697 // Do an exact check.
5698 NearLabel exact_check;
5699 if (cls.IsRegister()) {
5700 __ cmpl(out, cls.AsRegister<CpuRegister>());
5701 } else {
5702 DCHECK(cls.IsStackSlot()) << cls;
5703 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5704 }
5705 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005707 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005708 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005709 __ testl(out, out);
5710 // If `out` is null, we use it for the result, and jump to `done`.
5711 __ j(kEqual, &done);
5712 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5713 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005714 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 __ movl(out, Immediate(1));
5716 __ jmp(&done);
5717 break;
5718 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005719
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 case TypeCheckKind::kArrayCheck: {
5721 if (cls.IsRegister()) {
5722 __ cmpl(out, cls.AsRegister<CpuRegister>());
5723 } else {
5724 DCHECK(cls.IsStackSlot()) << cls;
5725 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5726 }
5727 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5729 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005730 codegen_->AddSlowPath(slow_path);
5731 __ j(kNotEqual, slow_path->GetEntryLabel());
5732 __ movl(out, Immediate(1));
5733 if (zero.IsLinked()) {
5734 __ jmp(&done);
5735 }
5736 break;
5737 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005738
Calin Juravle98893e12015-10-02 21:05:03 +01005739 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005740 case TypeCheckKind::kInterfaceCheck: {
5741 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005742 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743 // cases.
5744 //
5745 // We cannot directly call the InstanceofNonTrivial runtime
5746 // entry point without resorting to a type checking slow path
5747 // here (i.e. by calling InvokeRuntime directly), as it would
5748 // require to assign fixed registers for the inputs of this
5749 // HInstanceOf instruction (following the runtime calling
5750 // convention), which might be cluttered by the potential first
5751 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005752 //
5753 // TODO: Introduce a new runtime entry point taking the object
5754 // to test (instead of its class) as argument, and let it deal
5755 // with the read barrier issues. This will let us refactor this
5756 // case of the `switch` code as it was previously (with a direct
5757 // call to the runtime not using a type checking slow path).
5758 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759 DCHECK(locations->OnlyCallsOnSlowPath());
5760 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5761 /* is_fatal */ false);
5762 codegen_->AddSlowPath(slow_path);
5763 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 if (zero.IsLinked()) {
5765 __ jmp(&done);
5766 }
5767 break;
5768 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005769 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005770
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005771 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005772 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 __ xorl(out, out);
5774 }
5775
5776 if (done.IsLinked()) {
5777 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005778 }
5779
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005780 if (slow_path != nullptr) {
5781 __ Bind(slow_path->GetExitLabel());
5782 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005783}
5784
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005785void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5787 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5789 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 case TypeCheckKind::kExactCheck:
5791 case TypeCheckKind::kAbstractClassCheck:
5792 case TypeCheckKind::kClassHierarchyCheck:
5793 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5795 LocationSummary::kCallOnSlowPath :
5796 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 break;
5798 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 case TypeCheckKind::kUnresolvedCheck:
5800 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 call_kind = LocationSummary::kCallOnSlowPath;
5802 break;
5803 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5805 locations->SetInAt(0, Location::RequiresRegister());
5806 locations->SetInAt(1, Location::Any());
5807 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5808 locations->AddTemp(Location::RequiresRegister());
5809 // When read barriers are enabled, we need an additional temporary
5810 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005811 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005813 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005814}
5815
5816void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005817 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 Location obj_loc = locations->InAt(0);
5820 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005821 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 Location temp_loc = locations->GetTemp(0);
5823 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005824 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005825 locations->GetTemp(1) :
5826 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005827 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5828 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5829 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5830 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831
Roland Levillain0d5a2812015-11-13 10:07:31 +00005832 bool is_type_check_slow_path_fatal =
5833 (type_check_kind == TypeCheckKind::kExactCheck ||
5834 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5835 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5836 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5837 !instruction->CanThrowIntoCatchBlock();
5838 SlowPathCode* type_check_slow_path =
5839 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5840 is_type_check_slow_path_fatal);
5841 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 case TypeCheckKind::kExactCheck:
5845 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005846 NearLabel done;
5847 // Avoid null check if we know obj is not null.
5848 if (instruction->MustDoNullCheck()) {
5849 __ testl(obj, obj);
5850 __ j(kEqual, &done);
5851 }
5852
5853 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005854 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005855
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005856 if (cls.IsRegister()) {
5857 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5858 } else {
5859 DCHECK(cls.IsStackSlot()) << cls;
5860 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5861 }
5862 // Jump to slow path for throwing the exception or doing a
5863 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005865 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005866 break;
5867 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005868
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005870 NearLabel done;
5871 // Avoid null check if we know obj is not null.
5872 if (instruction->MustDoNullCheck()) {
5873 __ testl(obj, obj);
5874 __ j(kEqual, &done);
5875 }
5876
5877 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005878 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005879
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 // If the class is abstract, we eagerly fetch the super class of the
5881 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005882 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005885 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005886
5887 // If the class reference currently in `temp` is not null, jump
5888 // to the `compare_classes` label to compare it with the checked
5889 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 __ j(kNotEqual, &compare_classes);
5892 // Otherwise, jump to the slow path to throw the exception.
5893 //
5894 // But before, move back the object's class into `temp` before
5895 // going into the slow path, as it has been overwritten in the
5896 // meantime.
5897 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005898 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 __ jmp(type_check_slow_path->GetEntryLabel());
5900
5901 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 if (cls.IsRegister()) {
5903 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5904 } else {
5905 DCHECK(cls.IsStackSlot()) << cls;
5906 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5907 }
5908 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005909 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 break;
5911 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005914 NearLabel done;
5915 // Avoid null check if we know obj is not null.
5916 if (instruction->MustDoNullCheck()) {
5917 __ testl(obj, obj);
5918 __ j(kEqual, &done);
5919 }
5920
5921 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005922 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005923
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005924 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005925 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005926 __ Bind(&loop);
5927 if (cls.IsRegister()) {
5928 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5929 } else {
5930 DCHECK(cls.IsStackSlot()) << cls;
5931 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5932 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005933 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005936 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005937
5938 // If the class reference currently in `temp` is not null, jump
5939 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 __ testl(temp, temp);
5941 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 // Otherwise, jump to the slow path to throw the exception.
5943 //
5944 // But before, move back the object's class into `temp` before
5945 // going into the slow path, as it has been overwritten in the
5946 // meantime.
5947 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005948 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005949 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005950 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005951 break;
5952 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005953
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005954 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005955 // We cannot use a NearLabel here, as its range might be too
5956 // short in some cases when read barriers are enabled. This has
5957 // been observed for instance when the code emitted for this
5958 // case uses high x86-64 registers (R8-R15).
5959 Label done;
5960 // Avoid null check if we know obj is not null.
5961 if (instruction->MustDoNullCheck()) {
5962 __ testl(obj, obj);
5963 __ j(kEqual, &done);
5964 }
5965
5966 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005967 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005968
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005969 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005971 if (cls.IsRegister()) {
5972 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5973 } else {
5974 DCHECK(cls.IsStackSlot()) << cls;
5975 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5976 }
5977 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978
5979 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005980 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005981 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005982
5983 // If the component type is not null (i.e. the object is indeed
5984 // an array), jump to label `check_non_primitive_component_type`
5985 // to further check that this component type is not a primitive
5986 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988 __ j(kNotEqual, &check_non_primitive_component_type);
5989 // Otherwise, jump to the slow path to throw the exception.
5990 //
5991 // But before, move back the object's class into `temp` before
5992 // going into the slow path, as it has been overwritten in the
5993 // meantime.
5994 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005995 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996 __ jmp(type_check_slow_path->GetEntryLabel());
5997
5998 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000 __ j(kEqual, &done);
6001 // Same comment as above regarding `temp` and the slow path.
6002 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006003 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006004 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006005 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 break;
6007 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008
Calin Juravle98893e12015-10-02 21:05:03 +01006009 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006011 NearLabel done;
6012 // Avoid null check if we know obj is not null.
6013 if (instruction->MustDoNullCheck()) {
6014 __ testl(obj, obj);
6015 __ j(kEqual, &done);
6016 }
6017
6018 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006019 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006020
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006021 // We always go into the type check slow path for the unresolved
6022 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006023 //
6024 // We cannot directly call the CheckCast runtime entry point
6025 // without resorting to a type checking slow path here (i.e. by
6026 // calling InvokeRuntime directly), as it would require to
6027 // assign fixed registers for the inputs of this HInstanceOf
6028 // instruction (following the runtime calling convention), which
6029 // might be cluttered by the potential first read barrier
6030 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006031 //
6032 // TODO: Introduce a new runtime entry point taking the object
6033 // to test (instead of its class) as argument, and let it deal
6034 // with the read barrier issues. This will let us refactor this
6035 // case of the `switch` code as it was previously (with a direct
6036 // call to the runtime not using a type checking slow path).
6037 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006038 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006039 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006040 break;
6041 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006042
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006044}
6045
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006046void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6047 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006049 InvokeRuntimeCallingConvention calling_convention;
6050 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6051}
6052
6053void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006054 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006055 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006056 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006057 if (instruction->IsEnter()) {
6058 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6059 } else {
6060 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6061 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006062}
6063
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006064void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6065void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6066void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6067
6068void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6069 LocationSummary* locations =
6070 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6071 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6072 || instruction->GetResultType() == Primitive::kPrimLong);
6073 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006074 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006075 locations->SetOut(Location::SameAsFirstInput());
6076}
6077
6078void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6079 HandleBitwiseOperation(instruction);
6080}
6081
6082void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6083 HandleBitwiseOperation(instruction);
6084}
6085
6086void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6087 HandleBitwiseOperation(instruction);
6088}
6089
6090void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6091 LocationSummary* locations = instruction->GetLocations();
6092 Location first = locations->InAt(0);
6093 Location second = locations->InAt(1);
6094 DCHECK(first.Equals(locations->Out()));
6095
6096 if (instruction->GetResultType() == Primitive::kPrimInt) {
6097 if (second.IsRegister()) {
6098 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006099 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006100 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006101 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
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>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006105 }
6106 } else if (second.IsConstant()) {
6107 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6108 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006109 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006110 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006111 __ orl(first.AsRegister<CpuRegister>(), imm);
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>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006115 }
6116 } else {
6117 Address address(CpuRegister(RSP), second.GetStackIndex());
6118 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006119 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006120 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006121 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006122 } else {
6123 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006124 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006125 }
6126 }
6127 } else {
6128 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006129 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6130 bool second_is_constant = false;
6131 int64_t value = 0;
6132 if (second.IsConstant()) {
6133 second_is_constant = true;
6134 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006135 }
Mark Mendell40741f32015-04-20 22:10:34 -04006136 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006137
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006138 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006139 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006140 if (is_int32_value) {
6141 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6142 } else {
6143 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6144 }
6145 } else if (second.IsDoubleStackSlot()) {
6146 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006147 } else {
6148 __ andq(first_reg, second.AsRegister<CpuRegister>());
6149 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006150 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006151 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006152 if (is_int32_value) {
6153 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6154 } else {
6155 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6156 }
6157 } else if (second.IsDoubleStackSlot()) {
6158 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006159 } else {
6160 __ orq(first_reg, second.AsRegister<CpuRegister>());
6161 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006162 } else {
6163 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006164 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006165 if (is_int32_value) {
6166 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6167 } else {
6168 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6169 }
6170 } else if (second.IsDoubleStackSlot()) {
6171 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006172 } else {
6173 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6174 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006175 }
6176 }
6177}
6178
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006179void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6180 Location out,
6181 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006182 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006183 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6184 if (kEmitCompilerReadBarrier) {
6185 if (kUseBakerReadBarrier) {
6186 // Load with fast path based Baker's read barrier.
6187 // /* HeapReference<Object> */ out = *(out + offset)
6188 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006189 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006190 } else {
6191 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006192 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006193 // in the following move operation, as we will need it for the
6194 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006195 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006196 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006197 // /* HeapReference<Object> */ out = *(out + offset)
6198 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006199 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006200 }
6201 } else {
6202 // Plain load with no read barrier.
6203 // /* HeapReference<Object> */ out = *(out + offset)
6204 __ movl(out_reg, Address(out_reg, offset));
6205 __ MaybeUnpoisonHeapReference(out_reg);
6206 }
6207}
6208
6209void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6210 Location out,
6211 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006212 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006213 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6214 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6215 if (kEmitCompilerReadBarrier) {
6216 if (kUseBakerReadBarrier) {
6217 // Load with fast path based Baker's read barrier.
6218 // /* HeapReference<Object> */ out = *(obj + offset)
6219 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006220 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006221 } else {
6222 // Load with slow path based read barrier.
6223 // /* HeapReference<Object> */ out = *(obj + offset)
6224 __ movl(out_reg, Address(obj_reg, offset));
6225 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6226 }
6227 } else {
6228 // Plain load with no read barrier.
6229 // /* HeapReference<Object> */ out = *(obj + offset)
6230 __ movl(out_reg, Address(obj_reg, offset));
6231 __ MaybeUnpoisonHeapReference(out_reg);
6232 }
6233}
6234
6235void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6236 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006238 Label* fixup_label,
6239 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006240 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006241 if (requires_read_barrier) {
6242 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006243 if (kUseBakerReadBarrier) {
6244 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6245 // Baker's read barrier are used:
6246 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006248 // if (Thread::Current()->GetIsGcMarking()) {
6249 // root = ReadBarrier::Mark(root)
6250 // }
6251
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006252 // /* GcRoot<mirror::Object> */ root = *address
6253 __ movl(root_reg, address);
6254 if (fixup_label != nullptr) {
6255 __ Bind(fixup_label);
6256 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006257 static_assert(
6258 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6259 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6260 "have different sizes.");
6261 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6262 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6263 "have different sizes.");
6264
Vladimir Marko953437b2016-08-24 08:30:46 +00006265 // Slow path marking the GC root `root`.
6266 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6267 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006268 codegen_->AddSlowPath(slow_path);
6269
Andreas Gampe542451c2016-07-26 09:02:02 -07006270 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006271 /* no_rip */ true),
6272 Immediate(0));
6273 __ j(kNotEqual, slow_path->GetEntryLabel());
6274 __ Bind(slow_path->GetExitLabel());
6275 } else {
6276 // GC root loaded through a slow path for read barriers other
6277 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006278 // /* GcRoot<mirror::Object>* */ root = address
6279 __ leaq(root_reg, address);
6280 if (fixup_label != nullptr) {
6281 __ Bind(fixup_label);
6282 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006283 // /* mirror::Object* */ root = root->Read()
6284 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6285 }
6286 } else {
6287 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006288 // /* GcRoot<mirror::Object> */ root = *address
6289 __ movl(root_reg, address);
6290 if (fixup_label != nullptr) {
6291 __ Bind(fixup_label);
6292 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006293 // Note that GC roots are not affected by heap poisoning, thus we
6294 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006295 }
6296}
6297
6298void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6299 Location ref,
6300 CpuRegister obj,
6301 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006302 bool needs_null_check) {
6303 DCHECK(kEmitCompilerReadBarrier);
6304 DCHECK(kUseBakerReadBarrier);
6305
6306 // /* HeapReference<Object> */ ref = *(obj + offset)
6307 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006308 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006309}
6310
6311void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6312 Location ref,
6313 CpuRegister obj,
6314 uint32_t data_offset,
6315 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006316 bool needs_null_check) {
6317 DCHECK(kEmitCompilerReadBarrier);
6318 DCHECK(kUseBakerReadBarrier);
6319
Roland Levillain3d312422016-06-23 13:53:42 +01006320 static_assert(
6321 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6322 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006323 // /* HeapReference<Object> */ ref =
6324 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006325 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006326 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006327}
6328
6329void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6330 Location ref,
6331 CpuRegister obj,
6332 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006333 bool needs_null_check) {
6334 DCHECK(kEmitCompilerReadBarrier);
6335 DCHECK(kUseBakerReadBarrier);
6336
6337 // In slow path based read barriers, the read barrier call is
6338 // inserted after the original load. However, in fast path based
6339 // Baker's read barriers, we need to perform the load of
6340 // mirror::Object::monitor_ *before* the original reference load.
6341 // This load-load ordering is required by the read barrier.
6342 // The fast path/slow path (for Baker's algorithm) should look like:
6343 //
6344 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6345 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6346 // HeapReference<Object> ref = *src; // Original reference load.
6347 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6348 // if (is_gray) {
6349 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6350 // }
6351 //
6352 // Note: the original implementation in ReadBarrier::Barrier is
6353 // slightly more complex as:
6354 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006355 // the high-bits of rb_state, which are expected to be all zeroes
6356 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6357 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006358 // - it performs additional checks that we do not do here for
6359 // performance reasons.
6360
6361 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006362 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6363
Vladimir Marko953437b2016-08-24 08:30:46 +00006364 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6365 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6366 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6367 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6368 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6369 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6370 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6371
6372 // if (rb_state == ReadBarrier::gray_ptr_)
6373 // ref = ReadBarrier::Mark(ref);
6374 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6375 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006376 if (needs_null_check) {
6377 MaybeRecordImplicitNullCheck(instruction);
6378 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006379
6380 // Load fence to prevent load-load reordering.
6381 // Note that this is a no-op, thanks to the x86-64 memory model.
6382 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6383
6384 // The actual reference load.
6385 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006386 __ movl(ref_reg, src); // Flags are unaffected.
6387
6388 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6389 // Slow path marking the object `ref` when it is gray.
6390 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6391 instruction, ref, /* unpoison */ true);
6392 AddSlowPath(slow_path);
6393
6394 // We have done the "if" of the gray bit check above, now branch based on the flags.
6395 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006396
6397 // Object* ref = ref_addr->AsMirrorPtr()
6398 __ MaybeUnpoisonHeapReference(ref_reg);
6399
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006400 __ Bind(slow_path->GetExitLabel());
6401}
6402
6403void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6404 Location out,
6405 Location ref,
6406 Location obj,
6407 uint32_t offset,
6408 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 DCHECK(kEmitCompilerReadBarrier);
6410
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006411 // Insert a slow path based read barrier *after* the reference load.
6412 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 // If heap poisoning is enabled, the unpoisoning of the loaded
6414 // reference will be carried out by the runtime within the slow
6415 // path.
6416 //
6417 // Note that `ref` currently does not get unpoisoned (when heap
6418 // poisoning is enabled), which is alright as the `ref` argument is
6419 // not used by the artReadBarrierSlow entry point.
6420 //
6421 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6422 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6423 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6424 AddSlowPath(slow_path);
6425
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 __ jmp(slow_path->GetEntryLabel());
6427 __ Bind(slow_path->GetExitLabel());
6428}
6429
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006430void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6431 Location out,
6432 Location ref,
6433 Location obj,
6434 uint32_t offset,
6435 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006437 // Baker's read barriers shall be handled by the fast path
6438 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6439 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006440 // If heap poisoning is enabled, unpoisoning will be taken care of
6441 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006442 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006443 } else if (kPoisonHeapReferences) {
6444 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6445 }
6446}
6447
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006448void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6449 Location out,
6450 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 DCHECK(kEmitCompilerReadBarrier);
6452
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006453 // Insert a slow path based read barrier *after* the GC root load.
6454 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 // Note that GC roots are not affected by heap poisoning, so we do
6456 // not need to do anything special for this here.
6457 SlowPathCode* slow_path =
6458 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6459 AddSlowPath(slow_path);
6460
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461 __ jmp(slow_path->GetEntryLabel());
6462 __ Bind(slow_path->GetExitLabel());
6463}
6464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006465void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006466 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006467 LOG(FATAL) << "Unreachable";
6468}
6469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006470void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006471 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006472 LOG(FATAL) << "Unreachable";
6473}
6474
Mark Mendellfe57faa2015-09-18 09:26:15 -04006475// Simple implementation of packed switch - generate cascaded compare/jumps.
6476void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6477 LocationSummary* locations =
6478 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6479 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006480 locations->AddTemp(Location::RequiresRegister());
6481 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006482}
6483
6484void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6485 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006486 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006487 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006488 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6489 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6490 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006491 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6492
6493 // Should we generate smaller inline compare/jumps?
6494 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6495 // Figure out the correct compare values and jump conditions.
6496 // Handle the first compare/branch as a special case because it might
6497 // jump to the default case.
6498 DCHECK_GT(num_entries, 2u);
6499 Condition first_condition;
6500 uint32_t index;
6501 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6502 if (lower_bound != 0) {
6503 first_condition = kLess;
6504 __ cmpl(value_reg_in, Immediate(lower_bound));
6505 __ j(first_condition, codegen_->GetLabelOf(default_block));
6506 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6507
6508 index = 1;
6509 } else {
6510 // Handle all the compare/jumps below.
6511 first_condition = kBelow;
6512 index = 0;
6513 }
6514
6515 // Handle the rest of the compare/jumps.
6516 for (; index + 1 < num_entries; index += 2) {
6517 int32_t compare_to_value = lower_bound + index + 1;
6518 __ cmpl(value_reg_in, Immediate(compare_to_value));
6519 // Jump to successors[index] if value < case_value[index].
6520 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6521 // Jump to successors[index + 1] if value == case_value[index + 1].
6522 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6523 }
6524
6525 if (index != num_entries) {
6526 // There are an odd number of entries. Handle the last one.
6527 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006528 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006529 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6530 }
6531
6532 // And the default for any other value.
6533 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6534 __ jmp(codegen_->GetLabelOf(default_block));
6535 }
6536 return;
6537 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006538
6539 // Remove the bias, if needed.
6540 Register value_reg_out = value_reg_in.AsRegister();
6541 if (lower_bound != 0) {
6542 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6543 value_reg_out = temp_reg.AsRegister();
6544 }
6545 CpuRegister value_reg(value_reg_out);
6546
6547 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006548 __ cmpl(value_reg, Immediate(num_entries - 1));
6549 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006550
Mark Mendell9c86b482015-09-18 13:36:07 -04006551 // We are in the range of the table.
6552 // Load the address of the jump table in the constant area.
6553 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006554
Mark Mendell9c86b482015-09-18 13:36:07 -04006555 // Load the (signed) offset from the jump table.
6556 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6557
6558 // Add the offset to the address of the table base.
6559 __ addq(temp_reg, base_reg);
6560
6561 // And jump.
6562 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006563}
6564
Aart Bikc5d47542016-01-27 17:00:35 -08006565void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6566 if (value == 0) {
6567 __ xorl(dest, dest);
6568 } else {
6569 __ movl(dest, Immediate(value));
6570 }
6571}
6572
Mark Mendell92e83bf2015-05-07 11:25:03 -04006573void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6574 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006575 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006576 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006577 } else if (IsUint<32>(value)) {
6578 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006579 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6580 } else {
6581 __ movq(dest, Immediate(value));
6582 }
6583}
6584
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006585void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6586 if (value == 0) {
6587 __ xorps(dest, dest);
6588 } else {
6589 __ movss(dest, LiteralInt32Address(value));
6590 }
6591}
6592
6593void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6594 if (value == 0) {
6595 __ xorpd(dest, dest);
6596 } else {
6597 __ movsd(dest, LiteralInt64Address(value));
6598 }
6599}
6600
6601void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6602 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6603}
6604
6605void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6606 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6607}
6608
Aart Bika19616e2016-02-01 18:57:58 -08006609void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6610 if (value == 0) {
6611 __ testl(dest, dest);
6612 } else {
6613 __ cmpl(dest, Immediate(value));
6614 }
6615}
6616
6617void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6618 if (IsInt<32>(value)) {
6619 if (value == 0) {
6620 __ testq(dest, dest);
6621 } else {
6622 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6623 }
6624 } else {
6625 // Value won't fit in an int.
6626 __ cmpq(dest, LiteralInt64Address(value));
6627 }
6628}
6629
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006630void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6631 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006632 GenerateIntCompare(lhs_reg, rhs);
6633}
6634
6635void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006636 if (rhs.IsConstant()) {
6637 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006638 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006639 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006640 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006641 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006642 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006643 }
6644}
6645
6646void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6647 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6648 if (rhs.IsConstant()) {
6649 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6650 Compare64BitValue(lhs_reg, value);
6651 } else if (rhs.IsDoubleStackSlot()) {
6652 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6653 } else {
6654 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6655 }
6656}
6657
6658Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6659 Location index,
6660 ScaleFactor scale,
6661 uint32_t data_offset) {
6662 return index.IsConstant() ?
6663 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6664 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6665}
6666
Mark Mendellcfa410b2015-05-25 16:02:44 -04006667void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6668 DCHECK(dest.IsDoubleStackSlot());
6669 if (IsInt<32>(value)) {
6670 // Can move directly as an int32 constant.
6671 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6672 Immediate(static_cast<int32_t>(value)));
6673 } else {
6674 Load64BitValue(CpuRegister(TMP), value);
6675 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6676 }
6677}
6678
Mark Mendell9c86b482015-09-18 13:36:07 -04006679/**
6680 * Class to handle late fixup of offsets into constant area.
6681 */
6682class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6683 public:
6684 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6685 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6686
6687 protected:
6688 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6689
6690 CodeGeneratorX86_64* codegen_;
6691
6692 private:
6693 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6694 // Patch the correct offset for the instruction. We use the address of the
6695 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6696 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6697 int32_t relative_position = constant_offset - pos;
6698
6699 // Patch in the right value.
6700 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6701 }
6702
6703 // Location in constant area that the fixup refers to.
6704 size_t offset_into_constant_area_;
6705};
6706
6707/**
6708 t * Class to handle late fixup of offsets to a jump table that will be created in the
6709 * constant area.
6710 */
6711class JumpTableRIPFixup : public RIPFixup {
6712 public:
6713 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6714 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6715
6716 void CreateJumpTable() {
6717 X86_64Assembler* assembler = codegen_->GetAssembler();
6718
6719 // Ensure that the reference to the jump table has the correct offset.
6720 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6721 SetOffset(offset_in_constant_table);
6722
6723 // Compute the offset from the start of the function to this jump table.
6724 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6725
6726 // Populate the jump table with the correct values for the jump table.
6727 int32_t num_entries = switch_instr_->GetNumEntries();
6728 HBasicBlock* block = switch_instr_->GetBlock();
6729 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6730 // The value that we want is the target offset - the position of the table.
6731 for (int32_t i = 0; i < num_entries; i++) {
6732 HBasicBlock* b = successors[i];
6733 Label* l = codegen_->GetLabelOf(b);
6734 DCHECK(l->IsBound());
6735 int32_t offset_to_block = l->Position() - current_table_offset;
6736 assembler->AppendInt32(offset_to_block);
6737 }
6738 }
6739
6740 private:
6741 const HPackedSwitch* switch_instr_;
6742};
6743
Mark Mendellf55c3e02015-03-26 21:07:46 -04006744void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6745 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006746 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006747 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6748 // 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 -04006749 assembler->Align(4, 0);
6750 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006751
6752 // Populate any jump tables.
6753 for (auto jump_table : fixups_to_jump_tables_) {
6754 jump_table->CreateJumpTable();
6755 }
6756
6757 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006758 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006759 }
6760
6761 // And finish up.
6762 CodeGenerator::Finalize(allocator);
6763}
6764
Mark Mendellf55c3e02015-03-26 21:07:46 -04006765Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6766 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6767 return Address::RIP(fixup);
6768}
6769
6770Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6771 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6772 return Address::RIP(fixup);
6773}
6774
6775Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6776 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6777 return Address::RIP(fixup);
6778}
6779
6780Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6781 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6782 return Address::RIP(fixup);
6783}
6784
Andreas Gampe85b62f22015-09-09 13:15:38 -07006785// TODO: trg as memory.
6786void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6787 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006788 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006789 return;
6790 }
6791
6792 DCHECK_NE(type, Primitive::kPrimVoid);
6793
6794 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6795 if (trg.Equals(return_loc)) {
6796 return;
6797 }
6798
6799 // Let the parallel move resolver take care of all of this.
6800 HParallelMove parallel_move(GetGraph()->GetArena());
6801 parallel_move.AddMove(return_loc, trg, type, nullptr);
6802 GetMoveResolver()->EmitNativeCode(&parallel_move);
6803}
6804
Mark Mendell9c86b482015-09-18 13:36:07 -04006805Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6806 // Create a fixup to be used to create and address the jump table.
6807 JumpTableRIPFixup* table_fixup =
6808 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6809
6810 // We have to populate the jump tables.
6811 fixups_to_jump_tables_.push_back(table_fixup);
6812 return Address::RIP(table_fixup);
6813}
6814
Mark Mendellea5af682015-10-22 17:35:49 -04006815void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6816 const Address& addr_high,
6817 int64_t v,
6818 HInstruction* instruction) {
6819 if (IsInt<32>(v)) {
6820 int32_t v_32 = v;
6821 __ movq(addr_low, Immediate(v_32));
6822 MaybeRecordImplicitNullCheck(instruction);
6823 } else {
6824 // Didn't fit in a register. Do it in pieces.
6825 int32_t low_v = Low32Bits(v);
6826 int32_t high_v = High32Bits(v);
6827 __ movl(addr_low, Immediate(low_v));
6828 MaybeRecordImplicitNullCheck(instruction);
6829 __ movl(addr_high, Immediate(high_v));
6830 }
6831}
6832
Roland Levillain4d027112015-07-01 15:41:14 +01006833#undef __
6834
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006835} // namespace x86_64
6836} // namespace art