blob: b243ee0c59c08bb62827f948598fd43821fd189b [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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700290class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000292 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000293 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100297 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
298 : locations->Out();
299 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 DCHECK(instruction_->IsCheckCast()
301 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
Roland Levillain0d5a2812015-11-13 10:07:31 +0000303 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000305
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000306 if (!is_fatal_) {
307 SaveLiveRegisters(codegen, locations);
308 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 // We're moving two locations to locations that could overlap, so we need a parallel
311 // move resolver.
312 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000313 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100314 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000315 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100316 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100317 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100318 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
319 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000321 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100322 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000323 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700324 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 } else {
326 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100327 x86_64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000328 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331 if (!is_fatal_) {
332 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000333 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336 RestoreLiveRegisters(codegen, locations);
337 __ jmp(GetExitLabel());
338 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339 }
340
Alexandre Rames9931f312015-06-19 14:47:01 +0100341 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
342
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343 bool IsFatal() const OVERRIDE { return is_fatal_; }
344
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347
348 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
349};
350
Andreas Gampe85b62f22015-09-09 13:15:38 -0700351class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 public:
Aart Bik42249c32016-01-07 15:33:50 -0800353 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000354 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700355
356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000357 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100359 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000360 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
364
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
367};
368
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100369class ArraySetSlowPathX86_64 : public SlowPathCode {
370 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100372
373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 LocationSummary* locations = instruction_->GetLocations();
375 __ Bind(GetEntryLabel());
376 SaveLiveRegisters(codegen, locations);
377
378 InvokeRuntimeCallingConvention calling_convention;
379 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
380 parallel_move.AddMove(
381 locations->InAt(0),
382 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
383 Primitive::kPrimNot,
384 nullptr);
385 parallel_move.AddMove(
386 locations->InAt(1),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
388 Primitive::kPrimInt,
389 nullptr);
390 parallel_move.AddMove(
391 locations->InAt(2),
392 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
393 Primitive::kPrimNot,
394 nullptr);
395 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
396
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100398 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000399 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100400 RestoreLiveRegisters(codegen, locations);
401 __ jmp(GetExitLabel());
402 }
403
404 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
405
406 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
408};
409
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000410// Slow path marking an object during a read barrier.
411class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
412 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000413 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
414 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000415 DCHECK(kEmitCompilerReadBarrier);
416 }
417
418 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
419
420 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
421 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100422 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000423 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100424 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000425 DCHECK(instruction_->IsInstanceFieldGet() ||
426 instruction_->IsStaticFieldGet() ||
427 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100428 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000429 instruction_->IsLoadClass() ||
430 instruction_->IsLoadString() ||
431 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100432 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100433 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
434 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000435 << "Unexpected instruction in read barrier marking slow path: "
436 << instruction_->DebugName();
437
438 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000439 if (unpoison_) {
440 // Object* ref = ref_addr->AsMirrorPtr()
441 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
442 }
Roland Levillain4359e612016-07-20 11:32:19 +0100443 // No need to save live registers; it's taken care of by the
444 // entrypoint. Also, there is no need to update the stack mask,
445 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000446 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100447 DCHECK_NE(reg, RSP);
448 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
449 // "Compact" slow path, saving two moves.
450 //
451 // Instead of using the standard runtime calling convention (input
452 // and output in R0):
453 //
454 // RDI <- obj
455 // RAX <- ReadBarrierMark(RDI)
456 // obj <- RAX
457 //
458 // we just use rX (the register holding `obj`) as input and output
459 // of a dedicated entrypoint:
460 //
461 // rX <- ReadBarrierMarkRegX(rX)
462 //
463 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700464 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100465 // This runtime call does not require a stack map.
466 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000467 __ jmp(GetExitLabel());
468 }
469
470 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000471 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000472 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000473
474 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
475};
476
Roland Levillain0d5a2812015-11-13 10:07:31 +0000477// Slow path generating a read barrier for a heap reference.
478class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
479 public:
480 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
481 Location out,
482 Location ref,
483 Location obj,
484 uint32_t offset,
485 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000486 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000487 out_(out),
488 ref_(ref),
489 obj_(obj),
490 offset_(offset),
491 index_(index) {
492 DCHECK(kEmitCompilerReadBarrier);
493 // If `obj` is equal to `out` or `ref`, it means the initial
494 // object has been overwritten by (or after) the heap object
495 // reference load to be instrumented, e.g.:
496 //
497 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000498 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000499 //
500 // In that case, we have lost the information about the original
501 // object, and the emitted read barrier cannot work properly.
502 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
503 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
504}
505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
507 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
508 LocationSummary* locations = instruction_->GetLocations();
509 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
510 DCHECK(locations->CanCall());
511 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100512 DCHECK(instruction_->IsInstanceFieldGet() ||
513 instruction_->IsStaticFieldGet() ||
514 instruction_->IsArrayGet() ||
515 instruction_->IsInstanceOf() ||
516 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100517 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000518 << "Unexpected instruction in read barrier for heap reference slow path: "
519 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000520
521 __ Bind(GetEntryLabel());
522 SaveLiveRegisters(codegen, locations);
523
524 // We may have to change the index's value, but as `index_` is a
525 // constant member (like other "inputs" of this slow path),
526 // introduce a copy of it, `index`.
527 Location index = index_;
528 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100529 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000530 if (instruction_->IsArrayGet()) {
531 // Compute real offset and store it in index_.
532 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
533 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
534 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
535 // We are about to change the value of `index_reg` (see the
536 // calls to art::x86_64::X86_64Assembler::shll and
537 // art::x86_64::X86_64Assembler::AddImmediate below), but it
538 // has not been saved by the previous call to
539 // art::SlowPathCode::SaveLiveRegisters, as it is a
540 // callee-save register --
541 // art::SlowPathCode::SaveLiveRegisters does not consider
542 // callee-save registers, as it has been designed with the
543 // assumption that callee-save registers are supposed to be
544 // handled by the called function. So, as a callee-save
545 // register, `index_reg` _would_ eventually be saved onto
546 // the stack, but it would be too late: we would have
547 // changed its value earlier. Therefore, we manually save
548 // it here into another freely available register,
549 // `free_reg`, chosen of course among the caller-save
550 // registers (as a callee-save `free_reg` register would
551 // exhibit the same problem).
552 //
553 // Note we could have requested a temporary register from
554 // the register allocator instead; but we prefer not to, as
555 // this is a slow path, and we know we can find a
556 // caller-save register that is available.
557 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
558 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
559 index_reg = free_reg;
560 index = Location::RegisterLocation(index_reg);
561 } else {
562 // The initial register stored in `index_` has already been
563 // saved in the call to art::SlowPathCode::SaveLiveRegisters
564 // (as it is not a callee-save register), so we can freely
565 // use it.
566 }
567 // Shifting the index value contained in `index_reg` by the
568 // scale factor (2) cannot overflow in practice, as the
569 // runtime is unable to allocate object arrays with a size
570 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
571 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
572 static_assert(
573 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
574 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
575 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
576 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100577 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
578 // intrinsics, `index_` is not shifted by a scale factor of 2
579 // (as in the case of ArrayGet), as it is actually an offset
580 // to an object field within an object.
581 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000582 DCHECK(instruction_->GetLocations()->Intrinsified());
583 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
584 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
585 << instruction_->AsInvoke()->GetIntrinsic();
586 DCHECK_EQ(offset_, 0U);
587 DCHECK(index_.IsRegister());
588 }
589 }
590
591 // We're moving two or three locations to locations that could
592 // overlap, so we need a parallel move resolver.
593 InvokeRuntimeCallingConvention calling_convention;
594 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
595 parallel_move.AddMove(ref_,
596 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
597 Primitive::kPrimNot,
598 nullptr);
599 parallel_move.AddMove(obj_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
601 Primitive::kPrimNot,
602 nullptr);
603 if (index.IsValid()) {
604 parallel_move.AddMove(index,
605 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
606 Primitive::kPrimInt,
607 nullptr);
608 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
609 } else {
610 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
611 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
612 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100613 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000614 instruction_,
615 instruction_->GetDexPc(),
616 this);
617 CheckEntrypointTypes<
618 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
619 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
620
621 RestoreLiveRegisters(codegen, locations);
622 __ jmp(GetExitLabel());
623 }
624
625 const char* GetDescription() const OVERRIDE {
626 return "ReadBarrierForHeapReferenceSlowPathX86_64";
627 }
628
629 private:
630 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
631 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
632 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
633 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
634 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
635 return static_cast<CpuRegister>(i);
636 }
637 }
638 // We shall never fail to find a free caller-save register, as
639 // there are more than two core caller-save registers on x86-64
640 // (meaning it is possible to find one which is different from
641 // `ref` and `obj`).
642 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
643 LOG(FATAL) << "Could not find a free caller-save register";
644 UNREACHABLE();
645 }
646
Roland Levillain0d5a2812015-11-13 10:07:31 +0000647 const Location out_;
648 const Location ref_;
649 const Location obj_;
650 const uint32_t offset_;
651 // An additional location containing an index to an array.
652 // Only used for HArrayGet and the UnsafeGetObject &
653 // UnsafeGetObjectVolatile intrinsics.
654 const Location index_;
655
656 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
657};
658
659// Slow path generating a read barrier for a GC root.
660class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
661 public:
662 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000663 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000664 DCHECK(kEmitCompilerReadBarrier);
665 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000666
667 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
668 LocationSummary* locations = instruction_->GetLocations();
669 DCHECK(locations->CanCall());
670 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000671 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
672 << "Unexpected instruction in read barrier for GC root slow path: "
673 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000674
675 __ Bind(GetEntryLabel());
676 SaveLiveRegisters(codegen, locations);
677
678 InvokeRuntimeCallingConvention calling_convention;
679 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
680 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100681 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000682 instruction_,
683 instruction_->GetDexPc(),
684 this);
685 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
686 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
687
688 RestoreLiveRegisters(codegen, locations);
689 __ jmp(GetExitLabel());
690 }
691
692 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
693
694 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000695 const Location out_;
696 const Location root_;
697
698 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
699};
700
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100701#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100702// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
703#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100704
Roland Levillain4fa13f62015-07-06 18:11:54 +0100705inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700706 switch (cond) {
707 case kCondEQ: return kEqual;
708 case kCondNE: return kNotEqual;
709 case kCondLT: return kLess;
710 case kCondLE: return kLessEqual;
711 case kCondGT: return kGreater;
712 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700713 case kCondB: return kBelow;
714 case kCondBE: return kBelowEqual;
715 case kCondA: return kAbove;
716 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700717 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100718 LOG(FATAL) << "Unreachable";
719 UNREACHABLE();
720}
721
Aart Bike9f37602015-10-09 11:15:55 -0700722// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100723inline Condition X86_64FPCondition(IfCondition cond) {
724 switch (cond) {
725 case kCondEQ: return kEqual;
726 case kCondNE: return kNotEqual;
727 case kCondLT: return kBelow;
728 case kCondLE: return kBelowEqual;
729 case kCondGT: return kAbove;
730 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700731 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100732 };
733 LOG(FATAL) << "Unreachable";
734 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700735}
736
Vladimir Markodc151b22015-10-15 18:02:30 +0100737HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
738 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100739 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100740 switch (desired_dispatch_info.code_ptr_location) {
741 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
742 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
743 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
744 return HInvokeStaticOrDirect::DispatchInfo {
745 desired_dispatch_info.method_load_kind,
746 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
747 desired_dispatch_info.method_load_data,
748 0u
749 };
750 default:
751 return desired_dispatch_info;
752 }
753}
754
Serguei Katkov288c7a82016-05-16 11:53:15 +0600755Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
756 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800757 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000758 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
759 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100760 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000761 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100762 uint32_t offset =
763 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
764 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000765 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100766 }
Vladimir Marko58155012015-08-19 12:49:41 +0000767 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000768 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000769 break;
770 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
771 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
772 break;
773 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
774 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
775 method_patches_.emplace_back(invoke->GetTargetMethod());
776 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
777 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000778 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000779 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000780 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000781 // Bind a new fixup label at the end of the "movl" insn.
782 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100783 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000784 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000785 }
Vladimir Marko58155012015-08-19 12:49:41 +0000786 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000787 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000788 Register method_reg;
789 CpuRegister reg = temp.AsRegister<CpuRegister>();
790 if (current_method.IsRegister()) {
791 method_reg = current_method.AsRegister<Register>();
792 } else {
793 DCHECK(invoke->GetLocations()->Intrinsified());
794 DCHECK(!current_method.IsValid());
795 method_reg = reg.AsRegister();
796 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
797 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000798 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100799 __ movq(reg,
800 Address(CpuRegister(method_reg),
801 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100802 // temp = temp[index_in_cache];
803 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
804 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000805 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
806 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100807 }
Vladimir Marko58155012015-08-19 12:49:41 +0000808 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600809 return callee_method;
810}
811
812void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
813 Location temp) {
814 // All registers are assumed to be correctly set up.
815 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000816
817 switch (invoke->GetCodePtrLocation()) {
818 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
819 __ call(&frame_entry_label_);
820 break;
821 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
822 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
823 Label* label = &relative_call_patches_.back().label;
824 __ call(label); // Bind to the patch label, override at link time.
825 __ Bind(label); // Bind the label at the end of the "call" insn.
826 break;
827 }
828 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
829 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100830 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
831 LOG(FATAL) << "Unsupported";
832 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000833 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
834 // (callee_method + offset_of_quick_compiled_code)()
835 __ call(Address(callee_method.AsRegister<CpuRegister>(),
836 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700837 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000838 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000839 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800840
841 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800842}
843
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000844void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
845 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
846 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
847 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000848
849 // Use the calling convention instead of the location of the receiver, as
850 // intrinsics may have put the receiver in a different register. In the intrinsics
851 // slow path, the arguments have been moved to the right place, so here we are
852 // guaranteed that the receiver is the first register of the calling convention.
853 InvokeDexCallingConvention calling_convention;
854 Register receiver = calling_convention.GetRegisterAt(0);
855
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000856 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000857 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000858 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000859 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000860 // Instead of simply (possibly) unpoisoning `temp` here, we should
861 // emit a read barrier for the previous class reference load.
862 // However this is not required in practice, as this is an
863 // intermediate/temporary reference and because the current
864 // concurrent copying collector keeps the from-space memory
865 // intact/accessible until the end of the marking phase (the
866 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000867 __ MaybeUnpoisonHeapReference(temp);
868 // temp = temp->GetMethodAt(method_offset);
869 __ movq(temp, Address(temp, method_offset));
870 // call temp->GetEntryPoint();
871 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700872 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000873}
874
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000875void CodeGeneratorX86_64::RecordSimplePatch() {
876 if (GetCompilerOptions().GetIncludePatchInformation()) {
877 simple_patches_.emplace_back();
878 __ Bind(&simple_patches_.back());
879 }
880}
881
882void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
883 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
884 __ Bind(&string_patches_.back().label);
885}
886
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100887void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
888 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
889 __ Bind(&type_patches_.back().label);
890}
891
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000892Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
893 uint32_t element_offset) {
894 // Add a patch entry and return the label.
895 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
896 return &pc_relative_dex_cache_patches_.back().label;
897}
898
Vladimir Marko58155012015-08-19 12:49:41 +0000899void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
900 DCHECK(linker_patches->empty());
901 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000902 method_patches_.size() +
903 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000904 pc_relative_dex_cache_patches_.size() +
905 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100906 string_patches_.size() +
907 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000908 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000909 // The label points to the end of the "movl" insn but the literal offset for method
910 // patch needs to point to the embedded constant which occupies the last 4 bytes.
911 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000912 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000913 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000914 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
915 info.target_method.dex_file,
916 info.target_method.dex_method_index));
917 }
918 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000919 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000920 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
921 info.target_method.dex_file,
922 info.target_method.dex_method_index));
923 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000924 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
925 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000926 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
927 &info.target_dex_file,
928 info.label.Position(),
929 info.element_offset));
930 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000931 for (const Label& label : simple_patches_) {
932 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
933 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
934 }
935 for (const StringPatchInfo<Label>& info : string_patches_) {
936 // These are always PC-relative, see GetSupportedLoadStringKind().
937 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
938 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
939 &info.dex_file,
940 info.label.Position(),
941 info.string_index));
942 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100943 for (const TypePatchInfo<Label>& info : type_patches_) {
944 // These are always PC-relative, see GetSupportedLoadClassKind().
945 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
946 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
947 &info.dex_file,
948 info.label.Position(),
949 info.type_index));
950 }
Vladimir Marko58155012015-08-19 12:49:41 +0000951}
952
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100953void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100954 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100955}
956
957void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959}
960
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100961size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
962 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
963 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100964}
965
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100966size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
967 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
968 return kX86_64WordSize;
969}
970
971size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
972 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
973 return kX86_64WordSize;
974}
975
976size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
977 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
978 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100979}
980
Calin Juravle175dc732015-08-25 15:42:32 +0100981void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
982 HInstruction* instruction,
983 uint32_t dex_pc,
984 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100985 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100986 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
987 if (EntrypointRequiresStackMap(entrypoint)) {
988 RecordPcInfo(instruction, dex_pc, slow_path);
989 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100990}
991
Roland Levillaindec8f632016-07-22 17:10:06 +0100992void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
993 HInstruction* instruction,
994 SlowPathCode* slow_path) {
995 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100996 GenerateInvokeRuntime(entry_point_offset);
997}
998
999void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001000 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1001}
1002
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001003static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001004// Use a fake return address register to mimic Quick.
1005static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001006CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001007 const X86_64InstructionSetFeatures& isa_features,
1008 const CompilerOptions& compiler_options,
1009 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001010 : CodeGenerator(graph,
1011 kNumberOfCpuRegisters,
1012 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001013 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001014 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1015 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001016 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001017 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1018 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001019 compiler_options,
1020 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001021 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001022 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001023 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001024 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001025 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001026 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001027 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001028 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1029 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001030 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001031 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1032 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001033 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001034 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001035 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1036}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001038InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1039 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001040 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001041 assembler_(codegen->GetAssembler()),
1042 codegen_(codegen) {}
1043
David Brazdil58282f42016-01-14 12:45:10 +00001044void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001045 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001046 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001047
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001048 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001049 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001050}
1051
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001052static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001053 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001054}
David Srbecky9d8606d2015-04-12 09:35:32 +01001055
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001057 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058}
1059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001060void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001062 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001063 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001064 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001065 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001066
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001067 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001068 __ testq(CpuRegister(RAX), Address(
1069 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001070 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001071 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001072
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001073 if (HasEmptyFrame()) {
1074 return;
1075 }
1076
Nicolas Geoffray98893962015-01-21 12:32:32 +00001077 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001079 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001080 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001081 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1082 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001083 }
1084 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001085
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 int adjust = GetFrameSize() - GetCoreSpillSize();
1087 __ subq(CpuRegister(RSP), Immediate(adjust));
1088 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001089 uint32_t xmm_spill_location = GetFpuSpillStart();
1090 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001091
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001092 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1093 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001094 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1095 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1096 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001097 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001098 }
1099
Mathieu Chartiere401d142015-04-22 13:56:20 -07001100 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001101 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001102}
1103
1104void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001105 __ cfi().RememberState();
1106 if (!HasEmptyFrame()) {
1107 uint32_t xmm_spill_location = GetFpuSpillStart();
1108 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1109 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1110 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1111 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1112 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1113 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1114 }
1115 }
1116
1117 int adjust = GetFrameSize() - GetCoreSpillSize();
1118 __ addq(CpuRegister(RSP), Immediate(adjust));
1119 __ cfi().AdjustCFAOffset(-adjust);
1120
1121 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1122 Register reg = kCoreCalleeSaves[i];
1123 if (allocated_registers_.ContainsCoreRegister(reg)) {
1124 __ popq(CpuRegister(reg));
1125 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1126 __ cfi().Restore(DWARFReg(reg));
1127 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001128 }
1129 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001130 __ ret();
1131 __ cfi().RestoreState();
1132 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001133}
1134
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001135void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1136 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137}
1138
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139void CodeGeneratorX86_64::Move(Location destination, Location source) {
1140 if (source.Equals(destination)) {
1141 return;
1142 }
1143 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001144 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001145 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001146 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001148 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001150 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1151 } else if (source.IsConstant()) {
1152 HConstant* constant = source.GetConstant();
1153 if (constant->IsLongConstant()) {
1154 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1155 } else {
1156 Load32BitValue(dest, GetInt32ValueOf(constant));
1157 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001158 } else {
1159 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001160 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 }
1162 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001163 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001165 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001167 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1168 } else if (source.IsConstant()) {
1169 HConstant* constant = source.GetConstant();
1170 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1171 if (constant->IsFloatConstant()) {
1172 Load32BitValue(dest, static_cast<int32_t>(value));
1173 } else {
1174 Load64BitValue(dest, value);
1175 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001177 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001178 } else {
1179 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001180 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001181 }
1182 } else if (destination.IsStackSlot()) {
1183 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001185 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 } else if (source.IsFpuRegister()) {
1187 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001189 } else if (source.IsConstant()) {
1190 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001191 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001192 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001193 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001194 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001195 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1196 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197 }
1198 } else {
1199 DCHECK(destination.IsDoubleStackSlot());
1200 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001201 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 } else if (source.IsFpuRegister()) {
1204 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001206 } else if (source.IsConstant()) {
1207 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001208 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1209 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001210 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001211 } else {
1212 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001213 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1214 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 }
1216 }
1217}
1218
Calin Juravle175dc732015-08-25 15:42:32 +01001219void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1220 DCHECK(location.IsRegister());
1221 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1222}
1223
Calin Juravlee460d1d2015-09-29 04:52:17 +01001224void CodeGeneratorX86_64::MoveLocation(
1225 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1226 Move(dst, src);
1227}
1228
1229void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1230 if (location.IsRegister()) {
1231 locations->AddTemp(location);
1232 } else {
1233 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1234 }
1235}
1236
David Brazdilfc6a86a2015-06-26 10:33:45 +00001237void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001238 DCHECK(!successor->IsExitBlock());
1239
1240 HBasicBlock* block = got->GetBlock();
1241 HInstruction* previous = got->GetPrevious();
1242
1243 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001244 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001245 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1246 return;
1247 }
1248
1249 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1250 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1251 }
1252 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253 __ jmp(codegen_->GetLabelOf(successor));
1254 }
1255}
1256
David Brazdilfc6a86a2015-06-26 10:33:45 +00001257void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1258 got->SetLocations(nullptr);
1259}
1260
1261void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1262 HandleGoto(got, got->GetSuccessor());
1263}
1264
1265void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1266 try_boundary->SetLocations(nullptr);
1267}
1268
1269void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1270 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1271 if (!successor->IsExitBlock()) {
1272 HandleGoto(try_boundary, successor);
1273 }
1274}
1275
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1277 exit->SetLocations(nullptr);
1278}
1279
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001280void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001281}
1282
Mark Mendell152408f2015-12-31 12:28:50 -05001283template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001284void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001285 LabelType* true_label,
1286 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287 if (cond->IsFPConditionTrueIfNaN()) {
1288 __ j(kUnordered, true_label);
1289 } else if (cond->IsFPConditionFalseIfNaN()) {
1290 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001292 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001293}
1294
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001295void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001296 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001297
Mark Mendellc4701932015-04-10 13:18:51 -04001298 Location left = locations->InAt(0);
1299 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001300 Primitive::Type type = condition->InputAt(0)->GetType();
1301 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001302 case Primitive::kPrimBoolean:
1303 case Primitive::kPrimByte:
1304 case Primitive::kPrimChar:
1305 case Primitive::kPrimShort:
1306 case Primitive::kPrimInt:
1307 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001308 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001309 break;
1310 }
Mark Mendellc4701932015-04-10 13:18:51 -04001311 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001312 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001313 break;
1314 }
1315 case Primitive::kPrimFloat: {
1316 if (right.IsFpuRegister()) {
1317 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1318 } else if (right.IsConstant()) {
1319 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1320 codegen_->LiteralFloatAddress(
1321 right.GetConstant()->AsFloatConstant()->GetValue()));
1322 } else {
1323 DCHECK(right.IsStackSlot());
1324 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1325 Address(CpuRegister(RSP), right.GetStackIndex()));
1326 }
Mark Mendellc4701932015-04-10 13:18:51 -04001327 break;
1328 }
1329 case Primitive::kPrimDouble: {
1330 if (right.IsFpuRegister()) {
1331 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1332 } else if (right.IsConstant()) {
1333 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1334 codegen_->LiteralDoubleAddress(
1335 right.GetConstant()->AsDoubleConstant()->GetValue()));
1336 } else {
1337 DCHECK(right.IsDoubleStackSlot());
1338 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1339 Address(CpuRegister(RSP), right.GetStackIndex()));
1340 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001341 break;
1342 }
1343 default:
1344 LOG(FATAL) << "Unexpected condition type " << type;
1345 }
1346}
1347
1348template<class LabelType>
1349void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1350 LabelType* true_target_in,
1351 LabelType* false_target_in) {
1352 // Generated branching requires both targets to be explicit. If either of the
1353 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1354 LabelType fallthrough_target;
1355 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1356 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1357
1358 // Generate the comparison to set the CC.
1359 GenerateCompareTest(condition);
1360
1361 // Now generate the correct jump(s).
1362 Primitive::Type type = condition->InputAt(0)->GetType();
1363 switch (type) {
1364 case Primitive::kPrimLong: {
1365 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1366 break;
1367 }
1368 case Primitive::kPrimFloat: {
1369 GenerateFPJumps(condition, true_target, false_target);
1370 break;
1371 }
1372 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001373 GenerateFPJumps(condition, true_target, false_target);
1374 break;
1375 }
1376 default:
1377 LOG(FATAL) << "Unexpected condition type " << type;
1378 }
1379
David Brazdil0debae72015-11-12 18:37:00 +00001380 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001381 __ jmp(false_target);
1382 }
David Brazdil0debae72015-11-12 18:37:00 +00001383
1384 if (fallthrough_target.IsLinked()) {
1385 __ Bind(&fallthrough_target);
1386 }
Mark Mendellc4701932015-04-10 13:18:51 -04001387}
1388
David Brazdil0debae72015-11-12 18:37:00 +00001389static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1390 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1391 // are set only strictly before `branch`. We can't use the eflags on long
1392 // conditions if they are materialized due to the complex branching.
1393 return cond->IsCondition() &&
1394 cond->GetNext() == branch &&
1395 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1396}
1397
Mark Mendell152408f2015-12-31 12:28:50 -05001398template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001399void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001400 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001401 LabelType* true_target,
1402 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001403 HInstruction* cond = instruction->InputAt(condition_input_index);
1404
1405 if (true_target == nullptr && false_target == nullptr) {
1406 // Nothing to do. The code always falls through.
1407 return;
1408 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001409 // Constant condition, statically compared against "true" (integer value 1).
1410 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001411 if (true_target != nullptr) {
1412 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001413 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001415 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001416 if (false_target != nullptr) {
1417 __ jmp(false_target);
1418 }
1419 }
1420 return;
1421 }
1422
1423 // The following code generates these patterns:
1424 // (1) true_target == nullptr && false_target != nullptr
1425 // - opposite condition true => branch to false_target
1426 // (2) true_target != nullptr && false_target == nullptr
1427 // - condition true => branch to true_target
1428 // (3) true_target != nullptr && false_target != nullptr
1429 // - condition true => branch to true_target
1430 // - branch to false_target
1431 if (IsBooleanValueOrMaterializedCondition(cond)) {
1432 if (AreEflagsSetFrom(cond, instruction)) {
1433 if (true_target == nullptr) {
1434 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1435 } else {
1436 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1437 }
1438 } else {
1439 // Materialized condition, compare against 0.
1440 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1441 if (lhs.IsRegister()) {
1442 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1443 } else {
1444 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1445 }
1446 if (true_target == nullptr) {
1447 __ j(kEqual, false_target);
1448 } else {
1449 __ j(kNotEqual, true_target);
1450 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001451 }
1452 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001453 // Condition has not been materialized, use its inputs as the
1454 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001455 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001456
David Brazdil0debae72015-11-12 18:37:00 +00001457 // If this is a long or FP comparison that has been folded into
1458 // the HCondition, generate the comparison directly.
1459 Primitive::Type type = condition->InputAt(0)->GetType();
1460 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1461 GenerateCompareTestAndBranch(condition, true_target, false_target);
1462 return;
1463 }
1464
1465 Location lhs = condition->GetLocations()->InAt(0);
1466 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001467 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001468 if (true_target == nullptr) {
1469 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1470 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001471 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001472 }
Dave Allison20dfc792014-06-16 20:44:29 -07001473 }
David Brazdil0debae72015-11-12 18:37:00 +00001474
1475 // If neither branch falls through (case 3), the conditional branch to `true_target`
1476 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1477 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001478 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001479 }
1480}
1481
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001482void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1484 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001485 locations->SetInAt(0, Location::Any());
1486 }
1487}
1488
1489void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001490 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1491 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1492 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1493 nullptr : codegen_->GetLabelOf(true_successor);
1494 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1495 nullptr : codegen_->GetLabelOf(false_successor);
1496 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001497}
1498
1499void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1500 LocationSummary* locations = new (GetGraph()->GetArena())
1501 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001502 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001503 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504 locations->SetInAt(0, Location::Any());
1505 }
1506}
1507
1508void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001509 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001510 GenerateTestAndBranch<Label>(deoptimize,
1511 /* condition_input_index */ 0,
1512 slow_path->GetEntryLabel(),
1513 /* false_target */ nullptr);
1514}
1515
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001516static bool SelectCanUseCMOV(HSelect* select) {
1517 // There are no conditional move instructions for XMMs.
1518 if (Primitive::IsFloatingPointType(select->GetType())) {
1519 return false;
1520 }
1521
1522 // A FP condition doesn't generate the single CC that we need.
1523 HInstruction* condition = select->GetCondition();
1524 if (condition->IsCondition() &&
1525 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1526 return false;
1527 }
1528
1529 // We can generate a CMOV for this Select.
1530 return true;
1531}
1532
David Brazdil74eb1b22015-12-14 11:44:01 +00001533void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1535 if (Primitive::IsFloatingPointType(select->GetType())) {
1536 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001537 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001538 } else {
1539 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001540 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001541 if (select->InputAt(1)->IsConstant()) {
1542 locations->SetInAt(1, Location::RequiresRegister());
1543 } else {
1544 locations->SetInAt(1, Location::Any());
1545 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001546 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001547 locations->SetInAt(1, Location::Any());
1548 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001549 }
1550 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1551 locations->SetInAt(2, Location::RequiresRegister());
1552 }
1553 locations->SetOut(Location::SameAsFirstInput());
1554}
1555
1556void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1557 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001558 if (SelectCanUseCMOV(select)) {
1559 // If both the condition and the source types are integer, we can generate
1560 // a CMOV to implement Select.
1561 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001562 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001563 DCHECK(locations->InAt(0).Equals(locations->Out()));
1564
1565 HInstruction* select_condition = select->GetCondition();
1566 Condition cond = kNotEqual;
1567
1568 // Figure out how to test the 'condition'.
1569 if (select_condition->IsCondition()) {
1570 HCondition* condition = select_condition->AsCondition();
1571 if (!condition->IsEmittedAtUseSite()) {
1572 // This was a previously materialized condition.
1573 // Can we use the existing condition code?
1574 if (AreEflagsSetFrom(condition, select)) {
1575 // Materialization was the previous instruction. Condition codes are right.
1576 cond = X86_64IntegerCondition(condition->GetCondition());
1577 } else {
1578 // No, we have to recreate the condition code.
1579 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1580 __ testl(cond_reg, cond_reg);
1581 }
1582 } else {
1583 GenerateCompareTest(condition);
1584 cond = X86_64IntegerCondition(condition->GetCondition());
1585 }
1586 } else {
1587 // Must be a boolean condition, which needs to be compared to 0.
1588 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1589 __ testl(cond_reg, cond_reg);
1590 }
1591
1592 // If the condition is true, overwrite the output, which already contains false.
1593 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001594 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1595 if (value_true_loc.IsRegister()) {
1596 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1597 } else {
1598 __ cmov(cond,
1599 value_false,
1600 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1601 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001602 } else {
1603 NearLabel false_target;
1604 GenerateTestAndBranch<NearLabel>(select,
1605 /* condition_input_index */ 2,
1606 /* true_target */ nullptr,
1607 &false_target);
1608 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1609 __ Bind(&false_target);
1610 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611}
1612
David Srbecky0cf44932015-12-09 14:09:59 +00001613void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1614 new (GetGraph()->GetArena()) LocationSummary(info);
1615}
1616
David Srbeckyd28f4a02016-03-14 17:14:24 +00001617void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1618 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001619}
1620
1621void CodeGeneratorX86_64::GenerateNop() {
1622 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001623}
1624
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001625void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001626 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001627 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001628 // Handle the long/FP comparisons made in instruction simplification.
1629 switch (cond->InputAt(0)->GetType()) {
1630 case Primitive::kPrimLong:
1631 locations->SetInAt(0, Location::RequiresRegister());
1632 locations->SetInAt(1, Location::Any());
1633 break;
1634 case Primitive::kPrimFloat:
1635 case Primitive::kPrimDouble:
1636 locations->SetInAt(0, Location::RequiresFpuRegister());
1637 locations->SetInAt(1, Location::Any());
1638 break;
1639 default:
1640 locations->SetInAt(0, Location::RequiresRegister());
1641 locations->SetInAt(1, Location::Any());
1642 break;
1643 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001644 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001645 locations->SetOut(Location::RequiresRegister());
1646 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001647}
1648
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001649void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001650 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001651 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001652 }
Mark Mendellc4701932015-04-10 13:18:51 -04001653
1654 LocationSummary* locations = cond->GetLocations();
1655 Location lhs = locations->InAt(0);
1656 Location rhs = locations->InAt(1);
1657 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001658 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001659
1660 switch (cond->InputAt(0)->GetType()) {
1661 default:
1662 // Integer case.
1663
1664 // Clear output register: setcc only sets the low byte.
1665 __ xorl(reg, reg);
1666
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001667 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001668 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001669 return;
1670 case Primitive::kPrimLong:
1671 // Clear output register: setcc only sets the low byte.
1672 __ xorl(reg, reg);
1673
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001674 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001675 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001676 return;
1677 case Primitive::kPrimFloat: {
1678 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1679 if (rhs.IsConstant()) {
1680 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1681 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1682 } else if (rhs.IsStackSlot()) {
1683 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1684 } else {
1685 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1686 }
1687 GenerateFPJumps(cond, &true_label, &false_label);
1688 break;
1689 }
1690 case Primitive::kPrimDouble: {
1691 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1692 if (rhs.IsConstant()) {
1693 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1694 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1695 } else if (rhs.IsDoubleStackSlot()) {
1696 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1697 } else {
1698 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1699 }
1700 GenerateFPJumps(cond, &true_label, &false_label);
1701 break;
1702 }
1703 }
1704
1705 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001706 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001707
Roland Levillain4fa13f62015-07-06 18:11:54 +01001708 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001709 __ Bind(&false_label);
1710 __ xorl(reg, reg);
1711 __ jmp(&done_label);
1712
Roland Levillain4fa13f62015-07-06 18:11:54 +01001713 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001714 __ Bind(&true_label);
1715 __ movl(reg, Immediate(1));
1716 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001717}
1718
1719void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001720 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001721}
1722
1723void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001724 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001725}
1726
1727void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001728 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001729}
1730
1731void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001732 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001733}
1734
1735void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001736 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001737}
1738
1739void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001740 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001741}
1742
1743void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001744 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001745}
1746
1747void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001748 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001749}
1750
1751void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001752 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001753}
1754
1755void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001756 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001757}
1758
1759void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001760 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001761}
1762
1763void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001765}
1766
Aart Bike9f37602015-10-09 11:15:55 -07001767void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001769}
1770
1771void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001773}
1774
1775void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001777}
1778
1779void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001781}
1782
1783void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001785}
1786
1787void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001789}
1790
1791void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001793}
1794
1795void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001797}
1798
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001799void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001800 LocationSummary* locations =
1801 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001802 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001803 case Primitive::kPrimBoolean:
1804 case Primitive::kPrimByte:
1805 case Primitive::kPrimShort:
1806 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001807 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001808 case Primitive::kPrimLong: {
1809 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001810 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1812 break;
1813 }
1814 case Primitive::kPrimFloat:
1815 case Primitive::kPrimDouble: {
1816 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001817 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001818 locations->SetOut(Location::RequiresRegister());
1819 break;
1820 }
1821 default:
1822 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1823 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001824}
1825
1826void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001827 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001828 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001829 Location left = locations->InAt(0);
1830 Location right = locations->InAt(1);
1831
Mark Mendell0c9497d2015-08-21 09:30:05 -04001832 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001833 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001834 Condition less_cond = kLess;
1835
Calin Juravleddb7df22014-11-25 20:56:51 +00001836 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001837 case Primitive::kPrimBoolean:
1838 case Primitive::kPrimByte:
1839 case Primitive::kPrimShort:
1840 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001841 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001842 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08001843 break;
1844 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001845 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001846 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001847 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001848 }
1849 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001850 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1851 if (right.IsConstant()) {
1852 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1853 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1854 } else if (right.IsStackSlot()) {
1855 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1856 } else {
1857 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1858 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001859 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001860 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001861 break;
1862 }
1863 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001864 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1865 if (right.IsConstant()) {
1866 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1867 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1868 } else if (right.IsDoubleStackSlot()) {
1869 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1870 } else {
1871 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1872 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001873 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001874 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001875 break;
1876 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001877 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001878 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001879 }
Aart Bika19616e2016-02-01 18:57:58 -08001880
Calin Juravleddb7df22014-11-25 20:56:51 +00001881 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001882 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001883 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001884
Calin Juravle91debbc2014-11-26 19:01:09 +00001885 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001886 __ movl(out, Immediate(1));
1887 __ jmp(&done);
1888
1889 __ Bind(&less);
1890 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001891
1892 __ Bind(&done);
1893}
1894
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001895void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001898 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001899}
1900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001901void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001902 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001903}
1904
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001905void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1906 LocationSummary* locations =
1907 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1908 locations->SetOut(Location::ConstantLocation(constant));
1909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001912 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001913}
1914
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001915void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001918 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001919}
1920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001921void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001922 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001923}
1924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001925void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1926 LocationSummary* locations =
1927 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1928 locations->SetOut(Location::ConstantLocation(constant));
1929}
1930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001932 // Will be generated at use site.
1933}
1934
1935void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1938 locations->SetOut(Location::ConstantLocation(constant));
1939}
1940
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001941void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1942 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001943 // Will be generated at use site.
1944}
1945
Calin Juravle27df7582015-04-17 19:12:31 +01001946void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1947 memory_barrier->SetLocations(nullptr);
1948}
1949
1950void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001951 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001952}
1953
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1955 ret->SetLocations(nullptr);
1956}
1957
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001958void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001960}
1961
1962void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001963 LocationSummary* locations =
1964 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965 switch (ret->InputAt(0)->GetType()) {
1966 case Primitive::kPrimBoolean:
1967 case Primitive::kPrimByte:
1968 case Primitive::kPrimChar:
1969 case Primitive::kPrimShort:
1970 case Primitive::kPrimInt:
1971 case Primitive::kPrimNot:
1972 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974 break;
1975
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001976 case Primitive::kPrimFloat:
1977 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001978 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001979 break;
1980
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001981 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001982 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001984}
1985
1986void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1987 if (kIsDebugBuild) {
1988 switch (ret->InputAt(0)->GetType()) {
1989 case Primitive::kPrimBoolean:
1990 case Primitive::kPrimByte:
1991 case Primitive::kPrimChar:
1992 case Primitive::kPrimShort:
1993 case Primitive::kPrimInt:
1994 case Primitive::kPrimNot:
1995 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001997 break;
1998
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 case Primitive::kPrimFloat:
2000 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002001 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 XMM0);
2003 break;
2004
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002005 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002006 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002007 }
2008 }
2009 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002010}
2011
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002012Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2013 switch (type) {
2014 case Primitive::kPrimBoolean:
2015 case Primitive::kPrimByte:
2016 case Primitive::kPrimChar:
2017 case Primitive::kPrimShort:
2018 case Primitive::kPrimInt:
2019 case Primitive::kPrimNot:
2020 case Primitive::kPrimLong:
2021 return Location::RegisterLocation(RAX);
2022
2023 case Primitive::kPrimVoid:
2024 return Location::NoLocation();
2025
2026 case Primitive::kPrimDouble:
2027 case Primitive::kPrimFloat:
2028 return Location::FpuRegisterLocation(XMM0);
2029 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002030
2031 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002032}
2033
2034Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2035 return Location::RegisterLocation(kMethodRegisterArgument);
2036}
2037
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002038Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002039 switch (type) {
2040 case Primitive::kPrimBoolean:
2041 case Primitive::kPrimByte:
2042 case Primitive::kPrimChar:
2043 case Primitive::kPrimShort:
2044 case Primitive::kPrimInt:
2045 case Primitive::kPrimNot: {
2046 uint32_t index = gp_index_++;
2047 stack_index_++;
2048 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002050 } else {
2051 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2052 }
2053 }
2054
2055 case Primitive::kPrimLong: {
2056 uint32_t index = gp_index_;
2057 stack_index_ += 2;
2058 if (index < calling_convention.GetNumberOfRegisters()) {
2059 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002060 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002061 } else {
2062 gp_index_ += 2;
2063 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2064 }
2065 }
2066
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002067 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002068 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002069 stack_index_++;
2070 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002071 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002072 } else {
2073 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2074 }
2075 }
2076
2077 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002078 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002079 stack_index_ += 2;
2080 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002081 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002082 } else {
2083 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2084 }
2085 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002086
2087 case Primitive::kPrimVoid:
2088 LOG(FATAL) << "Unexpected parameter type " << type;
2089 break;
2090 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002091 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002092}
2093
Calin Juravle175dc732015-08-25 15:42:32 +01002094void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2095 // The trampoline uses the same calling convention as dex calling conventions,
2096 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2097 // the method_idx.
2098 HandleInvoke(invoke);
2099}
2100
2101void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2102 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2103}
2104
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002105void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002106 // Explicit clinit checks triggered by static invokes must have been pruned by
2107 // art::PrepareForRegisterAllocation.
2108 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002109
Mark Mendellfb8d2792015-03-31 22:16:59 -04002110 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002111 if (intrinsic.TryDispatch(invoke)) {
2112 return;
2113 }
2114
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002115 HandleInvoke(invoke);
2116}
2117
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002118static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2119 if (invoke->GetLocations()->Intrinsified()) {
2120 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2121 intrinsic.Dispatch(invoke);
2122 return true;
2123 }
2124 return false;
2125}
2126
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002127void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002128 // Explicit clinit checks triggered by static invokes must have been pruned by
2129 // art::PrepareForRegisterAllocation.
2130 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002131
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002132 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2133 return;
2134 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002135
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002136 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002137 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002138 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002139 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002140}
2141
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002142void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002143 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002144 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002145}
2146
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002147void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002148 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002149 if (intrinsic.TryDispatch(invoke)) {
2150 return;
2151 }
2152
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002153 HandleInvoke(invoke);
2154}
2155
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002156void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002157 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2158 return;
2159 }
2160
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002161 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002162 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002163 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002164}
2165
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002166void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2167 HandleInvoke(invoke);
2168 // Add the hidden argument.
2169 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2170}
2171
2172void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2173 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002174 LocationSummary* locations = invoke->GetLocations();
2175 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2176 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002177 Location receiver = locations->InAt(0);
2178 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2179
Roland Levillain0d5a2812015-11-13 10:07:31 +00002180 // Set the hidden argument. This is safe to do this here, as RAX
2181 // won't be modified thereafter, before the `call` instruction.
2182 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002183 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002184
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002185 if (receiver.IsStackSlot()) {
2186 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002187 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002188 __ movl(temp, Address(temp, class_offset));
2189 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002190 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002191 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002192 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002193 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002194 // Instead of simply (possibly) unpoisoning `temp` here, we should
2195 // emit a read barrier for the previous class reference load.
2196 // However this is not required in practice, as this is an
2197 // intermediate/temporary reference and because the current
2198 // concurrent copying collector keeps the from-space memory
2199 // intact/accessible until the end of the marking phase (the
2200 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002201 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002202 // temp = temp->GetAddressOfIMT()
2203 __ movq(temp,
2204 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2205 // temp = temp->GetImtEntryAt(method_offset);
2206 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002207 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002208 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002209 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002210 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002211 __ call(Address(
2212 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002213
2214 DCHECK(!codegen_->IsLeafMethod());
2215 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2216}
2217
Roland Levillain88cb1752014-10-20 16:36:47 +01002218void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2219 LocationSummary* locations =
2220 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2221 switch (neg->GetResultType()) {
2222 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002223 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002224 locations->SetInAt(0, Location::RequiresRegister());
2225 locations->SetOut(Location::SameAsFirstInput());
2226 break;
2227
Roland Levillain88cb1752014-10-20 16:36:47 +01002228 case Primitive::kPrimFloat:
2229 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002230 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002231 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002232 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002233 break;
2234
2235 default:
2236 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2237 }
2238}
2239
2240void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2241 LocationSummary* locations = neg->GetLocations();
2242 Location out = locations->Out();
2243 Location in = locations->InAt(0);
2244 switch (neg->GetResultType()) {
2245 case Primitive::kPrimInt:
2246 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002247 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002248 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002249 break;
2250
2251 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002252 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002253 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002254 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002255 break;
2256
Roland Levillain5368c212014-11-27 15:03:41 +00002257 case Primitive::kPrimFloat: {
2258 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002259 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002260 // Implement float negation with an exclusive or with value
2261 // 0x80000000 (mask for bit 31, representing the sign of a
2262 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002263 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002265 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002266 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002267
Roland Levillain5368c212014-11-27 15:03:41 +00002268 case Primitive::kPrimDouble: {
2269 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002270 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002271 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002272 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002273 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002274 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002277 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002278
2279 default:
2280 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2281 }
2282}
2283
Roland Levillaindff1f282014-11-05 14:15:05 +00002284void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2285 LocationSummary* locations =
2286 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2287 Primitive::Type result_type = conversion->GetResultType();
2288 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002289 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002290
David Brazdilb2bd1c52015-03-25 11:17:37 +00002291 // The Java language does not allow treating boolean as an integral type but
2292 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002293
Roland Levillaindff1f282014-11-05 14:15:05 +00002294 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002295 case Primitive::kPrimByte:
2296 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002297 case Primitive::kPrimLong:
2298 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002299 case Primitive::kPrimBoolean:
2300 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002301 case Primitive::kPrimShort:
2302 case Primitive::kPrimInt:
2303 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002304 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002305 locations->SetInAt(0, Location::Any());
2306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2307 break;
2308
2309 default:
2310 LOG(FATAL) << "Unexpected type conversion from " << input_type
2311 << " to " << result_type;
2312 }
2313 break;
2314
Roland Levillain01a8d712014-11-14 16:27:39 +00002315 case Primitive::kPrimShort:
2316 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002317 case Primitive::kPrimLong:
2318 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimInt:
2323 case Primitive::kPrimChar:
2324 // Processing a Dex `int-to-short' instruction.
2325 locations->SetInAt(0, Location::Any());
2326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2327 break;
2328
2329 default:
2330 LOG(FATAL) << "Unexpected type conversion from " << input_type
2331 << " to " << result_type;
2332 }
2333 break;
2334
Roland Levillain946e1432014-11-11 17:35:19 +00002335 case Primitive::kPrimInt:
2336 switch (input_type) {
2337 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002338 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002339 locations->SetInAt(0, Location::Any());
2340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2341 break;
2342
2343 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002344 // Processing a Dex `float-to-int' instruction.
2345 locations->SetInAt(0, Location::RequiresFpuRegister());
2346 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002347 break;
2348
Roland Levillain946e1432014-11-11 17:35:19 +00002349 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002350 // Processing a Dex `double-to-int' instruction.
2351 locations->SetInAt(0, Location::RequiresFpuRegister());
2352 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002353 break;
2354
2355 default:
2356 LOG(FATAL) << "Unexpected type conversion from " << input_type
2357 << " to " << result_type;
2358 }
2359 break;
2360
Roland Levillaindff1f282014-11-05 14:15:05 +00002361 case Primitive::kPrimLong:
2362 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002363 case Primitive::kPrimBoolean:
2364 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 case Primitive::kPrimByte:
2366 case Primitive::kPrimShort:
2367 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002368 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002369 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002370 // TODO: We would benefit from a (to-be-implemented)
2371 // Location::RegisterOrStackSlot requirement for this input.
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 locations->SetOut(Location::RequiresRegister());
2374 break;
2375
2376 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002377 // Processing a Dex `float-to-long' instruction.
2378 locations->SetInAt(0, Location::RequiresFpuRegister());
2379 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002380 break;
2381
Roland Levillaindff1f282014-11-05 14:15:05 +00002382 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002383 // Processing a Dex `double-to-long' instruction.
2384 locations->SetInAt(0, Location::RequiresFpuRegister());
2385 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002386 break;
2387
2388 default:
2389 LOG(FATAL) << "Unexpected type conversion from " << input_type
2390 << " to " << result_type;
2391 }
2392 break;
2393
Roland Levillain981e4542014-11-14 11:47:14 +00002394 case Primitive::kPrimChar:
2395 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002396 case Primitive::kPrimLong:
2397 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002398 case Primitive::kPrimBoolean:
2399 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002400 case Primitive::kPrimByte:
2401 case Primitive::kPrimShort:
2402 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002403 // Processing a Dex `int-to-char' instruction.
2404 locations->SetInAt(0, Location::Any());
2405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2406 break;
2407
2408 default:
2409 LOG(FATAL) << "Unexpected type conversion from " << input_type
2410 << " to " << result_type;
2411 }
2412 break;
2413
Roland Levillaindff1f282014-11-05 14:15:05 +00002414 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002415 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002416 case Primitive::kPrimBoolean:
2417 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002418 case Primitive::kPrimByte:
2419 case Primitive::kPrimShort:
2420 case Primitive::kPrimInt:
2421 case Primitive::kPrimChar:
2422 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002423 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002424 locations->SetOut(Location::RequiresFpuRegister());
2425 break;
2426
2427 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002428 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002429 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002430 locations->SetOut(Location::RequiresFpuRegister());
2431 break;
2432
Roland Levillaincff13742014-11-17 14:32:17 +00002433 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002434 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002435 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002436 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002437 break;
2438
2439 default:
2440 LOG(FATAL) << "Unexpected type conversion from " << input_type
2441 << " to " << result_type;
2442 };
2443 break;
2444
Roland Levillaindff1f282014-11-05 14:15:05 +00002445 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002446 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002447 case Primitive::kPrimBoolean:
2448 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002449 case Primitive::kPrimByte:
2450 case Primitive::kPrimShort:
2451 case Primitive::kPrimInt:
2452 case Primitive::kPrimChar:
2453 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002454 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002455 locations->SetOut(Location::RequiresFpuRegister());
2456 break;
2457
2458 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002459 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002460 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002461 locations->SetOut(Location::RequiresFpuRegister());
2462 break;
2463
Roland Levillaincff13742014-11-17 14:32:17 +00002464 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002465 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002466 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002467 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002474 break;
2475
2476 default:
2477 LOG(FATAL) << "Unexpected type conversion from " << input_type
2478 << " to " << result_type;
2479 }
2480}
2481
2482void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2483 LocationSummary* locations = conversion->GetLocations();
2484 Location out = locations->Out();
2485 Location in = locations->InAt(0);
2486 Primitive::Type result_type = conversion->GetResultType();
2487 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002488 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002490 case Primitive::kPrimByte:
2491 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002492 case Primitive::kPrimLong:
2493 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002494 case Primitive::kPrimBoolean:
2495 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002496 case Primitive::kPrimShort:
2497 case Primitive::kPrimInt:
2498 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002499 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002500 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002502 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002504 Address(CpuRegister(RSP), in.GetStackIndex()));
2505 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002507 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002508 }
2509 break;
2510
2511 default:
2512 LOG(FATAL) << "Unexpected type conversion from " << input_type
2513 << " to " << result_type;
2514 }
2515 break;
2516
Roland Levillain01a8d712014-11-14 16:27:39 +00002517 case Primitive::kPrimShort:
2518 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002519 case Primitive::kPrimLong:
2520 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002521 case Primitive::kPrimBoolean:
2522 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002523 case Primitive::kPrimByte:
2524 case Primitive::kPrimInt:
2525 case Primitive::kPrimChar:
2526 // Processing a Dex `int-to-short' instruction.
2527 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002529 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002531 Address(CpuRegister(RSP), in.GetStackIndex()));
2532 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002533 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002534 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002535 }
2536 break;
2537
2538 default:
2539 LOG(FATAL) << "Unexpected type conversion from " << input_type
2540 << " to " << result_type;
2541 }
2542 break;
2543
Roland Levillain946e1432014-11-11 17:35:19 +00002544 case Primitive::kPrimInt:
2545 switch (input_type) {
2546 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002547 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002548 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002550 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002552 Address(CpuRegister(RSP), in.GetStackIndex()));
2553 } else {
2554 DCHECK(in.IsConstant());
2555 DCHECK(in.GetConstant()->IsLongConstant());
2556 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002558 }
2559 break;
2560
Roland Levillain3f8f9362014-12-02 17:45:01 +00002561 case Primitive::kPrimFloat: {
2562 // Processing a Dex `float-to-int' instruction.
2563 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2564 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002565 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002566
2567 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002568 // if input >= (float)INT_MAX goto done
2569 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002570 __ j(kAboveEqual, &done);
2571 // if input == NaN goto nan
2572 __ j(kUnordered, &nan);
2573 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002574 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002575 __ jmp(&done);
2576 __ Bind(&nan);
2577 // output = 0
2578 __ xorl(output, output);
2579 __ Bind(&done);
2580 break;
2581 }
2582
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002583 case Primitive::kPrimDouble: {
2584 // Processing a Dex `double-to-int' instruction.
2585 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2586 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002587 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002588
2589 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002590 // if input >= (double)INT_MAX goto done
2591 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002592 __ j(kAboveEqual, &done);
2593 // if input == NaN goto nan
2594 __ j(kUnordered, &nan);
2595 // output = double-to-int-truncate(input)
2596 __ cvttsd2si(output, input);
2597 __ jmp(&done);
2598 __ Bind(&nan);
2599 // output = 0
2600 __ xorl(output, output);
2601 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002602 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002603 }
Roland Levillain946e1432014-11-11 17:35:19 +00002604
2605 default:
2606 LOG(FATAL) << "Unexpected type conversion from " << input_type
2607 << " to " << result_type;
2608 }
2609 break;
2610
Roland Levillaindff1f282014-11-05 14:15:05 +00002611 case Primitive::kPrimLong:
2612 switch (input_type) {
2613 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002614 case Primitive::kPrimBoolean:
2615 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002616 case Primitive::kPrimByte:
2617 case Primitive::kPrimShort:
2618 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002619 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002620 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002621 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002622 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002623 break;
2624
Roland Levillain624279f2014-12-04 11:54:28 +00002625 case Primitive::kPrimFloat: {
2626 // Processing a Dex `float-to-long' instruction.
2627 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2628 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002629 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002630
Mark Mendell92e83bf2015-05-07 11:25:03 -04002631 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002632 // if input >= (float)LONG_MAX goto done
2633 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002634 __ j(kAboveEqual, &done);
2635 // if input == NaN goto nan
2636 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002637 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002638 __ cvttss2si(output, input, true);
2639 __ jmp(&done);
2640 __ Bind(&nan);
2641 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002642 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002643 __ Bind(&done);
2644 break;
2645 }
2646
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002647 case Primitive::kPrimDouble: {
2648 // Processing a Dex `double-to-long' instruction.
2649 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2650 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002651 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002652
Mark Mendell92e83bf2015-05-07 11:25:03 -04002653 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002654 // if input >= (double)LONG_MAX goto done
2655 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002656 __ j(kAboveEqual, &done);
2657 // if input == NaN goto nan
2658 __ j(kUnordered, &nan);
2659 // output = double-to-long-truncate(input)
2660 __ cvttsd2si(output, input, true);
2661 __ jmp(&done);
2662 __ Bind(&nan);
2663 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002664 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002665 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002667 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002668
2669 default:
2670 LOG(FATAL) << "Unexpected type conversion from " << input_type
2671 << " to " << result_type;
2672 }
2673 break;
2674
Roland Levillain981e4542014-11-14 11:47:14 +00002675 case Primitive::kPrimChar:
2676 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002677 case Primitive::kPrimLong:
2678 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimShort:
2683 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002684 // Processing a Dex `int-to-char' instruction.
2685 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002687 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002689 Address(CpuRegister(RSP), in.GetStackIndex()));
2690 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002692 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002693 }
2694 break;
2695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 }
2700 break;
2701
Roland Levillaindff1f282014-11-05 14:15:05 +00002702 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002703 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002704 case Primitive::kPrimBoolean:
2705 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002706 case Primitive::kPrimByte:
2707 case Primitive::kPrimShort:
2708 case Primitive::kPrimInt:
2709 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002710 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002711 if (in.IsRegister()) {
2712 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2713 } else if (in.IsConstant()) {
2714 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2715 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002716 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002717 } else {
2718 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2719 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2720 }
Roland Levillaincff13742014-11-17 14:32:17 +00002721 break;
2722
2723 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002724 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002725 if (in.IsRegister()) {
2726 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2727 } else if (in.IsConstant()) {
2728 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2729 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002730 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002731 } else {
2732 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2733 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2734 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002735 break;
2736
Roland Levillaincff13742014-11-17 14:32:17 +00002737 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002738 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002739 if (in.IsFpuRegister()) {
2740 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2741 } else if (in.IsConstant()) {
2742 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2743 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002744 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002745 } else {
2746 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2747 Address(CpuRegister(RSP), in.GetStackIndex()));
2748 }
Roland Levillaincff13742014-11-17 14:32:17 +00002749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected type conversion from " << input_type
2753 << " to " << result_type;
2754 };
2755 break;
2756
Roland Levillaindff1f282014-11-05 14:15:05 +00002757 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002758 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002759 case Primitive::kPrimBoolean:
2760 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002761 case Primitive::kPrimByte:
2762 case Primitive::kPrimShort:
2763 case Primitive::kPrimInt:
2764 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002765 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002766 if (in.IsRegister()) {
2767 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2768 } else if (in.IsConstant()) {
2769 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2770 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002771 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002772 } else {
2773 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2774 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2775 }
Roland Levillaincff13742014-11-17 14:32:17 +00002776 break;
2777
2778 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002779 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002780 if (in.IsRegister()) {
2781 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2782 } else if (in.IsConstant()) {
2783 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2784 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002785 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002786 } else {
2787 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2788 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2789 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002790 break;
2791
Roland Levillaincff13742014-11-17 14:32:17 +00002792 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002793 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002794 if (in.IsFpuRegister()) {
2795 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2796 } else if (in.IsConstant()) {
2797 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2798 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002799 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002800 } else {
2801 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2802 Address(CpuRegister(RSP), in.GetStackIndex()));
2803 }
Roland Levillaincff13742014-11-17 14:32:17 +00002804 break;
2805
2806 default:
2807 LOG(FATAL) << "Unexpected type conversion from " << input_type
2808 << " to " << result_type;
2809 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002810 break;
2811
2812 default:
2813 LOG(FATAL) << "Unexpected type conversion from " << input_type
2814 << " to " << result_type;
2815 }
2816}
2817
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002818void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002819 LocationSummary* locations =
2820 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002821 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002822 case Primitive::kPrimInt: {
2823 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002824 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002826 break;
2827 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002828
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002829 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002830 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002831 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002832 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002834 break;
2835 }
2836
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002837 case Primitive::kPrimDouble:
2838 case Primitive::kPrimFloat: {
2839 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002840 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002841 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002842 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002843 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002844
2845 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002846 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002847 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002848}
2849
2850void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2851 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002852 Location first = locations->InAt(0);
2853 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002854 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002855
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002856 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002857 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002858 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002859 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2860 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002861 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2862 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002863 } else {
2864 __ leal(out.AsRegister<CpuRegister>(), Address(
2865 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2866 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002867 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002868 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2869 __ addl(out.AsRegister<CpuRegister>(),
2870 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2871 } else {
2872 __ leal(out.AsRegister<CpuRegister>(), Address(
2873 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2874 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002875 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002876 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002878 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002879 break;
2880 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002881
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002882 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002883 if (second.IsRegister()) {
2884 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2885 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002886 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2887 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002888 } else {
2889 __ leaq(out.AsRegister<CpuRegister>(), Address(
2890 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2891 }
2892 } else {
2893 DCHECK(second.IsConstant());
2894 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2895 int32_t int32_value = Low32Bits(value);
2896 DCHECK_EQ(int32_value, value);
2897 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2898 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2899 } else {
2900 __ leaq(out.AsRegister<CpuRegister>(), Address(
2901 first.AsRegister<CpuRegister>(), int32_value));
2902 }
2903 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 break;
2905 }
2906
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002908 if (second.IsFpuRegister()) {
2909 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2910 } else if (second.IsConstant()) {
2911 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002912 codegen_->LiteralFloatAddress(
2913 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002914 } else {
2915 DCHECK(second.IsStackSlot());
2916 __ addss(first.AsFpuRegister<XmmRegister>(),
2917 Address(CpuRegister(RSP), second.GetStackIndex()));
2918 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002919 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002920 }
2921
2922 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002923 if (second.IsFpuRegister()) {
2924 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2925 } else if (second.IsConstant()) {
2926 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002927 codegen_->LiteralDoubleAddress(
2928 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002929 } else {
2930 DCHECK(second.IsDoubleStackSlot());
2931 __ addsd(first.AsFpuRegister<XmmRegister>(),
2932 Address(CpuRegister(RSP), second.GetStackIndex()));
2933 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 break;
2935 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002936
2937 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002938 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002939 }
2940}
2941
2942void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002943 LocationSummary* locations =
2944 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002945 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002946 case Primitive::kPrimInt: {
2947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::Any());
2949 locations->SetOut(Location::SameAsFirstInput());
2950 break;
2951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002952 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002953 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002954 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002955 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002956 break;
2957 }
Calin Juravle11351682014-10-23 15:38:15 +01002958 case Primitive::kPrimFloat:
2959 case Primitive::kPrimDouble: {
2960 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002961 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002962 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002963 break;
Calin Juravle11351682014-10-23 15:38:15 +01002964 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002965 default:
Calin Juravle11351682014-10-23 15:38:15 +01002966 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002967 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002968}
2969
2970void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2971 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002972 Location first = locations->InAt(0);
2973 Location second = locations->InAt(1);
2974 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002975 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002976 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002977 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002978 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002979 } else if (second.IsConstant()) {
2980 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002982 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002983 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002984 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002985 break;
2986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002988 if (second.IsConstant()) {
2989 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2990 DCHECK(IsInt<32>(value));
2991 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2992 } else {
2993 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002995 break;
2996 }
2997
Calin Juravle11351682014-10-23 15:38:15 +01002998 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002999 if (second.IsFpuRegister()) {
3000 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3001 } else if (second.IsConstant()) {
3002 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003003 codegen_->LiteralFloatAddress(
3004 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003005 } else {
3006 DCHECK(second.IsStackSlot());
3007 __ subss(first.AsFpuRegister<XmmRegister>(),
3008 Address(CpuRegister(RSP), second.GetStackIndex()));
3009 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003010 break;
Calin Juravle11351682014-10-23 15:38:15 +01003011 }
3012
3013 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003014 if (second.IsFpuRegister()) {
3015 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3016 } else if (second.IsConstant()) {
3017 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003018 codegen_->LiteralDoubleAddress(
3019 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003020 } else {
3021 DCHECK(second.IsDoubleStackSlot());
3022 __ subsd(first.AsFpuRegister<XmmRegister>(),
3023 Address(CpuRegister(RSP), second.GetStackIndex()));
3024 }
Calin Juravle11351682014-10-23 15:38:15 +01003025 break;
3026 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003027
3028 default:
Calin Juravle11351682014-10-23 15:38:15 +01003029 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030 }
3031}
3032
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3034 LocationSummary* locations =
3035 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3036 switch (mul->GetResultType()) {
3037 case Primitive::kPrimInt: {
3038 locations->SetInAt(0, Location::RequiresRegister());
3039 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003040 if (mul->InputAt(1)->IsIntConstant()) {
3041 // Can use 3 operand multiply.
3042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3043 } else {
3044 locations->SetOut(Location::SameAsFirstInput());
3045 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003046 break;
3047 }
3048 case Primitive::kPrimLong: {
3049 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003050 locations->SetInAt(1, Location::Any());
3051 if (mul->InputAt(1)->IsLongConstant() &&
3052 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003053 // Can use 3 operand multiply.
3054 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3055 } else {
3056 locations->SetOut(Location::SameAsFirstInput());
3057 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003058 break;
3059 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003060 case Primitive::kPrimFloat:
3061 case Primitive::kPrimDouble: {
3062 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003063 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003064 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003066 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003067
3068 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003069 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003070 }
3071}
3072
3073void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3074 LocationSummary* locations = mul->GetLocations();
3075 Location first = locations->InAt(0);
3076 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003077 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003078 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003079 case Primitive::kPrimInt:
3080 // The constant may have ended up in a register, so test explicitly to avoid
3081 // problems where the output may not be the same as the first operand.
3082 if (mul->InputAt(1)->IsIntConstant()) {
3083 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3084 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3085 } else if (second.IsRegister()) {
3086 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003088 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003089 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003090 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003091 __ imull(first.AsRegister<CpuRegister>(),
3092 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003093 }
3094 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003095 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003096 // The constant may have ended up in a register, so test explicitly to avoid
3097 // problems where the output may not be the same as the first operand.
3098 if (mul->InputAt(1)->IsLongConstant()) {
3099 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3100 if (IsInt<32>(value)) {
3101 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3102 Immediate(static_cast<int32_t>(value)));
3103 } else {
3104 // Have to use the constant area.
3105 DCHECK(first.Equals(out));
3106 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3107 }
3108 } else if (second.IsRegister()) {
3109 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003110 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003111 } else {
3112 DCHECK(second.IsDoubleStackSlot());
3113 DCHECK(first.Equals(out));
3114 __ imulq(first.AsRegister<CpuRegister>(),
3115 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003116 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003117 break;
3118 }
3119
Calin Juravleb5bfa962014-10-21 18:02:24 +01003120 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003121 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003122 if (second.IsFpuRegister()) {
3123 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3124 } else if (second.IsConstant()) {
3125 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003126 codegen_->LiteralFloatAddress(
3127 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003128 } else {
3129 DCHECK(second.IsStackSlot());
3130 __ mulss(first.AsFpuRegister<XmmRegister>(),
3131 Address(CpuRegister(RSP), second.GetStackIndex()));
3132 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003133 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003134 }
3135
3136 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003137 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003138 if (second.IsFpuRegister()) {
3139 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3140 } else if (second.IsConstant()) {
3141 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003142 codegen_->LiteralDoubleAddress(
3143 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003144 } else {
3145 DCHECK(second.IsDoubleStackSlot());
3146 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3147 Address(CpuRegister(RSP), second.GetStackIndex()));
3148 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003149 break;
3150 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151
3152 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003153 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003154 }
3155}
3156
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003157void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3158 uint32_t stack_adjustment, bool is_float) {
3159 if (source.IsStackSlot()) {
3160 DCHECK(is_float);
3161 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3162 } else if (source.IsDoubleStackSlot()) {
3163 DCHECK(!is_float);
3164 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3165 } else {
3166 // Write the value to the temporary location on the stack and load to FP stack.
3167 if (is_float) {
3168 Location stack_temp = Location::StackSlot(temp_offset);
3169 codegen_->Move(stack_temp, source);
3170 __ flds(Address(CpuRegister(RSP), temp_offset));
3171 } else {
3172 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3173 codegen_->Move(stack_temp, source);
3174 __ fldl(Address(CpuRegister(RSP), temp_offset));
3175 }
3176 }
3177}
3178
3179void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3180 Primitive::Type type = rem->GetResultType();
3181 bool is_float = type == Primitive::kPrimFloat;
3182 size_t elem_size = Primitive::ComponentSize(type);
3183 LocationSummary* locations = rem->GetLocations();
3184 Location first = locations->InAt(0);
3185 Location second = locations->InAt(1);
3186 Location out = locations->Out();
3187
3188 // Create stack space for 2 elements.
3189 // TODO: enhance register allocator to ask for stack temporaries.
3190 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3191
3192 // Load the values to the FP stack in reverse order, using temporaries if needed.
3193 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3194 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3195
3196 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003197 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198 __ Bind(&retry);
3199 __ fprem();
3200
3201 // Move FP status to AX.
3202 __ fstsw();
3203
3204 // And see if the argument reduction is complete. This is signaled by the
3205 // C2 FPU flag bit set to 0.
3206 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3207 __ j(kNotEqual, &retry);
3208
3209 // We have settled on the final value. Retrieve it into an XMM register.
3210 // Store FP top of stack to real stack.
3211 if (is_float) {
3212 __ fsts(Address(CpuRegister(RSP), 0));
3213 } else {
3214 __ fstl(Address(CpuRegister(RSP), 0));
3215 }
3216
3217 // Pop the 2 items from the FP stack.
3218 __ fucompp();
3219
3220 // Load the value from the stack into an XMM register.
3221 DCHECK(out.IsFpuRegister()) << out;
3222 if (is_float) {
3223 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3224 } else {
3225 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3226 }
3227
3228 // And remove the temporary stack space we allocated.
3229 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3230}
3231
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003232void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3233 DCHECK(instruction->IsDiv() || instruction->IsRem());
3234
3235 LocationSummary* locations = instruction->GetLocations();
3236 Location second = locations->InAt(1);
3237 DCHECK(second.IsConstant());
3238
3239 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3240 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003241 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003242
3243 DCHECK(imm == 1 || imm == -1);
3244
3245 switch (instruction->GetResultType()) {
3246 case Primitive::kPrimInt: {
3247 if (instruction->IsRem()) {
3248 __ xorl(output_register, output_register);
3249 } else {
3250 __ movl(output_register, input_register);
3251 if (imm == -1) {
3252 __ negl(output_register);
3253 }
3254 }
3255 break;
3256 }
3257
3258 case Primitive::kPrimLong: {
3259 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003260 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003261 } else {
3262 __ movq(output_register, input_register);
3263 if (imm == -1) {
3264 __ negq(output_register);
3265 }
3266 }
3267 break;
3268 }
3269
3270 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003271 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272 }
3273}
3274
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003275void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003276 LocationSummary* locations = instruction->GetLocations();
3277 Location second = locations->InAt(1);
3278
3279 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3280 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3281
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003282 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003283 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3284 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003285
3286 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3287
3288 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003289 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003290 __ testl(numerator, numerator);
3291 __ cmov(kGreaterEqual, tmp, numerator);
3292 int shift = CTZ(imm);
3293 __ sarl(tmp, Immediate(shift));
3294
3295 if (imm < 0) {
3296 __ negl(tmp);
3297 }
3298
3299 __ movl(output_register, tmp);
3300 } else {
3301 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3302 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3303
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003304 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003305 __ addq(rdx, numerator);
3306 __ testq(numerator, numerator);
3307 __ cmov(kGreaterEqual, rdx, numerator);
3308 int shift = CTZ(imm);
3309 __ sarq(rdx, Immediate(shift));
3310
3311 if (imm < 0) {
3312 __ negq(rdx);
3313 }
3314
3315 __ movq(output_register, rdx);
3316 }
3317}
3318
3319void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3320 DCHECK(instruction->IsDiv() || instruction->IsRem());
3321
3322 LocationSummary* locations = instruction->GetLocations();
3323 Location second = locations->InAt(1);
3324
3325 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3326 : locations->GetTemp(0).AsRegister<CpuRegister>();
3327 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3328 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3329 : locations->Out().AsRegister<CpuRegister>();
3330 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3331
3332 DCHECK_EQ(RAX, eax.AsRegister());
3333 DCHECK_EQ(RDX, edx.AsRegister());
3334 if (instruction->IsDiv()) {
3335 DCHECK_EQ(RAX, out.AsRegister());
3336 } else {
3337 DCHECK_EQ(RDX, out.AsRegister());
3338 }
3339
3340 int64_t magic;
3341 int shift;
3342
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003343 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 if (instruction->GetResultType() == Primitive::kPrimInt) {
3345 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3346
3347 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3348
3349 __ movl(numerator, eax);
3350
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 __ movl(eax, Immediate(magic));
3352 __ imull(numerator);
3353
3354 if (imm > 0 && magic < 0) {
3355 __ addl(edx, numerator);
3356 } else if (imm < 0 && magic > 0) {
3357 __ subl(edx, numerator);
3358 }
3359
3360 if (shift != 0) {
3361 __ sarl(edx, Immediate(shift));
3362 }
3363
3364 __ movl(eax, edx);
3365 __ shrl(edx, Immediate(31));
3366 __ addl(edx, eax);
3367
3368 if (instruction->IsRem()) {
3369 __ movl(eax, numerator);
3370 __ imull(edx, Immediate(imm));
3371 __ subl(eax, edx);
3372 __ movl(edx, eax);
3373 } else {
3374 __ movl(eax, edx);
3375 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003376 } else {
3377 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3378
3379 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3380
3381 CpuRegister rax = eax;
3382 CpuRegister rdx = edx;
3383
3384 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3385
3386 // Save the numerator.
3387 __ movq(numerator, rax);
3388
3389 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003390 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391
3392 // RDX:RAX = magic * numerator
3393 __ imulq(numerator);
3394
3395 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003396 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003397 __ addq(rdx, numerator);
3398 } else if (imm < 0 && magic > 0) {
3399 // RDX -= numerator
3400 __ subq(rdx, numerator);
3401 }
3402
3403 // Shift if needed.
3404 if (shift != 0) {
3405 __ sarq(rdx, Immediate(shift));
3406 }
3407
3408 // RDX += 1 if RDX < 0
3409 __ movq(rax, rdx);
3410 __ shrq(rdx, Immediate(63));
3411 __ addq(rdx, rax);
3412
3413 if (instruction->IsRem()) {
3414 __ movq(rax, numerator);
3415
3416 if (IsInt<32>(imm)) {
3417 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3418 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003419 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003420 }
3421
3422 __ subq(rax, rdx);
3423 __ movq(rdx, rax);
3424 } else {
3425 __ movq(rax, rdx);
3426 }
3427 }
3428}
3429
Calin Juravlebacfec32014-11-14 15:54:36 +00003430void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3431 DCHECK(instruction->IsDiv() || instruction->IsRem());
3432 Primitive::Type type = instruction->GetResultType();
3433 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3434
3435 bool is_div = instruction->IsDiv();
3436 LocationSummary* locations = instruction->GetLocations();
3437
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3439 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003440
Roland Levillain271ab9c2014-11-27 15:23:57 +00003441 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003443
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003444 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003445 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003446
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 if (imm == 0) {
3448 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3449 } else if (imm == 1 || imm == -1) {
3450 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003451 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453 } else {
3454 DCHECK(imm <= -2 || imm >= 2);
3455 GenerateDivRemWithAnyConstant(instruction);
3456 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003457 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003458 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003459 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003460 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003461 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003462
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003463 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3464 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3465 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3466 // so it's safe to just use negl instead of more complex comparisons.
3467 if (type == Primitive::kPrimInt) {
3468 __ cmpl(second_reg, Immediate(-1));
3469 __ j(kEqual, slow_path->GetEntryLabel());
3470 // edx:eax <- sign-extended of eax
3471 __ cdq();
3472 // eax = quotient, edx = remainder
3473 __ idivl(second_reg);
3474 } else {
3475 __ cmpq(second_reg, Immediate(-1));
3476 __ j(kEqual, slow_path->GetEntryLabel());
3477 // rdx:rax <- sign-extended of rax
3478 __ cqo();
3479 // rax = quotient, rdx = remainder
3480 __ idivq(second_reg);
3481 }
3482 __ Bind(slow_path->GetExitLabel());
3483 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003484}
3485
Calin Juravle7c4954d2014-10-28 16:57:40 +00003486void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3487 LocationSummary* locations =
3488 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3489 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003490 case Primitive::kPrimInt:
3491 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003492 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003494 locations->SetOut(Location::SameAsFirstInput());
3495 // Intel uses edx:eax as the dividend.
3496 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003497 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3498 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3499 // output and request another temp.
3500 if (div->InputAt(1)->IsConstant()) {
3501 locations->AddTemp(Location::RequiresRegister());
3502 }
Calin Juravled0d48522014-11-04 16:40:20 +00003503 break;
3504 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003505
Calin Juravle7c4954d2014-10-28 16:57:40 +00003506 case Primitive::kPrimFloat:
3507 case Primitive::kPrimDouble: {
3508 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003509 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003510 locations->SetOut(Location::SameAsFirstInput());
3511 break;
3512 }
3513
3514 default:
3515 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3516 }
3517}
3518
3519void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3520 LocationSummary* locations = div->GetLocations();
3521 Location first = locations->InAt(0);
3522 Location second = locations->InAt(1);
3523 DCHECK(first.Equals(locations->Out()));
3524
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003525 Primitive::Type type = div->GetResultType();
3526 switch (type) {
3527 case Primitive::kPrimInt:
3528 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003530 break;
3531 }
3532
Calin Juravle7c4954d2014-10-28 16:57:40 +00003533 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003534 if (second.IsFpuRegister()) {
3535 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3536 } else if (second.IsConstant()) {
3537 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003538 codegen_->LiteralFloatAddress(
3539 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003540 } else {
3541 DCHECK(second.IsStackSlot());
3542 __ divss(first.AsFpuRegister<XmmRegister>(),
3543 Address(CpuRegister(RSP), second.GetStackIndex()));
3544 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003545 break;
3546 }
3547
3548 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003549 if (second.IsFpuRegister()) {
3550 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3551 } else if (second.IsConstant()) {
3552 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003553 codegen_->LiteralDoubleAddress(
3554 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003555 } else {
3556 DCHECK(second.IsDoubleStackSlot());
3557 __ divsd(first.AsFpuRegister<XmmRegister>(),
3558 Address(CpuRegister(RSP), second.GetStackIndex()));
3559 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003560 break;
3561 }
3562
3563 default:
3564 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3565 }
3566}
3567
Calin Juravlebacfec32014-11-14 15:54:36 +00003568void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003569 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003570 LocationSummary* locations =
3571 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003572
3573 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003574 case Primitive::kPrimInt:
3575 case Primitive::kPrimLong: {
3576 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003577 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3579 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3581 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3582 // output and request another temp.
3583 if (rem->InputAt(1)->IsConstant()) {
3584 locations->AddTemp(Location::RequiresRegister());
3585 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003586 break;
3587 }
3588
3589 case Primitive::kPrimFloat:
3590 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003591 locations->SetInAt(0, Location::Any());
3592 locations->SetInAt(1, Location::Any());
3593 locations->SetOut(Location::RequiresFpuRegister());
3594 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003595 break;
3596 }
3597
3598 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003599 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003600 }
3601}
3602
3603void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3604 Primitive::Type type = rem->GetResultType();
3605 switch (type) {
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimLong: {
3608 GenerateDivRemIntegral(rem);
3609 break;
3610 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003611 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003612 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003613 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003614 break;
3615 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003616 default:
3617 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3618 }
3619}
3620
Calin Juravled0d48522014-11-04 16:40:20 +00003621void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003622 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003623 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003624}
3625
3626void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003627 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003628 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3629 codegen_->AddSlowPath(slow_path);
3630
3631 LocationSummary* locations = instruction->GetLocations();
3632 Location value = locations->InAt(0);
3633
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003634 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003635 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003636 case Primitive::kPrimByte:
3637 case Primitive::kPrimChar:
3638 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003639 case Primitive::kPrimInt: {
3640 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003641 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003642 __ j(kEqual, slow_path->GetEntryLabel());
3643 } else if (value.IsStackSlot()) {
3644 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3645 __ j(kEqual, slow_path->GetEntryLabel());
3646 } else {
3647 DCHECK(value.IsConstant()) << value;
3648 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003649 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003650 }
3651 }
3652 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003653 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003654 case Primitive::kPrimLong: {
3655 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003657 __ j(kEqual, slow_path->GetEntryLabel());
3658 } else if (value.IsDoubleStackSlot()) {
3659 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3660 __ j(kEqual, slow_path->GetEntryLabel());
3661 } else {
3662 DCHECK(value.IsConstant()) << value;
3663 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003664 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003665 }
3666 }
3667 break;
3668 }
3669 default:
3670 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003671 }
Calin Juravled0d48522014-11-04 16:40:20 +00003672}
3673
Calin Juravle9aec02f2014-11-18 23:06:35 +00003674void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3675 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3676
3677 LocationSummary* locations =
3678 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3679
3680 switch (op->GetResultType()) {
3681 case Primitive::kPrimInt:
3682 case Primitive::kPrimLong: {
3683 locations->SetInAt(0, Location::RequiresRegister());
3684 // The shift count needs to be in CL.
3685 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3686 locations->SetOut(Location::SameAsFirstInput());
3687 break;
3688 }
3689 default:
3690 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3691 }
3692}
3693
3694void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3695 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3696
3697 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003698 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699 Location second = locations->InAt(1);
3700
3701 switch (op->GetResultType()) {
3702 case Primitive::kPrimInt: {
3703 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003705 if (op->IsShl()) {
3706 __ shll(first_reg, second_reg);
3707 } else if (op->IsShr()) {
3708 __ sarl(first_reg, second_reg);
3709 } else {
3710 __ shrl(first_reg, second_reg);
3711 }
3712 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003713 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003714 if (op->IsShl()) {
3715 __ shll(first_reg, imm);
3716 } else if (op->IsShr()) {
3717 __ sarl(first_reg, imm);
3718 } else {
3719 __ shrl(first_reg, imm);
3720 }
3721 }
3722 break;
3723 }
3724 case Primitive::kPrimLong: {
3725 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003727 if (op->IsShl()) {
3728 __ shlq(first_reg, second_reg);
3729 } else if (op->IsShr()) {
3730 __ sarq(first_reg, second_reg);
3731 } else {
3732 __ shrq(first_reg, second_reg);
3733 }
3734 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003735 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003736 if (op->IsShl()) {
3737 __ shlq(first_reg, imm);
3738 } else if (op->IsShr()) {
3739 __ sarq(first_reg, imm);
3740 } else {
3741 __ shrq(first_reg, imm);
3742 }
3743 }
3744 break;
3745 }
3746 default:
3747 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003748 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 }
3750}
3751
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003752void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3753 LocationSummary* locations =
3754 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3755
3756 switch (ror->GetResultType()) {
3757 case Primitive::kPrimInt:
3758 case Primitive::kPrimLong: {
3759 locations->SetInAt(0, Location::RequiresRegister());
3760 // The shift count needs to be in CL (unless it is a constant).
3761 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3762 locations->SetOut(Location::SameAsFirstInput());
3763 break;
3764 }
3765 default:
3766 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3767 UNREACHABLE();
3768 }
3769}
3770
3771void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3772 LocationSummary* locations = ror->GetLocations();
3773 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3774 Location second = locations->InAt(1);
3775
3776 switch (ror->GetResultType()) {
3777 case Primitive::kPrimInt:
3778 if (second.IsRegister()) {
3779 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3780 __ rorl(first_reg, second_reg);
3781 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003782 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003783 __ rorl(first_reg, imm);
3784 }
3785 break;
3786 case Primitive::kPrimLong:
3787 if (second.IsRegister()) {
3788 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3789 __ rorq(first_reg, second_reg);
3790 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003791 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003792 __ rorq(first_reg, imm);
3793 }
3794 break;
3795 default:
3796 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3797 UNREACHABLE();
3798 }
3799}
3800
Calin Juravle9aec02f2014-11-18 23:06:35 +00003801void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3802 HandleShift(shl);
3803}
3804
3805void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3806 HandleShift(shl);
3807}
3808
3809void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3810 HandleShift(shr);
3811}
3812
3813void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3814 HandleShift(shr);
3815}
3816
3817void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3818 HandleShift(ushr);
3819}
3820
3821void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3822 HandleShift(ushr);
3823}
3824
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003825void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003826 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003827 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003828 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003829 if (instruction->IsStringAlloc()) {
3830 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3831 } else {
3832 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3833 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3834 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003835 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003836}
3837
3838void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003839 // Note: if heap poisoning is enabled, the entry point takes cares
3840 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003841 if (instruction->IsStringAlloc()) {
3842 // String is allocated through StringFactory. Call NewEmptyString entry point.
3843 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003844 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003845 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3846 __ call(Address(temp, code_offset.SizeValue()));
3847 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3848 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003849 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003850 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3851 DCHECK(!codegen_->IsLeafMethod());
3852 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003853}
3854
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003855void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3856 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003857 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003858 InvokeRuntimeCallingConvention calling_convention;
3859 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003860 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003861 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003862 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003863}
3864
3865void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3866 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003867 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3868 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003869 // Note: if heap poisoning is enabled, the entry point takes cares
3870 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003871 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003872 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003873
3874 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003875}
3876
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003877void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003878 LocationSummary* locations =
3879 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003880 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3881 if (location.IsStackSlot()) {
3882 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3883 } else if (location.IsDoubleStackSlot()) {
3884 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3885 }
3886 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003887}
3888
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003889void InstructionCodeGeneratorX86_64::VisitParameterValue(
3890 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003891 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003892}
3893
3894void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3895 LocationSummary* locations =
3896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3897 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3898}
3899
3900void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3901 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3902 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003903}
3904
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003905void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3906 LocationSummary* locations =
3907 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3908 locations->SetInAt(0, Location::RequiresRegister());
3909 locations->SetOut(Location::RequiresRegister());
3910}
3911
3912void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3913 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003914 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003915 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003916 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003917 __ movq(locations->Out().AsRegister<CpuRegister>(),
3918 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003919 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003920 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003921 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003922 __ movq(locations->Out().AsRegister<CpuRegister>(),
3923 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3924 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003925 __ movq(locations->Out().AsRegister<CpuRegister>(),
3926 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003927 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003928}
3929
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003930void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003931 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003932 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003933 locations->SetInAt(0, Location::RequiresRegister());
3934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003935}
3936
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003937void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3938 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003939 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3940 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003941 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003942 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003943 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003944 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003945 break;
3946
3947 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003948 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003949 break;
3950
3951 default:
3952 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3953 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003954}
3955
David Brazdil66d126e2015-04-03 16:02:44 +01003956void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3957 LocationSummary* locations =
3958 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3959 locations->SetInAt(0, Location::RequiresRegister());
3960 locations->SetOut(Location::SameAsFirstInput());
3961}
3962
3963void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003964 LocationSummary* locations = bool_not->GetLocations();
3965 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3966 locations->Out().AsRegister<CpuRegister>().AsRegister());
3967 Location out = locations->Out();
3968 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3969}
3970
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003971void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003974 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003975 locations->SetInAt(i, Location::Any());
3976 }
3977 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003978}
3979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003980void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003981 LOG(FATAL) << "Unimplemented";
3982}
3983
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003984void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00003985 /*
3986 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003987 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00003988 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3989 */
3990 switch (kind) {
3991 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00003992 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00003993 break;
3994 }
3995 case MemBarrierKind::kAnyStore:
3996 case MemBarrierKind::kLoadAny:
3997 case MemBarrierKind::kStoreStore: {
3998 // nop
3999 break;
4000 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004001 case MemBarrierKind::kNTStoreStore:
4002 // Non-Temporal Store/Store needs an explicit fence.
4003 MemoryFence(/* non-temporal */ true);
4004 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004005 }
4006}
4007
4008void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4009 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4010
Roland Levillain0d5a2812015-11-13 10:07:31 +00004011 bool object_field_get_with_read_barrier =
4012 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004013 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004014 new (GetGraph()->GetArena()) LocationSummary(instruction,
4015 object_field_get_with_read_barrier ?
4016 LocationSummary::kCallOnSlowPath :
4017 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004018 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004019 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004020 }
Calin Juravle52c48962014-12-16 17:02:57 +00004021 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004022 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4023 locations->SetOut(Location::RequiresFpuRegister());
4024 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004025 // The output overlaps for an object field get when read barriers
4026 // are enabled: we do not want the move to overwrite the object's
4027 // location, as we need it to emit the read barrier.
4028 locations->SetOut(
4029 Location::RequiresRegister(),
4030 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004031 }
Calin Juravle52c48962014-12-16 17:02:57 +00004032}
4033
4034void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4035 const FieldInfo& field_info) {
4036 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4037
4038 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004039 Location base_loc = locations->InAt(0);
4040 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004041 Location out = locations->Out();
4042 bool is_volatile = field_info.IsVolatile();
4043 Primitive::Type field_type = field_info.GetFieldType();
4044 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4045
4046 switch (field_type) {
4047 case Primitive::kPrimBoolean: {
4048 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4049 break;
4050 }
4051
4052 case Primitive::kPrimByte: {
4053 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4054 break;
4055 }
4056
4057 case Primitive::kPrimShort: {
4058 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4059 break;
4060 }
4061
4062 case Primitive::kPrimChar: {
4063 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4064 break;
4065 }
4066
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004067 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004068 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4069 break;
4070 }
4071
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004072 case Primitive::kPrimNot: {
4073 // /* HeapReference<Object> */ out = *(base + offset)
4074 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004075 // Note that a potential implicit null check is handled in this
4076 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4077 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004078 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004079 if (is_volatile) {
4080 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4081 }
4082 } else {
4083 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4084 codegen_->MaybeRecordImplicitNullCheck(instruction);
4085 if (is_volatile) {
4086 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4087 }
4088 // If read barriers are enabled, emit read barriers other than
4089 // Baker's using a slow path (and also unpoison the loaded
4090 // reference, if heap poisoning is enabled).
4091 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4092 }
4093 break;
4094 }
4095
Calin Juravle52c48962014-12-16 17:02:57 +00004096 case Primitive::kPrimLong: {
4097 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4098 break;
4099 }
4100
4101 case Primitive::kPrimFloat: {
4102 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4103 break;
4104 }
4105
4106 case Primitive::kPrimDouble: {
4107 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4108 break;
4109 }
4110
4111 case Primitive::kPrimVoid:
4112 LOG(FATAL) << "Unreachable type " << field_type;
4113 UNREACHABLE();
4114 }
4115
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004116 if (field_type == Primitive::kPrimNot) {
4117 // Potential implicit null checks, in the case of reference
4118 // fields, are handled in the previous switch statement.
4119 } else {
4120 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004121 }
Roland Levillain4d027112015-07-01 15:41:14 +01004122
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004123 if (is_volatile) {
4124 if (field_type == Primitive::kPrimNot) {
4125 // Memory barriers, in the case of references, are also handled
4126 // in the previous switch statement.
4127 } else {
4128 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4129 }
Roland Levillain4d027112015-07-01 15:41:14 +01004130 }
Calin Juravle52c48962014-12-16 17:02:57 +00004131}
4132
4133void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4134 const FieldInfo& field_info) {
4135 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4136
4137 LocationSummary* locations =
4138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004139 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004140 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004141 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004142 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004143
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004144 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004145 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004146 if (is_volatile) {
4147 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4148 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4149 } else {
4150 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4151 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004152 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004153 if (is_volatile) {
4154 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4155 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4156 } else {
4157 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4158 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004159 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004160 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004161 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004162 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004163 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004164 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4165 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004166 locations->AddTemp(Location::RequiresRegister());
4167 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004168}
4169
Calin Juravle52c48962014-12-16 17:02:57 +00004170void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004171 const FieldInfo& field_info,
4172 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004173 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4174
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004175 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004176 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4177 Location value = locations->InAt(1);
4178 bool is_volatile = field_info.IsVolatile();
4179 Primitive::Type field_type = field_info.GetFieldType();
4180 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4181
4182 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004184 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185
Mark Mendellea5af682015-10-22 17:35:49 -04004186 bool maybe_record_implicit_null_check_done = false;
4187
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004188 switch (field_type) {
4189 case Primitive::kPrimBoolean:
4190 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004191 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004192 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004193 __ movb(Address(base, offset), Immediate(v));
4194 } else {
4195 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4196 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004197 break;
4198 }
4199
4200 case Primitive::kPrimShort:
4201 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004202 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004203 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004204 __ movw(Address(base, offset), Immediate(v));
4205 } else {
4206 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4207 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004208 break;
4209 }
4210
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004211 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004212 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004213 if (value.IsConstant()) {
4214 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004215 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4216 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4217 // Note: if heap poisoning is enabled, no need to poison
4218 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004219 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004220 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004221 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4222 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4223 __ movl(temp, value.AsRegister<CpuRegister>());
4224 __ PoisonHeapReference(temp);
4225 __ movl(Address(base, offset), temp);
4226 } else {
4227 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4228 }
Mark Mendell40741f32015-04-20 22:10:34 -04004229 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004230 break;
4231 }
4232
4233 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004234 if (value.IsConstant()) {
4235 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004236 codegen_->MoveInt64ToAddress(Address(base, offset),
4237 Address(base, offset + sizeof(int32_t)),
4238 v,
4239 instruction);
4240 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004241 } else {
4242 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4243 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244 break;
4245 }
4246
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004247 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004248 if (value.IsConstant()) {
4249 int32_t v =
4250 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4251 __ movl(Address(base, offset), Immediate(v));
4252 } else {
4253 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4254 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004255 break;
4256 }
4257
4258 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004259 if (value.IsConstant()) {
4260 int64_t v =
4261 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4262 codegen_->MoveInt64ToAddress(Address(base, offset),
4263 Address(base, offset + sizeof(int32_t)),
4264 v,
4265 instruction);
4266 maybe_record_implicit_null_check_done = true;
4267 } else {
4268 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4269 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004270 break;
4271 }
4272
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273 case Primitive::kPrimVoid:
4274 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004275 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004276 }
Calin Juravle52c48962014-12-16 17:02:57 +00004277
Mark Mendellea5af682015-10-22 17:35:49 -04004278 if (!maybe_record_implicit_null_check_done) {
4279 codegen_->MaybeRecordImplicitNullCheck(instruction);
4280 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004281
4282 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4283 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4284 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004285 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004286 }
4287
Calin Juravle52c48962014-12-16 17:02:57 +00004288 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004289 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004290 }
4291}
4292
4293void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4294 HandleFieldSet(instruction, instruction->GetFieldInfo());
4295}
4296
4297void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004298 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299}
4300
4301void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004302 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303}
4304
4305void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004306 HandleFieldGet(instruction, instruction->GetFieldInfo());
4307}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308
Calin Juravle52c48962014-12-16 17:02:57 +00004309void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4310 HandleFieldGet(instruction);
4311}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312
Calin Juravle52c48962014-12-16 17:02:57 +00004313void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4314 HandleFieldGet(instruction, instruction->GetFieldInfo());
4315}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316
Calin Juravle52c48962014-12-16 17:02:57 +00004317void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4318 HandleFieldSet(instruction, instruction->GetFieldInfo());
4319}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004320
Calin Juravle52c48962014-12-16 17:02:57 +00004321void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004322 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323}
4324
Calin Juravlee460d1d2015-09-29 04:52:17 +01004325void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4326 HUnresolvedInstanceFieldGet* instruction) {
4327 FieldAccessCallingConventionX86_64 calling_convention;
4328 codegen_->CreateUnresolvedFieldLocationSummary(
4329 instruction, instruction->GetFieldType(), calling_convention);
4330}
4331
4332void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4333 HUnresolvedInstanceFieldGet* instruction) {
4334 FieldAccessCallingConventionX86_64 calling_convention;
4335 codegen_->GenerateUnresolvedFieldAccess(instruction,
4336 instruction->GetFieldType(),
4337 instruction->GetFieldIndex(),
4338 instruction->GetDexPc(),
4339 calling_convention);
4340}
4341
4342void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4343 HUnresolvedInstanceFieldSet* instruction) {
4344 FieldAccessCallingConventionX86_64 calling_convention;
4345 codegen_->CreateUnresolvedFieldLocationSummary(
4346 instruction, instruction->GetFieldType(), calling_convention);
4347}
4348
4349void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4350 HUnresolvedInstanceFieldSet* instruction) {
4351 FieldAccessCallingConventionX86_64 calling_convention;
4352 codegen_->GenerateUnresolvedFieldAccess(instruction,
4353 instruction->GetFieldType(),
4354 instruction->GetFieldIndex(),
4355 instruction->GetDexPc(),
4356 calling_convention);
4357}
4358
4359void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4360 HUnresolvedStaticFieldGet* instruction) {
4361 FieldAccessCallingConventionX86_64 calling_convention;
4362 codegen_->CreateUnresolvedFieldLocationSummary(
4363 instruction, instruction->GetFieldType(), calling_convention);
4364}
4365
4366void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4367 HUnresolvedStaticFieldGet* instruction) {
4368 FieldAccessCallingConventionX86_64 calling_convention;
4369 codegen_->GenerateUnresolvedFieldAccess(instruction,
4370 instruction->GetFieldType(),
4371 instruction->GetFieldIndex(),
4372 instruction->GetDexPc(),
4373 calling_convention);
4374}
4375
4376void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4377 HUnresolvedStaticFieldSet* instruction) {
4378 FieldAccessCallingConventionX86_64 calling_convention;
4379 codegen_->CreateUnresolvedFieldLocationSummary(
4380 instruction, instruction->GetFieldType(), calling_convention);
4381}
4382
4383void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4384 HUnresolvedStaticFieldSet* instruction) {
4385 FieldAccessCallingConventionX86_64 calling_convention;
4386 codegen_->GenerateUnresolvedFieldAccess(instruction,
4387 instruction->GetFieldType(),
4388 instruction->GetFieldIndex(),
4389 instruction->GetDexPc(),
4390 calling_convention);
4391}
4392
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004393void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004394 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4395 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4396 ? Location::RequiresRegister()
4397 : Location::Any();
4398 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399}
4400
Calin Juravle2ae48182016-03-16 14:05:09 +00004401void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4402 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004403 return;
4404 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004405 LocationSummary* locations = instruction->GetLocations();
4406 Location obj = locations->InAt(0);
4407
4408 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004409 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004410}
4411
Calin Juravle2ae48182016-03-16 14:05:09 +00004412void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004413 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004414 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004415
4416 LocationSummary* locations = instruction->GetLocations();
4417 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418
4419 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004420 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004421 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004422 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004423 } else {
4424 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004425 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004426 __ jmp(slow_path->GetEntryLabel());
4427 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428 }
4429 __ j(kEqual, slow_path->GetEntryLabel());
4430}
4431
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004432void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004433 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004434}
4435
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004437 bool object_array_get_with_read_barrier =
4438 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004439 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004440 new (GetGraph()->GetArena()) LocationSummary(instruction,
4441 object_array_get_with_read_barrier ?
4442 LocationSummary::kCallOnSlowPath :
4443 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004444 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004445 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004446 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004447 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004448 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004449 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4450 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4451 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004452 // The output overlaps for an object array get when read barriers
4453 // are enabled: we do not want the move to overwrite the array's
4454 // location, as we need it to emit the read barrier.
4455 locations->SetOut(
4456 Location::RequiresRegister(),
4457 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004458 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004459}
4460
4461void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4462 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004463 Location obj_loc = locations->InAt(0);
4464 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004466 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004467 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004469 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004470 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004471 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004472 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004473 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474 break;
4475 }
4476
4477 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004478 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004479 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004480 break;
4481 }
4482
4483 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004484 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004485 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004486 break;
4487 }
4488
4489 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004490 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004491 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4492 // Branch cases into compressed and uncompressed for each index's type.
4493 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4494 NearLabel done, not_compressed;
4495 __ cmpl(Address(obj, count_offset), Immediate(0));
4496 codegen_->MaybeRecordImplicitNullCheck(instruction);
4497 __ j(kGreaterEqual, &not_compressed);
4498 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4499 __ jmp(&done);
4500 __ Bind(&not_compressed);
4501 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4502 __ Bind(&done);
4503 } else {
4504 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4505 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 break;
4507 }
4508
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004509 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004510 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004511 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004512 break;
4513 }
4514
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004515 case Primitive::kPrimNot: {
4516 static_assert(
4517 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4518 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004519 // /* HeapReference<Object> */ out =
4520 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4521 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004522 // Note that a potential implicit null check is handled in this
4523 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4524 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004525 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004526 } else {
4527 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004528 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4529 codegen_->MaybeRecordImplicitNullCheck(instruction);
4530 // If read barriers are enabled, emit read barriers other than
4531 // Baker's using a slow path (and also unpoison the loaded
4532 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004533 if (index.IsConstant()) {
4534 uint32_t offset =
4535 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004536 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4537 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004538 codegen_->MaybeGenerateReadBarrierSlow(
4539 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4540 }
4541 }
4542 break;
4543 }
4544
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004546 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004547 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004548 break;
4549 }
4550
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004551 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004552 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004553 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004554 break;
4555 }
4556
4557 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004558 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004559 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004560 break;
4561 }
4562
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004563 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004564 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004565 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566 }
Roland Levillain4d027112015-07-01 15:41:14 +01004567
4568 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004569 // Potential implicit null checks, in the case of reference
4570 // arrays, are handled in the previous switch statement.
4571 } else {
4572 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004573 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574}
4575
4576void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004577 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004578
4579 bool needs_write_barrier =
4580 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004581 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004582
Nicolas Geoffray39468442014-09-02 15:17:15 +01004583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004584 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004585 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004586 LocationSummary::kCallOnSlowPath :
4587 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004588
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004589 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004590 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4591 if (Primitive::IsFloatingPointType(value_type)) {
4592 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004593 } else {
4594 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4595 }
4596
4597 if (needs_write_barrier) {
4598 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004599 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004600 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602}
4603
4604void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4605 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004606 Location array_loc = locations->InAt(0);
4607 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004609 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004610 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004611 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004612 bool needs_write_barrier =
4613 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004614 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4615 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4616 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617
4618 switch (value_type) {
4619 case Primitive::kPrimBoolean:
4620 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004621 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004622 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004623 if (value.IsRegister()) {
4624 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004626 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004628 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 break;
4630 }
4631
4632 case Primitive::kPrimShort:
4633 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004634 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004635 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004636 if (value.IsRegister()) {
4637 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004639 DCHECK(value.IsConstant()) << value;
4640 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004642 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 break;
4644 }
4645
4646 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004648 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004649
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004650 if (!value.IsRegister()) {
4651 // Just setting null.
4652 DCHECK(instruction->InputAt(2)->IsNullConstant());
4653 DCHECK(value.IsConstant()) << value;
4654 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004655 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004656 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004657 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004658 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004659 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004660
4661 DCHECK(needs_write_barrier);
4662 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004663 // We cannot use a NearLabel for `done`, as its range may be too
4664 // short when Baker read barriers are enabled.
4665 Label done;
4666 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004667 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004668 Location temp_loc = locations->GetTemp(0);
4669 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004670 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4672 codegen_->AddSlowPath(slow_path);
4673 if (instruction->GetValueCanBeNull()) {
4674 __ testl(register_value, register_value);
4675 __ j(kNotEqual, &not_null);
4676 __ movl(address, Immediate(0));
4677 codegen_->MaybeRecordImplicitNullCheck(instruction);
4678 __ jmp(&done);
4679 __ Bind(&not_null);
4680 }
4681
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004682 // Note that when Baker read barriers are enabled, the type
4683 // checks are performed without read barriers. This is fine,
4684 // even in the case where a class object is in the from-space
4685 // after the flip, as a comparison involving such a type would
4686 // not produce a false positive; it may of course produce a
4687 // false negative, in which case we would take the ArraySet
4688 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004689
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004690 // /* HeapReference<Class> */ temp = array->klass_
4691 __ movl(temp, Address(array, class_offset));
4692 codegen_->MaybeRecordImplicitNullCheck(instruction);
4693 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004694
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004695 // /* HeapReference<Class> */ temp = temp->component_type_
4696 __ movl(temp, Address(temp, component_offset));
4697 // If heap poisoning is enabled, no need to unpoison `temp`
4698 // nor the object reference in `register_value->klass`, as
4699 // we are comparing two poisoned references.
4700 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004701
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004702 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4703 __ j(kEqual, &do_put);
4704 // If heap poisoning is enabled, the `temp` reference has
4705 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004706 __ MaybeUnpoisonHeapReference(temp);
4707
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004708 // If heap poisoning is enabled, no need to unpoison the
4709 // heap reference loaded below, as it is only used for a
4710 // comparison with null.
4711 __ cmpl(Address(temp, super_offset), Immediate(0));
4712 __ j(kNotEqual, slow_path->GetEntryLabel());
4713 __ Bind(&do_put);
4714 } else {
4715 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004716 }
4717 }
4718
4719 if (kPoisonHeapReferences) {
4720 __ movl(temp, register_value);
4721 __ PoisonHeapReference(temp);
4722 __ movl(address, temp);
4723 } else {
4724 __ movl(address, register_value);
4725 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004726 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004727 codegen_->MaybeRecordImplicitNullCheck(instruction);
4728 }
4729
4730 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4731 codegen_->MarkGCCard(
4732 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4733 __ Bind(&done);
4734
4735 if (slow_path != nullptr) {
4736 __ Bind(slow_path->GetExitLabel());
4737 }
4738
4739 break;
4740 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004742 case Primitive::kPrimInt: {
4743 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004744 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745 if (value.IsRegister()) {
4746 __ movl(address, value.AsRegister<CpuRegister>());
4747 } else {
4748 DCHECK(value.IsConstant()) << value;
4749 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4750 __ movl(address, Immediate(v));
4751 }
4752 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004753 break;
4754 }
4755
4756 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004757 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004758 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 if (value.IsRegister()) {
4760 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004761 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004764 Address address_high =
4765 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004766 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767 }
4768 break;
4769 }
4770
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004771 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004772 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004773 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004774 if (value.IsFpuRegister()) {
4775 __ movss(address, value.AsFpuRegister<XmmRegister>());
4776 } else {
4777 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004778 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04004779 __ movl(address, Immediate(v));
4780 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004781 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004782 break;
4783 }
4784
4785 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004786 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004787 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004788 if (value.IsFpuRegister()) {
4789 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4790 codegen_->MaybeRecordImplicitNullCheck(instruction);
4791 } else {
4792 int64_t v =
4793 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004794 Address address_high =
4795 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04004796 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4797 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004798 break;
4799 }
4800
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 case Primitive::kPrimVoid:
4802 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004803 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004804 }
4805}
4806
4807void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004808 LocationSummary* locations =
4809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004810 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04004811 if (!instruction->IsEmittedAtUseSite()) {
4812 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4813 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004814}
4815
4816void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04004817 if (instruction->IsEmittedAtUseSite()) {
4818 return;
4819 }
4820
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004822 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004823 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4824 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004825 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004826 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07004827 // Mask out most significant bit in case the array is String's array of char.
4828 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
4829 __ andl(out, Immediate(INT32_MAX));
4830 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831}
4832
4833void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004834 RegisterSet caller_saves = RegisterSet::Empty();
4835 InvokeRuntimeCallingConvention calling_convention;
4836 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4837 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4838 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004839 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04004840 HInstruction* length = instruction->InputAt(1);
4841 if (!length->IsEmittedAtUseSite()) {
4842 locations->SetInAt(1, Location::RegisterOrConstant(length));
4843 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844}
4845
4846void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4847 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004848 Location index_loc = locations->InAt(0);
4849 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04004850 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851
Mark Mendell99dbd682015-04-22 16:18:52 -04004852 if (length_loc.IsConstant()) {
4853 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4854 if (index_loc.IsConstant()) {
4855 // BCE will remove the bounds check if we are guarenteed to pass.
4856 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4857 if (index < 0 || index >= length) {
4858 codegen_->AddSlowPath(slow_path);
4859 __ jmp(slow_path->GetEntryLabel());
4860 } else {
4861 // Some optimization after BCE may have generated this, and we should not
4862 // generate a bounds check if it is a valid range.
4863 }
4864 return;
4865 }
4866
4867 // We have to reverse the jump condition because the length is the constant.
4868 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4869 __ cmpl(index_reg, Immediate(length));
4870 codegen_->AddSlowPath(slow_path);
4871 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004872 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04004873 HInstruction* array_length = instruction->InputAt(1);
4874 if (array_length->IsEmittedAtUseSite()) {
4875 // Address the length field in the array.
4876 DCHECK(array_length->IsArrayLength());
4877 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
4878 Location array_loc = array_length->GetLocations()->InAt(0);
4879 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07004880 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4881 CpuRegister length_reg = CpuRegister(TMP);
4882 __ movl(length_reg, array_len);
4883 codegen_->MaybeRecordImplicitNullCheck(array_length);
4884 __ andl(length_reg, Immediate(INT32_MAX));
4885 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04004886 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07004887 // Checking the bound for general case:
4888 // Array of char or String's array when the compression feature off.
4889 if (index_loc.IsConstant()) {
4890 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4891 __ cmpl(array_len, Immediate(value));
4892 } else {
4893 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
4894 }
4895 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04004896 }
Mark Mendell99dbd682015-04-22 16:18:52 -04004897 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004898 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04004899 }
4900 codegen_->AddSlowPath(slow_path);
4901 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004902 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004903}
4904
4905void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4906 CpuRegister card,
4907 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004908 CpuRegister value,
4909 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004910 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004911 if (value_can_be_null) {
4912 __ testl(value, value);
4913 __ j(kEqual, &is_null);
4914 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004915 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004916 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917 __ movq(temp, object);
4918 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004919 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004920 if (value_can_be_null) {
4921 __ Bind(&is_null);
4922 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923}
4924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004925void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004926 LOG(FATAL) << "Unimplemented";
4927}
4928
4929void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004930 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4931}
4932
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004933void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004934 LocationSummary* locations =
4935 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004936 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004937}
4938
4939void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004940 HBasicBlock* block = instruction->GetBlock();
4941 if (block->GetLoopInformation() != nullptr) {
4942 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4943 // The back edge will generate the suspend check.
4944 return;
4945 }
4946 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4947 // The goto will generate the suspend check.
4948 return;
4949 }
4950 GenerateSuspendCheck(instruction, nullptr);
4951}
4952
4953void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4954 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004955 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004956 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4957 if (slow_path == nullptr) {
4958 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4959 instruction->SetSlowPath(slow_path);
4960 codegen_->AddSlowPath(slow_path);
4961 if (successor != nullptr) {
4962 DCHECK(successor->IsLoopHeader());
4963 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4964 }
4965 } else {
4966 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4967 }
4968
Andreas Gampe542451c2016-07-26 09:02:02 -07004969 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004970 /* no_rip */ true),
4971 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004972 if (successor == nullptr) {
4973 __ j(kNotEqual, slow_path->GetEntryLabel());
4974 __ Bind(slow_path->GetReturnLabel());
4975 } else {
4976 __ j(kEqual, codegen_->GetLabelOf(successor));
4977 __ jmp(slow_path->GetEntryLabel());
4978 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004979}
4980
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004981X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4982 return codegen_->GetAssembler();
4983}
4984
4985void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004986 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004987 Location source = move->GetSource();
4988 Location destination = move->GetDestination();
4989
4990 if (source.IsRegister()) {
4991 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004992 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004993 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004994 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004995 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004996 } else {
4997 DCHECK(destination.IsDoubleStackSlot());
4998 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004999 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005000 }
5001 } else if (source.IsStackSlot()) {
5002 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005003 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005004 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005005 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005006 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005007 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005008 } else {
5009 DCHECK(destination.IsStackSlot());
5010 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5011 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5012 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005013 } else if (source.IsDoubleStackSlot()) {
5014 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005015 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005016 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005017 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005018 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5019 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005020 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005021 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005022 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5023 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5024 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005025 } else if (source.IsConstant()) {
5026 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005027 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5028 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005029 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005030 if (value == 0) {
5031 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5032 } else {
5033 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5034 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005035 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005036 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005037 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005038 }
5039 } else if (constant->IsLongConstant()) {
5040 int64_t value = constant->AsLongConstant()->GetValue();
5041 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005042 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005043 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005044 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005045 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005046 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005047 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005048 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005049 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005050 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005051 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005052 } else {
5053 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005054 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005055 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5056 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005057 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005058 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005059 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005060 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005061 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005062 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005063 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005064 } else {
5065 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005066 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005067 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005068 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005069 } else if (source.IsFpuRegister()) {
5070 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005071 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005072 } else if (destination.IsStackSlot()) {
5073 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005074 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005075 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005076 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005077 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005078 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005079 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005080 }
5081}
5082
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005083void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005084 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005085 __ movl(Address(CpuRegister(RSP), mem), reg);
5086 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005087}
5088
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005089void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005090 ScratchRegisterScope ensure_scratch(
5091 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5092
5093 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5094 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5095 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5096 Address(CpuRegister(RSP), mem2 + stack_offset));
5097 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5098 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5099 CpuRegister(ensure_scratch.GetRegister()));
5100}
5101
Mark Mendell8a1c7282015-06-29 15:41:28 -04005102void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5103 __ movq(CpuRegister(TMP), reg1);
5104 __ movq(reg1, reg2);
5105 __ movq(reg2, CpuRegister(TMP));
5106}
5107
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005108void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5109 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5110 __ movq(Address(CpuRegister(RSP), mem), reg);
5111 __ movq(reg, CpuRegister(TMP));
5112}
5113
5114void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5115 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005116 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005117
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005118 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5119 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5120 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5121 Address(CpuRegister(RSP), mem2 + stack_offset));
5122 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5123 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5124 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005125}
5126
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005127void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5128 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5129 __ movss(Address(CpuRegister(RSP), mem), reg);
5130 __ movd(reg, CpuRegister(TMP));
5131}
5132
5133void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5134 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5135 __ movsd(Address(CpuRegister(RSP), mem), reg);
5136 __ movd(reg, CpuRegister(TMP));
5137}
5138
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005139void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005140 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005141 Location source = move->GetSource();
5142 Location destination = move->GetDestination();
5143
5144 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005145 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005146 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005148 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005150 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005151 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5152 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005154 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005155 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005156 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5157 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005158 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005159 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5160 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5161 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005162 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005163 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005164 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005165 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005166 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005167 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005168 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005169 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005170 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005171 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005172 }
5173}
5174
5175
5176void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5177 __ pushq(CpuRegister(reg));
5178}
5179
5180
5181void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5182 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005183}
5184
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005185void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005186 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005187 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5188 Immediate(mirror::Class::kStatusInitialized));
5189 __ j(kLess, slow_path->GetEntryLabel());
5190 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005191 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005192}
5193
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005194HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5195 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005196 switch (desired_class_load_kind) {
5197 case HLoadClass::LoadKind::kReferrersClass:
5198 break;
5199 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5200 DCHECK(!GetCompilerOptions().GetCompilePic());
5201 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5202 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5203 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5204 DCHECK(GetCompilerOptions().GetCompilePic());
5205 break;
5206 case HLoadClass::LoadKind::kBootImageAddress:
5207 break;
5208 case HLoadClass::LoadKind::kDexCacheAddress:
5209 DCHECK(Runtime::Current()->UseJitCompilation());
5210 break;
5211 case HLoadClass::LoadKind::kDexCachePcRelative:
5212 DCHECK(!Runtime::Current()->UseJitCompilation());
5213 break;
5214 case HLoadClass::LoadKind::kDexCacheViaMethod:
5215 break;
5216 }
5217 return desired_class_load_kind;
5218}
5219
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005220void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005221 if (cls->NeedsAccessCheck()) {
5222 InvokeRuntimeCallingConvention calling_convention;
5223 CodeGenerator::CreateLoadClassLocationSummary(
5224 cls,
5225 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5226 Location::RegisterLocation(RAX),
5227 /* code_generator_supports_read_barrier */ true);
5228 return;
5229 }
5230
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005231 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5232 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005233 ? LocationSummary::kCallOnSlowPath
5234 : LocationSummary::kNoCall;
5235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005236 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005237 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005238 }
5239
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005240 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5241 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5242 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5243 locations->SetInAt(0, Location::RequiresRegister());
5244 }
5245 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005246}
5247
5248void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005249 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005250 if (cls->NeedsAccessCheck()) {
5251 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005252 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005253 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005254 return;
5255 }
5256
Roland Levillain0d5a2812015-11-13 10:07:31 +00005257 Location out_loc = locations->Out();
5258 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005259
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005260 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005261 bool generate_null_check = false;
5262 switch (cls->GetLoadKind()) {
5263 case HLoadClass::LoadKind::kReferrersClass: {
5264 DCHECK(!cls->CanCallRuntime());
5265 DCHECK(!cls->MustGenerateClinitCheck());
5266 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5267 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5268 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005269 cls,
5270 out_loc,
5271 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5272 /*fixup_label*/nullptr,
5273 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005274 break;
5275 }
5276 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005277 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005278 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5279 codegen_->RecordTypePatch(cls);
5280 break;
5281 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005282 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005283 DCHECK_NE(cls->GetAddress(), 0u);
5284 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5285 __ movl(out, Immediate(address)); // Zero-extended.
5286 codegen_->RecordSimplePatch();
5287 break;
5288 }
5289 case HLoadClass::LoadKind::kDexCacheAddress: {
5290 DCHECK_NE(cls->GetAddress(), 0u);
5291 // /* GcRoot<mirror::Class> */ out = *address
5292 if (IsUint<32>(cls->GetAddress())) {
5293 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005294 GenerateGcRootFieldLoad(cls,
5295 out_loc,
5296 address,
5297 /*fixup_label*/nullptr,
5298 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005299 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005300 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5301 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005302 GenerateGcRootFieldLoad(cls,
5303 out_loc,
5304 Address(out, 0),
5305 /*fixup_label*/nullptr,
5306 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005307 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005308 generate_null_check = !cls->IsInDexCache();
5309 break;
5310 }
5311 case HLoadClass::LoadKind::kDexCachePcRelative: {
5312 uint32_t offset = cls->GetDexCacheElementOffset();
5313 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5314 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5315 /* no_rip */ false);
5316 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005317 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005318 generate_null_check = !cls->IsInDexCache();
5319 break;
5320 }
5321 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5322 // /* GcRoot<mirror::Class>[] */ out =
5323 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5324 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5325 __ movq(out,
5326 Address(current_method,
5327 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5328 // /* GcRoot<mirror::Class> */ out = out[type_index]
5329 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005330 cls,
5331 out_loc,
5332 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5333 /*fixup_label*/nullptr,
5334 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005335 generate_null_check = !cls->IsInDexCache();
5336 break;
5337 }
5338 default:
5339 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5340 UNREACHABLE();
5341 }
5342
5343 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5344 DCHECK(cls->CanCallRuntime());
5345 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5346 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5347 codegen_->AddSlowPath(slow_path);
5348 if (generate_null_check) {
5349 __ testl(out, out);
5350 __ j(kEqual, slow_path->GetEntryLabel());
5351 }
5352 if (cls->MustGenerateClinitCheck()) {
5353 GenerateClassInitializationCheck(slow_path, out);
5354 } else {
5355 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005356 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005357 }
5358}
5359
5360void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5361 LocationSummary* locations =
5362 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5363 locations->SetInAt(0, Location::RequiresRegister());
5364 if (check->HasUses()) {
5365 locations->SetOut(Location::SameAsFirstInput());
5366 }
5367}
5368
5369void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005370 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005371 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005372 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005373 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005374 GenerateClassInitializationCheck(slow_path,
5375 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005376}
5377
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005378HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5379 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005380 switch (desired_string_load_kind) {
5381 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5382 DCHECK(!GetCompilerOptions().GetCompilePic());
5383 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5384 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5385 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5386 DCHECK(GetCompilerOptions().GetCompilePic());
5387 break;
5388 case HLoadString::LoadKind::kBootImageAddress:
5389 break;
5390 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005391 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005392 break;
5393 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005394 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005395 break;
5396 case HLoadString::LoadKind::kDexCacheViaMethod:
5397 break;
5398 }
5399 return desired_string_load_kind;
5400}
5401
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005402void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005403 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5404 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005405 : LocationSummary::kNoCall;
5406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005407 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5408 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005409 locations->SetOut(Location::RegisterLocation(RAX));
5410 } else {
5411 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005412 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005413}
5414
5415void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005416 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005417 Location out_loc = locations->Out();
5418 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005419
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005420 switch (load->GetLoadKind()) {
5421 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005422 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5423 codegen_->RecordStringPatch(load);
5424 return; // No dex cache slow path.
5425 }
5426 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005427 DCHECK_NE(load->GetAddress(), 0u);
5428 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5429 __ movl(out, Immediate(address)); // Zero-extended.
5430 codegen_->RecordSimplePatch();
5431 return; // No dex cache slow path.
5432 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005433 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005434 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005435 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005436
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005437 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005438 InvokeRuntimeCallingConvention calling_convention;
5439 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
5440 Immediate(load->GetStringIndex()));
5441 codegen_->InvokeRuntime(kQuickResolveString,
5442 load,
5443 load->GetDexPc());
5444 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005445}
5446
David Brazdilcb1c0552015-08-04 16:22:25 +01005447static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005448 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005449 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005450}
5451
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005452void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5453 LocationSummary* locations =
5454 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5455 locations->SetOut(Location::RequiresRegister());
5456}
5457
5458void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005459 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5460}
5461
5462void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5463 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5464}
5465
5466void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5467 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005468}
5469
5470void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5471 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005473 InvokeRuntimeCallingConvention calling_convention;
5474 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5475}
5476
5477void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005478 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005479 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005480}
5481
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005482static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5483 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005484 !kUseBakerReadBarrier &&
5485 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005486 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5487 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5488}
5489
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005490void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005492 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005493 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005494 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005495 case TypeCheckKind::kExactCheck:
5496 case TypeCheckKind::kAbstractClassCheck:
5497 case TypeCheckKind::kClassHierarchyCheck:
5498 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499 call_kind =
5500 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005501 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 break;
5503 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504 case TypeCheckKind::kUnresolvedCheck:
5505 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 call_kind = LocationSummary::kCallOnSlowPath;
5507 break;
5508 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005509
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005511 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005512 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005513 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514 locations->SetInAt(0, Location::RequiresRegister());
5515 locations->SetInAt(1, Location::Any());
5516 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5517 locations->SetOut(Location::RequiresRegister());
5518 // When read barriers are enabled, we need a temporary register for
5519 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005520 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005521 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005522 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005523}
5524
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005525void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005526 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005527 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005528 Location obj_loc = locations->InAt(0);
5529 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005530 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005531 Location out_loc = locations->Out();
5532 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005533 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005534 locations->GetTemp(0) :
5535 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005536 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005537 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5538 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5539 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005540 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005541 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005542
5543 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005544 // Avoid null check if we know obj is not null.
5545 if (instruction->MustDoNullCheck()) {
5546 __ testl(obj, obj);
5547 __ j(kEqual, &zero);
5548 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005549
Roland Levillain0d5a2812015-11-13 10:07:31 +00005550 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005551 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005552
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005553 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005554 case TypeCheckKind::kExactCheck: {
5555 if (cls.IsRegister()) {
5556 __ cmpl(out, cls.AsRegister<CpuRegister>());
5557 } else {
5558 DCHECK(cls.IsStackSlot()) << cls;
5559 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5560 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005561 if (zero.IsLinked()) {
5562 // Classes must be equal for the instanceof to succeed.
5563 __ j(kNotEqual, &zero);
5564 __ movl(out, Immediate(1));
5565 __ jmp(&done);
5566 } else {
5567 __ setcc(kEqual, out);
5568 // setcc only sets the low byte.
5569 __ andl(out, Immediate(1));
5570 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005571 break;
5572 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005573
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005574 case TypeCheckKind::kAbstractClassCheck: {
5575 // If the class is abstract, we eagerly fetch the super class of the
5576 // object to avoid doing a comparison we know will fail.
5577 NearLabel loop, success;
5578 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005579 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005580 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005581 __ testl(out, out);
5582 // If `out` is null, we use it for the result, and jump to `done`.
5583 __ j(kEqual, &done);
5584 if (cls.IsRegister()) {
5585 __ cmpl(out, cls.AsRegister<CpuRegister>());
5586 } else {
5587 DCHECK(cls.IsStackSlot()) << cls;
5588 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5589 }
5590 __ j(kNotEqual, &loop);
5591 __ movl(out, Immediate(1));
5592 if (zero.IsLinked()) {
5593 __ jmp(&done);
5594 }
5595 break;
5596 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005597
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005598 case TypeCheckKind::kClassHierarchyCheck: {
5599 // Walk over the class hierarchy to find a match.
5600 NearLabel loop, success;
5601 __ Bind(&loop);
5602 if (cls.IsRegister()) {
5603 __ cmpl(out, cls.AsRegister<CpuRegister>());
5604 } else {
5605 DCHECK(cls.IsStackSlot()) << cls;
5606 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5607 }
5608 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005609 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005610 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005611 __ testl(out, out);
5612 __ j(kNotEqual, &loop);
5613 // If `out` is null, we use it for the result, and jump to `done`.
5614 __ jmp(&done);
5615 __ Bind(&success);
5616 __ movl(out, Immediate(1));
5617 if (zero.IsLinked()) {
5618 __ jmp(&done);
5619 }
5620 break;
5621 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005622
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005623 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005624 // Do an exact check.
5625 NearLabel exact_check;
5626 if (cls.IsRegister()) {
5627 __ cmpl(out, cls.AsRegister<CpuRegister>());
5628 } else {
5629 DCHECK(cls.IsStackSlot()) << cls;
5630 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5631 }
5632 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005633 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005634 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005635 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005636 __ testl(out, out);
5637 // If `out` is null, we use it for the result, and jump to `done`.
5638 __ j(kEqual, &done);
5639 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5640 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005641 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005642 __ movl(out, Immediate(1));
5643 __ jmp(&done);
5644 break;
5645 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005646
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005647 case TypeCheckKind::kArrayCheck: {
5648 if (cls.IsRegister()) {
5649 __ cmpl(out, cls.AsRegister<CpuRegister>());
5650 } else {
5651 DCHECK(cls.IsStackSlot()) << cls;
5652 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5653 }
5654 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005655 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5656 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 codegen_->AddSlowPath(slow_path);
5658 __ j(kNotEqual, slow_path->GetEntryLabel());
5659 __ movl(out, Immediate(1));
5660 if (zero.IsLinked()) {
5661 __ jmp(&done);
5662 }
5663 break;
5664 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005665
Calin Juravle98893e12015-10-02 21:05:03 +01005666 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005667 case TypeCheckKind::kInterfaceCheck: {
5668 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005669 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005670 // cases.
5671 //
5672 // We cannot directly call the InstanceofNonTrivial runtime
5673 // entry point without resorting to a type checking slow path
5674 // here (i.e. by calling InvokeRuntime directly), as it would
5675 // require to assign fixed registers for the inputs of this
5676 // HInstanceOf instruction (following the runtime calling
5677 // convention), which might be cluttered by the potential first
5678 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005679 //
5680 // TODO: Introduce a new runtime entry point taking the object
5681 // to test (instead of its class) as argument, and let it deal
5682 // with the read barrier issues. This will let us refactor this
5683 // case of the `switch` code as it was previously (with a direct
5684 // call to the runtime not using a type checking slow path).
5685 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005686 DCHECK(locations->OnlyCallsOnSlowPath());
5687 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5688 /* is_fatal */ false);
5689 codegen_->AddSlowPath(slow_path);
5690 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005691 if (zero.IsLinked()) {
5692 __ jmp(&done);
5693 }
5694 break;
5695 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005696 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005697
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005699 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700 __ xorl(out, out);
5701 }
5702
5703 if (done.IsLinked()) {
5704 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005705 }
5706
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005707 if (slow_path != nullptr) {
5708 __ Bind(slow_path->GetExitLabel());
5709 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005710}
5711
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005712void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5714 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005715 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5716 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717 case TypeCheckKind::kExactCheck:
5718 case TypeCheckKind::kAbstractClassCheck:
5719 case TypeCheckKind::kClassHierarchyCheck:
5720 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005721 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5722 LocationSummary::kCallOnSlowPath :
5723 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 break;
5725 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005726 case TypeCheckKind::kUnresolvedCheck:
5727 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005728 call_kind = LocationSummary::kCallOnSlowPath;
5729 break;
5730 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5732 locations->SetInAt(0, Location::RequiresRegister());
5733 locations->SetInAt(1, Location::Any());
5734 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5735 locations->AddTemp(Location::RequiresRegister());
5736 // When read barriers are enabled, we need an additional temporary
5737 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005738 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005739 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005741}
5742
5743void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005744 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005745 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 Location obj_loc = locations->InAt(0);
5747 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005748 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749 Location temp_loc = locations->GetTemp(0);
5750 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005751 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005752 locations->GetTemp(1) :
5753 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5755 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5756 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5757 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005758
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759 bool is_type_check_slow_path_fatal =
5760 (type_check_kind == TypeCheckKind::kExactCheck ||
5761 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5762 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5763 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5764 !instruction->CanThrowIntoCatchBlock();
5765 SlowPathCode* type_check_slow_path =
5766 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5767 is_type_check_slow_path_fatal);
5768 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005769
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005771 case TypeCheckKind::kExactCheck:
5772 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005773 NearLabel done;
5774 // Avoid null check if we know obj is not null.
5775 if (instruction->MustDoNullCheck()) {
5776 __ testl(obj, obj);
5777 __ j(kEqual, &done);
5778 }
5779
5780 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005781 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005782
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783 if (cls.IsRegister()) {
5784 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5785 } else {
5786 DCHECK(cls.IsStackSlot()) << cls;
5787 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5788 }
5789 // Jump to slow path for throwing the exception or doing a
5790 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005791 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005792 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 break;
5794 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005795
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005796 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005797 NearLabel done;
5798 // Avoid null check if we know obj is not null.
5799 if (instruction->MustDoNullCheck()) {
5800 __ testl(obj, obj);
5801 __ j(kEqual, &done);
5802 }
5803
5804 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005805 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005806
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 // If the class is abstract, we eagerly fetch the super class of the
5808 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005810 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005811 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005812 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005813
5814 // If the class reference currently in `temp` is not null, jump
5815 // to the `compare_classes` label to compare it with the checked
5816 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005817 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005818 __ j(kNotEqual, &compare_classes);
5819 // Otherwise, jump to the slow path to throw the exception.
5820 //
5821 // But before, move back the object's class into `temp` before
5822 // going into the slow path, as it has been overwritten in the
5823 // meantime.
5824 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005825 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005826 __ jmp(type_check_slow_path->GetEntryLabel());
5827
5828 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 if (cls.IsRegister()) {
5830 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5831 } else {
5832 DCHECK(cls.IsStackSlot()) << cls;
5833 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5834 }
5835 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005836 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 break;
5838 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005841 NearLabel done;
5842 // Avoid null check if we know obj is not null.
5843 if (instruction->MustDoNullCheck()) {
5844 __ testl(obj, obj);
5845 __ j(kEqual, &done);
5846 }
5847
5848 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005849 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005850
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005852 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853 __ Bind(&loop);
5854 if (cls.IsRegister()) {
5855 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5856 } else {
5857 DCHECK(cls.IsStackSlot()) << cls;
5858 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5859 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005860 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005861
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005863 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864
5865 // If the class reference currently in `temp` is not null, jump
5866 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867 __ testl(temp, temp);
5868 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869 // Otherwise, jump to the slow path to throw the exception.
5870 //
5871 // But before, move back the object's class into `temp` before
5872 // going into the slow path, as it has been overwritten in the
5873 // meantime.
5874 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005875 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005877 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005878 break;
5879 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005882 // We cannot use a NearLabel here, as its range might be too
5883 // short in some cases when read barriers are enabled. This has
5884 // been observed for instance when the code emitted for this
5885 // case uses high x86-64 registers (R8-R15).
5886 Label done;
5887 // Avoid null check if we know obj is not null.
5888 if (instruction->MustDoNullCheck()) {
5889 __ testl(obj, obj);
5890 __ j(kEqual, &done);
5891 }
5892
5893 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005894 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005895
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005896 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005898 if (cls.IsRegister()) {
5899 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5900 } else {
5901 DCHECK(cls.IsStackSlot()) << cls;
5902 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5903 }
5904 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905
5906 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005907 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005908 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909
5910 // If the component type is not null (i.e. the object is indeed
5911 // an array), jump to label `check_non_primitive_component_type`
5912 // to further check that this component type is not a primitive
5913 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915 __ j(kNotEqual, &check_non_primitive_component_type);
5916 // Otherwise, jump to the slow path to throw the exception.
5917 //
5918 // But before, move back the object's class into `temp` before
5919 // going into the slow path, as it has been overwritten in the
5920 // meantime.
5921 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005922 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005923 __ jmp(type_check_slow_path->GetEntryLabel());
5924
5925 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005926 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005927 __ j(kEqual, &done);
5928 // Same comment as above regarding `temp` and the slow path.
5929 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005930 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005932 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005933 break;
5934 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935
Calin Juravle98893e12015-10-02 21:05:03 +01005936 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00005938 NearLabel done;
5939 // Avoid null check if we know obj is not null.
5940 if (instruction->MustDoNullCheck()) {
5941 __ testl(obj, obj);
5942 __ j(kEqual, &done);
5943 }
5944
5945 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005946 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005947
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005948 // We always go into the type check slow path for the unresolved
5949 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950 //
5951 // We cannot directly call the CheckCast runtime entry point
5952 // without resorting to a type checking slow path here (i.e. by
5953 // calling InvokeRuntime directly), as it would require to
5954 // assign fixed registers for the inputs of this HInstanceOf
5955 // instruction (following the runtime calling convention), which
5956 // might be cluttered by the potential first read barrier
5957 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005958 //
5959 // TODO: Introduce a new runtime entry point taking the object
5960 // to test (instead of its class) as argument, and let it deal
5961 // with the read barrier issues. This will let us refactor this
5962 // case of the `switch` code as it was previously (with a direct
5963 // call to the runtime not using a type checking slow path).
5964 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005966 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967 break;
5968 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005969
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005971}
5972
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005973void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5974 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005975 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005976 InvokeRuntimeCallingConvention calling_convention;
5977 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5978}
5979
5980void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005981 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01005982 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01005983 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005984 if (instruction->IsEnter()) {
5985 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5986 } else {
5987 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5988 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005989}
5990
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005991void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5992void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5993void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5994
5995void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5996 LocationSummary* locations =
5997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5998 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5999 || instruction->GetResultType() == Primitive::kPrimLong);
6000 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006001 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006002 locations->SetOut(Location::SameAsFirstInput());
6003}
6004
6005void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6006 HandleBitwiseOperation(instruction);
6007}
6008
6009void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6010 HandleBitwiseOperation(instruction);
6011}
6012
6013void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6014 HandleBitwiseOperation(instruction);
6015}
6016
6017void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6018 LocationSummary* locations = instruction->GetLocations();
6019 Location first = locations->InAt(0);
6020 Location second = locations->InAt(1);
6021 DCHECK(first.Equals(locations->Out()));
6022
6023 if (instruction->GetResultType() == Primitive::kPrimInt) {
6024 if (second.IsRegister()) {
6025 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006026 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006027 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006028 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006029 } else {
6030 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006031 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006032 }
6033 } else if (second.IsConstant()) {
6034 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6035 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006036 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006037 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006038 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006039 } else {
6040 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006041 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006042 }
6043 } else {
6044 Address address(CpuRegister(RSP), second.GetStackIndex());
6045 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006046 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006047 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006048 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006049 } else {
6050 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006051 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006052 }
6053 }
6054 } else {
6055 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006056 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6057 bool second_is_constant = false;
6058 int64_t value = 0;
6059 if (second.IsConstant()) {
6060 second_is_constant = true;
6061 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006062 }
Mark Mendell40741f32015-04-20 22:10:34 -04006063 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006064
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006065 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006066 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006067 if (is_int32_value) {
6068 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6069 } else {
6070 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6071 }
6072 } else if (second.IsDoubleStackSlot()) {
6073 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006074 } else {
6075 __ andq(first_reg, second.AsRegister<CpuRegister>());
6076 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006077 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006078 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006079 if (is_int32_value) {
6080 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6081 } else {
6082 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6083 }
6084 } else if (second.IsDoubleStackSlot()) {
6085 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006086 } else {
6087 __ orq(first_reg, second.AsRegister<CpuRegister>());
6088 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006089 } else {
6090 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006091 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006092 if (is_int32_value) {
6093 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6094 } else {
6095 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6096 }
6097 } else if (second.IsDoubleStackSlot()) {
6098 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006099 } else {
6100 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6101 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006102 }
6103 }
6104}
6105
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006106void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6107 Location out,
6108 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006109 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006110 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6111 if (kEmitCompilerReadBarrier) {
6112 if (kUseBakerReadBarrier) {
6113 // Load with fast path based Baker's read barrier.
6114 // /* HeapReference<Object> */ out = *(out + offset)
6115 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006116 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006117 } else {
6118 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006119 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006120 // in the following move operation, as we will need it for the
6121 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006122 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006123 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006124 // /* HeapReference<Object> */ out = *(out + offset)
6125 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006126 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006127 }
6128 } else {
6129 // Plain load with no read barrier.
6130 // /* HeapReference<Object> */ out = *(out + offset)
6131 __ movl(out_reg, Address(out_reg, offset));
6132 __ MaybeUnpoisonHeapReference(out_reg);
6133 }
6134}
6135
6136void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6137 Location out,
6138 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006139 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006140 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6141 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6142 if (kEmitCompilerReadBarrier) {
6143 if (kUseBakerReadBarrier) {
6144 // Load with fast path based Baker's read barrier.
6145 // /* HeapReference<Object> */ out = *(obj + offset)
6146 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006147 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006148 } else {
6149 // Load with slow path based read barrier.
6150 // /* HeapReference<Object> */ out = *(obj + offset)
6151 __ movl(out_reg, Address(obj_reg, offset));
6152 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6153 }
6154 } else {
6155 // Plain load with no read barrier.
6156 // /* HeapReference<Object> */ out = *(obj + offset)
6157 __ movl(out_reg, Address(obj_reg, offset));
6158 __ MaybeUnpoisonHeapReference(out_reg);
6159 }
6160}
6161
6162void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6163 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006164 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006165 Label* fixup_label,
6166 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006167 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006168 if (requires_read_barrier) {
6169 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006170 if (kUseBakerReadBarrier) {
6171 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6172 // Baker's read barrier are used:
6173 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006174 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006175 // if (Thread::Current()->GetIsGcMarking()) {
6176 // root = ReadBarrier::Mark(root)
6177 // }
6178
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006179 // /* GcRoot<mirror::Object> */ root = *address
6180 __ movl(root_reg, address);
6181 if (fixup_label != nullptr) {
6182 __ Bind(fixup_label);
6183 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006184 static_assert(
6185 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6186 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6187 "have different sizes.");
6188 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6189 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6190 "have different sizes.");
6191
Vladimir Marko953437b2016-08-24 08:30:46 +00006192 // Slow path marking the GC root `root`.
6193 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6194 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006195 codegen_->AddSlowPath(slow_path);
6196
Andreas Gampe542451c2016-07-26 09:02:02 -07006197 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006198 /* no_rip */ true),
6199 Immediate(0));
6200 __ j(kNotEqual, slow_path->GetEntryLabel());
6201 __ Bind(slow_path->GetExitLabel());
6202 } else {
6203 // GC root loaded through a slow path for read barriers other
6204 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006205 // /* GcRoot<mirror::Object>* */ root = address
6206 __ leaq(root_reg, address);
6207 if (fixup_label != nullptr) {
6208 __ Bind(fixup_label);
6209 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006210 // /* mirror::Object* */ root = root->Read()
6211 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6212 }
6213 } else {
6214 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006215 // /* GcRoot<mirror::Object> */ root = *address
6216 __ movl(root_reg, address);
6217 if (fixup_label != nullptr) {
6218 __ Bind(fixup_label);
6219 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006220 // Note that GC roots are not affected by heap poisoning, thus we
6221 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006222 }
6223}
6224
6225void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6226 Location ref,
6227 CpuRegister obj,
6228 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006229 bool needs_null_check) {
6230 DCHECK(kEmitCompilerReadBarrier);
6231 DCHECK(kUseBakerReadBarrier);
6232
6233 // /* HeapReference<Object> */ ref = *(obj + offset)
6234 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006235 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006236}
6237
6238void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6239 Location ref,
6240 CpuRegister obj,
6241 uint32_t data_offset,
6242 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006243 bool needs_null_check) {
6244 DCHECK(kEmitCompilerReadBarrier);
6245 DCHECK(kUseBakerReadBarrier);
6246
Roland Levillain3d312422016-06-23 13:53:42 +01006247 static_assert(
6248 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6249 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006250 // /* HeapReference<Object> */ ref =
6251 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006252 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006253 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006254}
6255
6256void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6257 Location ref,
6258 CpuRegister obj,
6259 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006260 bool needs_null_check) {
6261 DCHECK(kEmitCompilerReadBarrier);
6262 DCHECK(kUseBakerReadBarrier);
6263
6264 // In slow path based read barriers, the read barrier call is
6265 // inserted after the original load. However, in fast path based
6266 // Baker's read barriers, we need to perform the load of
6267 // mirror::Object::monitor_ *before* the original reference load.
6268 // This load-load ordering is required by the read barrier.
6269 // The fast path/slow path (for Baker's algorithm) should look like:
6270 //
6271 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6272 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6273 // HeapReference<Object> ref = *src; // Original reference load.
6274 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6275 // if (is_gray) {
6276 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6277 // }
6278 //
6279 // Note: the original implementation in ReadBarrier::Barrier is
6280 // slightly more complex as:
6281 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006282 // the high-bits of rb_state, which are expected to be all zeroes
6283 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6284 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006285 // - it performs additional checks that we do not do here for
6286 // performance reasons.
6287
6288 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006289 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6290
Vladimir Marko953437b2016-08-24 08:30:46 +00006291 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6292 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6293 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6294 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6295 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6296 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6297 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6298
6299 // if (rb_state == ReadBarrier::gray_ptr_)
6300 // ref = ReadBarrier::Mark(ref);
6301 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6302 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006303 if (needs_null_check) {
6304 MaybeRecordImplicitNullCheck(instruction);
6305 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006306
6307 // Load fence to prevent load-load reordering.
6308 // Note that this is a no-op, thanks to the x86-64 memory model.
6309 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6310
6311 // The actual reference load.
6312 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006313 __ movl(ref_reg, src); // Flags are unaffected.
6314
6315 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6316 // Slow path marking the object `ref` when it is gray.
6317 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6318 instruction, ref, /* unpoison */ true);
6319 AddSlowPath(slow_path);
6320
6321 // We have done the "if" of the gray bit check above, now branch based on the flags.
6322 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006323
6324 // Object* ref = ref_addr->AsMirrorPtr()
6325 __ MaybeUnpoisonHeapReference(ref_reg);
6326
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006327 __ Bind(slow_path->GetExitLabel());
6328}
6329
6330void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6331 Location out,
6332 Location ref,
6333 Location obj,
6334 uint32_t offset,
6335 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 DCHECK(kEmitCompilerReadBarrier);
6337
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006338 // Insert a slow path based read barrier *after* the reference load.
6339 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006340 // If heap poisoning is enabled, the unpoisoning of the loaded
6341 // reference will be carried out by the runtime within the slow
6342 // path.
6343 //
6344 // Note that `ref` currently does not get unpoisoned (when heap
6345 // poisoning is enabled), which is alright as the `ref` argument is
6346 // not used by the artReadBarrierSlow entry point.
6347 //
6348 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6349 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6350 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6351 AddSlowPath(slow_path);
6352
Roland Levillain0d5a2812015-11-13 10:07:31 +00006353 __ jmp(slow_path->GetEntryLabel());
6354 __ Bind(slow_path->GetExitLabel());
6355}
6356
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006357void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6358 Location out,
6359 Location ref,
6360 Location obj,
6361 uint32_t offset,
6362 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006364 // Baker's read barriers shall be handled by the fast path
6365 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6366 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006367 // If heap poisoning is enabled, unpoisoning will be taken care of
6368 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006369 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006370 } else if (kPoisonHeapReferences) {
6371 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6372 }
6373}
6374
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006375void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6376 Location out,
6377 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006378 DCHECK(kEmitCompilerReadBarrier);
6379
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006380 // Insert a slow path based read barrier *after* the GC root load.
6381 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 // Note that GC roots are not affected by heap poisoning, so we do
6383 // not need to do anything special for this here.
6384 SlowPathCode* slow_path =
6385 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6386 AddSlowPath(slow_path);
6387
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388 __ jmp(slow_path->GetEntryLabel());
6389 __ Bind(slow_path->GetExitLabel());
6390}
6391
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006392void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006393 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006394 LOG(FATAL) << "Unreachable";
6395}
6396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006397void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006398 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006399 LOG(FATAL) << "Unreachable";
6400}
6401
Mark Mendellfe57faa2015-09-18 09:26:15 -04006402// Simple implementation of packed switch - generate cascaded compare/jumps.
6403void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6404 LocationSummary* locations =
6405 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6406 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006407 locations->AddTemp(Location::RequiresRegister());
6408 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006409}
6410
6411void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6412 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006413 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006414 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006415 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6416 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6417 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006418 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6419
6420 // Should we generate smaller inline compare/jumps?
6421 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6422 // Figure out the correct compare values and jump conditions.
6423 // Handle the first compare/branch as a special case because it might
6424 // jump to the default case.
6425 DCHECK_GT(num_entries, 2u);
6426 Condition first_condition;
6427 uint32_t index;
6428 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6429 if (lower_bound != 0) {
6430 first_condition = kLess;
6431 __ cmpl(value_reg_in, Immediate(lower_bound));
6432 __ j(first_condition, codegen_->GetLabelOf(default_block));
6433 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6434
6435 index = 1;
6436 } else {
6437 // Handle all the compare/jumps below.
6438 first_condition = kBelow;
6439 index = 0;
6440 }
6441
6442 // Handle the rest of the compare/jumps.
6443 for (; index + 1 < num_entries; index += 2) {
6444 int32_t compare_to_value = lower_bound + index + 1;
6445 __ cmpl(value_reg_in, Immediate(compare_to_value));
6446 // Jump to successors[index] if value < case_value[index].
6447 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6448 // Jump to successors[index + 1] if value == case_value[index + 1].
6449 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6450 }
6451
6452 if (index != num_entries) {
6453 // There are an odd number of entries. Handle the last one.
6454 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006455 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006456 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6457 }
6458
6459 // And the default for any other value.
6460 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6461 __ jmp(codegen_->GetLabelOf(default_block));
6462 }
6463 return;
6464 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006465
6466 // Remove the bias, if needed.
6467 Register value_reg_out = value_reg_in.AsRegister();
6468 if (lower_bound != 0) {
6469 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6470 value_reg_out = temp_reg.AsRegister();
6471 }
6472 CpuRegister value_reg(value_reg_out);
6473
6474 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006475 __ cmpl(value_reg, Immediate(num_entries - 1));
6476 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006477
Mark Mendell9c86b482015-09-18 13:36:07 -04006478 // We are in the range of the table.
6479 // Load the address of the jump table in the constant area.
6480 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006481
Mark Mendell9c86b482015-09-18 13:36:07 -04006482 // Load the (signed) offset from the jump table.
6483 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6484
6485 // Add the offset to the address of the table base.
6486 __ addq(temp_reg, base_reg);
6487
6488 // And jump.
6489 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006490}
6491
Aart Bikc5d47542016-01-27 17:00:35 -08006492void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6493 if (value == 0) {
6494 __ xorl(dest, dest);
6495 } else {
6496 __ movl(dest, Immediate(value));
6497 }
6498}
6499
Mark Mendell92e83bf2015-05-07 11:25:03 -04006500void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6501 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006502 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006503 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006504 } else if (IsUint<32>(value)) {
6505 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006506 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6507 } else {
6508 __ movq(dest, Immediate(value));
6509 }
6510}
6511
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006512void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6513 if (value == 0) {
6514 __ xorps(dest, dest);
6515 } else {
6516 __ movss(dest, LiteralInt32Address(value));
6517 }
6518}
6519
6520void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6521 if (value == 0) {
6522 __ xorpd(dest, dest);
6523 } else {
6524 __ movsd(dest, LiteralInt64Address(value));
6525 }
6526}
6527
6528void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6529 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6530}
6531
6532void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6533 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6534}
6535
Aart Bika19616e2016-02-01 18:57:58 -08006536void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6537 if (value == 0) {
6538 __ testl(dest, dest);
6539 } else {
6540 __ cmpl(dest, Immediate(value));
6541 }
6542}
6543
6544void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6545 if (IsInt<32>(value)) {
6546 if (value == 0) {
6547 __ testq(dest, dest);
6548 } else {
6549 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6550 }
6551 } else {
6552 // Value won't fit in an int.
6553 __ cmpq(dest, LiteralInt64Address(value));
6554 }
6555}
6556
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006557void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6558 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006559 GenerateIntCompare(lhs_reg, rhs);
6560}
6561
6562void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006563 if (rhs.IsConstant()) {
6564 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006565 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006566 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006567 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006568 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006569 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006570 }
6571}
6572
6573void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6574 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6575 if (rhs.IsConstant()) {
6576 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6577 Compare64BitValue(lhs_reg, value);
6578 } else if (rhs.IsDoubleStackSlot()) {
6579 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6580 } else {
6581 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6582 }
6583}
6584
6585Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6586 Location index,
6587 ScaleFactor scale,
6588 uint32_t data_offset) {
6589 return index.IsConstant() ?
6590 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6591 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6592}
6593
Mark Mendellcfa410b2015-05-25 16:02:44 -04006594void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6595 DCHECK(dest.IsDoubleStackSlot());
6596 if (IsInt<32>(value)) {
6597 // Can move directly as an int32 constant.
6598 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6599 Immediate(static_cast<int32_t>(value)));
6600 } else {
6601 Load64BitValue(CpuRegister(TMP), value);
6602 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6603 }
6604}
6605
Mark Mendell9c86b482015-09-18 13:36:07 -04006606/**
6607 * Class to handle late fixup of offsets into constant area.
6608 */
6609class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6610 public:
6611 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6612 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6613
6614 protected:
6615 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6616
6617 CodeGeneratorX86_64* codegen_;
6618
6619 private:
6620 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6621 // Patch the correct offset for the instruction. We use the address of the
6622 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6623 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6624 int32_t relative_position = constant_offset - pos;
6625
6626 // Patch in the right value.
6627 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6628 }
6629
6630 // Location in constant area that the fixup refers to.
6631 size_t offset_into_constant_area_;
6632};
6633
6634/**
6635 t * Class to handle late fixup of offsets to a jump table that will be created in the
6636 * constant area.
6637 */
6638class JumpTableRIPFixup : public RIPFixup {
6639 public:
6640 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6641 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6642
6643 void CreateJumpTable() {
6644 X86_64Assembler* assembler = codegen_->GetAssembler();
6645
6646 // Ensure that the reference to the jump table has the correct offset.
6647 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6648 SetOffset(offset_in_constant_table);
6649
6650 // Compute the offset from the start of the function to this jump table.
6651 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6652
6653 // Populate the jump table with the correct values for the jump table.
6654 int32_t num_entries = switch_instr_->GetNumEntries();
6655 HBasicBlock* block = switch_instr_->GetBlock();
6656 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6657 // The value that we want is the target offset - the position of the table.
6658 for (int32_t i = 0; i < num_entries; i++) {
6659 HBasicBlock* b = successors[i];
6660 Label* l = codegen_->GetLabelOf(b);
6661 DCHECK(l->IsBound());
6662 int32_t offset_to_block = l->Position() - current_table_offset;
6663 assembler->AppendInt32(offset_to_block);
6664 }
6665 }
6666
6667 private:
6668 const HPackedSwitch* switch_instr_;
6669};
6670
Mark Mendellf55c3e02015-03-26 21:07:46 -04006671void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6672 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006673 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006674 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6675 // 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 -04006676 assembler->Align(4, 0);
6677 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006678
6679 // Populate any jump tables.
6680 for (auto jump_table : fixups_to_jump_tables_) {
6681 jump_table->CreateJumpTable();
6682 }
6683
6684 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006685 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006686 }
6687
6688 // And finish up.
6689 CodeGenerator::Finalize(allocator);
6690}
6691
Mark Mendellf55c3e02015-03-26 21:07:46 -04006692Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6693 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6694 return Address::RIP(fixup);
6695}
6696
6697Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6698 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6699 return Address::RIP(fixup);
6700}
6701
6702Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6703 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6704 return Address::RIP(fixup);
6705}
6706
6707Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6708 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6709 return Address::RIP(fixup);
6710}
6711
Andreas Gampe85b62f22015-09-09 13:15:38 -07006712// TODO: trg as memory.
6713void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6714 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006715 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006716 return;
6717 }
6718
6719 DCHECK_NE(type, Primitive::kPrimVoid);
6720
6721 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6722 if (trg.Equals(return_loc)) {
6723 return;
6724 }
6725
6726 // Let the parallel move resolver take care of all of this.
6727 HParallelMove parallel_move(GetGraph()->GetArena());
6728 parallel_move.AddMove(return_loc, trg, type, nullptr);
6729 GetMoveResolver()->EmitNativeCode(&parallel_move);
6730}
6731
Mark Mendell9c86b482015-09-18 13:36:07 -04006732Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6733 // Create a fixup to be used to create and address the jump table.
6734 JumpTableRIPFixup* table_fixup =
6735 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6736
6737 // We have to populate the jump tables.
6738 fixups_to_jump_tables_.push_back(table_fixup);
6739 return Address::RIP(table_fixup);
6740}
6741
Mark Mendellea5af682015-10-22 17:35:49 -04006742void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6743 const Address& addr_high,
6744 int64_t v,
6745 HInstruction* instruction) {
6746 if (IsInt<32>(v)) {
6747 int32_t v_32 = v;
6748 __ movq(addr_low, Immediate(v_32));
6749 MaybeRecordImplicitNullCheck(instruction);
6750 } else {
6751 // Didn't fit in a register. Do it in pieces.
6752 int32_t low_v = Low32Bits(v);
6753 int32_t high_v = High32Bits(v);
6754 __ movl(addr_low, Immediate(low_v));
6755 MaybeRecordImplicitNullCheck(instruction);
6756 __ movl(addr_high, Immediate(high_v));
6757 }
6758}
6759
Roland Levillain4d027112015-07-01 15:41:14 +01006760#undef __
6761
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006762} // namespace x86_64
6763} // namespace art