blob: 6518c32576555ffa2bf2bf451e8f6e531c47db0f [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 Levillain02b75802016-07-13 11:54:35 +0100460 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000461 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100462 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000463 DCHECK(instruction_->IsInstanceFieldGet() ||
464 instruction_->IsStaticFieldGet() ||
465 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100466 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000467 instruction_->IsLoadClass() ||
468 instruction_->IsLoadString() ||
469 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100470 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100471 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
472 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000473 << "Unexpected instruction in read barrier marking slow path: "
474 << instruction_->DebugName();
475
476 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000477 if (unpoison_) {
478 // Object* ref = ref_addr->AsMirrorPtr()
479 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
480 }
Roland Levillain4359e612016-07-20 11:32:19 +0100481 // No need to save live registers; it's taken care of by the
482 // entrypoint. Also, there is no need to update the stack mask,
483 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000484 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100485 DCHECK_NE(reg, RSP);
486 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
487 // "Compact" slow path, saving two moves.
488 //
489 // Instead of using the standard runtime calling convention (input
490 // and output in R0):
491 //
492 // RDI <- obj
493 // RAX <- ReadBarrierMark(RDI)
494 // obj <- RAX
495 //
496 // we just use rX (the register holding `obj`) as input and output
497 // of a dedicated entrypoint:
498 //
499 // rX <- ReadBarrierMarkRegX(rX)
500 //
501 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700502 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100503 // This runtime call does not require a stack map.
504 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000505 __ jmp(GetExitLabel());
506 }
507
508 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000509 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000510 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000511
512 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
513};
514
Roland Levillain0d5a2812015-11-13 10:07:31 +0000515// Slow path generating a read barrier for a heap reference.
516class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
517 public:
518 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
519 Location out,
520 Location ref,
521 Location obj,
522 uint32_t offset,
523 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000524 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525 out_(out),
526 ref_(ref),
527 obj_(obj),
528 offset_(offset),
529 index_(index) {
530 DCHECK(kEmitCompilerReadBarrier);
531 // If `obj` is equal to `out` or `ref`, it means the initial
532 // object has been overwritten by (or after) the heap object
533 // reference load to be instrumented, e.g.:
534 //
535 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000536 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000537 //
538 // In that case, we have lost the information about the original
539 // object, and the emitted read barrier cannot work properly.
540 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
541 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
542}
543
544 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
545 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
546 LocationSummary* locations = instruction_->GetLocations();
547 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
548 DCHECK(locations->CanCall());
549 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100550 DCHECK(instruction_->IsInstanceFieldGet() ||
551 instruction_->IsStaticFieldGet() ||
552 instruction_->IsArrayGet() ||
553 instruction_->IsInstanceOf() ||
554 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100555 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000556 << "Unexpected instruction in read barrier for heap reference slow path: "
557 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000558
559 __ Bind(GetEntryLabel());
560 SaveLiveRegisters(codegen, locations);
561
562 // We may have to change the index's value, but as `index_` is a
563 // constant member (like other "inputs" of this slow path),
564 // introduce a copy of it, `index`.
565 Location index = index_;
566 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100567 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000568 if (instruction_->IsArrayGet()) {
569 // Compute real offset and store it in index_.
570 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
571 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
572 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
573 // We are about to change the value of `index_reg` (see the
574 // calls to art::x86_64::X86_64Assembler::shll and
575 // art::x86_64::X86_64Assembler::AddImmediate below), but it
576 // has not been saved by the previous call to
577 // art::SlowPathCode::SaveLiveRegisters, as it is a
578 // callee-save register --
579 // art::SlowPathCode::SaveLiveRegisters does not consider
580 // callee-save registers, as it has been designed with the
581 // assumption that callee-save registers are supposed to be
582 // handled by the called function. So, as a callee-save
583 // register, `index_reg` _would_ eventually be saved onto
584 // the stack, but it would be too late: we would have
585 // changed its value earlier. Therefore, we manually save
586 // it here into another freely available register,
587 // `free_reg`, chosen of course among the caller-save
588 // registers (as a callee-save `free_reg` register would
589 // exhibit the same problem).
590 //
591 // Note we could have requested a temporary register from
592 // the register allocator instead; but we prefer not to, as
593 // this is a slow path, and we know we can find a
594 // caller-save register that is available.
595 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
596 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
597 index_reg = free_reg;
598 index = Location::RegisterLocation(index_reg);
599 } else {
600 // The initial register stored in `index_` has already been
601 // saved in the call to art::SlowPathCode::SaveLiveRegisters
602 // (as it is not a callee-save register), so we can freely
603 // use it.
604 }
605 // Shifting the index value contained in `index_reg` by the
606 // scale factor (2) cannot overflow in practice, as the
607 // runtime is unable to allocate object arrays with a size
608 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
609 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
610 static_assert(
611 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
612 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
613 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
614 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100615 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
616 // intrinsics, `index_` is not shifted by a scale factor of 2
617 // (as in the case of ArrayGet), as it is actually an offset
618 // to an object field within an object.
619 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000620 DCHECK(instruction_->GetLocations()->Intrinsified());
621 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
622 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
623 << instruction_->AsInvoke()->GetIntrinsic();
624 DCHECK_EQ(offset_, 0U);
625 DCHECK(index_.IsRegister());
626 }
627 }
628
629 // We're moving two or three locations to locations that could
630 // overlap, so we need a parallel move resolver.
631 InvokeRuntimeCallingConvention calling_convention;
632 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
633 parallel_move.AddMove(ref_,
634 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
635 Primitive::kPrimNot,
636 nullptr);
637 parallel_move.AddMove(obj_,
638 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
639 Primitive::kPrimNot,
640 nullptr);
641 if (index.IsValid()) {
642 parallel_move.AddMove(index,
643 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
644 Primitive::kPrimInt,
645 nullptr);
646 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
647 } else {
648 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
649 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
650 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100651 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000652 instruction_,
653 instruction_->GetDexPc(),
654 this);
655 CheckEntrypointTypes<
656 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
657 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
658
659 RestoreLiveRegisters(codegen, locations);
660 __ jmp(GetExitLabel());
661 }
662
663 const char* GetDescription() const OVERRIDE {
664 return "ReadBarrierForHeapReferenceSlowPathX86_64";
665 }
666
667 private:
668 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
669 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
670 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
671 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
672 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
673 return static_cast<CpuRegister>(i);
674 }
675 }
676 // We shall never fail to find a free caller-save register, as
677 // there are more than two core caller-save registers on x86-64
678 // (meaning it is possible to find one which is different from
679 // `ref` and `obj`).
680 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
681 LOG(FATAL) << "Could not find a free caller-save register";
682 UNREACHABLE();
683 }
684
Roland Levillain0d5a2812015-11-13 10:07:31 +0000685 const Location out_;
686 const Location ref_;
687 const Location obj_;
688 const uint32_t offset_;
689 // An additional location containing an index to an array.
690 // Only used for HArrayGet and the UnsafeGetObject &
691 // UnsafeGetObjectVolatile intrinsics.
692 const Location index_;
693
694 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
695};
696
697// Slow path generating a read barrier for a GC root.
698class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
699 public:
700 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000701 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000702 DCHECK(kEmitCompilerReadBarrier);
703 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000704
705 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
706 LocationSummary* locations = instruction_->GetLocations();
707 DCHECK(locations->CanCall());
708 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000709 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
710 << "Unexpected instruction in read barrier for GC root slow path: "
711 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000712
713 __ Bind(GetEntryLabel());
714 SaveLiveRegisters(codegen, locations);
715
716 InvokeRuntimeCallingConvention calling_convention;
717 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
718 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100719 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000720 instruction_,
721 instruction_->GetDexPc(),
722 this);
723 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
724 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
725
726 RestoreLiveRegisters(codegen, locations);
727 __ jmp(GetExitLabel());
728 }
729
730 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
731
732 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000733 const Location out_;
734 const Location root_;
735
736 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
737};
738
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100739#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100740// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
741#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100742
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700744 switch (cond) {
745 case kCondEQ: return kEqual;
746 case kCondNE: return kNotEqual;
747 case kCondLT: return kLess;
748 case kCondLE: return kLessEqual;
749 case kCondGT: return kGreater;
750 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700751 case kCondB: return kBelow;
752 case kCondBE: return kBelowEqual;
753 case kCondA: return kAbove;
754 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700755 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100756 LOG(FATAL) << "Unreachable";
757 UNREACHABLE();
758}
759
Aart Bike9f37602015-10-09 11:15:55 -0700760// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100761inline Condition X86_64FPCondition(IfCondition cond) {
762 switch (cond) {
763 case kCondEQ: return kEqual;
764 case kCondNE: return kNotEqual;
765 case kCondLT: return kBelow;
766 case kCondLE: return kBelowEqual;
767 case kCondGT: return kAbove;
768 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700769 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100770 };
771 LOG(FATAL) << "Unreachable";
772 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700773}
774
Vladimir Markodc151b22015-10-15 18:02:30 +0100775HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
776 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100777 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100778 switch (desired_dispatch_info.code_ptr_location) {
779 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
780 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
781 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
782 return HInvokeStaticOrDirect::DispatchInfo {
783 desired_dispatch_info.method_load_kind,
784 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
785 desired_dispatch_info.method_load_data,
786 0u
787 };
788 default:
789 return desired_dispatch_info;
790 }
791}
792
Serguei Katkov288c7a82016-05-16 11:53:15 +0600793Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
794 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800795 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000796 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
797 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100798 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000799 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100800 uint32_t offset =
801 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
802 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000803 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100804 }
Vladimir Marko58155012015-08-19 12:49:41 +0000805 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000806 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000807 break;
808 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
809 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
810 break;
811 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
812 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000813 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
814 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000815 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
816 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000817 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000818 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000819 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000820 // Bind a new fixup label at the end of the "movl" insn.
821 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100822 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000823 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000824 }
Vladimir Marko58155012015-08-19 12:49:41 +0000825 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000826 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000827 Register method_reg;
828 CpuRegister reg = temp.AsRegister<CpuRegister>();
829 if (current_method.IsRegister()) {
830 method_reg = current_method.AsRegister<Register>();
831 } else {
832 DCHECK(invoke->GetLocations()->Intrinsified());
833 DCHECK(!current_method.IsValid());
834 method_reg = reg.AsRegister();
835 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000837 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100838 __ movq(reg,
839 Address(CpuRegister(method_reg),
840 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100841 // temp = temp[index_in_cache];
842 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
843 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000844 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
845 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100846 }
Vladimir Marko58155012015-08-19 12:49:41 +0000847 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600848 return callee_method;
849}
850
851void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
852 Location temp) {
853 // All registers are assumed to be correctly set up.
854 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000855
856 switch (invoke->GetCodePtrLocation()) {
857 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
858 __ call(&frame_entry_label_);
859 break;
860 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000861 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
862 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000863 Label* label = &relative_call_patches_.back().label;
864 __ call(label); // Bind to the patch label, override at link time.
865 __ Bind(label); // Bind the label at the end of the "call" insn.
866 break;
867 }
868 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
869 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100870 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
871 LOG(FATAL) << "Unsupported";
872 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000873 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
874 // (callee_method + offset_of_quick_compiled_code)()
875 __ call(Address(callee_method.AsRegister<CpuRegister>(),
876 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700877 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000878 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000879 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800880
881 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800882}
883
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000884void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
885 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
886 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
887 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000888
889 // Use the calling convention instead of the location of the receiver, as
890 // intrinsics may have put the receiver in a different register. In the intrinsics
891 // slow path, the arguments have been moved to the right place, so here we are
892 // guaranteed that the receiver is the first register of the calling convention.
893 InvokeDexCallingConvention calling_convention;
894 Register receiver = calling_convention.GetRegisterAt(0);
895
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000896 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000898 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000899 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000900 // Instead of simply (possibly) unpoisoning `temp` here, we should
901 // emit a read barrier for the previous class reference load.
902 // However this is not required in practice, as this is an
903 // intermediate/temporary reference and because the current
904 // concurrent copying collector keeps the from-space memory
905 // intact/accessible until the end of the marking phase (the
906 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000907 __ MaybeUnpoisonHeapReference(temp);
908 // temp = temp->GetMethodAt(method_offset);
909 __ movq(temp, Address(temp, method_offset));
910 // call temp->GetEntryPoint();
911 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700912 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000913}
914
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000915void CodeGeneratorX86_64::RecordSimplePatch() {
916 if (GetCompilerOptions().GetIncludePatchInformation()) {
917 simple_patches_.emplace_back();
918 __ Bind(&simple_patches_.back());
919 }
920}
921
Vladimir Markoaad75c62016-10-03 08:46:48 +0000922void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
923 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000924 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
925 __ Bind(&string_patches_.back().label);
926}
927
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100928void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
929 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
930 __ Bind(&type_patches_.back().label);
931}
932
Vladimir Markoaad75c62016-10-03 08:46:48 +0000933Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
934 DCHECK(!GetCompilerOptions().IsBootImage());
935 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
936 return &string_patches_.back().label;
937}
938
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
940 uint32_t element_offset) {
941 // Add a patch entry and return the label.
942 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
943 return &pc_relative_dex_cache_patches_.back().label;
944}
945
Vladimir Markoaad75c62016-10-03 08:46:48 +0000946// The label points to the end of the "movl" or another instruction but the literal offset
947// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
948constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
949
950template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
951inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
952 const ArenaDeque<PatchInfo<Label>>& infos,
953 ArenaVector<LinkerPatch>* linker_patches) {
954 for (const PatchInfo<Label>& info : infos) {
955 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
956 linker_patches->push_back(
957 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
958 }
959}
960
Vladimir Marko58155012015-08-19 12:49:41 +0000961void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
962 DCHECK(linker_patches->empty());
963 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000964 method_patches_.size() +
965 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000966 pc_relative_dex_cache_patches_.size() +
967 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100968 string_patches_.size() +
969 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000970 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000971 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000972 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000973 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +0000974 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000975 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000976 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000977 linker_patches->push_back(
978 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +0000979 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000980 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
981 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000982 for (const Label& label : simple_patches_) {
983 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
984 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
985 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000986 if (!GetCompilerOptions().IsBootImage()) {
987 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
988 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000989 // These are always PC-relative, see GetSupportedLoadStringKind().
Vladimir Markoaad75c62016-10-03 08:46:48 +0000990 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000991 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000992 // These are always PC-relative, see GetSupportedLoadClassKind().
993 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +0000994}
995
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100996void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100997 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998}
999
1000void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001001 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002}
1003
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001004size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1005 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1006 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001007}
1008
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001009size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1010 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1011 return kX86_64WordSize;
1012}
1013
1014size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1015 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1016 return kX86_64WordSize;
1017}
1018
1019size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1020 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1021 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001022}
1023
Calin Juravle175dc732015-08-25 15:42:32 +01001024void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1025 HInstruction* instruction,
1026 uint32_t dex_pc,
1027 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001028 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001029 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1030 if (EntrypointRequiresStackMap(entrypoint)) {
1031 RecordPcInfo(instruction, dex_pc, slow_path);
1032 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001033}
1034
Roland Levillaindec8f632016-07-22 17:10:06 +01001035void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1036 HInstruction* instruction,
1037 SlowPathCode* slow_path) {
1038 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001039 GenerateInvokeRuntime(entry_point_offset);
1040}
1041
1042void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001043 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1044}
1045
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001046static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001047// Use a fake return address register to mimic Quick.
1048static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001049CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001050 const X86_64InstructionSetFeatures& isa_features,
1051 const CompilerOptions& compiler_options,
1052 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001053 : CodeGenerator(graph,
1054 kNumberOfCpuRegisters,
1055 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001056 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001057 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1058 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001059 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001060 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1061 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001062 compiler_options,
1063 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001064 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001066 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001067 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001068 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001069 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001070 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001071 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1072 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001073 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001074 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1075 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001076 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001077 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1079}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001081InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1082 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001083 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084 assembler_(codegen->GetAssembler()),
1085 codegen_(codegen) {}
1086
David Brazdil58282f42016-01-14 12:45:10 +00001087void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001089 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001091 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001092 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093}
1094
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001095static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001096 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001097}
David Srbecky9d8606d2015-04-12 09:35:32 +01001098
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001099static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001100 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001101}
1102
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001104 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001105 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001106 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001107 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001108 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001109
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001110 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001111 __ testq(CpuRegister(RAX), Address(
1112 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001113 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001114 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001115
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001116 if (HasEmptyFrame()) {
1117 return;
1118 }
1119
Nicolas Geoffray98893962015-01-21 12:32:32 +00001120 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001121 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001122 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001123 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001124 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1125 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001126 }
1127 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001128
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001129 int adjust = GetFrameSize() - GetCoreSpillSize();
1130 __ subq(CpuRegister(RSP), Immediate(adjust));
1131 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001132 uint32_t xmm_spill_location = GetFpuSpillStart();
1133 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001134
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001135 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1136 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001137 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1138 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1139 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001140 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001141 }
1142
Mathieu Chartiere401d142015-04-22 13:56:20 -07001143 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001144 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001145}
1146
1147void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001148 __ cfi().RememberState();
1149 if (!HasEmptyFrame()) {
1150 uint32_t xmm_spill_location = GetFpuSpillStart();
1151 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1152 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1153 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1154 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1155 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1156 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1157 }
1158 }
1159
1160 int adjust = GetFrameSize() - GetCoreSpillSize();
1161 __ addq(CpuRegister(RSP), Immediate(adjust));
1162 __ cfi().AdjustCFAOffset(-adjust);
1163
1164 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1165 Register reg = kCoreCalleeSaves[i];
1166 if (allocated_registers_.ContainsCoreRegister(reg)) {
1167 __ popq(CpuRegister(reg));
1168 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1169 __ cfi().Restore(DWARFReg(reg));
1170 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001171 }
1172 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001173 __ ret();
1174 __ cfi().RestoreState();
1175 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001176}
1177
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001178void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1179 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180}
1181
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182void CodeGeneratorX86_64::Move(Location destination, Location source) {
1183 if (source.Equals(destination)) {
1184 return;
1185 }
1186 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001187 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001189 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001191 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001192 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001193 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1194 } else if (source.IsConstant()) {
1195 HConstant* constant = source.GetConstant();
1196 if (constant->IsLongConstant()) {
1197 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1198 } else {
1199 Load32BitValue(dest, GetInt32ValueOf(constant));
1200 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201 } else {
1202 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001203 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 }
1205 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001206 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001208 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001210 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1211 } else if (source.IsConstant()) {
1212 HConstant* constant = source.GetConstant();
1213 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1214 if (constant->IsFloatConstant()) {
1215 Load32BitValue(dest, static_cast<int32_t>(value));
1216 } else {
1217 Load64BitValue(dest, value);
1218 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001219 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001220 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 } else {
1222 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001223 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224 }
1225 } else if (destination.IsStackSlot()) {
1226 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else if (source.IsFpuRegister()) {
1230 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001232 } else if (source.IsConstant()) {
1233 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001234 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001235 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001237 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001238 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1239 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001240 }
1241 } else {
1242 DCHECK(destination.IsDoubleStackSlot());
1243 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 } else if (source.IsFpuRegister()) {
1247 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001248 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001249 } else if (source.IsConstant()) {
1250 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001251 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1252 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001253 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001254 } else {
1255 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001256 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1257 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001258 }
1259 }
1260}
1261
Calin Juravle175dc732015-08-25 15:42:32 +01001262void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1263 DCHECK(location.IsRegister());
1264 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1265}
1266
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267void CodeGeneratorX86_64::MoveLocation(
1268 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1269 Move(dst, src);
1270}
1271
1272void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1273 if (location.IsRegister()) {
1274 locations->AddTemp(location);
1275 } else {
1276 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1277 }
1278}
1279
David Brazdilfc6a86a2015-06-26 10:33:45 +00001280void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001281 DCHECK(!successor->IsExitBlock());
1282
1283 HBasicBlock* block = got->GetBlock();
1284 HInstruction* previous = got->GetPrevious();
1285
1286 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001287 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001288 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1289 return;
1290 }
1291
1292 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1293 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1294 }
1295 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296 __ jmp(codegen_->GetLabelOf(successor));
1297 }
1298}
1299
David Brazdilfc6a86a2015-06-26 10:33:45 +00001300void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1301 got->SetLocations(nullptr);
1302}
1303
1304void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1305 HandleGoto(got, got->GetSuccessor());
1306}
1307
1308void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1309 try_boundary->SetLocations(nullptr);
1310}
1311
1312void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1313 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1314 if (!successor->IsExitBlock()) {
1315 HandleGoto(try_boundary, successor);
1316 }
1317}
1318
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1320 exit->SetLocations(nullptr);
1321}
1322
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001323void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324}
1325
Mark Mendell152408f2015-12-31 12:28:50 -05001326template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001327void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001328 LabelType* true_label,
1329 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001330 if (cond->IsFPConditionTrueIfNaN()) {
1331 __ j(kUnordered, true_label);
1332 } else if (cond->IsFPConditionFalseIfNaN()) {
1333 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001334 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001335 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001336}
1337
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001338void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001339 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001340
Mark Mendellc4701932015-04-10 13:18:51 -04001341 Location left = locations->InAt(0);
1342 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001343 Primitive::Type type = condition->InputAt(0)->GetType();
1344 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001345 case Primitive::kPrimBoolean:
1346 case Primitive::kPrimByte:
1347 case Primitive::kPrimChar:
1348 case Primitive::kPrimShort:
1349 case Primitive::kPrimInt:
1350 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001351 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001352 break;
1353 }
Mark Mendellc4701932015-04-10 13:18:51 -04001354 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001355 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001356 break;
1357 }
1358 case Primitive::kPrimFloat: {
1359 if (right.IsFpuRegister()) {
1360 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1361 } else if (right.IsConstant()) {
1362 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1363 codegen_->LiteralFloatAddress(
1364 right.GetConstant()->AsFloatConstant()->GetValue()));
1365 } else {
1366 DCHECK(right.IsStackSlot());
1367 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1368 Address(CpuRegister(RSP), right.GetStackIndex()));
1369 }
Mark Mendellc4701932015-04-10 13:18:51 -04001370 break;
1371 }
1372 case Primitive::kPrimDouble: {
1373 if (right.IsFpuRegister()) {
1374 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1375 } else if (right.IsConstant()) {
1376 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1377 codegen_->LiteralDoubleAddress(
1378 right.GetConstant()->AsDoubleConstant()->GetValue()));
1379 } else {
1380 DCHECK(right.IsDoubleStackSlot());
1381 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1382 Address(CpuRegister(RSP), right.GetStackIndex()));
1383 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001384 break;
1385 }
1386 default:
1387 LOG(FATAL) << "Unexpected condition type " << type;
1388 }
1389}
1390
1391template<class LabelType>
1392void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1393 LabelType* true_target_in,
1394 LabelType* false_target_in) {
1395 // Generated branching requires both targets to be explicit. If either of the
1396 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1397 LabelType fallthrough_target;
1398 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1399 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1400
1401 // Generate the comparison to set the CC.
1402 GenerateCompareTest(condition);
1403
1404 // Now generate the correct jump(s).
1405 Primitive::Type type = condition->InputAt(0)->GetType();
1406 switch (type) {
1407 case Primitive::kPrimLong: {
1408 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1409 break;
1410 }
1411 case Primitive::kPrimFloat: {
1412 GenerateFPJumps(condition, true_target, false_target);
1413 break;
1414 }
1415 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001416 GenerateFPJumps(condition, true_target, false_target);
1417 break;
1418 }
1419 default:
1420 LOG(FATAL) << "Unexpected condition type " << type;
1421 }
1422
David Brazdil0debae72015-11-12 18:37:00 +00001423 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001424 __ jmp(false_target);
1425 }
David Brazdil0debae72015-11-12 18:37:00 +00001426
1427 if (fallthrough_target.IsLinked()) {
1428 __ Bind(&fallthrough_target);
1429 }
Mark Mendellc4701932015-04-10 13:18:51 -04001430}
1431
David Brazdil0debae72015-11-12 18:37:00 +00001432static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1433 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1434 // are set only strictly before `branch`. We can't use the eflags on long
1435 // conditions if they are materialized due to the complex branching.
1436 return cond->IsCondition() &&
1437 cond->GetNext() == branch &&
1438 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1439}
1440
Mark Mendell152408f2015-12-31 12:28:50 -05001441template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001442void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001443 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001444 LabelType* true_target,
1445 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001446 HInstruction* cond = instruction->InputAt(condition_input_index);
1447
1448 if (true_target == nullptr && false_target == nullptr) {
1449 // Nothing to do. The code always falls through.
1450 return;
1451 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001452 // Constant condition, statically compared against "true" (integer value 1).
1453 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001454 if (true_target != nullptr) {
1455 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001456 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001457 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001458 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001459 if (false_target != nullptr) {
1460 __ jmp(false_target);
1461 }
1462 }
1463 return;
1464 }
1465
1466 // The following code generates these patterns:
1467 // (1) true_target == nullptr && false_target != nullptr
1468 // - opposite condition true => branch to false_target
1469 // (2) true_target != nullptr && false_target == nullptr
1470 // - condition true => branch to true_target
1471 // (3) true_target != nullptr && false_target != nullptr
1472 // - condition true => branch to true_target
1473 // - branch to false_target
1474 if (IsBooleanValueOrMaterializedCondition(cond)) {
1475 if (AreEflagsSetFrom(cond, instruction)) {
1476 if (true_target == nullptr) {
1477 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1478 } else {
1479 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1480 }
1481 } else {
1482 // Materialized condition, compare against 0.
1483 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1484 if (lhs.IsRegister()) {
1485 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1486 } else {
1487 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1488 }
1489 if (true_target == nullptr) {
1490 __ j(kEqual, false_target);
1491 } else {
1492 __ j(kNotEqual, true_target);
1493 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001494 }
1495 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001496 // Condition has not been materialized, use its inputs as the
1497 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001498 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001499
David Brazdil0debae72015-11-12 18:37:00 +00001500 // If this is a long or FP comparison that has been folded into
1501 // the HCondition, generate the comparison directly.
1502 Primitive::Type type = condition->InputAt(0)->GetType();
1503 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1504 GenerateCompareTestAndBranch(condition, true_target, false_target);
1505 return;
1506 }
1507
1508 Location lhs = condition->GetLocations()->InAt(0);
1509 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001510 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001511 if (true_target == nullptr) {
1512 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1513 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001514 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001515 }
Dave Allison20dfc792014-06-16 20:44:29 -07001516 }
David Brazdil0debae72015-11-12 18:37:00 +00001517
1518 // If neither branch falls through (case 3), the conditional branch to `true_target`
1519 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1520 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001521 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001522 }
1523}
1524
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001525void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1527 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001528 locations->SetInAt(0, Location::Any());
1529 }
1530}
1531
1532void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001533 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1534 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1535 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1536 nullptr : codegen_->GetLabelOf(true_successor);
1537 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1538 nullptr : codegen_->GetLabelOf(false_successor);
1539 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001540}
1541
1542void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1543 LocationSummary* locations = new (GetGraph()->GetArena())
1544 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001545 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001546 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001547 locations->SetInAt(0, Location::Any());
1548 }
1549}
1550
1551void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001552 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001553 GenerateTestAndBranch<Label>(deoptimize,
1554 /* condition_input_index */ 0,
1555 slow_path->GetEntryLabel(),
1556 /* false_target */ nullptr);
1557}
1558
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001559static bool SelectCanUseCMOV(HSelect* select) {
1560 // There are no conditional move instructions for XMMs.
1561 if (Primitive::IsFloatingPointType(select->GetType())) {
1562 return false;
1563 }
1564
1565 // A FP condition doesn't generate the single CC that we need.
1566 HInstruction* condition = select->GetCondition();
1567 if (condition->IsCondition() &&
1568 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1569 return false;
1570 }
1571
1572 // We can generate a CMOV for this Select.
1573 return true;
1574}
1575
David Brazdil74eb1b22015-12-14 11:44:01 +00001576void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1578 if (Primitive::IsFloatingPointType(select->GetType())) {
1579 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001580 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001581 } else {
1582 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001583 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001584 if (select->InputAt(1)->IsConstant()) {
1585 locations->SetInAt(1, Location::RequiresRegister());
1586 } else {
1587 locations->SetInAt(1, Location::Any());
1588 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001589 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001590 locations->SetInAt(1, Location::Any());
1591 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001592 }
1593 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1594 locations->SetInAt(2, Location::RequiresRegister());
1595 }
1596 locations->SetOut(Location::SameAsFirstInput());
1597}
1598
1599void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1600 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001601 if (SelectCanUseCMOV(select)) {
1602 // If both the condition and the source types are integer, we can generate
1603 // a CMOV to implement Select.
1604 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001605 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001606 DCHECK(locations->InAt(0).Equals(locations->Out()));
1607
1608 HInstruction* select_condition = select->GetCondition();
1609 Condition cond = kNotEqual;
1610
1611 // Figure out how to test the 'condition'.
1612 if (select_condition->IsCondition()) {
1613 HCondition* condition = select_condition->AsCondition();
1614 if (!condition->IsEmittedAtUseSite()) {
1615 // This was a previously materialized condition.
1616 // Can we use the existing condition code?
1617 if (AreEflagsSetFrom(condition, select)) {
1618 // Materialization was the previous instruction. Condition codes are right.
1619 cond = X86_64IntegerCondition(condition->GetCondition());
1620 } else {
1621 // No, we have to recreate the condition code.
1622 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1623 __ testl(cond_reg, cond_reg);
1624 }
1625 } else {
1626 GenerateCompareTest(condition);
1627 cond = X86_64IntegerCondition(condition->GetCondition());
1628 }
1629 } else {
1630 // Must be a boolean condition, which needs to be compared to 0.
1631 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1632 __ testl(cond_reg, cond_reg);
1633 }
1634
1635 // If the condition is true, overwrite the output, which already contains false.
1636 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001637 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1638 if (value_true_loc.IsRegister()) {
1639 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1640 } else {
1641 __ cmov(cond,
1642 value_false,
1643 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1644 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001645 } else {
1646 NearLabel false_target;
1647 GenerateTestAndBranch<NearLabel>(select,
1648 /* condition_input_index */ 2,
1649 /* true_target */ nullptr,
1650 &false_target);
1651 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1652 __ Bind(&false_target);
1653 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001654}
1655
David Srbecky0cf44932015-12-09 14:09:59 +00001656void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1657 new (GetGraph()->GetArena()) LocationSummary(info);
1658}
1659
David Srbeckyd28f4a02016-03-14 17:14:24 +00001660void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1661 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001662}
1663
1664void CodeGeneratorX86_64::GenerateNop() {
1665 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001666}
1667
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001668void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001669 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001670 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001671 // Handle the long/FP comparisons made in instruction simplification.
1672 switch (cond->InputAt(0)->GetType()) {
1673 case Primitive::kPrimLong:
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::Any());
1676 break;
1677 case Primitive::kPrimFloat:
1678 case Primitive::kPrimDouble:
1679 locations->SetInAt(0, Location::RequiresFpuRegister());
1680 locations->SetInAt(1, Location::Any());
1681 break;
1682 default:
1683 locations->SetInAt(0, Location::RequiresRegister());
1684 locations->SetInAt(1, Location::Any());
1685 break;
1686 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001687 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001688 locations->SetOut(Location::RequiresRegister());
1689 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690}
1691
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001692void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001693 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001694 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001695 }
Mark Mendellc4701932015-04-10 13:18:51 -04001696
1697 LocationSummary* locations = cond->GetLocations();
1698 Location lhs = locations->InAt(0);
1699 Location rhs = locations->InAt(1);
1700 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001701 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001702
1703 switch (cond->InputAt(0)->GetType()) {
1704 default:
1705 // Integer case.
1706
1707 // Clear output register: setcc only sets the low byte.
1708 __ xorl(reg, reg);
1709
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001710 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001711 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001712 return;
1713 case Primitive::kPrimLong:
1714 // Clear output register: setcc only sets the low byte.
1715 __ xorl(reg, reg);
1716
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001717 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001718 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001719 return;
1720 case Primitive::kPrimFloat: {
1721 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1722 if (rhs.IsConstant()) {
1723 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1724 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1725 } else if (rhs.IsStackSlot()) {
1726 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1727 } else {
1728 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1729 }
1730 GenerateFPJumps(cond, &true_label, &false_label);
1731 break;
1732 }
1733 case Primitive::kPrimDouble: {
1734 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1735 if (rhs.IsConstant()) {
1736 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1737 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1738 } else if (rhs.IsDoubleStackSlot()) {
1739 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1740 } else {
1741 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1742 }
1743 GenerateFPJumps(cond, &true_label, &false_label);
1744 break;
1745 }
1746 }
1747
1748 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001749 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001750
Roland Levillain4fa13f62015-07-06 18:11:54 +01001751 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001752 __ Bind(&false_label);
1753 __ xorl(reg, reg);
1754 __ jmp(&done_label);
1755
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ Bind(&true_label);
1758 __ movl(reg, Immediate(1));
1759 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001760}
1761
1762void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001764}
1765
1766void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001768}
1769
1770void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001772}
1773
1774void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001776}
1777
1778void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001780}
1781
1782void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001796}
1797
1798void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001800}
1801
1802void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001804}
1805
1806void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001808}
1809
Aart Bike9f37602015-10-09 11:15:55 -07001810void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001812}
1813
1814void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001816}
1817
1818void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001820}
1821
1822void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001824}
1825
1826void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001828}
1829
1830void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001832}
1833
1834void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001836}
1837
1838void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001840}
1841
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001842void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001843 LocationSummary* locations =
1844 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001845 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001846 case Primitive::kPrimBoolean:
1847 case Primitive::kPrimByte:
1848 case Primitive::kPrimShort:
1849 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001850 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001851 case Primitive::kPrimLong: {
1852 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001853 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1855 break;
1856 }
1857 case Primitive::kPrimFloat:
1858 case Primitive::kPrimDouble: {
1859 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001860 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001861 locations->SetOut(Location::RequiresRegister());
1862 break;
1863 }
1864 default:
1865 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1866 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001867}
1868
1869void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001870 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001871 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001872 Location left = locations->InAt(0);
1873 Location right = locations->InAt(1);
1874
Mark Mendell0c9497d2015-08-21 09:30:05 -04001875 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001876 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001877 Condition less_cond = kLess;
1878
Calin Juravleddb7df22014-11-25 20:56:51 +00001879 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001880 case Primitive::kPrimBoolean:
1881 case Primitive::kPrimByte:
1882 case Primitive::kPrimShort:
1883 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001884 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001885 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08001886 break;
1887 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001888 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001889 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001890 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001891 }
1892 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001893 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1894 if (right.IsConstant()) {
1895 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1896 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1897 } else if (right.IsStackSlot()) {
1898 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1899 } else {
1900 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1901 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001902 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001903 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001904 break;
1905 }
1906 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001907 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1908 if (right.IsConstant()) {
1909 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1910 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1911 } else if (right.IsDoubleStackSlot()) {
1912 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1913 } else {
1914 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1915 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001916 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001917 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001918 break;
1919 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001920 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001922 }
Aart Bika19616e2016-02-01 18:57:58 -08001923
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001925 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001926 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001927
Calin Juravle91debbc2014-11-26 19:01:09 +00001928 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001929 __ movl(out, Immediate(1));
1930 __ jmp(&done);
1931
1932 __ Bind(&less);
1933 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001934
1935 __ Bind(&done);
1936}
1937
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001938void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001939 LocationSummary* locations =
1940 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001941 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001942}
1943
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001944void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001945 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001946}
1947
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001948void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1949 LocationSummary* locations =
1950 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1951 locations->SetOut(Location::ConstantLocation(constant));
1952}
1953
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001954void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001955 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001956}
1957
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001958void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001959 LocationSummary* locations =
1960 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001961 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001962}
1963
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001964void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001965 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001966}
1967
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001968void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1969 LocationSummary* locations =
1970 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1971 locations->SetOut(Location::ConstantLocation(constant));
1972}
1973
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001974void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001975 // Will be generated at use site.
1976}
1977
1978void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1981 locations->SetOut(Location::ConstantLocation(constant));
1982}
1983
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001984void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1985 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001986 // Will be generated at use site.
1987}
1988
Calin Juravle27df7582015-04-17 19:12:31 +01001989void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1990 memory_barrier->SetLocations(nullptr);
1991}
1992
1993void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001994 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001995}
1996
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001997void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1998 ret->SetLocations(nullptr);
1999}
2000
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002001void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002003}
2004
2005void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002006 LocationSummary* locations =
2007 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008 switch (ret->InputAt(0)->GetType()) {
2009 case Primitive::kPrimBoolean:
2010 case Primitive::kPrimByte:
2011 case Primitive::kPrimChar:
2012 case Primitive::kPrimShort:
2013 case Primitive::kPrimInt:
2014 case Primitive::kPrimNot:
2015 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002016 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002017 break;
2018
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002019 case Primitive::kPrimFloat:
2020 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002021 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002022 break;
2023
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002024 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002025 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002026 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002027}
2028
2029void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2030 if (kIsDebugBuild) {
2031 switch (ret->InputAt(0)->GetType()) {
2032 case Primitive::kPrimBoolean:
2033 case Primitive::kPrimByte:
2034 case Primitive::kPrimChar:
2035 case Primitive::kPrimShort:
2036 case Primitive::kPrimInt:
2037 case Primitive::kPrimNot:
2038 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002040 break;
2041
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002042 case Primitive::kPrimFloat:
2043 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002045 XMM0);
2046 break;
2047
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002048 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002049 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002050 }
2051 }
2052 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002053}
2054
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002055Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2056 switch (type) {
2057 case Primitive::kPrimBoolean:
2058 case Primitive::kPrimByte:
2059 case Primitive::kPrimChar:
2060 case Primitive::kPrimShort:
2061 case Primitive::kPrimInt:
2062 case Primitive::kPrimNot:
2063 case Primitive::kPrimLong:
2064 return Location::RegisterLocation(RAX);
2065
2066 case Primitive::kPrimVoid:
2067 return Location::NoLocation();
2068
2069 case Primitive::kPrimDouble:
2070 case Primitive::kPrimFloat:
2071 return Location::FpuRegisterLocation(XMM0);
2072 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002073
2074 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002075}
2076
2077Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2078 return Location::RegisterLocation(kMethodRegisterArgument);
2079}
2080
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002081Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002082 switch (type) {
2083 case Primitive::kPrimBoolean:
2084 case Primitive::kPrimByte:
2085 case Primitive::kPrimChar:
2086 case Primitive::kPrimShort:
2087 case Primitive::kPrimInt:
2088 case Primitive::kPrimNot: {
2089 uint32_t index = gp_index_++;
2090 stack_index_++;
2091 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002092 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002093 } else {
2094 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2095 }
2096 }
2097
2098 case Primitive::kPrimLong: {
2099 uint32_t index = gp_index_;
2100 stack_index_ += 2;
2101 if (index < calling_convention.GetNumberOfRegisters()) {
2102 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002103 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104 } else {
2105 gp_index_ += 2;
2106 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2107 }
2108 }
2109
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002110 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002111 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002112 stack_index_++;
2113 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002114 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002115 } else {
2116 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2117 }
2118 }
2119
2120 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002121 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002122 stack_index_ += 2;
2123 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 } else {
2126 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2127 }
2128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002129
2130 case Primitive::kPrimVoid:
2131 LOG(FATAL) << "Unexpected parameter type " << type;
2132 break;
2133 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002134 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002135}
2136
Calin Juravle175dc732015-08-25 15:42:32 +01002137void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2138 // The trampoline uses the same calling convention as dex calling conventions,
2139 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2140 // the method_idx.
2141 HandleInvoke(invoke);
2142}
2143
2144void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2145 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2146}
2147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002148void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002149 // Explicit clinit checks triggered by static invokes must have been pruned by
2150 // art::PrepareForRegisterAllocation.
2151 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002152
Mark Mendellfb8d2792015-03-31 22:16:59 -04002153 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002154 if (intrinsic.TryDispatch(invoke)) {
2155 return;
2156 }
2157
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002158 HandleInvoke(invoke);
2159}
2160
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2162 if (invoke->GetLocations()->Intrinsified()) {
2163 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2164 intrinsic.Dispatch(invoke);
2165 return true;
2166 }
2167 return false;
2168}
2169
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002170void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002171 // Explicit clinit checks triggered by static invokes must have been pruned by
2172 // art::PrepareForRegisterAllocation.
2173 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002174
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002175 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2176 return;
2177 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002178
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002179 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002180 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002181 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002182 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183}
2184
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002185void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002186 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002187 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002188}
2189
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002190void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002191 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192 if (intrinsic.TryDispatch(invoke)) {
2193 return;
2194 }
2195
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002196 HandleInvoke(invoke);
2197}
2198
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002199void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002200 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2201 return;
2202 }
2203
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002204 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002205 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002206 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207}
2208
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002209void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2210 HandleInvoke(invoke);
2211 // Add the hidden argument.
2212 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2213}
2214
2215void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2216 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002217 LocationSummary* locations = invoke->GetLocations();
2218 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2219 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002220 Location receiver = locations->InAt(0);
2221 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2222
Roland Levillain0d5a2812015-11-13 10:07:31 +00002223 // Set the hidden argument. This is safe to do this here, as RAX
2224 // won't be modified thereafter, before the `call` instruction.
2225 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002226 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002228 if (receiver.IsStackSlot()) {
2229 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002230 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002231 __ movl(temp, Address(temp, class_offset));
2232 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002233 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002235 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002236 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002237 // Instead of simply (possibly) unpoisoning `temp` here, we should
2238 // emit a read barrier for the previous class reference load.
2239 // However this is not required in practice, as this is an
2240 // intermediate/temporary reference and because the current
2241 // concurrent copying collector keeps the from-space memory
2242 // intact/accessible until the end of the marking phase (the
2243 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002244 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002245 // temp = temp->GetAddressOfIMT()
2246 __ movq(temp,
2247 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2248 // temp = temp->GetImtEntryAt(method_offset);
2249 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002250 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002251 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002252 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002254 __ call(Address(
2255 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256
2257 DCHECK(!codegen_->IsLeafMethod());
2258 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2259}
2260
Roland Levillain88cb1752014-10-20 16:36:47 +01002261void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2262 LocationSummary* locations =
2263 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2264 switch (neg->GetResultType()) {
2265 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002266 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002267 locations->SetInAt(0, Location::RequiresRegister());
2268 locations->SetOut(Location::SameAsFirstInput());
2269 break;
2270
Roland Levillain88cb1752014-10-20 16:36:47 +01002271 case Primitive::kPrimFloat:
2272 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002273 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002274 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002275 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 break;
2277
2278 default:
2279 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2280 }
2281}
2282
2283void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2284 LocationSummary* locations = neg->GetLocations();
2285 Location out = locations->Out();
2286 Location in = locations->InAt(0);
2287 switch (neg->GetResultType()) {
2288 case Primitive::kPrimInt:
2289 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002290 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002291 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002292 break;
2293
2294 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002295 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002296 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002298 break;
2299
Roland Levillain5368c212014-11-27 15:03:41 +00002300 case Primitive::kPrimFloat: {
2301 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002302 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002303 // Implement float negation with an exclusive or with value
2304 // 0x80000000 (mask for bit 31, representing the sign of a
2305 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002306 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002307 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002308 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002309 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002310
Roland Levillain5368c212014-11-27 15:03:41 +00002311 case Primitive::kPrimDouble: {
2312 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002313 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002314 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002315 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002316 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002317 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002318 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002319 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002320 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002321
2322 default:
2323 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2324 }
2325}
2326
Roland Levillaindff1f282014-11-05 14:15:05 +00002327void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2328 LocationSummary* locations =
2329 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2330 Primitive::Type result_type = conversion->GetResultType();
2331 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002332 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002333
David Brazdilb2bd1c52015-03-25 11:17:37 +00002334 // The Java language does not allow treating boolean as an integral type but
2335 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002336
Roland Levillaindff1f282014-11-05 14:15:05 +00002337 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002338 case Primitive::kPrimByte:
2339 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002340 case Primitive::kPrimLong:
2341 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002342 case Primitive::kPrimBoolean:
2343 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002344 case Primitive::kPrimShort:
2345 case Primitive::kPrimInt:
2346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002347 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002348 locations->SetInAt(0, Location::Any());
2349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
2356 break;
2357
Roland Levillain01a8d712014-11-14 16:27:39 +00002358 case Primitive::kPrimShort:
2359 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002360 case Primitive::kPrimLong:
2361 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-short' instruction.
2368 locations->SetInAt(0, Location::Any());
2369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2370 break;
2371
2372 default:
2373 LOG(FATAL) << "Unexpected type conversion from " << input_type
2374 << " to " << result_type;
2375 }
2376 break;
2377
Roland Levillain946e1432014-11-11 17:35:19 +00002378 case Primitive::kPrimInt:
2379 switch (input_type) {
2380 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002381 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002382 locations->SetInAt(0, Location::Any());
2383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2384 break;
2385
2386 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002387 // Processing a Dex `float-to-int' instruction.
2388 locations->SetInAt(0, Location::RequiresFpuRegister());
2389 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002390 break;
2391
Roland Levillain946e1432014-11-11 17:35:19 +00002392 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002393 // Processing a Dex `double-to-int' instruction.
2394 locations->SetInAt(0, Location::RequiresFpuRegister());
2395 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002396 break;
2397
2398 default:
2399 LOG(FATAL) << "Unexpected type conversion from " << input_type
2400 << " to " << result_type;
2401 }
2402 break;
2403
Roland Levillaindff1f282014-11-05 14:15:05 +00002404 case Primitive::kPrimLong:
2405 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002406 case Primitive::kPrimBoolean:
2407 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002408 case Primitive::kPrimByte:
2409 case Primitive::kPrimShort:
2410 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002411 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002412 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002413 // TODO: We would benefit from a (to-be-implemented)
2414 // Location::RegisterOrStackSlot requirement for this input.
2415 locations->SetInAt(0, Location::RequiresRegister());
2416 locations->SetOut(Location::RequiresRegister());
2417 break;
2418
2419 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002420 // Processing a Dex `float-to-long' instruction.
2421 locations->SetInAt(0, Location::RequiresFpuRegister());
2422 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002423 break;
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002426 // Processing a Dex `double-to-long' instruction.
2427 locations->SetInAt(0, Location::RequiresFpuRegister());
2428 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002429 break;
2430
2431 default:
2432 LOG(FATAL) << "Unexpected type conversion from " << input_type
2433 << " to " << result_type;
2434 }
2435 break;
2436
Roland Levillain981e4542014-11-14 11:47:14 +00002437 case Primitive::kPrimChar:
2438 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002439 case Primitive::kPrimLong:
2440 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002441 case Primitive::kPrimBoolean:
2442 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002443 case Primitive::kPrimByte:
2444 case Primitive::kPrimShort:
2445 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002446 // Processing a Dex `int-to-char' instruction.
2447 locations->SetInAt(0, Location::Any());
2448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2449 break;
2450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillaindff1f282014-11-05 14:15:05 +00002457 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002458 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002459 case Primitive::kPrimBoolean:
2460 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002461 case Primitive::kPrimByte:
2462 case Primitive::kPrimShort:
2463 case Primitive::kPrimInt:
2464 case Primitive::kPrimChar:
2465 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002466 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002467 locations->SetOut(Location::RequiresFpuRegister());
2468 break;
2469
2470 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002471 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002472 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002473 locations->SetOut(Location::RequiresFpuRegister());
2474 break;
2475
Roland Levillaincff13742014-11-17 14:32:17 +00002476 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002477 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002478 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002479 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002480 break;
2481
2482 default:
2483 LOG(FATAL) << "Unexpected type conversion from " << input_type
2484 << " to " << result_type;
2485 };
2486 break;
2487
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002489 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002490 case Primitive::kPrimBoolean:
2491 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002492 case Primitive::kPrimByte:
2493 case Primitive::kPrimShort:
2494 case Primitive::kPrimInt:
2495 case Primitive::kPrimChar:
2496 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002497 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002498 locations->SetOut(Location::RequiresFpuRegister());
2499 break;
2500
2501 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002502 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002503 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002504 locations->SetOut(Location::RequiresFpuRegister());
2505 break;
2506
Roland Levillaincff13742014-11-17 14:32:17 +00002507 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002508 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002509 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002510 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected type conversion from " << input_type
2515 << " to " << result_type;
2516 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 break;
2518
2519 default:
2520 LOG(FATAL) << "Unexpected type conversion from " << input_type
2521 << " to " << result_type;
2522 }
2523}
2524
2525void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2526 LocationSummary* locations = conversion->GetLocations();
2527 Location out = locations->Out();
2528 Location in = locations->InAt(0);
2529 Primitive::Type result_type = conversion->GetResultType();
2530 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002531 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002533 case Primitive::kPrimByte:
2534 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002535 case Primitive::kPrimLong:
2536 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002537 case Primitive::kPrimBoolean:
2538 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002539 case Primitive::kPrimShort:
2540 case Primitive::kPrimInt:
2541 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002542 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002543 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002544 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002545 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002546 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002547 Address(CpuRegister(RSP), in.GetStackIndex()));
2548 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002550 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002551 }
2552 break;
2553
2554 default:
2555 LOG(FATAL) << "Unexpected type conversion from " << input_type
2556 << " to " << result_type;
2557 }
2558 break;
2559
Roland Levillain01a8d712014-11-14 16:27:39 +00002560 case Primitive::kPrimShort:
2561 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002562 case Primitive::kPrimLong:
2563 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002564 case Primitive::kPrimBoolean:
2565 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002566 case Primitive::kPrimByte:
2567 case Primitive::kPrimInt:
2568 case Primitive::kPrimChar:
2569 // Processing a Dex `int-to-short' instruction.
2570 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002571 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002572 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002573 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002574 Address(CpuRegister(RSP), in.GetStackIndex()));
2575 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002577 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002578 }
2579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected type conversion from " << input_type
2583 << " to " << result_type;
2584 }
2585 break;
2586
Roland Levillain946e1432014-11-11 17:35:19 +00002587 case Primitive::kPrimInt:
2588 switch (input_type) {
2589 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002591 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002593 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002595 Address(CpuRegister(RSP), in.GetStackIndex()));
2596 } else {
2597 DCHECK(in.IsConstant());
2598 DCHECK(in.GetConstant()->IsLongConstant());
2599 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002601 }
2602 break;
2603
Roland Levillain3f8f9362014-12-02 17:45:01 +00002604 case Primitive::kPrimFloat: {
2605 // Processing a Dex `float-to-int' instruction.
2606 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2607 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002608 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002609
2610 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002611 // if input >= (float)INT_MAX goto done
2612 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002613 __ j(kAboveEqual, &done);
2614 // if input == NaN goto nan
2615 __ j(kUnordered, &nan);
2616 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002617 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002618 __ jmp(&done);
2619 __ Bind(&nan);
2620 // output = 0
2621 __ xorl(output, output);
2622 __ Bind(&done);
2623 break;
2624 }
2625
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002626 case Primitive::kPrimDouble: {
2627 // Processing a Dex `double-to-int' instruction.
2628 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2629 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002630 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631
2632 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002633 // if input >= (double)INT_MAX goto done
2634 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002635 __ j(kAboveEqual, &done);
2636 // if input == NaN goto nan
2637 __ j(kUnordered, &nan);
2638 // output = double-to-int-truncate(input)
2639 __ cvttsd2si(output, input);
2640 __ jmp(&done);
2641 __ Bind(&nan);
2642 // output = 0
2643 __ xorl(output, output);
2644 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002645 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002646 }
Roland Levillain946e1432014-11-11 17:35:19 +00002647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 }
2652 break;
2653
Roland Levillaindff1f282014-11-05 14:15:05 +00002654 case Primitive::kPrimLong:
2655 switch (input_type) {
2656 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002657 case Primitive::kPrimBoolean:
2658 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimByte:
2660 case Primitive::kPrimShort:
2661 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002662 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002663 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002664 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002665 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 break;
2667
Roland Levillain624279f2014-12-04 11:54:28 +00002668 case Primitive::kPrimFloat: {
2669 // Processing a Dex `float-to-long' instruction.
2670 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2671 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002672 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002673
Mark Mendell92e83bf2015-05-07 11:25:03 -04002674 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002675 // if input >= (float)LONG_MAX goto done
2676 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002677 __ j(kAboveEqual, &done);
2678 // if input == NaN goto nan
2679 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002680 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002681 __ cvttss2si(output, input, true);
2682 __ jmp(&done);
2683 __ Bind(&nan);
2684 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002685 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002686 __ Bind(&done);
2687 break;
2688 }
2689
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002690 case Primitive::kPrimDouble: {
2691 // Processing a Dex `double-to-long' instruction.
2692 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2693 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002694 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002695
Mark Mendell92e83bf2015-05-07 11:25:03 -04002696 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002697 // if input >= (double)LONG_MAX goto done
2698 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002699 __ j(kAboveEqual, &done);
2700 // if input == NaN goto nan
2701 __ j(kUnordered, &nan);
2702 // output = double-to-long-truncate(input)
2703 __ cvttsd2si(output, input, true);
2704 __ jmp(&done);
2705 __ Bind(&nan);
2706 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002707 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002708 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002709 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002710 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716 break;
2717
Roland Levillain981e4542014-11-14 11:47:14 +00002718 case Primitive::kPrimChar:
2719 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002720 case Primitive::kPrimLong:
2721 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002722 case Primitive::kPrimBoolean:
2723 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002724 case Primitive::kPrimByte:
2725 case Primitive::kPrimShort:
2726 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002727 // Processing a Dex `int-to-char' instruction.
2728 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002730 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002732 Address(CpuRegister(RSP), in.GetStackIndex()));
2733 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002735 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002736 }
2737 break;
2738
2739 default:
2740 LOG(FATAL) << "Unexpected type conversion from " << input_type
2741 << " to " << result_type;
2742 }
2743 break;
2744
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002746 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002747 case Primitive::kPrimBoolean:
2748 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002749 case Primitive::kPrimByte:
2750 case Primitive::kPrimShort:
2751 case Primitive::kPrimInt:
2752 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002753 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002754 if (in.IsRegister()) {
2755 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2756 } else if (in.IsConstant()) {
2757 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2758 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002759 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002760 } else {
2761 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2762 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2763 }
Roland Levillaincff13742014-11-17 14:32:17 +00002764 break;
2765
2766 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002767 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002768 if (in.IsRegister()) {
2769 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2770 } else if (in.IsConstant()) {
2771 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2772 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002773 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002774 } else {
2775 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2776 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2777 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002778 break;
2779
Roland Levillaincff13742014-11-17 14:32:17 +00002780 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002781 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002782 if (in.IsFpuRegister()) {
2783 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2784 } else if (in.IsConstant()) {
2785 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2786 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002787 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002788 } else {
2789 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2790 Address(CpuRegister(RSP), in.GetStackIndex()));
2791 }
Roland Levillaincff13742014-11-17 14:32:17 +00002792 break;
2793
2794 default:
2795 LOG(FATAL) << "Unexpected type conversion from " << input_type
2796 << " to " << result_type;
2797 };
2798 break;
2799
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002801 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002802 case Primitive::kPrimBoolean:
2803 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002804 case Primitive::kPrimByte:
2805 case Primitive::kPrimShort:
2806 case Primitive::kPrimInt:
2807 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002808 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002809 if (in.IsRegister()) {
2810 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2811 } else if (in.IsConstant()) {
2812 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2813 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002814 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002815 } else {
2816 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2817 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2818 }
Roland Levillaincff13742014-11-17 14:32:17 +00002819 break;
2820
2821 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002822 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002823 if (in.IsRegister()) {
2824 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2825 } else if (in.IsConstant()) {
2826 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2827 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002828 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002829 } else {
2830 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2831 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2832 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002833 break;
2834
Roland Levillaincff13742014-11-17 14:32:17 +00002835 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002836 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002837 if (in.IsFpuRegister()) {
2838 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2839 } else if (in.IsConstant()) {
2840 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2841 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002842 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002843 } else {
2844 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2845 Address(CpuRegister(RSP), in.GetStackIndex()));
2846 }
Roland Levillaincff13742014-11-17 14:32:17 +00002847 break;
2848
2849 default:
2850 LOG(FATAL) << "Unexpected type conversion from " << input_type
2851 << " to " << result_type;
2852 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002853 break;
2854
2855 default:
2856 LOG(FATAL) << "Unexpected type conversion from " << input_type
2857 << " to " << result_type;
2858 }
2859}
2860
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002861void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002862 LocationSummary* locations =
2863 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002864 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002865 case Primitive::kPrimInt: {
2866 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002867 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002869 break;
2870 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002871
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002872 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002873 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002874 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002875 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002876 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002877 break;
2878 }
2879
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002880 case Primitive::kPrimDouble:
2881 case Primitive::kPrimFloat: {
2882 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002883 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002884 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002885 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002886 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002887
2888 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002889 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002890 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002891}
2892
2893void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2894 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002895 Location first = locations->InAt(0);
2896 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002897 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002898
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002899 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002900 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002901 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002902 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2903 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002904 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2905 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002906 } else {
2907 __ leal(out.AsRegister<CpuRegister>(), Address(
2908 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2909 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002910 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002911 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2912 __ addl(out.AsRegister<CpuRegister>(),
2913 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2914 } else {
2915 __ leal(out.AsRegister<CpuRegister>(), Address(
2916 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2917 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002918 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002919 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002920 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002921 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002922 break;
2923 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002925 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002926 if (second.IsRegister()) {
2927 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2928 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002929 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2930 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002931 } else {
2932 __ leaq(out.AsRegister<CpuRegister>(), Address(
2933 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2934 }
2935 } else {
2936 DCHECK(second.IsConstant());
2937 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2938 int32_t int32_value = Low32Bits(value);
2939 DCHECK_EQ(int32_value, value);
2940 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2941 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2942 } else {
2943 __ leaq(out.AsRegister<CpuRegister>(), Address(
2944 first.AsRegister<CpuRegister>(), int32_value));
2945 }
2946 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002947 break;
2948 }
2949
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002951 if (second.IsFpuRegister()) {
2952 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2953 } else if (second.IsConstant()) {
2954 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002955 codegen_->LiteralFloatAddress(
2956 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002957 } else {
2958 DCHECK(second.IsStackSlot());
2959 __ addss(first.AsFpuRegister<XmmRegister>(),
2960 Address(CpuRegister(RSP), second.GetStackIndex()));
2961 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002962 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 }
2964
2965 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002966 if (second.IsFpuRegister()) {
2967 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2968 } else if (second.IsConstant()) {
2969 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002970 codegen_->LiteralDoubleAddress(
2971 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002972 } else {
2973 DCHECK(second.IsDoubleStackSlot());
2974 __ addsd(first.AsFpuRegister<XmmRegister>(),
2975 Address(CpuRegister(RSP), second.GetStackIndex()));
2976 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 break;
2978 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002979
2980 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002982 }
2983}
2984
2985void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002986 LocationSummary* locations =
2987 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002988 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002989 case Primitive::kPrimInt: {
2990 locations->SetInAt(0, Location::RequiresRegister());
2991 locations->SetInAt(1, Location::Any());
2992 locations->SetOut(Location::SameAsFirstInput());
2993 break;
2994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002995 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002996 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002997 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002998 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002999 break;
3000 }
Calin Juravle11351682014-10-23 15:38:15 +01003001 case Primitive::kPrimFloat:
3002 case Primitive::kPrimDouble: {
3003 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003004 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003005 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003006 break;
Calin Juravle11351682014-10-23 15:38:15 +01003007 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003008 default:
Calin Juravle11351682014-10-23 15:38:15 +01003009 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003010 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011}
3012
3013void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3014 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003015 Location first = locations->InAt(0);
3016 Location second = locations->InAt(1);
3017 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003018 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003019 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003020 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003022 } else if (second.IsConstant()) {
3023 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003025 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003027 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003028 break;
3029 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003031 if (second.IsConstant()) {
3032 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3033 DCHECK(IsInt<32>(value));
3034 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3035 } else {
3036 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003038 break;
3039 }
3040
Calin Juravle11351682014-10-23 15:38:15 +01003041 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003042 if (second.IsFpuRegister()) {
3043 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3044 } else if (second.IsConstant()) {
3045 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003046 codegen_->LiteralFloatAddress(
3047 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003048 } else {
3049 DCHECK(second.IsStackSlot());
3050 __ subss(first.AsFpuRegister<XmmRegister>(),
3051 Address(CpuRegister(RSP), second.GetStackIndex()));
3052 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003053 break;
Calin Juravle11351682014-10-23 15:38:15 +01003054 }
3055
3056 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003057 if (second.IsFpuRegister()) {
3058 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3059 } else if (second.IsConstant()) {
3060 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003061 codegen_->LiteralDoubleAddress(
3062 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003063 } else {
3064 DCHECK(second.IsDoubleStackSlot());
3065 __ subsd(first.AsFpuRegister<XmmRegister>(),
3066 Address(CpuRegister(RSP), second.GetStackIndex()));
3067 }
Calin Juravle11351682014-10-23 15:38:15 +01003068 break;
3069 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070
3071 default:
Calin Juravle11351682014-10-23 15:38:15 +01003072 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003073 }
3074}
3075
Calin Juravle34bacdf2014-10-07 20:23:36 +01003076void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3077 LocationSummary* locations =
3078 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3079 switch (mul->GetResultType()) {
3080 case Primitive::kPrimInt: {
3081 locations->SetInAt(0, Location::RequiresRegister());
3082 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003083 if (mul->InputAt(1)->IsIntConstant()) {
3084 // Can use 3 operand multiply.
3085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3086 } else {
3087 locations->SetOut(Location::SameAsFirstInput());
3088 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003089 break;
3090 }
3091 case Primitive::kPrimLong: {
3092 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003093 locations->SetInAt(1, Location::Any());
3094 if (mul->InputAt(1)->IsLongConstant() &&
3095 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003096 // Can use 3 operand multiply.
3097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3098 } else {
3099 locations->SetOut(Location::SameAsFirstInput());
3100 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003101 break;
3102 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003103 case Primitive::kPrimFloat:
3104 case Primitive::kPrimDouble: {
3105 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003106 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003107 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003108 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003109 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110
3111 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003113 }
3114}
3115
3116void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3117 LocationSummary* locations = mul->GetLocations();
3118 Location first = locations->InAt(0);
3119 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003120 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003121 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003122 case Primitive::kPrimInt:
3123 // The constant may have ended up in a register, so test explicitly to avoid
3124 // problems where the output may not be the same as the first operand.
3125 if (mul->InputAt(1)->IsIntConstant()) {
3126 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3127 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3128 } else if (second.IsRegister()) {
3129 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003130 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003132 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003133 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003134 __ imull(first.AsRegister<CpuRegister>(),
3135 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003136 }
3137 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003139 // The constant may have ended up in a register, so test explicitly to avoid
3140 // problems where the output may not be the same as the first operand.
3141 if (mul->InputAt(1)->IsLongConstant()) {
3142 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3143 if (IsInt<32>(value)) {
3144 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3145 Immediate(static_cast<int32_t>(value)));
3146 } else {
3147 // Have to use the constant area.
3148 DCHECK(first.Equals(out));
3149 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3150 }
3151 } else if (second.IsRegister()) {
3152 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003153 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003154 } else {
3155 DCHECK(second.IsDoubleStackSlot());
3156 DCHECK(first.Equals(out));
3157 __ imulq(first.AsRegister<CpuRegister>(),
3158 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003159 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 break;
3161 }
3162
Calin Juravleb5bfa962014-10-21 18:02:24 +01003163 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003164 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003165 if (second.IsFpuRegister()) {
3166 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3167 } else if (second.IsConstant()) {
3168 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003169 codegen_->LiteralFloatAddress(
3170 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003171 } else {
3172 DCHECK(second.IsStackSlot());
3173 __ mulss(first.AsFpuRegister<XmmRegister>(),
3174 Address(CpuRegister(RSP), second.GetStackIndex()));
3175 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003177 }
3178
3179 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003180 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003181 if (second.IsFpuRegister()) {
3182 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3183 } else if (second.IsConstant()) {
3184 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003185 codegen_->LiteralDoubleAddress(
3186 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003187 } else {
3188 DCHECK(second.IsDoubleStackSlot());
3189 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3190 Address(CpuRegister(RSP), second.GetStackIndex()));
3191 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003192 break;
3193 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194
3195 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003196 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 }
3198}
3199
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003200void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3201 uint32_t stack_adjustment, bool is_float) {
3202 if (source.IsStackSlot()) {
3203 DCHECK(is_float);
3204 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3205 } else if (source.IsDoubleStackSlot()) {
3206 DCHECK(!is_float);
3207 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3208 } else {
3209 // Write the value to the temporary location on the stack and load to FP stack.
3210 if (is_float) {
3211 Location stack_temp = Location::StackSlot(temp_offset);
3212 codegen_->Move(stack_temp, source);
3213 __ flds(Address(CpuRegister(RSP), temp_offset));
3214 } else {
3215 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3216 codegen_->Move(stack_temp, source);
3217 __ fldl(Address(CpuRegister(RSP), temp_offset));
3218 }
3219 }
3220}
3221
3222void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3223 Primitive::Type type = rem->GetResultType();
3224 bool is_float = type == Primitive::kPrimFloat;
3225 size_t elem_size = Primitive::ComponentSize(type);
3226 LocationSummary* locations = rem->GetLocations();
3227 Location first = locations->InAt(0);
3228 Location second = locations->InAt(1);
3229 Location out = locations->Out();
3230
3231 // Create stack space for 2 elements.
3232 // TODO: enhance register allocator to ask for stack temporaries.
3233 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3234
3235 // Load the values to the FP stack in reverse order, using temporaries if needed.
3236 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3237 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3238
3239 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003240 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003241 __ Bind(&retry);
3242 __ fprem();
3243
3244 // Move FP status to AX.
3245 __ fstsw();
3246
3247 // And see if the argument reduction is complete. This is signaled by the
3248 // C2 FPU flag bit set to 0.
3249 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3250 __ j(kNotEqual, &retry);
3251
3252 // We have settled on the final value. Retrieve it into an XMM register.
3253 // Store FP top of stack to real stack.
3254 if (is_float) {
3255 __ fsts(Address(CpuRegister(RSP), 0));
3256 } else {
3257 __ fstl(Address(CpuRegister(RSP), 0));
3258 }
3259
3260 // Pop the 2 items from the FP stack.
3261 __ fucompp();
3262
3263 // Load the value from the stack into an XMM register.
3264 DCHECK(out.IsFpuRegister()) << out;
3265 if (is_float) {
3266 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3267 } else {
3268 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3269 }
3270
3271 // And remove the temporary stack space we allocated.
3272 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3273}
3274
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003275void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3276 DCHECK(instruction->IsDiv() || instruction->IsRem());
3277
3278 LocationSummary* locations = instruction->GetLocations();
3279 Location second = locations->InAt(1);
3280 DCHECK(second.IsConstant());
3281
3282 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3283 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003284 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003285
3286 DCHECK(imm == 1 || imm == -1);
3287
3288 switch (instruction->GetResultType()) {
3289 case Primitive::kPrimInt: {
3290 if (instruction->IsRem()) {
3291 __ xorl(output_register, output_register);
3292 } else {
3293 __ movl(output_register, input_register);
3294 if (imm == -1) {
3295 __ negl(output_register);
3296 }
3297 }
3298 break;
3299 }
3300
3301 case Primitive::kPrimLong: {
3302 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003303 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003304 } else {
3305 __ movq(output_register, input_register);
3306 if (imm == -1) {
3307 __ negq(output_register);
3308 }
3309 }
3310 break;
3311 }
3312
3313 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003314 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315 }
3316}
3317
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003318void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003319 LocationSummary* locations = instruction->GetLocations();
3320 Location second = locations->InAt(1);
3321
3322 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3323 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3324
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003325 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003326 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3327 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003328
3329 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3330
3331 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003332 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333 __ testl(numerator, numerator);
3334 __ cmov(kGreaterEqual, tmp, numerator);
3335 int shift = CTZ(imm);
3336 __ sarl(tmp, Immediate(shift));
3337
3338 if (imm < 0) {
3339 __ negl(tmp);
3340 }
3341
3342 __ movl(output_register, tmp);
3343 } else {
3344 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3345 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3346
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003347 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003348 __ addq(rdx, numerator);
3349 __ testq(numerator, numerator);
3350 __ cmov(kGreaterEqual, rdx, numerator);
3351 int shift = CTZ(imm);
3352 __ sarq(rdx, Immediate(shift));
3353
3354 if (imm < 0) {
3355 __ negq(rdx);
3356 }
3357
3358 __ movq(output_register, rdx);
3359 }
3360}
3361
3362void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3363 DCHECK(instruction->IsDiv() || instruction->IsRem());
3364
3365 LocationSummary* locations = instruction->GetLocations();
3366 Location second = locations->InAt(1);
3367
3368 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3369 : locations->GetTemp(0).AsRegister<CpuRegister>();
3370 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3371 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3372 : locations->Out().AsRegister<CpuRegister>();
3373 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3374
3375 DCHECK_EQ(RAX, eax.AsRegister());
3376 DCHECK_EQ(RDX, edx.AsRegister());
3377 if (instruction->IsDiv()) {
3378 DCHECK_EQ(RAX, out.AsRegister());
3379 } else {
3380 DCHECK_EQ(RDX, out.AsRegister());
3381 }
3382
3383 int64_t magic;
3384 int shift;
3385
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003386 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003387 if (instruction->GetResultType() == Primitive::kPrimInt) {
3388 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3389
3390 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3391
3392 __ movl(numerator, eax);
3393
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003394 __ movl(eax, Immediate(magic));
3395 __ imull(numerator);
3396
3397 if (imm > 0 && magic < 0) {
3398 __ addl(edx, numerator);
3399 } else if (imm < 0 && magic > 0) {
3400 __ subl(edx, numerator);
3401 }
3402
3403 if (shift != 0) {
3404 __ sarl(edx, Immediate(shift));
3405 }
3406
3407 __ movl(eax, edx);
3408 __ shrl(edx, Immediate(31));
3409 __ addl(edx, eax);
3410
3411 if (instruction->IsRem()) {
3412 __ movl(eax, numerator);
3413 __ imull(edx, Immediate(imm));
3414 __ subl(eax, edx);
3415 __ movl(edx, eax);
3416 } else {
3417 __ movl(eax, edx);
3418 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419 } else {
3420 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3421
3422 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3423
3424 CpuRegister rax = eax;
3425 CpuRegister rdx = edx;
3426
3427 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3428
3429 // Save the numerator.
3430 __ movq(numerator, rax);
3431
3432 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003433 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434
3435 // RDX:RAX = magic * numerator
3436 __ imulq(numerator);
3437
3438 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003439 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 __ addq(rdx, numerator);
3441 } else if (imm < 0 && magic > 0) {
3442 // RDX -= numerator
3443 __ subq(rdx, numerator);
3444 }
3445
3446 // Shift if needed.
3447 if (shift != 0) {
3448 __ sarq(rdx, Immediate(shift));
3449 }
3450
3451 // RDX += 1 if RDX < 0
3452 __ movq(rax, rdx);
3453 __ shrq(rdx, Immediate(63));
3454 __ addq(rdx, rax);
3455
3456 if (instruction->IsRem()) {
3457 __ movq(rax, numerator);
3458
3459 if (IsInt<32>(imm)) {
3460 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3461 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003462 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003463 }
3464
3465 __ subq(rax, rdx);
3466 __ movq(rdx, rax);
3467 } else {
3468 __ movq(rax, rdx);
3469 }
3470 }
3471}
3472
Calin Juravlebacfec32014-11-14 15:54:36 +00003473void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3474 DCHECK(instruction->IsDiv() || instruction->IsRem());
3475 Primitive::Type type = instruction->GetResultType();
3476 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3477
3478 bool is_div = instruction->IsDiv();
3479 LocationSummary* locations = instruction->GetLocations();
3480
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3482 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003483
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003485 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003486
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003488 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003489
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 if (imm == 0) {
3491 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3492 } else if (imm == 1 || imm == -1) {
3493 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003494 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003495 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496 } else {
3497 DCHECK(imm <= -2 || imm >= 2);
3498 GenerateDivRemWithAnyConstant(instruction);
3499 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003500 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003501 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003503 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003505
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3507 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3508 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3509 // so it's safe to just use negl instead of more complex comparisons.
3510 if (type == Primitive::kPrimInt) {
3511 __ cmpl(second_reg, Immediate(-1));
3512 __ j(kEqual, slow_path->GetEntryLabel());
3513 // edx:eax <- sign-extended of eax
3514 __ cdq();
3515 // eax = quotient, edx = remainder
3516 __ idivl(second_reg);
3517 } else {
3518 __ cmpq(second_reg, Immediate(-1));
3519 __ j(kEqual, slow_path->GetEntryLabel());
3520 // rdx:rax <- sign-extended of rax
3521 __ cqo();
3522 // rax = quotient, rdx = remainder
3523 __ idivq(second_reg);
3524 }
3525 __ Bind(slow_path->GetExitLabel());
3526 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003527}
3528
Calin Juravle7c4954d2014-10-28 16:57:40 +00003529void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3530 LocationSummary* locations =
3531 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3532 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003533 case Primitive::kPrimInt:
3534 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003535 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003537 locations->SetOut(Location::SameAsFirstInput());
3538 // Intel uses edx:eax as the dividend.
3539 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3541 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3542 // output and request another temp.
3543 if (div->InputAt(1)->IsConstant()) {
3544 locations->AddTemp(Location::RequiresRegister());
3545 }
Calin Juravled0d48522014-11-04 16:40:20 +00003546 break;
3547 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003548
Calin Juravle7c4954d2014-10-28 16:57:40 +00003549 case Primitive::kPrimFloat:
3550 case Primitive::kPrimDouble: {
3551 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003552 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003553 locations->SetOut(Location::SameAsFirstInput());
3554 break;
3555 }
3556
3557 default:
3558 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3559 }
3560}
3561
3562void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3563 LocationSummary* locations = div->GetLocations();
3564 Location first = locations->InAt(0);
3565 Location second = locations->InAt(1);
3566 DCHECK(first.Equals(locations->Out()));
3567
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003568 Primitive::Type type = div->GetResultType();
3569 switch (type) {
3570 case Primitive::kPrimInt:
3571 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003572 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003573 break;
3574 }
3575
Calin Juravle7c4954d2014-10-28 16:57:40 +00003576 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003577 if (second.IsFpuRegister()) {
3578 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3579 } else if (second.IsConstant()) {
3580 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003581 codegen_->LiteralFloatAddress(
3582 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003583 } else {
3584 DCHECK(second.IsStackSlot());
3585 __ divss(first.AsFpuRegister<XmmRegister>(),
3586 Address(CpuRegister(RSP), second.GetStackIndex()));
3587 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003588 break;
3589 }
3590
3591 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003592 if (second.IsFpuRegister()) {
3593 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3594 } else if (second.IsConstant()) {
3595 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003596 codegen_->LiteralDoubleAddress(
3597 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003598 } else {
3599 DCHECK(second.IsDoubleStackSlot());
3600 __ divsd(first.AsFpuRegister<XmmRegister>(),
3601 Address(CpuRegister(RSP), second.GetStackIndex()));
3602 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003603 break;
3604 }
3605
3606 default:
3607 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3608 }
3609}
3610
Calin Juravlebacfec32014-11-14 15:54:36 +00003611void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003612 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003613 LocationSummary* locations =
3614 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003615
3616 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003617 case Primitive::kPrimInt:
3618 case Primitive::kPrimLong: {
3619 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003621 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3622 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3624 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3625 // output and request another temp.
3626 if (rem->InputAt(1)->IsConstant()) {
3627 locations->AddTemp(Location::RequiresRegister());
3628 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003629 break;
3630 }
3631
3632 case Primitive::kPrimFloat:
3633 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003634 locations->SetInAt(0, Location::Any());
3635 locations->SetInAt(1, Location::Any());
3636 locations->SetOut(Location::RequiresFpuRegister());
3637 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 break;
3639 }
3640
3641 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003642 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003643 }
3644}
3645
3646void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3647 Primitive::Type type = rem->GetResultType();
3648 switch (type) {
3649 case Primitive::kPrimInt:
3650 case Primitive::kPrimLong: {
3651 GenerateDivRemIntegral(rem);
3652 break;
3653 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003654 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003655 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003656 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003657 break;
3658 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003659 default:
3660 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3661 }
3662}
3663
Calin Juravled0d48522014-11-04 16:40:20 +00003664void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003665 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003666 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003667}
3668
3669void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003670 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003671 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3672 codegen_->AddSlowPath(slow_path);
3673
3674 LocationSummary* locations = instruction->GetLocations();
3675 Location value = locations->InAt(0);
3676
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003677 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003678 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003679 case Primitive::kPrimByte:
3680 case Primitive::kPrimChar:
3681 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003682 case Primitive::kPrimInt: {
3683 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003685 __ j(kEqual, slow_path->GetEntryLabel());
3686 } else if (value.IsStackSlot()) {
3687 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3688 __ j(kEqual, slow_path->GetEntryLabel());
3689 } else {
3690 DCHECK(value.IsConstant()) << value;
3691 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003692 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003693 }
3694 }
3695 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003696 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003697 case Primitive::kPrimLong: {
3698 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003700 __ j(kEqual, slow_path->GetEntryLabel());
3701 } else if (value.IsDoubleStackSlot()) {
3702 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3703 __ j(kEqual, slow_path->GetEntryLabel());
3704 } else {
3705 DCHECK(value.IsConstant()) << value;
3706 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003707 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003708 }
3709 }
3710 break;
3711 }
3712 default:
3713 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003714 }
Calin Juravled0d48522014-11-04 16:40:20 +00003715}
3716
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3718 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3719
3720 LocationSummary* locations =
3721 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3722
3723 switch (op->GetResultType()) {
3724 case Primitive::kPrimInt:
3725 case Primitive::kPrimLong: {
3726 locations->SetInAt(0, Location::RequiresRegister());
3727 // The shift count needs to be in CL.
3728 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3729 locations->SetOut(Location::SameAsFirstInput());
3730 break;
3731 }
3732 default:
3733 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3734 }
3735}
3736
3737void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3738 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3739
3740 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003742 Location second = locations->InAt(1);
3743
3744 switch (op->GetResultType()) {
3745 case Primitive::kPrimInt: {
3746 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 if (op->IsShl()) {
3749 __ shll(first_reg, second_reg);
3750 } else if (op->IsShr()) {
3751 __ sarl(first_reg, second_reg);
3752 } else {
3753 __ shrl(first_reg, second_reg);
3754 }
3755 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003756 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003757 if (op->IsShl()) {
3758 __ shll(first_reg, imm);
3759 } else if (op->IsShr()) {
3760 __ sarl(first_reg, imm);
3761 } else {
3762 __ shrl(first_reg, imm);
3763 }
3764 }
3765 break;
3766 }
3767 case Primitive::kPrimLong: {
3768 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003769 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003770 if (op->IsShl()) {
3771 __ shlq(first_reg, second_reg);
3772 } else if (op->IsShr()) {
3773 __ sarq(first_reg, second_reg);
3774 } else {
3775 __ shrq(first_reg, second_reg);
3776 }
3777 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003778 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003779 if (op->IsShl()) {
3780 __ shlq(first_reg, imm);
3781 } else if (op->IsShr()) {
3782 __ sarq(first_reg, imm);
3783 } else {
3784 __ shrq(first_reg, imm);
3785 }
3786 }
3787 break;
3788 }
3789 default:
3790 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003791 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003792 }
3793}
3794
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003795void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3796 LocationSummary* locations =
3797 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3798
3799 switch (ror->GetResultType()) {
3800 case Primitive::kPrimInt:
3801 case Primitive::kPrimLong: {
3802 locations->SetInAt(0, Location::RequiresRegister());
3803 // The shift count needs to be in CL (unless it is a constant).
3804 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3805 locations->SetOut(Location::SameAsFirstInput());
3806 break;
3807 }
3808 default:
3809 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3810 UNREACHABLE();
3811 }
3812}
3813
3814void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3815 LocationSummary* locations = ror->GetLocations();
3816 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3817 Location second = locations->InAt(1);
3818
3819 switch (ror->GetResultType()) {
3820 case Primitive::kPrimInt:
3821 if (second.IsRegister()) {
3822 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3823 __ rorl(first_reg, second_reg);
3824 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003825 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003826 __ rorl(first_reg, imm);
3827 }
3828 break;
3829 case Primitive::kPrimLong:
3830 if (second.IsRegister()) {
3831 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3832 __ rorq(first_reg, second_reg);
3833 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003834 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003835 __ rorq(first_reg, imm);
3836 }
3837 break;
3838 default:
3839 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3840 UNREACHABLE();
3841 }
3842}
3843
Calin Juravle9aec02f2014-11-18 23:06:35 +00003844void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3845 HandleShift(shl);
3846}
3847
3848void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3849 HandleShift(shl);
3850}
3851
3852void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3853 HandleShift(shr);
3854}
3855
3856void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3857 HandleShift(shr);
3858}
3859
3860void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3861 HandleShift(ushr);
3862}
3863
3864void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3865 HandleShift(ushr);
3866}
3867
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003868void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003869 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003870 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003871 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003872 if (instruction->IsStringAlloc()) {
3873 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3874 } else {
3875 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3876 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3877 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003878 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003879}
3880
3881void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003882 // Note: if heap poisoning is enabled, the entry point takes cares
3883 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003884 if (instruction->IsStringAlloc()) {
3885 // String is allocated through StringFactory. Call NewEmptyString entry point.
3886 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003887 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003888 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3889 __ call(Address(temp, code_offset.SizeValue()));
3890 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3891 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003892 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003893 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3894 DCHECK(!codegen_->IsLeafMethod());
3895 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003896}
3897
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003898void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3899 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003901 InvokeRuntimeCallingConvention calling_convention;
3902 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003903 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003904 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003905 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003906}
3907
3908void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3909 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003910 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3911 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003912 // Note: if heap poisoning is enabled, the entry point takes cares
3913 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003914 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003915 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003916
3917 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003918}
3919
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003920void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003921 LocationSummary* locations =
3922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003923 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3924 if (location.IsStackSlot()) {
3925 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3926 } else if (location.IsDoubleStackSlot()) {
3927 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3928 }
3929 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003930}
3931
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003932void InstructionCodeGeneratorX86_64::VisitParameterValue(
3933 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003934 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003935}
3936
3937void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3938 LocationSummary* locations =
3939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3940 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3941}
3942
3943void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3944 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3945 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003946}
3947
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003948void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3949 LocationSummary* locations =
3950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3951 locations->SetInAt(0, Location::RequiresRegister());
3952 locations->SetOut(Location::RequiresRegister());
3953}
3954
3955void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3956 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003957 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003958 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003959 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003960 __ movq(locations->Out().AsRegister<CpuRegister>(),
3961 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003962 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003963 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003964 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003965 __ movq(locations->Out().AsRegister<CpuRegister>(),
3966 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3967 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003968 __ movq(locations->Out().AsRegister<CpuRegister>(),
3969 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003970 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003971}
3972
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003973void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003974 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003975 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003976 locations->SetInAt(0, Location::RequiresRegister());
3977 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003978}
3979
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003980void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3981 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003982 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3983 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003984 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003985 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003986 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003987 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003988 break;
3989
3990 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003991 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003992 break;
3993
3994 default:
3995 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3996 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003997}
3998
David Brazdil66d126e2015-04-03 16:02:44 +01003999void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4000 LocationSummary* locations =
4001 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4002 locations->SetInAt(0, Location::RequiresRegister());
4003 locations->SetOut(Location::SameAsFirstInput());
4004}
4005
4006void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004007 LocationSummary* locations = bool_not->GetLocations();
4008 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4009 locations->Out().AsRegister<CpuRegister>().AsRegister());
4010 Location out = locations->Out();
4011 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4012}
4013
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004014void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004015 LocationSummary* locations =
4016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004017 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004018 locations->SetInAt(i, Location::Any());
4019 }
4020 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004021}
4022
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004023void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004024 LOG(FATAL) << "Unimplemented";
4025}
4026
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004027void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004028 /*
4029 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004030 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004031 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4032 */
4033 switch (kind) {
4034 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004035 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004036 break;
4037 }
4038 case MemBarrierKind::kAnyStore:
4039 case MemBarrierKind::kLoadAny:
4040 case MemBarrierKind::kStoreStore: {
4041 // nop
4042 break;
4043 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004044 case MemBarrierKind::kNTStoreStore:
4045 // Non-Temporal Store/Store needs an explicit fence.
4046 MemoryFence(/* non-temporal */ true);
4047 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004048 }
4049}
4050
4051void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4052 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4053
Roland Levillain0d5a2812015-11-13 10:07:31 +00004054 bool object_field_get_with_read_barrier =
4055 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004056 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004057 new (GetGraph()->GetArena()) LocationSummary(instruction,
4058 object_field_get_with_read_barrier ?
4059 LocationSummary::kCallOnSlowPath :
4060 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004061 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004062 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004063 }
Calin Juravle52c48962014-12-16 17:02:57 +00004064 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004065 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4066 locations->SetOut(Location::RequiresFpuRegister());
4067 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004068 // The output overlaps for an object field get when read barriers
4069 // are enabled: we do not want the move to overwrite the object's
4070 // location, as we need it to emit the read barrier.
4071 locations->SetOut(
4072 Location::RequiresRegister(),
4073 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004074 }
Calin Juravle52c48962014-12-16 17:02:57 +00004075}
4076
4077void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4078 const FieldInfo& field_info) {
4079 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4080
4081 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004082 Location base_loc = locations->InAt(0);
4083 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004084 Location out = locations->Out();
4085 bool is_volatile = field_info.IsVolatile();
4086 Primitive::Type field_type = field_info.GetFieldType();
4087 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4088
4089 switch (field_type) {
4090 case Primitive::kPrimBoolean: {
4091 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4092 break;
4093 }
4094
4095 case Primitive::kPrimByte: {
4096 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4097 break;
4098 }
4099
4100 case Primitive::kPrimShort: {
4101 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4102 break;
4103 }
4104
4105 case Primitive::kPrimChar: {
4106 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4107 break;
4108 }
4109
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004110 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004111 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4112 break;
4113 }
4114
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004115 case Primitive::kPrimNot: {
4116 // /* HeapReference<Object> */ out = *(base + offset)
4117 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004118 // Note that a potential implicit null check is handled in this
4119 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4120 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004121 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004122 if (is_volatile) {
4123 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4124 }
4125 } else {
4126 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4127 codegen_->MaybeRecordImplicitNullCheck(instruction);
4128 if (is_volatile) {
4129 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4130 }
4131 // If read barriers are enabled, emit read barriers other than
4132 // Baker's using a slow path (and also unpoison the loaded
4133 // reference, if heap poisoning is enabled).
4134 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4135 }
4136 break;
4137 }
4138
Calin Juravle52c48962014-12-16 17:02:57 +00004139 case Primitive::kPrimLong: {
4140 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4141 break;
4142 }
4143
4144 case Primitive::kPrimFloat: {
4145 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4146 break;
4147 }
4148
4149 case Primitive::kPrimDouble: {
4150 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4151 break;
4152 }
4153
4154 case Primitive::kPrimVoid:
4155 LOG(FATAL) << "Unreachable type " << field_type;
4156 UNREACHABLE();
4157 }
4158
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004159 if (field_type == Primitive::kPrimNot) {
4160 // Potential implicit null checks, in the case of reference
4161 // fields, are handled in the previous switch statement.
4162 } else {
4163 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004164 }
Roland Levillain4d027112015-07-01 15:41:14 +01004165
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004166 if (is_volatile) {
4167 if (field_type == Primitive::kPrimNot) {
4168 // Memory barriers, in the case of references, are also handled
4169 // in the previous switch statement.
4170 } else {
4171 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4172 }
Roland Levillain4d027112015-07-01 15:41:14 +01004173 }
Calin Juravle52c48962014-12-16 17:02:57 +00004174}
4175
4176void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4177 const FieldInfo& field_info) {
4178 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4179
4180 LocationSummary* locations =
4181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004182 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004183 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004184 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004185 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004186
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004187 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004188 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004189 if (is_volatile) {
4190 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4191 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4192 } else {
4193 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4194 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004195 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004196 if (is_volatile) {
4197 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4198 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4199 } else {
4200 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4201 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004202 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004203 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004204 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004205 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004206 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004207 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4208 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004209 locations->AddTemp(Location::RequiresRegister());
4210 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004211}
4212
Calin Juravle52c48962014-12-16 17:02:57 +00004213void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004214 const FieldInfo& field_info,
4215 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004216 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4217
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004219 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4220 Location value = locations->InAt(1);
4221 bool is_volatile = field_info.IsVolatile();
4222 Primitive::Type field_type = field_info.GetFieldType();
4223 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4224
4225 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004226 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004227 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004228
Mark Mendellea5af682015-10-22 17:35:49 -04004229 bool maybe_record_implicit_null_check_done = false;
4230
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004231 switch (field_type) {
4232 case Primitive::kPrimBoolean:
4233 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004234 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004235 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004236 __ movb(Address(base, offset), Immediate(v));
4237 } else {
4238 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4239 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004240 break;
4241 }
4242
4243 case Primitive::kPrimShort:
4244 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004245 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004246 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004247 __ movw(Address(base, offset), Immediate(v));
4248 } else {
4249 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4250 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004251 break;
4252 }
4253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004254 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004256 if (value.IsConstant()) {
4257 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004258 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4259 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4260 // Note: if heap poisoning is enabled, no need to poison
4261 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004262 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004263 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004264 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4265 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4266 __ movl(temp, value.AsRegister<CpuRegister>());
4267 __ PoisonHeapReference(temp);
4268 __ movl(Address(base, offset), temp);
4269 } else {
4270 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4271 }
Mark Mendell40741f32015-04-20 22:10:34 -04004272 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273 break;
4274 }
4275
4276 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004277 if (value.IsConstant()) {
4278 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004279 codegen_->MoveInt64ToAddress(Address(base, offset),
4280 Address(base, offset + sizeof(int32_t)),
4281 v,
4282 instruction);
4283 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004284 } else {
4285 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4286 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004287 break;
4288 }
4289
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004290 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004291 if (value.IsConstant()) {
4292 int32_t v =
4293 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4294 __ movl(Address(base, offset), Immediate(v));
4295 } else {
4296 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4297 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004298 break;
4299 }
4300
4301 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004302 if (value.IsConstant()) {
4303 int64_t v =
4304 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4305 codegen_->MoveInt64ToAddress(Address(base, offset),
4306 Address(base, offset + sizeof(int32_t)),
4307 v,
4308 instruction);
4309 maybe_record_implicit_null_check_done = true;
4310 } else {
4311 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4312 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004313 break;
4314 }
4315
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316 case Primitive::kPrimVoid:
4317 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004318 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319 }
Calin Juravle52c48962014-12-16 17:02:57 +00004320
Mark Mendellea5af682015-10-22 17:35:49 -04004321 if (!maybe_record_implicit_null_check_done) {
4322 codegen_->MaybeRecordImplicitNullCheck(instruction);
4323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004324
4325 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4326 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4327 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004328 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004329 }
4330
Calin Juravle52c48962014-12-16 17:02:57 +00004331 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004332 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004333 }
4334}
4335
4336void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4337 HandleFieldSet(instruction, instruction->GetFieldInfo());
4338}
4339
4340void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004341 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004342}
4343
4344void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004345 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004346}
4347
4348void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004349 HandleFieldGet(instruction, instruction->GetFieldInfo());
4350}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004351
Calin Juravle52c48962014-12-16 17:02:57 +00004352void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4353 HandleFieldGet(instruction);
4354}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355
Calin Juravle52c48962014-12-16 17:02:57 +00004356void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4357 HandleFieldGet(instruction, instruction->GetFieldInfo());
4358}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359
Calin Juravle52c48962014-12-16 17:02:57 +00004360void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4361 HandleFieldSet(instruction, instruction->GetFieldInfo());
4362}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363
Calin Juravle52c48962014-12-16 17:02:57 +00004364void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004365 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004366}
4367
Calin Juravlee460d1d2015-09-29 04:52:17 +01004368void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4369 HUnresolvedInstanceFieldGet* instruction) {
4370 FieldAccessCallingConventionX86_64 calling_convention;
4371 codegen_->CreateUnresolvedFieldLocationSummary(
4372 instruction, instruction->GetFieldType(), calling_convention);
4373}
4374
4375void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4376 HUnresolvedInstanceFieldGet* instruction) {
4377 FieldAccessCallingConventionX86_64 calling_convention;
4378 codegen_->GenerateUnresolvedFieldAccess(instruction,
4379 instruction->GetFieldType(),
4380 instruction->GetFieldIndex(),
4381 instruction->GetDexPc(),
4382 calling_convention);
4383}
4384
4385void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4386 HUnresolvedInstanceFieldSet* instruction) {
4387 FieldAccessCallingConventionX86_64 calling_convention;
4388 codegen_->CreateUnresolvedFieldLocationSummary(
4389 instruction, instruction->GetFieldType(), calling_convention);
4390}
4391
4392void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4393 HUnresolvedInstanceFieldSet* instruction) {
4394 FieldAccessCallingConventionX86_64 calling_convention;
4395 codegen_->GenerateUnresolvedFieldAccess(instruction,
4396 instruction->GetFieldType(),
4397 instruction->GetFieldIndex(),
4398 instruction->GetDexPc(),
4399 calling_convention);
4400}
4401
4402void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4403 HUnresolvedStaticFieldGet* instruction) {
4404 FieldAccessCallingConventionX86_64 calling_convention;
4405 codegen_->CreateUnresolvedFieldLocationSummary(
4406 instruction, instruction->GetFieldType(), calling_convention);
4407}
4408
4409void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4410 HUnresolvedStaticFieldGet* instruction) {
4411 FieldAccessCallingConventionX86_64 calling_convention;
4412 codegen_->GenerateUnresolvedFieldAccess(instruction,
4413 instruction->GetFieldType(),
4414 instruction->GetFieldIndex(),
4415 instruction->GetDexPc(),
4416 calling_convention);
4417}
4418
4419void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4420 HUnresolvedStaticFieldSet* instruction) {
4421 FieldAccessCallingConventionX86_64 calling_convention;
4422 codegen_->CreateUnresolvedFieldLocationSummary(
4423 instruction, instruction->GetFieldType(), calling_convention);
4424}
4425
4426void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4427 HUnresolvedStaticFieldSet* instruction) {
4428 FieldAccessCallingConventionX86_64 calling_convention;
4429 codegen_->GenerateUnresolvedFieldAccess(instruction,
4430 instruction->GetFieldType(),
4431 instruction->GetFieldIndex(),
4432 instruction->GetDexPc(),
4433 calling_convention);
4434}
4435
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004437 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4438 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4439 ? Location::RequiresRegister()
4440 : Location::Any();
4441 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442}
4443
Calin Juravle2ae48182016-03-16 14:05:09 +00004444void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4445 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004446 return;
4447 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004448 LocationSummary* locations = instruction->GetLocations();
4449 Location obj = locations->InAt(0);
4450
4451 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004452 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004453}
4454
Calin Juravle2ae48182016-03-16 14:05:09 +00004455void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004456 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004457 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458
4459 LocationSummary* locations = instruction->GetLocations();
4460 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461
4462 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004463 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004464 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004466 } else {
4467 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004468 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004469 __ jmp(slow_path->GetEntryLabel());
4470 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471 }
4472 __ j(kEqual, slow_path->GetEntryLabel());
4473}
4474
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004475void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004476 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004477}
4478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004480 bool object_array_get_with_read_barrier =
4481 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004482 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004483 new (GetGraph()->GetArena()) LocationSummary(instruction,
4484 object_array_get_with_read_barrier ?
4485 LocationSummary::kCallOnSlowPath :
4486 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004487 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004488 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004489 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004490 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004491 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004492 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4493 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4494 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004495 // The output overlaps for an object array get when read barriers
4496 // are enabled: we do not want the move to overwrite the array's
4497 // location, as we need it to emit the read barrier.
4498 locations->SetOut(
4499 Location::RequiresRegister(),
4500 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004501 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004502}
4503
4504void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4505 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004506 Location obj_loc = locations->InAt(0);
4507 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004508 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004509 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004510 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004512 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004513 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004515 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004516 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517 break;
4518 }
4519
4520 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004521 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004522 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 break;
4524 }
4525
4526 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004527 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004528 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 break;
4530 }
4531
4532 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004533 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004534 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4535 // Branch cases into compressed and uncompressed for each index's type.
4536 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4537 NearLabel done, not_compressed;
4538 __ cmpl(Address(obj, count_offset), Immediate(0));
4539 codegen_->MaybeRecordImplicitNullCheck(instruction);
4540 __ j(kGreaterEqual, &not_compressed);
4541 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4542 __ jmp(&done);
4543 __ Bind(&not_compressed);
4544 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4545 __ Bind(&done);
4546 } else {
4547 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4548 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 break;
4550 }
4551
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004552 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004553 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004554 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004555 break;
4556 }
4557
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004558 case Primitive::kPrimNot: {
4559 static_assert(
4560 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4561 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004562 // /* HeapReference<Object> */ out =
4563 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4564 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004565 // Note that a potential implicit null check is handled in this
4566 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4567 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004568 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004569 } else {
4570 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004571 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4572 codegen_->MaybeRecordImplicitNullCheck(instruction);
4573 // If read barriers are enabled, emit read barriers other than
4574 // Baker's using a slow path (and also unpoison the loaded
4575 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004576 if (index.IsConstant()) {
4577 uint32_t offset =
4578 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004579 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4580 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004581 codegen_->MaybeGenerateReadBarrierSlow(
4582 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4583 }
4584 }
4585 break;
4586 }
4587
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004589 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004590 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 break;
4592 }
4593
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004594 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004595 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004596 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004597 break;
4598 }
4599
4600 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004601 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004602 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004603 break;
4604 }
4605
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004606 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004607 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004608 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 }
Roland Levillain4d027112015-07-01 15:41:14 +01004610
4611 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004612 // Potential implicit null checks, in the case of reference
4613 // arrays, are handled in the previous switch statement.
4614 } else {
4615 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617}
4618
4619void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004620 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004621
4622 bool needs_write_barrier =
4623 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004624 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004625
Nicolas Geoffray39468442014-09-02 15:17:15 +01004626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004627 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004628 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004629 LocationSummary::kCallOnSlowPath :
4630 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004631
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004633 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4634 if (Primitive::IsFloatingPointType(value_type)) {
4635 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004636 } else {
4637 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4638 }
4639
4640 if (needs_write_barrier) {
4641 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004642 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004643 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004644 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004645}
4646
4647void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4648 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004649 Location array_loc = locations->InAt(0);
4650 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004652 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004653 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004654 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004655 bool needs_write_barrier =
4656 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004657 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4658 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4659 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660
4661 switch (value_type) {
4662 case Primitive::kPrimBoolean:
4663 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004664 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004665 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004666 if (value.IsRegister()) {
4667 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004669 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004671 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004672 break;
4673 }
4674
4675 case Primitive::kPrimShort:
4676 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004677 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004678 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004679 if (value.IsRegister()) {
4680 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004681 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682 DCHECK(value.IsConstant()) << value;
4683 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004684 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004685 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004686 break;
4687 }
4688
4689 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004690 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004691 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004692
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004693 if (!value.IsRegister()) {
4694 // Just setting null.
4695 DCHECK(instruction->InputAt(2)->IsNullConstant());
4696 DCHECK(value.IsConstant()) << value;
4697 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004699 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004700 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004701 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004702 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004703
4704 DCHECK(needs_write_barrier);
4705 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004706 // We cannot use a NearLabel for `done`, as its range may be too
4707 // short when Baker read barriers are enabled.
4708 Label done;
4709 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004710 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004711 Location temp_loc = locations->GetTemp(0);
4712 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004713 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004714 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4715 codegen_->AddSlowPath(slow_path);
4716 if (instruction->GetValueCanBeNull()) {
4717 __ testl(register_value, register_value);
4718 __ j(kNotEqual, &not_null);
4719 __ movl(address, Immediate(0));
4720 codegen_->MaybeRecordImplicitNullCheck(instruction);
4721 __ jmp(&done);
4722 __ Bind(&not_null);
4723 }
4724
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004725 // Note that when Baker read barriers are enabled, the type
4726 // checks are performed without read barriers. This is fine,
4727 // even in the case where a class object is in the from-space
4728 // after the flip, as a comparison involving such a type would
4729 // not produce a false positive; it may of course produce a
4730 // false negative, in which case we would take the ArraySet
4731 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004732
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004733 // /* HeapReference<Class> */ temp = array->klass_
4734 __ movl(temp, Address(array, class_offset));
4735 codegen_->MaybeRecordImplicitNullCheck(instruction);
4736 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004737
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004738 // /* HeapReference<Class> */ temp = temp->component_type_
4739 __ movl(temp, Address(temp, component_offset));
4740 // If heap poisoning is enabled, no need to unpoison `temp`
4741 // nor the object reference in `register_value->klass`, as
4742 // we are comparing two poisoned references.
4743 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004744
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004745 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4746 __ j(kEqual, &do_put);
4747 // If heap poisoning is enabled, the `temp` reference has
4748 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004749 __ MaybeUnpoisonHeapReference(temp);
4750
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004751 // If heap poisoning is enabled, no need to unpoison the
4752 // heap reference loaded below, as it is only used for a
4753 // comparison with null.
4754 __ cmpl(Address(temp, super_offset), Immediate(0));
4755 __ j(kNotEqual, slow_path->GetEntryLabel());
4756 __ Bind(&do_put);
4757 } else {
4758 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 }
4760 }
4761
4762 if (kPoisonHeapReferences) {
4763 __ movl(temp, register_value);
4764 __ PoisonHeapReference(temp);
4765 __ movl(address, temp);
4766 } else {
4767 __ movl(address, register_value);
4768 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004769 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004770 codegen_->MaybeRecordImplicitNullCheck(instruction);
4771 }
4772
4773 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4774 codegen_->MarkGCCard(
4775 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4776 __ Bind(&done);
4777
4778 if (slow_path != nullptr) {
4779 __ Bind(slow_path->GetExitLabel());
4780 }
4781
4782 break;
4783 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004784
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004785 case Primitive::kPrimInt: {
4786 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004787 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004788 if (value.IsRegister()) {
4789 __ movl(address, value.AsRegister<CpuRegister>());
4790 } else {
4791 DCHECK(value.IsConstant()) << value;
4792 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4793 __ movl(address, Immediate(v));
4794 }
4795 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004796 break;
4797 }
4798
4799 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004800 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004801 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004802 if (value.IsRegister()) {
4803 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004805 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004807 Address address_high =
4808 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004809 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 }
4811 break;
4812 }
4813
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004814 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004815 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004816 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004817 if (value.IsFpuRegister()) {
4818 __ movss(address, value.AsFpuRegister<XmmRegister>());
4819 } else {
4820 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004821 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004822 __ movl(address, Immediate(v));
4823 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004825 break;
4826 }
4827
4828 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004829 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004830 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004831 if (value.IsFpuRegister()) {
4832 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4833 codegen_->MaybeRecordImplicitNullCheck(instruction);
4834 } else {
4835 int64_t v =
4836 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004837 Address address_high =
4838 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004839 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4840 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004841 break;
4842 }
4843
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844 case Primitive::kPrimVoid:
4845 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004846 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004847 }
4848}
4849
4850void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004851 LocationSummary* locations =
4852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004853 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04004854 if (!instruction->IsEmittedAtUseSite()) {
4855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4856 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857}
4858
4859void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04004860 if (instruction->IsEmittedAtUseSite()) {
4861 return;
4862 }
4863
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004865 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004866 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4867 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004869 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07004870 // Mask out most significant bit in case the array is String's array of char.
4871 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
4872 __ andl(out, Immediate(INT32_MAX));
4873 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004874}
4875
4876void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004877 RegisterSet caller_saves = RegisterSet::Empty();
4878 InvokeRuntimeCallingConvention calling_convention;
4879 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4880 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4881 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004882 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04004883 HInstruction* length = instruction->InputAt(1);
4884 if (!length->IsEmittedAtUseSite()) {
4885 locations->SetInAt(1, Location::RegisterOrConstant(length));
4886 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887}
4888
4889void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4890 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004891 Location index_loc = locations->InAt(0);
4892 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04004893 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894
Mark Mendell99dbd682015-04-22 16:18:52 -04004895 if (length_loc.IsConstant()) {
4896 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4897 if (index_loc.IsConstant()) {
4898 // BCE will remove the bounds check if we are guarenteed to pass.
4899 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4900 if (index < 0 || index >= length) {
4901 codegen_->AddSlowPath(slow_path);
4902 __ jmp(slow_path->GetEntryLabel());
4903 } else {
4904 // Some optimization after BCE may have generated this, and we should not
4905 // generate a bounds check if it is a valid range.
4906 }
4907 return;
4908 }
4909
4910 // We have to reverse the jump condition because the length is the constant.
4911 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4912 __ cmpl(index_reg, Immediate(length));
4913 codegen_->AddSlowPath(slow_path);
4914 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004915 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04004916 HInstruction* array_length = instruction->InputAt(1);
4917 if (array_length->IsEmittedAtUseSite()) {
4918 // Address the length field in the array.
4919 DCHECK(array_length->IsArrayLength());
4920 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
4921 Location array_loc = array_length->GetLocations()->InAt(0);
4922 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07004923 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4924 CpuRegister length_reg = CpuRegister(TMP);
4925 __ movl(length_reg, array_len);
4926 codegen_->MaybeRecordImplicitNullCheck(array_length);
4927 __ andl(length_reg, Immediate(INT32_MAX));
4928 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04004929 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07004930 // Checking the bound for general case:
4931 // Array of char or String's array when the compression feature off.
4932 if (index_loc.IsConstant()) {
4933 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4934 __ cmpl(array_len, Immediate(value));
4935 } else {
4936 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
4937 }
4938 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04004939 }
Mark Mendell99dbd682015-04-22 16:18:52 -04004940 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004941 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04004942 }
4943 codegen_->AddSlowPath(slow_path);
4944 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946}
4947
4948void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4949 CpuRegister card,
4950 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004951 CpuRegister value,
4952 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004953 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004954 if (value_can_be_null) {
4955 __ testl(value, value);
4956 __ j(kEqual, &is_null);
4957 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004958 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004959 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004960 __ movq(temp, object);
4961 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004962 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004963 if (value_can_be_null) {
4964 __ Bind(&is_null);
4965 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966}
4967
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004968void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004969 LOG(FATAL) << "Unimplemented";
4970}
4971
4972void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004973 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4974}
4975
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004976void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004977 LocationSummary* locations =
4978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004979 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004980}
4981
4982void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004983 HBasicBlock* block = instruction->GetBlock();
4984 if (block->GetLoopInformation() != nullptr) {
4985 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4986 // The back edge will generate the suspend check.
4987 return;
4988 }
4989 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4990 // The goto will generate the suspend check.
4991 return;
4992 }
4993 GenerateSuspendCheck(instruction, nullptr);
4994}
4995
4996void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4997 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004998 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004999 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5000 if (slow_path == nullptr) {
5001 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5002 instruction->SetSlowPath(slow_path);
5003 codegen_->AddSlowPath(slow_path);
5004 if (successor != nullptr) {
5005 DCHECK(successor->IsLoopHeader());
5006 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5007 }
5008 } else {
5009 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5010 }
5011
Andreas Gampe542451c2016-07-26 09:02:02 -07005012 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005013 /* no_rip */ true),
5014 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005015 if (successor == nullptr) {
5016 __ j(kNotEqual, slow_path->GetEntryLabel());
5017 __ Bind(slow_path->GetReturnLabel());
5018 } else {
5019 __ j(kEqual, codegen_->GetLabelOf(successor));
5020 __ jmp(slow_path->GetEntryLabel());
5021 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005022}
5023
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005024X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5025 return codegen_->GetAssembler();
5026}
5027
5028void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005029 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005030 Location source = move->GetSource();
5031 Location destination = move->GetDestination();
5032
5033 if (source.IsRegister()) {
5034 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005035 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005036 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005037 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005038 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005039 } else {
5040 DCHECK(destination.IsDoubleStackSlot());
5041 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005042 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005043 }
5044 } else if (source.IsStackSlot()) {
5045 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005046 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005047 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005048 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005049 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005050 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005051 } else {
5052 DCHECK(destination.IsStackSlot());
5053 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5054 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5055 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005056 } else if (source.IsDoubleStackSlot()) {
5057 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005058 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005059 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005060 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005061 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5062 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005063 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005064 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005065 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5066 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5067 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005068 } else if (source.IsConstant()) {
5069 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005070 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5071 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005072 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005073 if (value == 0) {
5074 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5075 } else {
5076 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5077 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005078 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005079 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005080 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005081 }
5082 } else if (constant->IsLongConstant()) {
5083 int64_t value = constant->AsLongConstant()->GetValue();
5084 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005085 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005086 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005087 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005088 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005089 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005090 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005091 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005092 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005093 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005094 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005095 } else {
5096 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005097 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005098 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5099 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005100 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005101 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005102 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005103 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005104 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005105 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005106 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005107 } else {
5108 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005109 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005110 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005111 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005112 } else if (source.IsFpuRegister()) {
5113 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005114 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005115 } else if (destination.IsStackSlot()) {
5116 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005117 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005118 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005119 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005120 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005122 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005123 }
5124}
5125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005126void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005127 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005128 __ movl(Address(CpuRegister(RSP), mem), reg);
5129 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005130}
5131
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005132void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005133 ScratchRegisterScope ensure_scratch(
5134 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5135
5136 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5137 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5138 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5139 Address(CpuRegister(RSP), mem2 + stack_offset));
5140 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5141 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5142 CpuRegister(ensure_scratch.GetRegister()));
5143}
5144
Mark Mendell8a1c7282015-06-29 15:41:28 -04005145void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5146 __ movq(CpuRegister(TMP), reg1);
5147 __ movq(reg1, reg2);
5148 __ movq(reg2, CpuRegister(TMP));
5149}
5150
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005151void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5152 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5153 __ movq(Address(CpuRegister(RSP), mem), reg);
5154 __ movq(reg, CpuRegister(TMP));
5155}
5156
5157void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5158 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005159 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005160
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005161 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5162 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5163 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5164 Address(CpuRegister(RSP), mem2 + stack_offset));
5165 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5166 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5167 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005168}
5169
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005170void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5171 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5172 __ movss(Address(CpuRegister(RSP), mem), reg);
5173 __ movd(reg, CpuRegister(TMP));
5174}
5175
5176void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5177 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5178 __ movsd(Address(CpuRegister(RSP), mem), reg);
5179 __ movd(reg, CpuRegister(TMP));
5180}
5181
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005182void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005183 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005184 Location source = move->GetSource();
5185 Location destination = move->GetDestination();
5186
5187 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005188 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005189 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005190 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005191 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005192 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005193 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005194 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5195 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005196 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005197 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005198 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005199 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5200 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005201 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005202 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5203 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5204 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005206 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005208 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005212 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005213 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005215 }
5216}
5217
5218
5219void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5220 __ pushq(CpuRegister(reg));
5221}
5222
5223
5224void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5225 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005226}
5227
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005228void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005229 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005230 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5231 Immediate(mirror::Class::kStatusInitialized));
5232 __ j(kLess, slow_path->GetEntryLabel());
5233 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005234 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005235}
5236
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005237HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5238 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005239 switch (desired_class_load_kind) {
5240 case HLoadClass::LoadKind::kReferrersClass:
5241 break;
5242 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5243 DCHECK(!GetCompilerOptions().GetCompilePic());
5244 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5245 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5246 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5247 DCHECK(GetCompilerOptions().GetCompilePic());
5248 break;
5249 case HLoadClass::LoadKind::kBootImageAddress:
5250 break;
5251 case HLoadClass::LoadKind::kDexCacheAddress:
5252 DCHECK(Runtime::Current()->UseJitCompilation());
5253 break;
5254 case HLoadClass::LoadKind::kDexCachePcRelative:
5255 DCHECK(!Runtime::Current()->UseJitCompilation());
5256 break;
5257 case HLoadClass::LoadKind::kDexCacheViaMethod:
5258 break;
5259 }
5260 return desired_class_load_kind;
5261}
5262
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005263void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005264 if (cls->NeedsAccessCheck()) {
5265 InvokeRuntimeCallingConvention calling_convention;
5266 CodeGenerator::CreateLoadClassLocationSummary(
5267 cls,
5268 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5269 Location::RegisterLocation(RAX),
5270 /* code_generator_supports_read_barrier */ true);
5271 return;
5272 }
5273
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005274 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5275 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005276 ? LocationSummary::kCallOnSlowPath
5277 : LocationSummary::kNoCall;
5278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005279 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005280 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005281 }
5282
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005283 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5284 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5285 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5286 locations->SetInAt(0, Location::RequiresRegister());
5287 }
5288 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005289}
5290
5291void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005292 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005293 if (cls->NeedsAccessCheck()) {
5294 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005295 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005296 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005297 return;
5298 }
5299
Roland Levillain0d5a2812015-11-13 10:07:31 +00005300 Location out_loc = locations->Out();
5301 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005302
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005303 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005304 bool generate_null_check = false;
5305 switch (cls->GetLoadKind()) {
5306 case HLoadClass::LoadKind::kReferrersClass: {
5307 DCHECK(!cls->CanCallRuntime());
5308 DCHECK(!cls->MustGenerateClinitCheck());
5309 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5310 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5311 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005312 cls,
5313 out_loc,
5314 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5315 /*fixup_label*/nullptr,
5316 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005317 break;
5318 }
5319 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005320 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005321 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5322 codegen_->RecordTypePatch(cls);
5323 break;
5324 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005325 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005326 DCHECK_NE(cls->GetAddress(), 0u);
5327 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5328 __ movl(out, Immediate(address)); // Zero-extended.
5329 codegen_->RecordSimplePatch();
5330 break;
5331 }
5332 case HLoadClass::LoadKind::kDexCacheAddress: {
5333 DCHECK_NE(cls->GetAddress(), 0u);
5334 // /* GcRoot<mirror::Class> */ out = *address
5335 if (IsUint<32>(cls->GetAddress())) {
5336 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005337 GenerateGcRootFieldLoad(cls,
5338 out_loc,
5339 address,
5340 /*fixup_label*/nullptr,
5341 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005342 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005343 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5344 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005345 GenerateGcRootFieldLoad(cls,
5346 out_loc,
5347 Address(out, 0),
5348 /*fixup_label*/nullptr,
5349 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005350 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005351 generate_null_check = !cls->IsInDexCache();
5352 break;
5353 }
5354 case HLoadClass::LoadKind::kDexCachePcRelative: {
5355 uint32_t offset = cls->GetDexCacheElementOffset();
5356 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5357 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5358 /* no_rip */ false);
5359 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005360 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005361 generate_null_check = !cls->IsInDexCache();
5362 break;
5363 }
5364 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5365 // /* GcRoot<mirror::Class>[] */ out =
5366 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5367 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5368 __ movq(out,
5369 Address(current_method,
5370 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5371 // /* GcRoot<mirror::Class> */ out = out[type_index]
5372 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005373 cls,
5374 out_loc,
5375 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5376 /*fixup_label*/nullptr,
5377 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005378 generate_null_check = !cls->IsInDexCache();
5379 break;
5380 }
5381 default:
5382 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5383 UNREACHABLE();
5384 }
5385
5386 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5387 DCHECK(cls->CanCallRuntime());
5388 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5389 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5390 codegen_->AddSlowPath(slow_path);
5391 if (generate_null_check) {
5392 __ testl(out, out);
5393 __ j(kEqual, slow_path->GetEntryLabel());
5394 }
5395 if (cls->MustGenerateClinitCheck()) {
5396 GenerateClassInitializationCheck(slow_path, out);
5397 } else {
5398 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005400 }
5401}
5402
5403void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5404 LocationSummary* locations =
5405 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5406 locations->SetInAt(0, Location::RequiresRegister());
5407 if (check->HasUses()) {
5408 locations->SetOut(Location::SameAsFirstInput());
5409 }
5410}
5411
5412void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005413 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005414 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005415 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005417 GenerateClassInitializationCheck(slow_path,
5418 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005419}
5420
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005421HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5422 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005423 switch (desired_string_load_kind) {
5424 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5425 DCHECK(!GetCompilerOptions().GetCompilePic());
5426 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5427 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5428 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5429 DCHECK(GetCompilerOptions().GetCompilePic());
5430 break;
5431 case HLoadString::LoadKind::kBootImageAddress:
5432 break;
5433 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005434 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005435 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005436 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005437 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005438 break;
5439 case HLoadString::LoadKind::kDexCacheViaMethod:
5440 break;
5441 }
5442 return desired_string_load_kind;
5443}
5444
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005445void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005446 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005447 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5448 ? LocationSummary::kCallOnMainOnly
5449 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005450 : LocationSummary::kNoCall;
5451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005452 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005453 locations->SetOut(Location::RegisterLocation(RAX));
5454 } else {
5455 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005456 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5457 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5458 // Rely on the pResolveString and/or marking to save everything.
5459 // Custom calling convention: RAX serves as both input and output.
5460 RegisterSet caller_saves = RegisterSet::Empty();
5461 caller_saves.Add(Location::RegisterLocation(RAX));
5462 locations->SetCustomSlowPathCallerSaves(caller_saves);
5463 } else {
5464 // For non-Baker read barrier we have a temp-clobbering call.
5465 }
5466 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005467 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005468}
5469
5470void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005471 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472 Location out_loc = locations->Out();
5473 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005475 switch (load->GetLoadKind()) {
5476 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005478 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005479 return; // No dex cache slow path.
5480 }
5481 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005482 DCHECK_NE(load->GetAddress(), 0u);
5483 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5484 __ movl(out, Immediate(address)); // Zero-extended.
5485 codegen_->RecordSimplePatch();
5486 return; // No dex cache slow path.
5487 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005488 case HLoadString::LoadKind::kBssEntry: {
5489 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5490 /* no_rip */ false);
5491 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5492 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5493 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5494 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5495 codegen_->AddSlowPath(slow_path);
5496 __ testl(out, out);
5497 __ j(kEqual, slow_path->GetEntryLabel());
5498 __ Bind(slow_path->GetExitLabel());
5499 return;
5500 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005501 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005502 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005503 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005505 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005506 // Custom calling convention: RAX serves as both input and output.
5507 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex()));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005508 codegen_->InvokeRuntime(kQuickResolveString,
5509 load,
5510 load->GetDexPc());
5511 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005512}
5513
David Brazdilcb1c0552015-08-04 16:22:25 +01005514static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005515 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005516 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005517}
5518
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005519void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5520 LocationSummary* locations =
5521 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5522 locations->SetOut(Location::RequiresRegister());
5523}
5524
5525void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005526 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5527}
5528
5529void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5530 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5531}
5532
5533void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5534 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005535}
5536
5537void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5538 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005540 InvokeRuntimeCallingConvention calling_convention;
5541 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5542}
5543
5544void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005545 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005546 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005547}
5548
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005549static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5550 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005551 !kUseBakerReadBarrier &&
5552 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005553 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5554 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5555}
5556
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005557void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005559 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005560 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005561 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005562 case TypeCheckKind::kExactCheck:
5563 case TypeCheckKind::kAbstractClassCheck:
5564 case TypeCheckKind::kClassHierarchyCheck:
5565 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005566 call_kind =
5567 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005568 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 break;
5570 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005571 case TypeCheckKind::kUnresolvedCheck:
5572 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573 call_kind = LocationSummary::kCallOnSlowPath;
5574 break;
5575 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005576
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005578 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005579 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005580 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005581 locations->SetInAt(0, Location::RequiresRegister());
5582 locations->SetInAt(1, Location::Any());
5583 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5584 locations->SetOut(Location::RequiresRegister());
5585 // When read barriers are enabled, we need a temporary register for
5586 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005587 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005588 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005589 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005590}
5591
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005592void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005593 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005594 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005595 Location obj_loc = locations->InAt(0);
5596 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005597 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005598 Location out_loc = locations->Out();
5599 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005600 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005601 locations->GetTemp(0) :
5602 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005603 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5605 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5606 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005607 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005608 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005609
5610 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005611 // Avoid null check if we know obj is not null.
5612 if (instruction->MustDoNullCheck()) {
5613 __ testl(obj, obj);
5614 __ j(kEqual, &zero);
5615 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005616
Roland Levillain0d5a2812015-11-13 10:07:31 +00005617 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005618 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005620 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005621 case TypeCheckKind::kExactCheck: {
5622 if (cls.IsRegister()) {
5623 __ cmpl(out, cls.AsRegister<CpuRegister>());
5624 } else {
5625 DCHECK(cls.IsStackSlot()) << cls;
5626 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5627 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005628 if (zero.IsLinked()) {
5629 // Classes must be equal for the instanceof to succeed.
5630 __ j(kNotEqual, &zero);
5631 __ movl(out, Immediate(1));
5632 __ jmp(&done);
5633 } else {
5634 __ setcc(kEqual, out);
5635 // setcc only sets the low byte.
5636 __ andl(out, Immediate(1));
5637 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005638 break;
5639 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005640
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 case TypeCheckKind::kAbstractClassCheck: {
5642 // If the class is abstract, we eagerly fetch the super class of the
5643 // object to avoid doing a comparison we know will fail.
5644 NearLabel loop, success;
5645 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005646 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005647 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005648 __ testl(out, out);
5649 // If `out` is null, we use it for the result, and jump to `done`.
5650 __ j(kEqual, &done);
5651 if (cls.IsRegister()) {
5652 __ cmpl(out, cls.AsRegister<CpuRegister>());
5653 } else {
5654 DCHECK(cls.IsStackSlot()) << cls;
5655 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5656 }
5657 __ j(kNotEqual, &loop);
5658 __ movl(out, Immediate(1));
5659 if (zero.IsLinked()) {
5660 __ jmp(&done);
5661 }
5662 break;
5663 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005664
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665 case TypeCheckKind::kClassHierarchyCheck: {
5666 // Walk over the class hierarchy to find a match.
5667 NearLabel loop, success;
5668 __ Bind(&loop);
5669 if (cls.IsRegister()) {
5670 __ cmpl(out, cls.AsRegister<CpuRegister>());
5671 } else {
5672 DCHECK(cls.IsStackSlot()) << cls;
5673 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5674 }
5675 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005676 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005677 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005678 __ testl(out, out);
5679 __ j(kNotEqual, &loop);
5680 // If `out` is null, we use it for the result, and jump to `done`.
5681 __ jmp(&done);
5682 __ Bind(&success);
5683 __ movl(out, Immediate(1));
5684 if (zero.IsLinked()) {
5685 __ jmp(&done);
5686 }
5687 break;
5688 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005689
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005690 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005691 // Do an exact check.
5692 NearLabel exact_check;
5693 if (cls.IsRegister()) {
5694 __ cmpl(out, cls.AsRegister<CpuRegister>());
5695 } else {
5696 DCHECK(cls.IsStackSlot()) << cls;
5697 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5698 }
5699 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005700 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005701 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005702 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703 __ testl(out, out);
5704 // If `out` is null, we use it for the result, and jump to `done`.
5705 __ j(kEqual, &done);
5706 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5707 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005708 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005709 __ movl(out, Immediate(1));
5710 __ jmp(&done);
5711 break;
5712 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005713
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 case TypeCheckKind::kArrayCheck: {
5715 if (cls.IsRegister()) {
5716 __ cmpl(out, cls.AsRegister<CpuRegister>());
5717 } else {
5718 DCHECK(cls.IsStackSlot()) << cls;
5719 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5720 }
5721 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5723 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 codegen_->AddSlowPath(slow_path);
5725 __ j(kNotEqual, slow_path->GetEntryLabel());
5726 __ movl(out, Immediate(1));
5727 if (zero.IsLinked()) {
5728 __ jmp(&done);
5729 }
5730 break;
5731 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732
Calin Juravle98893e12015-10-02 21:05:03 +01005733 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734 case TypeCheckKind::kInterfaceCheck: {
5735 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005736 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 // cases.
5738 //
5739 // We cannot directly call the InstanceofNonTrivial runtime
5740 // entry point without resorting to a type checking slow path
5741 // here (i.e. by calling InvokeRuntime directly), as it would
5742 // require to assign fixed registers for the inputs of this
5743 // HInstanceOf instruction (following the runtime calling
5744 // convention), which might be cluttered by the potential first
5745 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005746 //
5747 // TODO: Introduce a new runtime entry point taking the object
5748 // to test (instead of its class) as argument, and let it deal
5749 // with the read barrier issues. This will let us refactor this
5750 // case of the `switch` code as it was previously (with a direct
5751 // call to the runtime not using a type checking slow path).
5752 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005753 DCHECK(locations->OnlyCallsOnSlowPath());
5754 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5755 /* is_fatal */ false);
5756 codegen_->AddSlowPath(slow_path);
5757 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005758 if (zero.IsLinked()) {
5759 __ jmp(&done);
5760 }
5761 break;
5762 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005763 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005764
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005766 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005767 __ xorl(out, out);
5768 }
5769
5770 if (done.IsLinked()) {
5771 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005772 }
5773
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005774 if (slow_path != nullptr) {
5775 __ Bind(slow_path->GetExitLabel());
5776 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005777}
5778
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005779void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005780 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5781 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005782 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5783 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005784 case TypeCheckKind::kExactCheck:
5785 case TypeCheckKind::kAbstractClassCheck:
5786 case TypeCheckKind::kClassHierarchyCheck:
5787 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5789 LocationSummary::kCallOnSlowPath :
5790 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005791 break;
5792 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005793 case TypeCheckKind::kUnresolvedCheck:
5794 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 call_kind = LocationSummary::kCallOnSlowPath;
5796 break;
5797 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005798 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5799 locations->SetInAt(0, Location::RequiresRegister());
5800 locations->SetInAt(1, Location::Any());
5801 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5802 locations->AddTemp(Location::RequiresRegister());
5803 // When read barriers are enabled, we need an additional temporary
5804 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005805 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005808}
5809
5810void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005811 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005812 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005813 Location obj_loc = locations->InAt(0);
5814 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005815 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 Location temp_loc = locations->GetTemp(0);
5817 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005818 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005819 locations->GetTemp(1) :
5820 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005821 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5822 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5823 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5824 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825
Roland Levillain0d5a2812015-11-13 10:07:31 +00005826 bool is_type_check_slow_path_fatal =
5827 (type_check_kind == TypeCheckKind::kExactCheck ||
5828 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5829 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5830 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5831 !instruction->CanThrowIntoCatchBlock();
5832 SlowPathCode* type_check_slow_path =
5833 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5834 is_type_check_slow_path_fatal);
5835 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 case TypeCheckKind::kExactCheck:
5839 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005840 NearLabel done;
5841 // Avoid null check if we know obj is not null.
5842 if (instruction->MustDoNullCheck()) {
5843 __ testl(obj, obj);
5844 __ j(kEqual, &done);
5845 }
5846
5847 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005848 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005849
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005850 if (cls.IsRegister()) {
5851 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5852 } else {
5853 DCHECK(cls.IsStackSlot()) << cls;
5854 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5855 }
5856 // Jump to slow path for throwing the exception or doing a
5857 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005859 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 break;
5861 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005864 NearLabel done;
5865 // Avoid null check if we know obj is not null.
5866 if (instruction->MustDoNullCheck()) {
5867 __ testl(obj, obj);
5868 __ j(kEqual, &done);
5869 }
5870
5871 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005872 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005873
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874 // If the class is abstract, we eagerly fetch the super class of the
5875 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005879 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880
5881 // If the class reference currently in `temp` is not null, jump
5882 // to the `compare_classes` label to compare it with the checked
5883 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005885 __ j(kNotEqual, &compare_classes);
5886 // Otherwise, jump to the slow path to throw the exception.
5887 //
5888 // But before, move back the object's class into `temp` before
5889 // going into the slow path, as it has been overwritten in the
5890 // meantime.
5891 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005892 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 __ jmp(type_check_slow_path->GetEntryLabel());
5894
5895 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 if (cls.IsRegister()) {
5897 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5898 } else {
5899 DCHECK(cls.IsStackSlot()) << cls;
5900 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5901 }
5902 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005903 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005904 break;
5905 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005906
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005908 NearLabel done;
5909 // Avoid null check if we know obj is not null.
5910 if (instruction->MustDoNullCheck()) {
5911 __ testl(obj, obj);
5912 __ j(kEqual, &done);
5913 }
5914
5915 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005916 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005917
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005919 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005920 __ Bind(&loop);
5921 if (cls.IsRegister()) {
5922 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5923 } else {
5924 DCHECK(cls.IsStackSlot()) << cls;
5925 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5926 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005927 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928
Roland Levillain0d5a2812015-11-13 10:07:31 +00005929 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005930 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931
5932 // If the class reference currently in `temp` is not null, jump
5933 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 __ testl(temp, temp);
5935 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936 // Otherwise, jump to the slow path to throw the exception.
5937 //
5938 // But before, move back the object's class into `temp` before
5939 // going into the slow path, as it has been overwritten in the
5940 // meantime.
5941 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005942 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005943 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005944 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 break;
5946 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005949 // We cannot use a NearLabel here, as its range might be too
5950 // short in some cases when read barriers are enabled. This has
5951 // been observed for instance when the code emitted for this
5952 // case uses high x86-64 registers (R8-R15).
5953 Label done;
5954 // Avoid null check if we know obj is not null.
5955 if (instruction->MustDoNullCheck()) {
5956 __ testl(obj, obj);
5957 __ j(kEqual, &done);
5958 }
5959
5960 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005961 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005962
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005963 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005965 if (cls.IsRegister()) {
5966 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5967 } else {
5968 DCHECK(cls.IsStackSlot()) << cls;
5969 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5970 }
5971 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972
5973 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005974 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005975 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976
5977 // If the component type is not null (i.e. the object is indeed
5978 // an array), jump to label `check_non_primitive_component_type`
5979 // to further check that this component type is not a primitive
5980 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005982 __ j(kNotEqual, &check_non_primitive_component_type);
5983 // Otherwise, jump to the slow path to throw the exception.
5984 //
5985 // But before, move back the object's class into `temp` before
5986 // going into the slow path, as it has been overwritten in the
5987 // meantime.
5988 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005989 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 __ jmp(type_check_slow_path->GetEntryLabel());
5991
5992 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 __ j(kEqual, &done);
5995 // Same comment as above regarding `temp` and the slow path.
5996 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005997 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005999 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 break;
6001 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006002
Calin Juravle98893e12015-10-02 21:05:03 +01006003 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006004 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006005 NearLabel done;
6006 // Avoid null check if we know obj is not null.
6007 if (instruction->MustDoNullCheck()) {
6008 __ testl(obj, obj);
6009 __ j(kEqual, &done);
6010 }
6011
6012 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006013 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006014
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006015 // We always go into the type check slow path for the unresolved
6016 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006017 //
6018 // We cannot directly call the CheckCast runtime entry point
6019 // without resorting to a type checking slow path here (i.e. by
6020 // calling InvokeRuntime directly), as it would require to
6021 // assign fixed registers for the inputs of this HInstanceOf
6022 // instruction (following the runtime calling convention), which
6023 // might be cluttered by the potential first read barrier
6024 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006025 //
6026 // TODO: Introduce a new runtime entry point taking the object
6027 // to test (instead of its class) as argument, and let it deal
6028 // with the read barrier issues. This will let us refactor this
6029 // case of the `switch` code as it was previously (with a direct
6030 // call to the runtime not using a type checking slow path).
6031 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006032 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006033 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 break;
6035 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006038}
6039
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006040void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6041 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006043 InvokeRuntimeCallingConvention calling_convention;
6044 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6045}
6046
6047void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006048 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006049 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006050 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006051 if (instruction->IsEnter()) {
6052 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6053 } else {
6054 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6055 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006056}
6057
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006058void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6059void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6060void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6061
6062void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6063 LocationSummary* locations =
6064 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6065 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6066 || instruction->GetResultType() == Primitive::kPrimLong);
6067 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006068 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006069 locations->SetOut(Location::SameAsFirstInput());
6070}
6071
6072void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6073 HandleBitwiseOperation(instruction);
6074}
6075
6076void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6077 HandleBitwiseOperation(instruction);
6078}
6079
6080void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6081 HandleBitwiseOperation(instruction);
6082}
6083
6084void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6085 LocationSummary* locations = instruction->GetLocations();
6086 Location first = locations->InAt(0);
6087 Location second = locations->InAt(1);
6088 DCHECK(first.Equals(locations->Out()));
6089
6090 if (instruction->GetResultType() == Primitive::kPrimInt) {
6091 if (second.IsRegister()) {
6092 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006093 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006094 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006095 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006096 } else {
6097 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006098 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006099 }
6100 } else if (second.IsConstant()) {
6101 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6102 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006103 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006104 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006105 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006106 } else {
6107 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006108 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006109 }
6110 } else {
6111 Address address(CpuRegister(RSP), second.GetStackIndex());
6112 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006113 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006114 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006115 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006116 } else {
6117 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006118 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006119 }
6120 }
6121 } else {
6122 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006123 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6124 bool second_is_constant = false;
6125 int64_t value = 0;
6126 if (second.IsConstant()) {
6127 second_is_constant = true;
6128 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006129 }
Mark Mendell40741f32015-04-20 22:10:34 -04006130 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006131
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006132 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006133 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006134 if (is_int32_value) {
6135 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6136 } else {
6137 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6138 }
6139 } else if (second.IsDoubleStackSlot()) {
6140 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006141 } else {
6142 __ andq(first_reg, second.AsRegister<CpuRegister>());
6143 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006144 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006145 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006146 if (is_int32_value) {
6147 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6148 } else {
6149 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6150 }
6151 } else if (second.IsDoubleStackSlot()) {
6152 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006153 } else {
6154 __ orq(first_reg, second.AsRegister<CpuRegister>());
6155 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006156 } else {
6157 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006158 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006159 if (is_int32_value) {
6160 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6161 } else {
6162 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6163 }
6164 } else if (second.IsDoubleStackSlot()) {
6165 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006166 } else {
6167 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6168 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006169 }
6170 }
6171}
6172
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006173void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6174 Location out,
6175 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006176 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006177 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6178 if (kEmitCompilerReadBarrier) {
6179 if (kUseBakerReadBarrier) {
6180 // Load with fast path based Baker's read barrier.
6181 // /* HeapReference<Object> */ out = *(out + offset)
6182 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006183 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006184 } else {
6185 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006186 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006187 // in the following move operation, as we will need it for the
6188 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006189 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006190 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006191 // /* HeapReference<Object> */ out = *(out + offset)
6192 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006193 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006194 }
6195 } else {
6196 // Plain load with no read barrier.
6197 // /* HeapReference<Object> */ out = *(out + offset)
6198 __ movl(out_reg, Address(out_reg, offset));
6199 __ MaybeUnpoisonHeapReference(out_reg);
6200 }
6201}
6202
6203void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6204 Location out,
6205 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006206 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006207 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6208 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6209 if (kEmitCompilerReadBarrier) {
6210 if (kUseBakerReadBarrier) {
6211 // Load with fast path based Baker's read barrier.
6212 // /* HeapReference<Object> */ out = *(obj + offset)
6213 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006214 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006215 } else {
6216 // Load with slow path based read barrier.
6217 // /* HeapReference<Object> */ out = *(obj + offset)
6218 __ movl(out_reg, Address(obj_reg, offset));
6219 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6220 }
6221 } else {
6222 // Plain load with no read barrier.
6223 // /* HeapReference<Object> */ out = *(obj + offset)
6224 __ movl(out_reg, Address(obj_reg, offset));
6225 __ MaybeUnpoisonHeapReference(out_reg);
6226 }
6227}
6228
6229void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6230 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006231 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006232 Label* fixup_label,
6233 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006234 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006235 if (requires_read_barrier) {
6236 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006237 if (kUseBakerReadBarrier) {
6238 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6239 // Baker's read barrier are used:
6240 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006242 // if (Thread::Current()->GetIsGcMarking()) {
6243 // root = ReadBarrier::Mark(root)
6244 // }
6245
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006246 // /* GcRoot<mirror::Object> */ root = *address
6247 __ movl(root_reg, address);
6248 if (fixup_label != nullptr) {
6249 __ Bind(fixup_label);
6250 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006251 static_assert(
6252 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6253 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6254 "have different sizes.");
6255 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6256 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6257 "have different sizes.");
6258
Vladimir Marko953437b2016-08-24 08:30:46 +00006259 // Slow path marking the GC root `root`.
6260 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6261 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006262 codegen_->AddSlowPath(slow_path);
6263
Andreas Gampe542451c2016-07-26 09:02:02 -07006264 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006265 /* no_rip */ true),
6266 Immediate(0));
6267 __ j(kNotEqual, slow_path->GetEntryLabel());
6268 __ Bind(slow_path->GetExitLabel());
6269 } else {
6270 // GC root loaded through a slow path for read barriers other
6271 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006272 // /* GcRoot<mirror::Object>* */ root = address
6273 __ leaq(root_reg, address);
6274 if (fixup_label != nullptr) {
6275 __ Bind(fixup_label);
6276 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006277 // /* mirror::Object* */ root = root->Read()
6278 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6279 }
6280 } else {
6281 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 // /* GcRoot<mirror::Object> */ root = *address
6283 __ movl(root_reg, address);
6284 if (fixup_label != nullptr) {
6285 __ Bind(fixup_label);
6286 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006287 // Note that GC roots are not affected by heap poisoning, thus we
6288 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006289 }
6290}
6291
6292void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6293 Location ref,
6294 CpuRegister obj,
6295 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006296 bool needs_null_check) {
6297 DCHECK(kEmitCompilerReadBarrier);
6298 DCHECK(kUseBakerReadBarrier);
6299
6300 // /* HeapReference<Object> */ ref = *(obj + offset)
6301 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006302 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006303}
6304
6305void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6306 Location ref,
6307 CpuRegister obj,
6308 uint32_t data_offset,
6309 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006310 bool needs_null_check) {
6311 DCHECK(kEmitCompilerReadBarrier);
6312 DCHECK(kUseBakerReadBarrier);
6313
Roland Levillain3d312422016-06-23 13:53:42 +01006314 static_assert(
6315 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6316 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006317 // /* HeapReference<Object> */ ref =
6318 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006319 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006320 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006321}
6322
6323void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6324 Location ref,
6325 CpuRegister obj,
6326 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006327 bool needs_null_check) {
6328 DCHECK(kEmitCompilerReadBarrier);
6329 DCHECK(kUseBakerReadBarrier);
6330
6331 // In slow path based read barriers, the read barrier call is
6332 // inserted after the original load. However, in fast path based
6333 // Baker's read barriers, we need to perform the load of
6334 // mirror::Object::monitor_ *before* the original reference load.
6335 // This load-load ordering is required by the read barrier.
6336 // The fast path/slow path (for Baker's algorithm) should look like:
6337 //
6338 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6339 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6340 // HeapReference<Object> ref = *src; // Original reference load.
6341 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6342 // if (is_gray) {
6343 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6344 // }
6345 //
6346 // Note: the original implementation in ReadBarrier::Barrier is
6347 // slightly more complex as:
6348 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006349 // the high-bits of rb_state, which are expected to be all zeroes
6350 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6351 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006352 // - it performs additional checks that we do not do here for
6353 // performance reasons.
6354
6355 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006356 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6357
Vladimir Marko953437b2016-08-24 08:30:46 +00006358 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6359 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6360 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6361 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6362 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6363 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6364 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6365
6366 // if (rb_state == ReadBarrier::gray_ptr_)
6367 // ref = ReadBarrier::Mark(ref);
6368 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6369 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006370 if (needs_null_check) {
6371 MaybeRecordImplicitNullCheck(instruction);
6372 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006373
6374 // Load fence to prevent load-load reordering.
6375 // Note that this is a no-op, thanks to the x86-64 memory model.
6376 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6377
6378 // The actual reference load.
6379 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006380 __ movl(ref_reg, src); // Flags are unaffected.
6381
6382 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6383 // Slow path marking the object `ref` when it is gray.
6384 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6385 instruction, ref, /* unpoison */ true);
6386 AddSlowPath(slow_path);
6387
6388 // We have done the "if" of the gray bit check above, now branch based on the flags.
6389 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006390
6391 // Object* ref = ref_addr->AsMirrorPtr()
6392 __ MaybeUnpoisonHeapReference(ref_reg);
6393
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006394 __ Bind(slow_path->GetExitLabel());
6395}
6396
6397void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6398 Location out,
6399 Location ref,
6400 Location obj,
6401 uint32_t offset,
6402 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 DCHECK(kEmitCompilerReadBarrier);
6404
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006405 // Insert a slow path based read barrier *after* the reference load.
6406 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006407 // If heap poisoning is enabled, the unpoisoning of the loaded
6408 // reference will be carried out by the runtime within the slow
6409 // path.
6410 //
6411 // Note that `ref` currently does not get unpoisoned (when heap
6412 // poisoning is enabled), which is alright as the `ref` argument is
6413 // not used by the artReadBarrierSlow entry point.
6414 //
6415 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6416 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6417 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6418 AddSlowPath(slow_path);
6419
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 __ jmp(slow_path->GetEntryLabel());
6421 __ Bind(slow_path->GetExitLabel());
6422}
6423
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006424void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6425 Location out,
6426 Location ref,
6427 Location obj,
6428 uint32_t offset,
6429 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006431 // Baker's read barriers shall be handled by the fast path
6432 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6433 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434 // If heap poisoning is enabled, unpoisoning will be taken care of
6435 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006436 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006437 } else if (kPoisonHeapReferences) {
6438 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6439 }
6440}
6441
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006442void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6443 Location out,
6444 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 DCHECK(kEmitCompilerReadBarrier);
6446
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006447 // Insert a slow path based read barrier *after* the GC root load.
6448 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 // Note that GC roots are not affected by heap poisoning, so we do
6450 // not need to do anything special for this here.
6451 SlowPathCode* slow_path =
6452 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6453 AddSlowPath(slow_path);
6454
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 __ jmp(slow_path->GetEntryLabel());
6456 __ Bind(slow_path->GetExitLabel());
6457}
6458
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006459void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006460 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006461 LOG(FATAL) << "Unreachable";
6462}
6463
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006464void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006465 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006466 LOG(FATAL) << "Unreachable";
6467}
6468
Mark Mendellfe57faa2015-09-18 09:26:15 -04006469// Simple implementation of packed switch - generate cascaded compare/jumps.
6470void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6471 LocationSummary* locations =
6472 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6473 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006474 locations->AddTemp(Location::RequiresRegister());
6475 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006476}
6477
6478void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6479 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006480 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006481 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006482 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6483 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6484 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006485 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6486
6487 // Should we generate smaller inline compare/jumps?
6488 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6489 // Figure out the correct compare values and jump conditions.
6490 // Handle the first compare/branch as a special case because it might
6491 // jump to the default case.
6492 DCHECK_GT(num_entries, 2u);
6493 Condition first_condition;
6494 uint32_t index;
6495 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6496 if (lower_bound != 0) {
6497 first_condition = kLess;
6498 __ cmpl(value_reg_in, Immediate(lower_bound));
6499 __ j(first_condition, codegen_->GetLabelOf(default_block));
6500 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6501
6502 index = 1;
6503 } else {
6504 // Handle all the compare/jumps below.
6505 first_condition = kBelow;
6506 index = 0;
6507 }
6508
6509 // Handle the rest of the compare/jumps.
6510 for (; index + 1 < num_entries; index += 2) {
6511 int32_t compare_to_value = lower_bound + index + 1;
6512 __ cmpl(value_reg_in, Immediate(compare_to_value));
6513 // Jump to successors[index] if value < case_value[index].
6514 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6515 // Jump to successors[index + 1] if value == case_value[index + 1].
6516 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6517 }
6518
6519 if (index != num_entries) {
6520 // There are an odd number of entries. Handle the last one.
6521 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006522 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006523 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6524 }
6525
6526 // And the default for any other value.
6527 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6528 __ jmp(codegen_->GetLabelOf(default_block));
6529 }
6530 return;
6531 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006532
6533 // Remove the bias, if needed.
6534 Register value_reg_out = value_reg_in.AsRegister();
6535 if (lower_bound != 0) {
6536 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6537 value_reg_out = temp_reg.AsRegister();
6538 }
6539 CpuRegister value_reg(value_reg_out);
6540
6541 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006542 __ cmpl(value_reg, Immediate(num_entries - 1));
6543 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006544
Mark Mendell9c86b482015-09-18 13:36:07 -04006545 // We are in the range of the table.
6546 // Load the address of the jump table in the constant area.
6547 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006548
Mark Mendell9c86b482015-09-18 13:36:07 -04006549 // Load the (signed) offset from the jump table.
6550 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6551
6552 // Add the offset to the address of the table base.
6553 __ addq(temp_reg, base_reg);
6554
6555 // And jump.
6556 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006557}
6558
Aart Bikc5d47542016-01-27 17:00:35 -08006559void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6560 if (value == 0) {
6561 __ xorl(dest, dest);
6562 } else {
6563 __ movl(dest, Immediate(value));
6564 }
6565}
6566
Mark Mendell92e83bf2015-05-07 11:25:03 -04006567void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6568 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006569 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006570 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006571 } else if (IsUint<32>(value)) {
6572 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006573 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6574 } else {
6575 __ movq(dest, Immediate(value));
6576 }
6577}
6578
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006579void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6580 if (value == 0) {
6581 __ xorps(dest, dest);
6582 } else {
6583 __ movss(dest, LiteralInt32Address(value));
6584 }
6585}
6586
6587void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6588 if (value == 0) {
6589 __ xorpd(dest, dest);
6590 } else {
6591 __ movsd(dest, LiteralInt64Address(value));
6592 }
6593}
6594
6595void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6596 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6597}
6598
6599void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6600 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6601}
6602
Aart Bika19616e2016-02-01 18:57:58 -08006603void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6604 if (value == 0) {
6605 __ testl(dest, dest);
6606 } else {
6607 __ cmpl(dest, Immediate(value));
6608 }
6609}
6610
6611void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6612 if (IsInt<32>(value)) {
6613 if (value == 0) {
6614 __ testq(dest, dest);
6615 } else {
6616 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6617 }
6618 } else {
6619 // Value won't fit in an int.
6620 __ cmpq(dest, LiteralInt64Address(value));
6621 }
6622}
6623
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006624void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6625 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006626 GenerateIntCompare(lhs_reg, rhs);
6627}
6628
6629void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006630 if (rhs.IsConstant()) {
6631 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006632 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006633 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006634 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006635 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006636 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006637 }
6638}
6639
6640void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6641 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6642 if (rhs.IsConstant()) {
6643 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6644 Compare64BitValue(lhs_reg, value);
6645 } else if (rhs.IsDoubleStackSlot()) {
6646 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6647 } else {
6648 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6649 }
6650}
6651
6652Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6653 Location index,
6654 ScaleFactor scale,
6655 uint32_t data_offset) {
6656 return index.IsConstant() ?
6657 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6658 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6659}
6660
Mark Mendellcfa410b2015-05-25 16:02:44 -04006661void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6662 DCHECK(dest.IsDoubleStackSlot());
6663 if (IsInt<32>(value)) {
6664 // Can move directly as an int32 constant.
6665 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6666 Immediate(static_cast<int32_t>(value)));
6667 } else {
6668 Load64BitValue(CpuRegister(TMP), value);
6669 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6670 }
6671}
6672
Mark Mendell9c86b482015-09-18 13:36:07 -04006673/**
6674 * Class to handle late fixup of offsets into constant area.
6675 */
6676class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6677 public:
6678 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6679 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6680
6681 protected:
6682 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6683
6684 CodeGeneratorX86_64* codegen_;
6685
6686 private:
6687 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6688 // Patch the correct offset for the instruction. We use the address of the
6689 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6690 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6691 int32_t relative_position = constant_offset - pos;
6692
6693 // Patch in the right value.
6694 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6695 }
6696
6697 // Location in constant area that the fixup refers to.
6698 size_t offset_into_constant_area_;
6699};
6700
6701/**
6702 t * Class to handle late fixup of offsets to a jump table that will be created in the
6703 * constant area.
6704 */
6705class JumpTableRIPFixup : public RIPFixup {
6706 public:
6707 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6708 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6709
6710 void CreateJumpTable() {
6711 X86_64Assembler* assembler = codegen_->GetAssembler();
6712
6713 // Ensure that the reference to the jump table has the correct offset.
6714 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6715 SetOffset(offset_in_constant_table);
6716
6717 // Compute the offset from the start of the function to this jump table.
6718 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6719
6720 // Populate the jump table with the correct values for the jump table.
6721 int32_t num_entries = switch_instr_->GetNumEntries();
6722 HBasicBlock* block = switch_instr_->GetBlock();
6723 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6724 // The value that we want is the target offset - the position of the table.
6725 for (int32_t i = 0; i < num_entries; i++) {
6726 HBasicBlock* b = successors[i];
6727 Label* l = codegen_->GetLabelOf(b);
6728 DCHECK(l->IsBound());
6729 int32_t offset_to_block = l->Position() - current_table_offset;
6730 assembler->AppendInt32(offset_to_block);
6731 }
6732 }
6733
6734 private:
6735 const HPackedSwitch* switch_instr_;
6736};
6737
Mark Mendellf55c3e02015-03-26 21:07:46 -04006738void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6739 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006740 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006741 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6742 // 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 -04006743 assembler->Align(4, 0);
6744 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006745
6746 // Populate any jump tables.
6747 for (auto jump_table : fixups_to_jump_tables_) {
6748 jump_table->CreateJumpTable();
6749 }
6750
6751 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006752 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006753 }
6754
6755 // And finish up.
6756 CodeGenerator::Finalize(allocator);
6757}
6758
Mark Mendellf55c3e02015-03-26 21:07:46 -04006759Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6760 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6761 return Address::RIP(fixup);
6762}
6763
6764Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6765 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6766 return Address::RIP(fixup);
6767}
6768
6769Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6770 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6771 return Address::RIP(fixup);
6772}
6773
6774Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6775 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6776 return Address::RIP(fixup);
6777}
6778
Andreas Gampe85b62f22015-09-09 13:15:38 -07006779// TODO: trg as memory.
6780void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6781 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006782 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006783 return;
6784 }
6785
6786 DCHECK_NE(type, Primitive::kPrimVoid);
6787
6788 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6789 if (trg.Equals(return_loc)) {
6790 return;
6791 }
6792
6793 // Let the parallel move resolver take care of all of this.
6794 HParallelMove parallel_move(GetGraph()->GetArena());
6795 parallel_move.AddMove(return_loc, trg, type, nullptr);
6796 GetMoveResolver()->EmitNativeCode(&parallel_move);
6797}
6798
Mark Mendell9c86b482015-09-18 13:36:07 -04006799Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6800 // Create a fixup to be used to create and address the jump table.
6801 JumpTableRIPFixup* table_fixup =
6802 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6803
6804 // We have to populate the jump tables.
6805 fixups_to_jump_tables_.push_back(table_fixup);
6806 return Address::RIP(table_fixup);
6807}
6808
Mark Mendellea5af682015-10-22 17:35:49 -04006809void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6810 const Address& addr_high,
6811 int64_t v,
6812 HInstruction* instruction) {
6813 if (IsInt<32>(v)) {
6814 int32_t v_32 = v;
6815 __ movq(addr_low, Immediate(v_32));
6816 MaybeRecordImplicitNullCheck(instruction);
6817 } else {
6818 // Didn't fit in a register. Do it in pieces.
6819 int32_t low_v = Low32Bits(v);
6820 int32_t high_v = High32Bits(v);
6821 __ movl(addr_low, Immediate(low_v));
6822 MaybeRecordImplicitNullCheck(instruction);
6823 __ movl(addr_high, Immediate(high_v));
6824 }
6825}
6826
Roland Levillain4d027112015-07-01 15:41:14 +01006827#undef __
6828
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006829} // namespace x86_64
6830} // namespace art