blob: cac33cddb8b942d6e6f4df216416b2459ddc2da4 [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
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070054// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, 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 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000069 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
70 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());
David Brazdil77a48ae2015-09-15 12:34:04 +000091 if (instruction_->CanThrowIntoCatchBlock()) {
92 // Live registers will be restored in the catch block if caught.
93 SaveLiveRegisters(codegen, instruction_->GetLocations());
94 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000095 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
96 instruction_,
97 instruction_->GetDexPc(),
98 this);
Roland Levillain888d0672015-11-23 18:53:50 +000099 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000100 }
101
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 bool IsFatal() const OVERRIDE { return true; }
103
Alexandre Rames9931f312015-06-19 14:47:01 +0100104 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
105
Calin Juravled0d48522014-11-04 16:40:20 +0000106 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
113 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000114
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000116 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negl(cpu_reg_);
120 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400121 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 }
123
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 } else {
125 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negq(cpu_reg_);
128 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400129 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000130 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 }
Calin Juravled0d48522014-11-04 16:40:20 +0000132 __ jmp(GetExitLabel());
133 }
134
Alexandre Rames9931f312015-06-19 14:47:01 +0100135 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
136
Calin Juravled0d48522014-11-04 16:40:20 +0000137 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000139 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 const bool is_div_;
141 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000142};
143
Andreas Gampe85b62f22015-09-09 13:15:38 -0700144class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100146 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000153 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
154 instruction_,
155 instruction_->GetDexPc(),
156 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000157 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000158 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 if (successor_ == nullptr) {
160 __ jmp(GetReturnLabel());
161 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000162 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 }
165
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 Label* GetReturnLabel() {
167 DCHECK(successor_ == nullptr);
168 return &return_label_;
169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100171 HBasicBlock* GetSuccessor() const {
172 return successor_;
173 }
174
Alexandre Rames9931f312015-06-19 14:47:01 +0100175 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
176
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000179 Label return_label_;
180
181 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
182};
183
Andreas Gampe85b62f22015-09-09 13:15:38 -0700184class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100186 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000187 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100190 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000191 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000193 if (instruction_->CanThrowIntoCatchBlock()) {
194 // Live registers will be restored in the catch block if caught.
195 SaveLiveRegisters(codegen, instruction_->GetLocations());
196 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 // We're moving two locations to locations that could overlap, so we need a parallel
198 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000200 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100201 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000202 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100203 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100205 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
206 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100207 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
208 ? QUICK_ENTRY_POINT(pThrowStringBounds)
209 : QUICK_ENTRY_POINT(pThrowArrayBounds);
210 x86_64_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000211 instruction_,
212 instruction_->GetDexPc(),
213 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100214 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000215 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100216 }
217
Alexandre Rames8158f282015-08-07 10:26:17 +0100218 bool IsFatal() const OVERRIDE { return true; }
219
Alexandre Rames9931f312015-06-19 14:47:01 +0100220 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
221
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100222 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100223 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
224};
225
Andreas Gampe85b62f22015-09-09 13:15:38 -0700226class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 LoadClassSlowPathX86_64(HLoadClass* cls,
229 HInstruction* at,
230 uint32_t dex_pc,
231 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000232 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
234 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000238 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000241 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000245 x86_64_codegen->InvokeRuntime(do_clinit_ ?
246 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
247 QUICK_ENTRY_POINT(pInitializeType),
248 at_,
249 dex_pc_,
250 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000251 if (do_clinit_) {
252 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
253 } else {
254 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
255 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100256
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 if (out.IsValid()) {
260 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000261 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 }
263
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100265 __ jmp(GetExitLabel());
266 }
267
Alexandre Rames9931f312015-06-19 14:47:01 +0100268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
269
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100270 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 // The class this slow path will load.
272 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100273
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 // The instruction where this slow path is happening.
275 // (Might be the load class or an initialization check).
276 HInstruction* const at_;
277
278 // The dex PC of `at_`.
279 const uint32_t dex_pc_;
280
281 // Whether to initialize the class.
282 const bool do_clinit_;
283
284 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285};
286
Andreas Gampe85b62f22015-09-09 13:15:38 -0700287class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000289 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000290
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 LocationSummary* locations = instruction_->GetLocations();
293 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
294
Roland Levillain0d5a2812015-11-13 10:07:31 +0000295 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000296 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000298
299 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000300 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
301 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000302 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
303 instruction_,
304 instruction_->GetDexPc(),
305 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000306 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000307 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000308 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000309 __ jmp(GetExitLabel());
310 }
311
Alexandre Rames9931f312015-06-19 14:47:01 +0100312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
313
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000314 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000315 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
316};
317
Andreas Gampe85b62f22015-09-09 13:15:38 -0700318class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000320 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
326 : locations->Out();
327 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 DCHECK(instruction_->IsCheckCast()
329 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
Roland Levillain0d5a2812015-11-13 10:07:31 +0000331 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 if (!is_fatal_) {
335 SaveLiveRegisters(codegen, locations);
336 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 // We're moving two locations to locations that could overlap, so we need a parallel
339 // move resolver.
340 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000341 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100342 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100344 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100345 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100346 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
347 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000350 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
351 instruction_,
352 dex_pc,
353 this);
354 CheckEntrypointTypes<
355 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 } else {
357 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000358 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
359 instruction_,
360 dex_pc,
361 this);
362 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 if (!is_fatal_) {
366 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000367 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 RestoreLiveRegisters(codegen, locations);
371 __ jmp(GetExitLabel());
372 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 bool IsFatal() const OVERRIDE { return is_fatal_; }
378
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000380 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381
382 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
383};
384
Andreas Gampe85b62f22015-09-09 13:15:38 -0700385class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 public:
Aart Bik42249c32016-01-07 15:33:50 -0800387 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000391 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000394 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800395 instruction_,
396 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000398 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 }
400
Alexandre Rames9931f312015-06-19 14:47:01 +0100401 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
402
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
405};
406
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407class ArraySetSlowPathX86_64 : public SlowPathCode {
408 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000409 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100410
411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
412 LocationSummary* locations = instruction_->GetLocations();
413 __ Bind(GetEntryLabel());
414 SaveLiveRegisters(codegen, locations);
415
416 InvokeRuntimeCallingConvention calling_convention;
417 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
418 parallel_move.AddMove(
419 locations->InAt(0),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
421 Primitive::kPrimNot,
422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(1),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
426 Primitive::kPrimInt,
427 nullptr);
428 parallel_move.AddMove(
429 locations->InAt(2),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
431 Primitive::kPrimNot,
432 nullptr);
433 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
434
Roland Levillain0d5a2812015-11-13 10:07:31 +0000435 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
436 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
437 instruction_,
438 instruction_->GetDexPc(),
439 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000440 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 RestoreLiveRegisters(codegen, locations);
442 __ jmp(GetExitLabel());
443 }
444
445 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
446
447 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100448 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
449};
450
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000451// Slow path marking an object during a read barrier.
452class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
453 public:
454 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000455 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000456 DCHECK(kEmitCompilerReadBarrier);
457 }
458
459 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
460
461 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
462 LocationSummary* locations = instruction_->GetLocations();
463 Register reg_out = out_.AsRegister<Register>();
464 DCHECK(locations->CanCall());
465 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
466 DCHECK(instruction_->IsInstanceFieldGet() ||
467 instruction_->IsStaticFieldGet() ||
468 instruction_->IsArrayGet() ||
469 instruction_->IsLoadClass() ||
470 instruction_->IsLoadString() ||
471 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100472 instruction_->IsCheckCast() ||
473 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
474 instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000475 << "Unexpected instruction in read barrier marking slow path: "
476 << instruction_->DebugName();
477
478 __ Bind(GetEntryLabel());
479 SaveLiveRegisters(codegen, locations);
480
481 InvokeRuntimeCallingConvention calling_convention;
482 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
483 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
484 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
485 instruction_,
486 instruction_->GetDexPc(),
487 this);
488 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
489 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
490
491 RestoreLiveRegisters(codegen, locations);
492 __ jmp(GetExitLabel());
493 }
494
495 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000496 const Location out_;
497 const Location obj_;
498
499 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
500};
501
Roland Levillain0d5a2812015-11-13 10:07:31 +0000502// Slow path generating a read barrier for a heap reference.
503class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
504 public:
505 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
506 Location out,
507 Location ref,
508 Location obj,
509 uint32_t offset,
510 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000511 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000512 out_(out),
513 ref_(ref),
514 obj_(obj),
515 offset_(offset),
516 index_(index) {
517 DCHECK(kEmitCompilerReadBarrier);
518 // If `obj` is equal to `out` or `ref`, it means the initial
519 // object has been overwritten by (or after) the heap object
520 // reference load to be instrumented, e.g.:
521 //
522 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000523 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000524 //
525 // In that case, we have lost the information about the original
526 // object, and the emitted read barrier cannot work properly.
527 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
528 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
529}
530
531 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
532 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
533 LocationSummary* locations = instruction_->GetLocations();
534 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
535 DCHECK(locations->CanCall());
536 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100537 DCHECK(instruction_->IsInstanceFieldGet() ||
538 instruction_->IsStaticFieldGet() ||
539 instruction_->IsArrayGet() ||
540 instruction_->IsInstanceOf() ||
541 instruction_->IsCheckCast() ||
542 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000543 instruction_->GetLocations()->Intrinsified()))
544 << "Unexpected instruction in read barrier for heap reference slow path: "
545 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000546
547 __ Bind(GetEntryLabel());
548 SaveLiveRegisters(codegen, locations);
549
550 // We may have to change the index's value, but as `index_` is a
551 // constant member (like other "inputs" of this slow path),
552 // introduce a copy of it, `index`.
553 Location index = index_;
554 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100555 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000556 if (instruction_->IsArrayGet()) {
557 // Compute real offset and store it in index_.
558 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
559 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
560 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
561 // We are about to change the value of `index_reg` (see the
562 // calls to art::x86_64::X86_64Assembler::shll and
563 // art::x86_64::X86_64Assembler::AddImmediate below), but it
564 // has not been saved by the previous call to
565 // art::SlowPathCode::SaveLiveRegisters, as it is a
566 // callee-save register --
567 // art::SlowPathCode::SaveLiveRegisters does not consider
568 // callee-save registers, as it has been designed with the
569 // assumption that callee-save registers are supposed to be
570 // handled by the called function. So, as a callee-save
571 // register, `index_reg` _would_ eventually be saved onto
572 // the stack, but it would be too late: we would have
573 // changed its value earlier. Therefore, we manually save
574 // it here into another freely available register,
575 // `free_reg`, chosen of course among the caller-save
576 // registers (as a callee-save `free_reg` register would
577 // exhibit the same problem).
578 //
579 // Note we could have requested a temporary register from
580 // the register allocator instead; but we prefer not to, as
581 // this is a slow path, and we know we can find a
582 // caller-save register that is available.
583 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
584 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
585 index_reg = free_reg;
586 index = Location::RegisterLocation(index_reg);
587 } else {
588 // The initial register stored in `index_` has already been
589 // saved in the call to art::SlowPathCode::SaveLiveRegisters
590 // (as it is not a callee-save register), so we can freely
591 // use it.
592 }
593 // Shifting the index value contained in `index_reg` by the
594 // scale factor (2) cannot overflow in practice, as the
595 // runtime is unable to allocate object arrays with a size
596 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
597 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
598 static_assert(
599 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
600 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
601 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
602 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100603 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
604 // intrinsics, `index_` is not shifted by a scale factor of 2
605 // (as in the case of ArrayGet), as it is actually an offset
606 // to an object field within an object.
607 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000608 DCHECK(instruction_->GetLocations()->Intrinsified());
609 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
610 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
611 << instruction_->AsInvoke()->GetIntrinsic();
612 DCHECK_EQ(offset_, 0U);
613 DCHECK(index_.IsRegister());
614 }
615 }
616
617 // We're moving two or three locations to locations that could
618 // overlap, so we need a parallel move resolver.
619 InvokeRuntimeCallingConvention calling_convention;
620 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
621 parallel_move.AddMove(ref_,
622 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
623 Primitive::kPrimNot,
624 nullptr);
625 parallel_move.AddMove(obj_,
626 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
627 Primitive::kPrimNot,
628 nullptr);
629 if (index.IsValid()) {
630 parallel_move.AddMove(index,
631 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
632 Primitive::kPrimInt,
633 nullptr);
634 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
635 } else {
636 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
637 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
638 }
639 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
640 instruction_,
641 instruction_->GetDexPc(),
642 this);
643 CheckEntrypointTypes<
644 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
645 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
646
647 RestoreLiveRegisters(codegen, locations);
648 __ jmp(GetExitLabel());
649 }
650
651 const char* GetDescription() const OVERRIDE {
652 return "ReadBarrierForHeapReferenceSlowPathX86_64";
653 }
654
655 private:
656 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
657 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
658 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
659 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
660 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
661 return static_cast<CpuRegister>(i);
662 }
663 }
664 // We shall never fail to find a free caller-save register, as
665 // there are more than two core caller-save registers on x86-64
666 // (meaning it is possible to find one which is different from
667 // `ref` and `obj`).
668 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
669 LOG(FATAL) << "Could not find a free caller-save register";
670 UNREACHABLE();
671 }
672
Roland Levillain0d5a2812015-11-13 10:07:31 +0000673 const Location out_;
674 const Location ref_;
675 const Location obj_;
676 const uint32_t offset_;
677 // An additional location containing an index to an array.
678 // Only used for HArrayGet and the UnsafeGetObject &
679 // UnsafeGetObjectVolatile intrinsics.
680 const Location index_;
681
682 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
683};
684
685// Slow path generating a read barrier for a GC root.
686class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
687 public:
688 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000689 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000690 DCHECK(kEmitCompilerReadBarrier);
691 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692
693 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
694 LocationSummary* locations = instruction_->GetLocations();
695 DCHECK(locations->CanCall());
696 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000697 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
698 << "Unexpected instruction in read barrier for GC root slow path: "
699 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700
701 __ Bind(GetEntryLabel());
702 SaveLiveRegisters(codegen, locations);
703
704 InvokeRuntimeCallingConvention calling_convention;
705 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
706 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
707 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
708 instruction_,
709 instruction_->GetDexPc(),
710 this);
711 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
712 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
713
714 RestoreLiveRegisters(codegen, locations);
715 __ jmp(GetExitLabel());
716 }
717
718 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
719
720 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721 const Location out_;
722 const Location root_;
723
724 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
725};
726
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100727#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700728// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
729#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100730
Roland Levillain4fa13f62015-07-06 18:11:54 +0100731inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700732 switch (cond) {
733 case kCondEQ: return kEqual;
734 case kCondNE: return kNotEqual;
735 case kCondLT: return kLess;
736 case kCondLE: return kLessEqual;
737 case kCondGT: return kGreater;
738 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700739 case kCondB: return kBelow;
740 case kCondBE: return kBelowEqual;
741 case kCondA: return kAbove;
742 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700743 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 LOG(FATAL) << "Unreachable";
745 UNREACHABLE();
746}
747
Aart Bike9f37602015-10-09 11:15:55 -0700748// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100749inline Condition X86_64FPCondition(IfCondition cond) {
750 switch (cond) {
751 case kCondEQ: return kEqual;
752 case kCondNE: return kNotEqual;
753 case kCondLT: return kBelow;
754 case kCondLE: return kBelowEqual;
755 case kCondGT: return kAbove;
756 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700757 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100758 };
759 LOG(FATAL) << "Unreachable";
760 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700761}
762
Vladimir Markodc151b22015-10-15 18:02:30 +0100763HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
764 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
765 MethodReference target_method ATTRIBUTE_UNUSED) {
766 switch (desired_dispatch_info.code_ptr_location) {
767 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
768 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
769 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
770 return HInvokeStaticOrDirect::DispatchInfo {
771 desired_dispatch_info.method_load_kind,
772 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
773 desired_dispatch_info.method_load_data,
774 0u
775 };
776 default:
777 return desired_dispatch_info;
778 }
779}
780
Serguei Katkov288c7a82016-05-16 11:53:15 +0600781Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
782 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800783 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000784 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
785 switch (invoke->GetMethodLoadKind()) {
786 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
787 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000788 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000789 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000790 break;
791 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000792 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000793 break;
794 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
795 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
796 break;
797 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
798 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
799 method_patches_.emplace_back(invoke->GetTargetMethod());
800 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
801 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000802 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000803 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000804 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000805 // Bind a new fixup label at the end of the "movl" insn.
806 uint32_t offset = invoke->GetDexCacheArrayOffset();
807 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000808 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000809 }
Vladimir Marko58155012015-08-19 12:49:41 +0000810 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000811 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000812 Register method_reg;
813 CpuRegister reg = temp.AsRegister<CpuRegister>();
814 if (current_method.IsRegister()) {
815 method_reg = current_method.AsRegister<Register>();
816 } else {
817 DCHECK(invoke->GetLocations()->Intrinsified());
818 DCHECK(!current_method.IsValid());
819 method_reg = reg.AsRegister();
820 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
821 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100823 __ movq(reg,
824 Address(CpuRegister(method_reg),
825 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100826 // temp = temp[index_in_cache];
827 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
828 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000829 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
830 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100831 }
Vladimir Marko58155012015-08-19 12:49:41 +0000832 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600833 return callee_method;
834}
835
836void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
837 Location temp) {
838 // All registers are assumed to be correctly set up.
839 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000840
841 switch (invoke->GetCodePtrLocation()) {
842 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
843 __ call(&frame_entry_label_);
844 break;
845 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
846 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
847 Label* label = &relative_call_patches_.back().label;
848 __ call(label); // Bind to the patch label, override at link time.
849 __ Bind(label); // Bind the label at the end of the "call" insn.
850 break;
851 }
852 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
853 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100854 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
855 LOG(FATAL) << "Unsupported";
856 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000857 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
858 // (callee_method + offset_of_quick_compiled_code)()
859 __ call(Address(callee_method.AsRegister<CpuRegister>(),
860 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
861 kX86_64WordSize).SizeValue()));
862 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000863 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800864
865 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800866}
867
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000868void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
869 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
870 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
871 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000872
873 // Use the calling convention instead of the location of the receiver, as
874 // intrinsics may have put the receiver in a different register. In the intrinsics
875 // slow path, the arguments have been moved to the right place, so here we are
876 // guaranteed that the receiver is the first register of the calling convention.
877 InvokeDexCallingConvention calling_convention;
878 Register receiver = calling_convention.GetRegisterAt(0);
879
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000880 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000881 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000882 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000883 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000884 // Instead of simply (possibly) unpoisoning `temp` here, we should
885 // emit a read barrier for the previous class reference load.
886 // However this is not required in practice, as this is an
887 // intermediate/temporary reference and because the current
888 // concurrent copying collector keeps the from-space memory
889 // intact/accessible until the end of the marking phase (the
890 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000891 __ MaybeUnpoisonHeapReference(temp);
892 // temp = temp->GetMethodAt(method_offset);
893 __ movq(temp, Address(temp, method_offset));
894 // call temp->GetEntryPoint();
895 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
896 kX86_64WordSize).SizeValue()));
897}
898
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000899void CodeGeneratorX86_64::RecordSimplePatch() {
900 if (GetCompilerOptions().GetIncludePatchInformation()) {
901 simple_patches_.emplace_back();
902 __ Bind(&simple_patches_.back());
903 }
904}
905
906void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
907 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
908 __ Bind(&string_patches_.back().label);
909}
910
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100911void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
912 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
913 __ Bind(&type_patches_.back().label);
914}
915
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000916Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
917 uint32_t element_offset) {
918 // Add a patch entry and return the label.
919 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
920 return &pc_relative_dex_cache_patches_.back().label;
921}
922
Vladimir Marko58155012015-08-19 12:49:41 +0000923void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
924 DCHECK(linker_patches->empty());
925 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000926 method_patches_.size() +
927 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000928 pc_relative_dex_cache_patches_.size() +
929 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100930 string_patches_.size() +
931 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000932 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000933 // The label points to the end of the "movl" insn but the literal offset for method
934 // patch needs to point to the embedded constant which occupies the last 4 bytes.
935 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000936 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000937 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000938 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
939 info.target_method.dex_file,
940 info.target_method.dex_method_index));
941 }
942 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000943 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000944 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
945 info.target_method.dex_file,
946 info.target_method.dex_method_index));
947 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000948 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
949 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000950 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
951 &info.target_dex_file,
952 info.label.Position(),
953 info.element_offset));
954 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000955 for (const Label& label : simple_patches_) {
956 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
957 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
958 }
959 for (const StringPatchInfo<Label>& info : string_patches_) {
960 // These are always PC-relative, see GetSupportedLoadStringKind().
961 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
962 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
963 &info.dex_file,
964 info.label.Position(),
965 info.string_index));
966 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100967 for (const TypePatchInfo<Label>& info : type_patches_) {
968 // These are always PC-relative, see GetSupportedLoadClassKind().
969 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
970 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
971 &info.dex_file,
972 info.label.Position(),
973 info.type_index));
974 }
Vladimir Marko58155012015-08-19 12:49:41 +0000975}
976
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100978 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100979}
980
981void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100982 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983}
984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100985size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
986 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
987 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100988}
989
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100990size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
991 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
992 return kX86_64WordSize;
993}
994
995size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
996 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
997 return kX86_64WordSize;
998}
999
1000size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1001 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1002 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001003}
1004
Calin Juravle175dc732015-08-25 15:42:32 +01001005void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1006 HInstruction* instruction,
1007 uint32_t dex_pc,
1008 SlowPathCode* slow_path) {
1009 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
1010 instruction,
1011 dex_pc,
1012 slow_path);
1013}
1014
1015void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +01001016 HInstruction* instruction,
1017 uint32_t dex_pc,
1018 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001019 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001020 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +01001021 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +01001022}
1023
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001024static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001025// Use a fake return address register to mimic Quick.
1026static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001027CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001028 const X86_64InstructionSetFeatures& isa_features,
1029 const CompilerOptions& compiler_options,
1030 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001031 : CodeGenerator(graph,
1032 kNumberOfCpuRegisters,
1033 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001034 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001035 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1036 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001037 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001038 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1039 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001040 compiler_options,
1041 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001042 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001043 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001044 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001045 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001046 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001047 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001048 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001049 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1050 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001051 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001052 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1053 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001054 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001055 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001056 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1057}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001058
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001059InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1060 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001061 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062 assembler_(codegen->GetAssembler()),
1063 codegen_(codegen) {}
1064
David Brazdil58282f42016-01-14 12:45:10 +00001065void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001066 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001067 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001069 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001070 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071}
1072
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001074 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001075}
David Srbecky9d8606d2015-04-12 09:35:32 +01001076
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001077static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001078 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001079}
1080
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001082 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001083 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001084 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001085 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001086 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001087
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001088 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001089 __ testq(CpuRegister(RAX), Address(
1090 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001091 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001092 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001093
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001094 if (HasEmptyFrame()) {
1095 return;
1096 }
1097
Nicolas Geoffray98893962015-01-21 12:32:32 +00001098 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001099 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001100 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001101 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001102 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1103 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001104 }
1105 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001106
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001107 int adjust = GetFrameSize() - GetCoreSpillSize();
1108 __ subq(CpuRegister(RSP), Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001110 uint32_t xmm_spill_location = GetFpuSpillStart();
1111 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001112
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001113 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1114 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001115 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1116 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1117 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001118 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001119 }
1120
Mathieu Chartiere401d142015-04-22 13:56:20 -07001121 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001122 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123}
1124
1125void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001126 __ cfi().RememberState();
1127 if (!HasEmptyFrame()) {
1128 uint32_t xmm_spill_location = GetFpuSpillStart();
1129 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1130 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1131 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1132 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1133 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1134 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1135 }
1136 }
1137
1138 int adjust = GetFrameSize() - GetCoreSpillSize();
1139 __ addq(CpuRegister(RSP), Immediate(adjust));
1140 __ cfi().AdjustCFAOffset(-adjust);
1141
1142 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1143 Register reg = kCoreCalleeSaves[i];
1144 if (allocated_registers_.ContainsCoreRegister(reg)) {
1145 __ popq(CpuRegister(reg));
1146 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1147 __ cfi().Restore(DWARFReg(reg));
1148 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001149 }
1150 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001151 __ ret();
1152 __ cfi().RestoreState();
1153 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001154}
1155
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001156void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1157 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001158}
1159
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160void CodeGeneratorX86_64::Move(Location destination, Location source) {
1161 if (source.Equals(destination)) {
1162 return;
1163 }
1164 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001165 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001167 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001169 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001170 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001171 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1172 } else if (source.IsConstant()) {
1173 HConstant* constant = source.GetConstant();
1174 if (constant->IsLongConstant()) {
1175 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1176 } else {
1177 Load32BitValue(dest, GetInt32ValueOf(constant));
1178 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001179 } else {
1180 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001181 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 }
1183 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001184 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001185 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001186 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001188 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1189 } else if (source.IsConstant()) {
1190 HConstant* constant = source.GetConstant();
1191 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1192 if (constant->IsFloatConstant()) {
1193 Load32BitValue(dest, static_cast<int32_t>(value));
1194 } else {
1195 Load64BitValue(dest, value);
1196 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001198 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001199 } else {
1200 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001201 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 }
1203 } else if (destination.IsStackSlot()) {
1204 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001206 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 } else if (source.IsFpuRegister()) {
1208 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001210 } else if (source.IsConstant()) {
1211 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001212 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001213 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001214 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001215 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001216 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1217 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218 }
1219 } else {
1220 DCHECK(destination.IsDoubleStackSlot());
1221 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 } else if (source.IsFpuRegister()) {
1225 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001227 } else if (source.IsConstant()) {
1228 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001229 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001230 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001231 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001232 } else {
1233 DCHECK(constant->IsLongConstant());
1234 value = constant->AsLongConstant()->GetValue();
1235 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001236 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001237 } else {
1238 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001239 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1240 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 }
1242 }
1243}
1244
Calin Juravle175dc732015-08-25 15:42:32 +01001245void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1246 DCHECK(location.IsRegister());
1247 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1248}
1249
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250void CodeGeneratorX86_64::MoveLocation(
1251 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1252 Move(dst, src);
1253}
1254
1255void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1256 if (location.IsRegister()) {
1257 locations->AddTemp(location);
1258 } else {
1259 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1260 }
1261}
1262
David Brazdilfc6a86a2015-06-26 10:33:45 +00001263void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001264 DCHECK(!successor->IsExitBlock());
1265
1266 HBasicBlock* block = got->GetBlock();
1267 HInstruction* previous = got->GetPrevious();
1268
1269 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001270 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001271 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1272 return;
1273 }
1274
1275 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1276 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1277 }
1278 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001279 __ jmp(codegen_->GetLabelOf(successor));
1280 }
1281}
1282
David Brazdilfc6a86a2015-06-26 10:33:45 +00001283void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1284 got->SetLocations(nullptr);
1285}
1286
1287void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1288 HandleGoto(got, got->GetSuccessor());
1289}
1290
1291void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1292 try_boundary->SetLocations(nullptr);
1293}
1294
1295void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1296 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1297 if (!successor->IsExitBlock()) {
1298 HandleGoto(try_boundary, successor);
1299 }
1300}
1301
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001302void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1303 exit->SetLocations(nullptr);
1304}
1305
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001306void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001307}
1308
Mark Mendell152408f2015-12-31 12:28:50 -05001309template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001310void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001311 LabelType* true_label,
1312 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001313 if (cond->IsFPConditionTrueIfNaN()) {
1314 __ j(kUnordered, true_label);
1315 } else if (cond->IsFPConditionFalseIfNaN()) {
1316 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001317 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001318 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001319}
1320
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001321void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001322 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001323
Mark Mendellc4701932015-04-10 13:18:51 -04001324 Location left = locations->InAt(0);
1325 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001326 Primitive::Type type = condition->InputAt(0)->GetType();
1327 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001328 case Primitive::kPrimBoolean:
1329 case Primitive::kPrimByte:
1330 case Primitive::kPrimChar:
1331 case Primitive::kPrimShort:
1332 case Primitive::kPrimInt:
1333 case Primitive::kPrimNot: {
1334 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1335 if (right.IsConstant()) {
1336 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1337 if (value == 0) {
1338 __ testl(left_reg, left_reg);
1339 } else {
1340 __ cmpl(left_reg, Immediate(value));
1341 }
1342 } else if (right.IsStackSlot()) {
1343 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1344 } else {
1345 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1346 }
1347 break;
1348 }
Mark Mendellc4701932015-04-10 13:18:51 -04001349 case Primitive::kPrimLong: {
1350 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1351 if (right.IsConstant()) {
1352 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001353 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 } else if (right.IsDoubleStackSlot()) {
1355 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1356 } else {
1357 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1358 }
Mark Mendellc4701932015-04-10 13:18:51 -04001359 break;
1360 }
1361 case Primitive::kPrimFloat: {
1362 if (right.IsFpuRegister()) {
1363 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1364 } else if (right.IsConstant()) {
1365 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1366 codegen_->LiteralFloatAddress(
1367 right.GetConstant()->AsFloatConstant()->GetValue()));
1368 } else {
1369 DCHECK(right.IsStackSlot());
1370 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1371 Address(CpuRegister(RSP), right.GetStackIndex()));
1372 }
Mark Mendellc4701932015-04-10 13:18:51 -04001373 break;
1374 }
1375 case Primitive::kPrimDouble: {
1376 if (right.IsFpuRegister()) {
1377 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1378 } else if (right.IsConstant()) {
1379 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1380 codegen_->LiteralDoubleAddress(
1381 right.GetConstant()->AsDoubleConstant()->GetValue()));
1382 } else {
1383 DCHECK(right.IsDoubleStackSlot());
1384 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1385 Address(CpuRegister(RSP), right.GetStackIndex()));
1386 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001387 break;
1388 }
1389 default:
1390 LOG(FATAL) << "Unexpected condition type " << type;
1391 }
1392}
1393
1394template<class LabelType>
1395void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1396 LabelType* true_target_in,
1397 LabelType* false_target_in) {
1398 // Generated branching requires both targets to be explicit. If either of the
1399 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1400 LabelType fallthrough_target;
1401 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1402 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1403
1404 // Generate the comparison to set the CC.
1405 GenerateCompareTest(condition);
1406
1407 // Now generate the correct jump(s).
1408 Primitive::Type type = condition->InputAt(0)->GetType();
1409 switch (type) {
1410 case Primitive::kPrimLong: {
1411 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1412 break;
1413 }
1414 case Primitive::kPrimFloat: {
1415 GenerateFPJumps(condition, true_target, false_target);
1416 break;
1417 }
1418 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001419 GenerateFPJumps(condition, true_target, false_target);
1420 break;
1421 }
1422 default:
1423 LOG(FATAL) << "Unexpected condition type " << type;
1424 }
1425
David Brazdil0debae72015-11-12 18:37:00 +00001426 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001427 __ jmp(false_target);
1428 }
David Brazdil0debae72015-11-12 18:37:00 +00001429
1430 if (fallthrough_target.IsLinked()) {
1431 __ Bind(&fallthrough_target);
1432 }
Mark Mendellc4701932015-04-10 13:18:51 -04001433}
1434
David Brazdil0debae72015-11-12 18:37:00 +00001435static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1436 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1437 // are set only strictly before `branch`. We can't use the eflags on long
1438 // conditions if they are materialized due to the complex branching.
1439 return cond->IsCondition() &&
1440 cond->GetNext() == branch &&
1441 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1442}
1443
Mark Mendell152408f2015-12-31 12:28:50 -05001444template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001445void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001446 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001447 LabelType* true_target,
1448 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001449 HInstruction* cond = instruction->InputAt(condition_input_index);
1450
1451 if (true_target == nullptr && false_target == nullptr) {
1452 // Nothing to do. The code always falls through.
1453 return;
1454 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001455 // Constant condition, statically compared against "true" (integer value 1).
1456 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001457 if (true_target != nullptr) {
1458 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001459 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001460 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001461 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001462 if (false_target != nullptr) {
1463 __ jmp(false_target);
1464 }
1465 }
1466 return;
1467 }
1468
1469 // The following code generates these patterns:
1470 // (1) true_target == nullptr && false_target != nullptr
1471 // - opposite condition true => branch to false_target
1472 // (2) true_target != nullptr && false_target == nullptr
1473 // - condition true => branch to true_target
1474 // (3) true_target != nullptr && false_target != nullptr
1475 // - condition true => branch to true_target
1476 // - branch to false_target
1477 if (IsBooleanValueOrMaterializedCondition(cond)) {
1478 if (AreEflagsSetFrom(cond, instruction)) {
1479 if (true_target == nullptr) {
1480 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1481 } else {
1482 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1483 }
1484 } else {
1485 // Materialized condition, compare against 0.
1486 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1487 if (lhs.IsRegister()) {
1488 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1489 } else {
1490 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1491 }
1492 if (true_target == nullptr) {
1493 __ j(kEqual, false_target);
1494 } else {
1495 __ j(kNotEqual, true_target);
1496 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001497 }
1498 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001499 // Condition has not been materialized, use its inputs as the
1500 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001501 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001502
David Brazdil0debae72015-11-12 18:37:00 +00001503 // If this is a long or FP comparison that has been folded into
1504 // the HCondition, generate the comparison directly.
1505 Primitive::Type type = condition->InputAt(0)->GetType();
1506 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1507 GenerateCompareTestAndBranch(condition, true_target, false_target);
1508 return;
1509 }
1510
1511 Location lhs = condition->GetLocations()->InAt(0);
1512 Location rhs = condition->GetLocations()->InAt(1);
1513 if (rhs.IsRegister()) {
1514 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1515 } else if (rhs.IsConstant()) {
1516 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001517 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001518 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001519 __ cmpl(lhs.AsRegister<CpuRegister>(),
1520 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1521 }
1522 if (true_target == nullptr) {
1523 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1524 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001525 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001526 }
Dave Allison20dfc792014-06-16 20:44:29 -07001527 }
David Brazdil0debae72015-11-12 18:37:00 +00001528
1529 // If neither branch falls through (case 3), the conditional branch to `true_target`
1530 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1531 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001532 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001533 }
1534}
1535
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001536void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1538 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001539 locations->SetInAt(0, Location::Any());
1540 }
1541}
1542
1543void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001544 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1545 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1546 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1547 nullptr : codegen_->GetLabelOf(true_successor);
1548 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1549 nullptr : codegen_->GetLabelOf(false_successor);
1550 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001551}
1552
1553void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1554 LocationSummary* locations = new (GetGraph()->GetArena())
1555 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001556 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001557 locations->SetInAt(0, Location::Any());
1558 }
1559}
1560
1561void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001562 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001563 GenerateTestAndBranch<Label>(deoptimize,
1564 /* condition_input_index */ 0,
1565 slow_path->GetEntryLabel(),
1566 /* false_target */ nullptr);
1567}
1568
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001569static bool SelectCanUseCMOV(HSelect* select) {
1570 // There are no conditional move instructions for XMMs.
1571 if (Primitive::IsFloatingPointType(select->GetType())) {
1572 return false;
1573 }
1574
1575 // A FP condition doesn't generate the single CC that we need.
1576 HInstruction* condition = select->GetCondition();
1577 if (condition->IsCondition() &&
1578 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1579 return false;
1580 }
1581
1582 // We can generate a CMOV for this Select.
1583 return true;
1584}
1585
David Brazdil74eb1b22015-12-14 11:44:01 +00001586void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1588 if (Primitive::IsFloatingPointType(select->GetType())) {
1589 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001590 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001591 } else {
1592 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001593 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001594 if (select->InputAt(1)->IsConstant()) {
1595 locations->SetInAt(1, Location::RequiresRegister());
1596 } else {
1597 locations->SetInAt(1, Location::Any());
1598 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001599 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001600 locations->SetInAt(1, Location::Any());
1601 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001602 }
1603 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1604 locations->SetInAt(2, Location::RequiresRegister());
1605 }
1606 locations->SetOut(Location::SameAsFirstInput());
1607}
1608
1609void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1610 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001611 if (SelectCanUseCMOV(select)) {
1612 // If both the condition and the source types are integer, we can generate
1613 // a CMOV to implement Select.
1614 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001615 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001616 DCHECK(locations->InAt(0).Equals(locations->Out()));
1617
1618 HInstruction* select_condition = select->GetCondition();
1619 Condition cond = kNotEqual;
1620
1621 // Figure out how to test the 'condition'.
1622 if (select_condition->IsCondition()) {
1623 HCondition* condition = select_condition->AsCondition();
1624 if (!condition->IsEmittedAtUseSite()) {
1625 // This was a previously materialized condition.
1626 // Can we use the existing condition code?
1627 if (AreEflagsSetFrom(condition, select)) {
1628 // Materialization was the previous instruction. Condition codes are right.
1629 cond = X86_64IntegerCondition(condition->GetCondition());
1630 } else {
1631 // No, we have to recreate the condition code.
1632 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1633 __ testl(cond_reg, cond_reg);
1634 }
1635 } else {
1636 GenerateCompareTest(condition);
1637 cond = X86_64IntegerCondition(condition->GetCondition());
1638 }
1639 } else {
1640 // Must be a boolean condition, which needs to be compared to 0.
1641 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1642 __ testl(cond_reg, cond_reg);
1643 }
1644
1645 // If the condition is true, overwrite the output, which already contains false.
1646 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001647 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1648 if (value_true_loc.IsRegister()) {
1649 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1650 } else {
1651 __ cmov(cond,
1652 value_false,
1653 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1654 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001655 } else {
1656 NearLabel false_target;
1657 GenerateTestAndBranch<NearLabel>(select,
1658 /* condition_input_index */ 2,
1659 /* true_target */ nullptr,
1660 &false_target);
1661 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1662 __ Bind(&false_target);
1663 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664}
1665
David Srbecky0cf44932015-12-09 14:09:59 +00001666void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1667 new (GetGraph()->GetArena()) LocationSummary(info);
1668}
1669
David Srbeckyd28f4a02016-03-14 17:14:24 +00001670void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1671 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001672}
1673
1674void CodeGeneratorX86_64::GenerateNop() {
1675 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001676}
1677
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001680 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001681 // Handle the long/FP comparisons made in instruction simplification.
1682 switch (cond->InputAt(0)->GetType()) {
1683 case Primitive::kPrimLong:
1684 locations->SetInAt(0, Location::RequiresRegister());
1685 locations->SetInAt(1, Location::Any());
1686 break;
1687 case Primitive::kPrimFloat:
1688 case Primitive::kPrimDouble:
1689 locations->SetInAt(0, Location::RequiresFpuRegister());
1690 locations->SetInAt(1, Location::Any());
1691 break;
1692 default:
1693 locations->SetInAt(0, Location::RequiresRegister());
1694 locations->SetInAt(1, Location::Any());
1695 break;
1696 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001697 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001698 locations->SetOut(Location::RequiresRegister());
1699 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001700}
1701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001703 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001704 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001705 }
Mark Mendellc4701932015-04-10 13:18:51 -04001706
1707 LocationSummary* locations = cond->GetLocations();
1708 Location lhs = locations->InAt(0);
1709 Location rhs = locations->InAt(1);
1710 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001711 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001712
1713 switch (cond->InputAt(0)->GetType()) {
1714 default:
1715 // Integer case.
1716
1717 // Clear output register: setcc only sets the low byte.
1718 __ xorl(reg, reg);
1719
1720 if (rhs.IsRegister()) {
1721 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1722 } else if (rhs.IsConstant()) {
1723 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001724 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001725 } else {
1726 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1727 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001728 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001729 return;
1730 case Primitive::kPrimLong:
1731 // Clear output register: setcc only sets the low byte.
1732 __ xorl(reg, reg);
1733
1734 if (rhs.IsRegister()) {
1735 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1736 } else if (rhs.IsConstant()) {
1737 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001738 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 } else {
1740 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1741 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001742 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001743 return;
1744 case Primitive::kPrimFloat: {
1745 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1746 if (rhs.IsConstant()) {
1747 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1748 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1749 } else if (rhs.IsStackSlot()) {
1750 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1751 } else {
1752 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1753 }
1754 GenerateFPJumps(cond, &true_label, &false_label);
1755 break;
1756 }
1757 case Primitive::kPrimDouble: {
1758 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1759 if (rhs.IsConstant()) {
1760 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1761 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1762 } else if (rhs.IsDoubleStackSlot()) {
1763 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1764 } else {
1765 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1766 }
1767 GenerateFPJumps(cond, &true_label, &false_label);
1768 break;
1769 }
1770 }
1771
1772 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001773 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001774
Roland Levillain4fa13f62015-07-06 18:11:54 +01001775 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001776 __ Bind(&false_label);
1777 __ xorl(reg, reg);
1778 __ jmp(&done_label);
1779
Roland Levillain4fa13f62015-07-06 18:11:54 +01001780 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001781 __ Bind(&true_label);
1782 __ movl(reg, Immediate(1));
1783 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001796}
1797
1798void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001800}
1801
1802void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001804}
1805
1806void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001808}
1809
1810void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001812}
1813
1814void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001816}
1817
1818void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001820}
1821
1822void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001824}
1825
1826void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001828}
1829
1830void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001832}
1833
Aart Bike9f37602015-10-09 11:15:55 -07001834void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001836}
1837
1838void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001840}
1841
1842void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001844}
1845
1846void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001848}
1849
1850void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001852}
1853
1854void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001856}
1857
1858void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001859 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001860}
1861
1862void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001863 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001864}
1865
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001866void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001867 LocationSummary* locations =
1868 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001869 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001870 case Primitive::kPrimBoolean:
1871 case Primitive::kPrimByte:
1872 case Primitive::kPrimShort:
1873 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001874 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001875 case Primitive::kPrimLong: {
1876 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001877 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1879 break;
1880 }
1881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble: {
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001884 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001885 locations->SetOut(Location::RequiresRegister());
1886 break;
1887 }
1888 default:
1889 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1890 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001891}
1892
1893void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001894 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001895 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001896 Location left = locations->InAt(0);
1897 Location right = locations->InAt(1);
1898
Mark Mendell0c9497d2015-08-21 09:30:05 -04001899 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001900 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001901 Condition less_cond = kLess;
1902
Calin Juravleddb7df22014-11-25 20:56:51 +00001903 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001904 case Primitive::kPrimBoolean:
1905 case Primitive::kPrimByte:
1906 case Primitive::kPrimShort:
1907 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001908 case Primitive::kPrimInt: {
1909 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1910 if (right.IsConstant()) {
1911 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1912 codegen_->Compare32BitValue(left_reg, value);
1913 } else if (right.IsStackSlot()) {
1914 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1915 } else {
1916 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1917 }
1918 break;
1919 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001920 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001921 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1922 if (right.IsConstant()) {
1923 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001924 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001925 } else if (right.IsDoubleStackSlot()) {
1926 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001927 } else {
1928 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1929 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001930 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001931 }
1932 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001933 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1934 if (right.IsConstant()) {
1935 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1936 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1937 } else if (right.IsStackSlot()) {
1938 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1939 } else {
1940 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1941 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001942 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001943 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001944 break;
1945 }
1946 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001947 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1948 if (right.IsConstant()) {
1949 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1950 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1951 } else if (right.IsDoubleStackSlot()) {
1952 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1953 } else {
1954 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1955 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001956 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001957 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001958 break;
1959 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001960 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001961 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001962 }
Aart Bika19616e2016-02-01 18:57:58 -08001963
Calin Juravleddb7df22014-11-25 20:56:51 +00001964 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001965 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001966 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001967
Calin Juravle91debbc2014-11-26 19:01:09 +00001968 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001969 __ movl(out, Immediate(1));
1970 __ jmp(&done);
1971
1972 __ Bind(&less);
1973 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001974
1975 __ Bind(&done);
1976}
1977
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001981 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001982}
1983
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001984void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001985 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001986}
1987
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001988void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1989 LocationSummary* locations =
1990 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1991 locations->SetOut(Location::ConstantLocation(constant));
1992}
1993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001994void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001995 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001996}
1997
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002001 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002}
2003
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002004void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002005 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002006}
2007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002008void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2011 locations->SetOut(Location::ConstantLocation(constant));
2012}
2013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002014void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002015 // Will be generated at use site.
2016}
2017
2018void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2021 locations->SetOut(Location::ConstantLocation(constant));
2022}
2023
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002024void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2025 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002026 // Will be generated at use site.
2027}
2028
Calin Juravle27df7582015-04-17 19:12:31 +01002029void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2030 memory_barrier->SetLocations(nullptr);
2031}
2032
2033void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002034 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002035}
2036
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002037void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2038 ret->SetLocations(nullptr);
2039}
2040
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002042 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002043}
2044
2045void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002046 LocationSummary* locations =
2047 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002048 switch (ret->InputAt(0)->GetType()) {
2049 case Primitive::kPrimBoolean:
2050 case Primitive::kPrimByte:
2051 case Primitive::kPrimChar:
2052 case Primitive::kPrimShort:
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimNot:
2055 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002057 break;
2058
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002059 case Primitive::kPrimFloat:
2060 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002061 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002062 break;
2063
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002064 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002065 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002066 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002067}
2068
2069void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2070 if (kIsDebugBuild) {
2071 switch (ret->InputAt(0)->GetType()) {
2072 case Primitive::kPrimBoolean:
2073 case Primitive::kPrimByte:
2074 case Primitive::kPrimChar:
2075 case Primitive::kPrimShort:
2076 case Primitive::kPrimInt:
2077 case Primitive::kPrimNot:
2078 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002080 break;
2081
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002082 case Primitive::kPrimFloat:
2083 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002085 XMM0);
2086 break;
2087
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002088 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002089 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090 }
2091 }
2092 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002093}
2094
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002095Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2096 switch (type) {
2097 case Primitive::kPrimBoolean:
2098 case Primitive::kPrimByte:
2099 case Primitive::kPrimChar:
2100 case Primitive::kPrimShort:
2101 case Primitive::kPrimInt:
2102 case Primitive::kPrimNot:
2103 case Primitive::kPrimLong:
2104 return Location::RegisterLocation(RAX);
2105
2106 case Primitive::kPrimVoid:
2107 return Location::NoLocation();
2108
2109 case Primitive::kPrimDouble:
2110 case Primitive::kPrimFloat:
2111 return Location::FpuRegisterLocation(XMM0);
2112 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002113
2114 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002115}
2116
2117Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2118 return Location::RegisterLocation(kMethodRegisterArgument);
2119}
2120
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002121Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002122 switch (type) {
2123 case Primitive::kPrimBoolean:
2124 case Primitive::kPrimByte:
2125 case Primitive::kPrimChar:
2126 case Primitive::kPrimShort:
2127 case Primitive::kPrimInt:
2128 case Primitive::kPrimNot: {
2129 uint32_t index = gp_index_++;
2130 stack_index_++;
2131 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002133 } else {
2134 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2135 }
2136 }
2137
2138 case Primitive::kPrimLong: {
2139 uint32_t index = gp_index_;
2140 stack_index_ += 2;
2141 if (index < calling_convention.GetNumberOfRegisters()) {
2142 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144 } else {
2145 gp_index_ += 2;
2146 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2147 }
2148 }
2149
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002150 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002151 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002152 stack_index_++;
2153 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002154 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002155 } else {
2156 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2157 }
2158 }
2159
2160 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002161 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002162 stack_index_ += 2;
2163 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 } else {
2166 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2167 }
2168 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002169
2170 case Primitive::kPrimVoid:
2171 LOG(FATAL) << "Unexpected parameter type " << type;
2172 break;
2173 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002174 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002175}
2176
Calin Juravle175dc732015-08-25 15:42:32 +01002177void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2178 // The trampoline uses the same calling convention as dex calling conventions,
2179 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2180 // the method_idx.
2181 HandleInvoke(invoke);
2182}
2183
2184void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2185 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2186}
2187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002188void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002189 // Explicit clinit checks triggered by static invokes must have been pruned by
2190 // art::PrepareForRegisterAllocation.
2191 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002192
Mark Mendellfb8d2792015-03-31 22:16:59 -04002193 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194 if (intrinsic.TryDispatch(invoke)) {
2195 return;
2196 }
2197
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002198 HandleInvoke(invoke);
2199}
2200
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002201static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2202 if (invoke->GetLocations()->Intrinsified()) {
2203 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2204 intrinsic.Dispatch(invoke);
2205 return true;
2206 }
2207 return false;
2208}
2209
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002210void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002211 // Explicit clinit checks triggered by static invokes must have been pruned by
2212 // art::PrepareForRegisterAllocation.
2213 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002214
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002215 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2216 return;
2217 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002218
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002219 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002220 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002221 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002222 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002223}
2224
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002225void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002226 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002227 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228}
2229
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002231 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002232 if (intrinsic.TryDispatch(invoke)) {
2233 return;
2234 }
2235
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 HandleInvoke(invoke);
2237}
2238
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002239void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002240 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2241 return;
2242 }
2243
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002244 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002245 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002246 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002247}
2248
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2250 HandleInvoke(invoke);
2251 // Add the hidden argument.
2252 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2253}
2254
2255void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2256 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 LocationSummary* locations = invoke->GetLocations();
2258 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2259 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 Location receiver = locations->InAt(0);
2261 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2262
Roland Levillain0d5a2812015-11-13 10:07:31 +00002263 // Set the hidden argument. This is safe to do this here, as RAX
2264 // won't be modified thereafter, before the `call` instruction.
2265 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002266 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002268 if (receiver.IsStackSlot()) {
2269 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002270 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 __ movl(temp, Address(temp, class_offset));
2272 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002273 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002274 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002276 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002277 // Instead of simply (possibly) unpoisoning `temp` here, we should
2278 // emit a read barrier for the previous class reference load.
2279 // However this is not required in practice, as this is an
2280 // intermediate/temporary reference and because the current
2281 // concurrent copying collector keeps the from-space memory
2282 // intact/accessible until the end of the marking phase (the
2283 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002284 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002285 // temp = temp->GetAddressOfIMT()
2286 __ movq(temp,
2287 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2288 // temp = temp->GetImtEntryAt(method_offset);
2289 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2290 invoke->GetImtIndex() % ImTable::kSize, kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002291 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002292 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002293 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002294 __ call(Address(temp,
2295 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002296
2297 DCHECK(!codegen_->IsLeafMethod());
2298 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2299}
2300
Roland Levillain88cb1752014-10-20 16:36:47 +01002301void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2302 LocationSummary* locations =
2303 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2304 switch (neg->GetResultType()) {
2305 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002306 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetOut(Location::SameAsFirstInput());
2309 break;
2310
Roland Levillain88cb1752014-10-20 16:36:47 +01002311 case Primitive::kPrimFloat:
2312 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002314 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002315 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002316 break;
2317
2318 default:
2319 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2320 }
2321}
2322
2323void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2324 LocationSummary* locations = neg->GetLocations();
2325 Location out = locations->Out();
2326 Location in = locations->InAt(0);
2327 switch (neg->GetResultType()) {
2328 case Primitive::kPrimInt:
2329 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002330 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002332 break;
2333
2334 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002335 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002336 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002338 break;
2339
Roland Levillain5368c212014-11-27 15:03:41 +00002340 case Primitive::kPrimFloat: {
2341 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002342 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002343 // Implement float negation with an exclusive or with value
2344 // 0x80000000 (mask for bit 31, representing the sign of a
2345 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002346 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002348 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002349 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002350
Roland Levillain5368c212014-11-27 15:03:41 +00002351 case Primitive::kPrimDouble: {
2352 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002353 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002354 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002355 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002356 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002357 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002359 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002360 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002361
2362 default:
2363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2364 }
2365}
2366
Roland Levillaindff1f282014-11-05 14:15:05 +00002367void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2368 LocationSummary* locations =
2369 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2370 Primitive::Type result_type = conversion->GetResultType();
2371 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002372 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002373
David Brazdilb2bd1c52015-03-25 11:17:37 +00002374 // The Java language does not allow treating boolean as an integral type but
2375 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002376
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002378 case Primitive::kPrimByte:
2379 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002380 case Primitive::kPrimLong:
2381 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002382 case Primitive::kPrimBoolean:
2383 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002384 case Primitive::kPrimShort:
2385 case Primitive::kPrimInt:
2386 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002387 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002388 locations->SetInAt(0, Location::Any());
2389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2390 break;
2391
2392 default:
2393 LOG(FATAL) << "Unexpected type conversion from " << input_type
2394 << " to " << result_type;
2395 }
2396 break;
2397
Roland Levillain01a8d712014-11-14 16:27:39 +00002398 case Primitive::kPrimShort:
2399 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002400 case Primitive::kPrimLong:
2401 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002402 case Primitive::kPrimBoolean:
2403 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002404 case Primitive::kPrimByte:
2405 case Primitive::kPrimInt:
2406 case Primitive::kPrimChar:
2407 // Processing a Dex `int-to-short' instruction.
2408 locations->SetInAt(0, Location::Any());
2409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2410 break;
2411
2412 default:
2413 LOG(FATAL) << "Unexpected type conversion from " << input_type
2414 << " to " << result_type;
2415 }
2416 break;
2417
Roland Levillain946e1432014-11-11 17:35:19 +00002418 case Primitive::kPrimInt:
2419 switch (input_type) {
2420 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002421 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002422 locations->SetInAt(0, Location::Any());
2423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424 break;
2425
2426 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002427 // Processing a Dex `float-to-int' instruction.
2428 locations->SetInAt(0, Location::RequiresFpuRegister());
2429 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002430 break;
2431
Roland Levillain946e1432014-11-11 17:35:19 +00002432 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 // Processing a Dex `double-to-int' instruction.
2434 locations->SetInAt(0, Location::RequiresFpuRegister());
2435 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002436 break;
2437
2438 default:
2439 LOG(FATAL) << "Unexpected type conversion from " << input_type
2440 << " to " << result_type;
2441 }
2442 break;
2443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 case Primitive::kPrimLong:
2445 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002451 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 // TODO: We would benefit from a (to-be-implemented)
2454 // Location::RegisterOrStackSlot requirement for this input.
2455 locations->SetInAt(0, Location::RequiresRegister());
2456 locations->SetOut(Location::RequiresRegister());
2457 break;
2458
2459 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002460 // Processing a Dex `float-to-long' instruction.
2461 locations->SetInAt(0, Location::RequiresFpuRegister());
2462 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002463 break;
2464
Roland Levillaindff1f282014-11-05 14:15:05 +00002465 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002466 // Processing a Dex `double-to-long' instruction.
2467 locations->SetInAt(0, Location::RequiresFpuRegister());
2468 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Roland Levillain981e4542014-11-14 11:47:14 +00002477 case Primitive::kPrimChar:
2478 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002479 case Primitive::kPrimLong:
2480 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002481 case Primitive::kPrimBoolean:
2482 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimShort:
2485 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002486 // Processing a Dex `int-to-char' instruction.
2487 locations->SetInAt(0, Location::Any());
2488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 }
2495 break;
2496
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimInt:
2504 case Primitive::kPrimChar:
2505 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002506 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002507 locations->SetOut(Location::RequiresFpuRegister());
2508 break;
2509
2510 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002511 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002512 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002513 locations->SetOut(Location::RequiresFpuRegister());
2514 break;
2515
Roland Levillaincff13742014-11-17 14:32:17 +00002516 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002517 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002518 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002519 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 };
2526 break;
2527
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002529 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
2535 case Primitive::kPrimChar:
2536 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002537 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002538 locations->SetOut(Location::RequiresFpuRegister());
2539 break;
2540
2541 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002542 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002543 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002544 locations->SetOut(Location::RequiresFpuRegister());
2545 break;
2546
Roland Levillaincff13742014-11-17 14:32:17 +00002547 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002548 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002549 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
2556 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563}
2564
2565void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2566 LocationSummary* locations = conversion->GetLocations();
2567 Location out = locations->Out();
2568 Location in = locations->InAt(0);
2569 Primitive::Type result_type = conversion->GetResultType();
2570 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002571 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002573 case Primitive::kPrimByte:
2574 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002575 case Primitive::kPrimLong:
2576 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002577 case Primitive::kPrimBoolean:
2578 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002582 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002583 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002585 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002586 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002587 Address(CpuRegister(RSP), in.GetStackIndex()));
2588 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002590 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002591 }
2592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
Roland Levillain01a8d712014-11-14 16:27:39 +00002600 case Primitive::kPrimShort:
2601 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002602 case Primitive::kPrimLong:
2603 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002604 case Primitive::kPrimBoolean:
2605 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002606 case Primitive::kPrimByte:
2607 case Primitive::kPrimInt:
2608 case Primitive::kPrimChar:
2609 // Processing a Dex `int-to-short' instruction.
2610 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002612 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002614 Address(CpuRegister(RSP), in.GetStackIndex()));
2615 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002616 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002617 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002618 }
2619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 }
2625 break;
2626
Roland Levillain946e1432014-11-11 17:35:19 +00002627 case Primitive::kPrimInt:
2628 switch (input_type) {
2629 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002630 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002631 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002632 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002633 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002634 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002635 Address(CpuRegister(RSP), in.GetStackIndex()));
2636 } else {
2637 DCHECK(in.IsConstant());
2638 DCHECK(in.GetConstant()->IsLongConstant());
2639 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002641 }
2642 break;
2643
Roland Levillain3f8f9362014-12-02 17:45:01 +00002644 case Primitive::kPrimFloat: {
2645 // Processing a Dex `float-to-int' instruction.
2646 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2647 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002648 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002649
2650 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002651 // if input >= (float)INT_MAX goto done
2652 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002653 __ j(kAboveEqual, &done);
2654 // if input == NaN goto nan
2655 __ j(kUnordered, &nan);
2656 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002657 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002658 __ jmp(&done);
2659 __ Bind(&nan);
2660 // output = 0
2661 __ xorl(output, output);
2662 __ Bind(&done);
2663 break;
2664 }
2665
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002666 case Primitive::kPrimDouble: {
2667 // Processing a Dex `double-to-int' instruction.
2668 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2669 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002670 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002671
2672 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002673 // if input >= (double)INT_MAX goto done
2674 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002675 __ j(kAboveEqual, &done);
2676 // if input == NaN goto nan
2677 __ j(kUnordered, &nan);
2678 // output = double-to-int-truncate(input)
2679 __ cvttsd2si(output, input);
2680 __ jmp(&done);
2681 __ Bind(&nan);
2682 // output = 0
2683 __ xorl(output, output);
2684 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002685 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002686 }
Roland Levillain946e1432014-11-11 17:35:19 +00002687
2688 default:
2689 LOG(FATAL) << "Unexpected type conversion from " << input_type
2690 << " to " << result_type;
2691 }
2692 break;
2693
Roland Levillaindff1f282014-11-05 14:15:05 +00002694 case Primitive::kPrimLong:
2695 switch (input_type) {
2696 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002697 case Primitive::kPrimBoolean:
2698 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002699 case Primitive::kPrimByte:
2700 case Primitive::kPrimShort:
2701 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002702 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002703 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002704 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002706 break;
2707
Roland Levillain624279f2014-12-04 11:54:28 +00002708 case Primitive::kPrimFloat: {
2709 // Processing a Dex `float-to-long' instruction.
2710 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2711 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002712 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002713
Mark Mendell92e83bf2015-05-07 11:25:03 -04002714 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002715 // if input >= (float)LONG_MAX goto done
2716 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002717 __ j(kAboveEqual, &done);
2718 // if input == NaN goto nan
2719 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002720 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002721 __ cvttss2si(output, input, true);
2722 __ jmp(&done);
2723 __ Bind(&nan);
2724 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002725 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002726 __ Bind(&done);
2727 break;
2728 }
2729
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002730 case Primitive::kPrimDouble: {
2731 // Processing a Dex `double-to-long' instruction.
2732 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2733 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002734 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002735
Mark Mendell92e83bf2015-05-07 11:25:03 -04002736 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002737 // if input >= (double)LONG_MAX goto done
2738 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002739 __ j(kAboveEqual, &done);
2740 // if input == NaN goto nan
2741 __ j(kUnordered, &nan);
2742 // output = double-to-long-truncate(input)
2743 __ cvttsd2si(output, input, true);
2744 __ jmp(&done);
2745 __ Bind(&nan);
2746 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002747 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002748 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002749 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002750 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002751
2752 default:
2753 LOG(FATAL) << "Unexpected type conversion from " << input_type
2754 << " to " << result_type;
2755 }
2756 break;
2757
Roland Levillain981e4542014-11-14 11:47:14 +00002758 case Primitive::kPrimChar:
2759 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002760 case Primitive::kPrimLong:
2761 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002762 case Primitive::kPrimBoolean:
2763 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002764 case Primitive::kPrimByte:
2765 case Primitive::kPrimShort:
2766 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002767 // Processing a Dex `int-to-char' instruction.
2768 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002770 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002772 Address(CpuRegister(RSP), in.GetStackIndex()));
2773 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002775 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002776 }
2777 break;
2778
2779 default:
2780 LOG(FATAL) << "Unexpected type conversion from " << input_type
2781 << " to " << result_type;
2782 }
2783 break;
2784
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002786 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002787 case Primitive::kPrimBoolean:
2788 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002789 case Primitive::kPrimByte:
2790 case Primitive::kPrimShort:
2791 case Primitive::kPrimInt:
2792 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002793 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002794 if (in.IsRegister()) {
2795 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2796 } else if (in.IsConstant()) {
2797 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2798 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002799 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002800 } else {
2801 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2802 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2803 }
Roland Levillaincff13742014-11-17 14:32:17 +00002804 break;
2805
2806 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002808 if (in.IsRegister()) {
2809 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2810 } else if (in.IsConstant()) {
2811 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2812 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002813 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002814 } else {
2815 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2816 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2817 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002818 break;
2819
Roland Levillaincff13742014-11-17 14:32:17 +00002820 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002821 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002822 if (in.IsFpuRegister()) {
2823 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2824 } else if (in.IsConstant()) {
2825 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2826 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002827 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002828 } else {
2829 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2830 Address(CpuRegister(RSP), in.GetStackIndex()));
2831 }
Roland Levillaincff13742014-11-17 14:32:17 +00002832 break;
2833
2834 default:
2835 LOG(FATAL) << "Unexpected type conversion from " << input_type
2836 << " to " << result_type;
2837 };
2838 break;
2839
Roland Levillaindff1f282014-11-05 14:15:05 +00002840 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002841 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002842 case Primitive::kPrimBoolean:
2843 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002844 case Primitive::kPrimByte:
2845 case Primitive::kPrimShort:
2846 case Primitive::kPrimInt:
2847 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002848 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002849 if (in.IsRegister()) {
2850 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2851 } else if (in.IsConstant()) {
2852 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2853 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002854 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002855 } else {
2856 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2857 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2858 }
Roland Levillaincff13742014-11-17 14:32:17 +00002859 break;
2860
2861 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002862 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002863 if (in.IsRegister()) {
2864 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2865 } else if (in.IsConstant()) {
2866 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2867 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002868 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002869 } else {
2870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2871 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2872 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002873 break;
2874
Roland Levillaincff13742014-11-17 14:32:17 +00002875 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002876 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002877 if (in.IsFpuRegister()) {
2878 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2879 } else if (in.IsConstant()) {
2880 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2881 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002882 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002883 } else {
2884 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2885 Address(CpuRegister(RSP), in.GetStackIndex()));
2886 }
Roland Levillaincff13742014-11-17 14:32:17 +00002887 break;
2888
2889 default:
2890 LOG(FATAL) << "Unexpected type conversion from " << input_type
2891 << " to " << result_type;
2892 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002893 break;
2894
2895 default:
2896 LOG(FATAL) << "Unexpected type conversion from " << input_type
2897 << " to " << result_type;
2898 }
2899}
2900
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002901void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002902 LocationSummary* locations =
2903 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002905 case Primitive::kPrimInt: {
2906 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002907 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002909 break;
2910 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002911
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002912 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002913 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002914 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002915 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002917 break;
2918 }
2919
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002920 case Primitive::kPrimDouble:
2921 case Primitive::kPrimFloat: {
2922 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002923 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002925 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002926 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002927
2928 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002930 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002931}
2932
2933void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2934 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935 Location first = locations->InAt(0);
2936 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002937 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002938
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002939 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002940 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002941 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002942 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2943 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002944 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2945 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002946 } else {
2947 __ leal(out.AsRegister<CpuRegister>(), Address(
2948 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2949 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2952 __ addl(out.AsRegister<CpuRegister>(),
2953 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2954 } else {
2955 __ leal(out.AsRegister<CpuRegister>(), Address(
2956 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2957 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002958 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002959 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002961 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002962 break;
2963 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002964
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002965 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002966 if (second.IsRegister()) {
2967 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2968 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002969 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2970 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002971 } else {
2972 __ leaq(out.AsRegister<CpuRegister>(), Address(
2973 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2974 }
2975 } else {
2976 DCHECK(second.IsConstant());
2977 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2978 int32_t int32_value = Low32Bits(value);
2979 DCHECK_EQ(int32_value, value);
2980 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2981 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2982 } else {
2983 __ leaq(out.AsRegister<CpuRegister>(), Address(
2984 first.AsRegister<CpuRegister>(), int32_value));
2985 }
2986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 break;
2988 }
2989
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002991 if (second.IsFpuRegister()) {
2992 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2993 } else if (second.IsConstant()) {
2994 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002995 codegen_->LiteralFloatAddress(
2996 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002997 } else {
2998 DCHECK(second.IsStackSlot());
2999 __ addss(first.AsFpuRegister<XmmRegister>(),
3000 Address(CpuRegister(RSP), second.GetStackIndex()));
3001 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003002 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 }
3004
3005 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003006 if (second.IsFpuRegister()) {
3007 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3008 } else if (second.IsConstant()) {
3009 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003010 codegen_->LiteralDoubleAddress(
3011 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003012 } else {
3013 DCHECK(second.IsDoubleStackSlot());
3014 __ addsd(first.AsFpuRegister<XmmRegister>(),
3015 Address(CpuRegister(RSP), second.GetStackIndex()));
3016 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003017 break;
3018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019
3020 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003021 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003022 }
3023}
3024
3025void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003026 LocationSummary* locations =
3027 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003028 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003029 case Primitive::kPrimInt: {
3030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetInAt(1, Location::Any());
3032 locations->SetOut(Location::SameAsFirstInput());
3033 break;
3034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003036 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003037 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003038 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 break;
3040 }
Calin Juravle11351682014-10-23 15:38:15 +01003041 case Primitive::kPrimFloat:
3042 case Primitive::kPrimDouble: {
3043 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003044 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003045 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003046 break;
Calin Juravle11351682014-10-23 15:38:15 +01003047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003048 default:
Calin Juravle11351682014-10-23 15:38:15 +01003049 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003050 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003051}
3052
3053void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3054 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003055 Location first = locations->InAt(0);
3056 Location second = locations->InAt(1);
3057 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003059 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003060 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003061 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003062 } else if (second.IsConstant()) {
3063 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003065 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003067 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003068 break;
3069 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003071 if (second.IsConstant()) {
3072 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3073 DCHECK(IsInt<32>(value));
3074 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3075 } else {
3076 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3077 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 break;
3079 }
3080
Calin Juravle11351682014-10-23 15:38:15 +01003081 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003082 if (second.IsFpuRegister()) {
3083 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3084 } else if (second.IsConstant()) {
3085 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003086 codegen_->LiteralFloatAddress(
3087 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003088 } else {
3089 DCHECK(second.IsStackSlot());
3090 __ subss(first.AsFpuRegister<XmmRegister>(),
3091 Address(CpuRegister(RSP), second.GetStackIndex()));
3092 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093 break;
Calin Juravle11351682014-10-23 15:38:15 +01003094 }
3095
3096 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003097 if (second.IsFpuRegister()) {
3098 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3099 } else if (second.IsConstant()) {
3100 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003101 codegen_->LiteralDoubleAddress(
3102 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003103 } else {
3104 DCHECK(second.IsDoubleStackSlot());
3105 __ subsd(first.AsFpuRegister<XmmRegister>(),
3106 Address(CpuRegister(RSP), second.GetStackIndex()));
3107 }
Calin Juravle11351682014-10-23 15:38:15 +01003108 break;
3109 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003110
3111 default:
Calin Juravle11351682014-10-23 15:38:15 +01003112 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003113 }
3114}
3115
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3117 LocationSummary* locations =
3118 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3119 switch (mul->GetResultType()) {
3120 case Primitive::kPrimInt: {
3121 locations->SetInAt(0, Location::RequiresRegister());
3122 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003123 if (mul->InputAt(1)->IsIntConstant()) {
3124 // Can use 3 operand multiply.
3125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3126 } else {
3127 locations->SetOut(Location::SameAsFirstInput());
3128 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 break;
3130 }
3131 case Primitive::kPrimLong: {
3132 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003133 locations->SetInAt(1, Location::Any());
3134 if (mul->InputAt(1)->IsLongConstant() &&
3135 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003136 // Can use 3 operand multiply.
3137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3138 } else {
3139 locations->SetOut(Location::SameAsFirstInput());
3140 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141 break;
3142 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003143 case Primitive::kPrimFloat:
3144 case Primitive::kPrimDouble: {
3145 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003146 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003147 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003149 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150
3151 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003152 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153 }
3154}
3155
3156void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3157 LocationSummary* locations = mul->GetLocations();
3158 Location first = locations->InAt(0);
3159 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003160 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003162 case Primitive::kPrimInt:
3163 // The constant may have ended up in a register, so test explicitly to avoid
3164 // problems where the output may not be the same as the first operand.
3165 if (mul->InputAt(1)->IsIntConstant()) {
3166 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3167 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3168 } else if (second.IsRegister()) {
3169 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003170 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003172 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003174 __ imull(first.AsRegister<CpuRegister>(),
3175 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176 }
3177 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003179 // The constant may have ended up in a register, so test explicitly to avoid
3180 // problems where the output may not be the same as the first operand.
3181 if (mul->InputAt(1)->IsLongConstant()) {
3182 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3183 if (IsInt<32>(value)) {
3184 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3185 Immediate(static_cast<int32_t>(value)));
3186 } else {
3187 // Have to use the constant area.
3188 DCHECK(first.Equals(out));
3189 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3190 }
3191 } else if (second.IsRegister()) {
3192 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003193 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003194 } else {
3195 DCHECK(second.IsDoubleStackSlot());
3196 DCHECK(first.Equals(out));
3197 __ imulq(first.AsRegister<CpuRegister>(),
3198 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 break;
3201 }
3202
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003204 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003205 if (second.IsFpuRegister()) {
3206 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3207 } else if (second.IsConstant()) {
3208 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003209 codegen_->LiteralFloatAddress(
3210 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003211 } else {
3212 DCHECK(second.IsStackSlot());
3213 __ mulss(first.AsFpuRegister<XmmRegister>(),
3214 Address(CpuRegister(RSP), second.GetStackIndex()));
3215 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003217 }
3218
3219 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003220 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003221 if (second.IsFpuRegister()) {
3222 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3223 } else if (second.IsConstant()) {
3224 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003225 codegen_->LiteralDoubleAddress(
3226 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003227 } else {
3228 DCHECK(second.IsDoubleStackSlot());
3229 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3230 Address(CpuRegister(RSP), second.GetStackIndex()));
3231 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003232 break;
3233 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003234
3235 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003236 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237 }
3238}
3239
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003240void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3241 uint32_t stack_adjustment, bool is_float) {
3242 if (source.IsStackSlot()) {
3243 DCHECK(is_float);
3244 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3245 } else if (source.IsDoubleStackSlot()) {
3246 DCHECK(!is_float);
3247 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3248 } else {
3249 // Write the value to the temporary location on the stack and load to FP stack.
3250 if (is_float) {
3251 Location stack_temp = Location::StackSlot(temp_offset);
3252 codegen_->Move(stack_temp, source);
3253 __ flds(Address(CpuRegister(RSP), temp_offset));
3254 } else {
3255 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3256 codegen_->Move(stack_temp, source);
3257 __ fldl(Address(CpuRegister(RSP), temp_offset));
3258 }
3259 }
3260}
3261
3262void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3263 Primitive::Type type = rem->GetResultType();
3264 bool is_float = type == Primitive::kPrimFloat;
3265 size_t elem_size = Primitive::ComponentSize(type);
3266 LocationSummary* locations = rem->GetLocations();
3267 Location first = locations->InAt(0);
3268 Location second = locations->InAt(1);
3269 Location out = locations->Out();
3270
3271 // Create stack space for 2 elements.
3272 // TODO: enhance register allocator to ask for stack temporaries.
3273 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3274
3275 // Load the values to the FP stack in reverse order, using temporaries if needed.
3276 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3277 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3278
3279 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003280 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003281 __ Bind(&retry);
3282 __ fprem();
3283
3284 // Move FP status to AX.
3285 __ fstsw();
3286
3287 // And see if the argument reduction is complete. This is signaled by the
3288 // C2 FPU flag bit set to 0.
3289 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3290 __ j(kNotEqual, &retry);
3291
3292 // We have settled on the final value. Retrieve it into an XMM register.
3293 // Store FP top of stack to real stack.
3294 if (is_float) {
3295 __ fsts(Address(CpuRegister(RSP), 0));
3296 } else {
3297 __ fstl(Address(CpuRegister(RSP), 0));
3298 }
3299
3300 // Pop the 2 items from the FP stack.
3301 __ fucompp();
3302
3303 // Load the value from the stack into an XMM register.
3304 DCHECK(out.IsFpuRegister()) << out;
3305 if (is_float) {
3306 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3307 } else {
3308 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3309 }
3310
3311 // And remove the temporary stack space we allocated.
3312 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3313}
3314
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3316 DCHECK(instruction->IsDiv() || instruction->IsRem());
3317
3318 LocationSummary* locations = instruction->GetLocations();
3319 Location second = locations->InAt(1);
3320 DCHECK(second.IsConstant());
3321
3322 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3323 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003324 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003325
3326 DCHECK(imm == 1 || imm == -1);
3327
3328 switch (instruction->GetResultType()) {
3329 case Primitive::kPrimInt: {
3330 if (instruction->IsRem()) {
3331 __ xorl(output_register, output_register);
3332 } else {
3333 __ movl(output_register, input_register);
3334 if (imm == -1) {
3335 __ negl(output_register);
3336 }
3337 }
3338 break;
3339 }
3340
3341 case Primitive::kPrimLong: {
3342 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003343 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 } else {
3345 __ movq(output_register, input_register);
3346 if (imm == -1) {
3347 __ negq(output_register);
3348 }
3349 }
3350 break;
3351 }
3352
3353 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003354 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003355 }
3356}
3357
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003358void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 LocationSummary* locations = instruction->GetLocations();
3360 Location second = locations->InAt(1);
3361
3362 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3363 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3364
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003365 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003366 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3367 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003368
3369 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3370
3371 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003372 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003373 __ testl(numerator, numerator);
3374 __ cmov(kGreaterEqual, tmp, numerator);
3375 int shift = CTZ(imm);
3376 __ sarl(tmp, Immediate(shift));
3377
3378 if (imm < 0) {
3379 __ negl(tmp);
3380 }
3381
3382 __ movl(output_register, tmp);
3383 } else {
3384 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3385 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3386
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003387 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 __ addq(rdx, numerator);
3389 __ testq(numerator, numerator);
3390 __ cmov(kGreaterEqual, rdx, numerator);
3391 int shift = CTZ(imm);
3392 __ sarq(rdx, Immediate(shift));
3393
3394 if (imm < 0) {
3395 __ negq(rdx);
3396 }
3397
3398 __ movq(output_register, rdx);
3399 }
3400}
3401
3402void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3403 DCHECK(instruction->IsDiv() || instruction->IsRem());
3404
3405 LocationSummary* locations = instruction->GetLocations();
3406 Location second = locations->InAt(1);
3407
3408 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3409 : locations->GetTemp(0).AsRegister<CpuRegister>();
3410 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3411 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3412 : locations->Out().AsRegister<CpuRegister>();
3413 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3414
3415 DCHECK_EQ(RAX, eax.AsRegister());
3416 DCHECK_EQ(RDX, edx.AsRegister());
3417 if (instruction->IsDiv()) {
3418 DCHECK_EQ(RAX, out.AsRegister());
3419 } else {
3420 DCHECK_EQ(RDX, out.AsRegister());
3421 }
3422
3423 int64_t magic;
3424 int shift;
3425
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003426 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 if (instruction->GetResultType() == Primitive::kPrimInt) {
3428 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3429
3430 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3431
3432 __ movl(numerator, eax);
3433
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 __ movl(eax, Immediate(magic));
3435 __ imull(numerator);
3436
3437 if (imm > 0 && magic < 0) {
3438 __ addl(edx, numerator);
3439 } else if (imm < 0 && magic > 0) {
3440 __ subl(edx, numerator);
3441 }
3442
3443 if (shift != 0) {
3444 __ sarl(edx, Immediate(shift));
3445 }
3446
3447 __ movl(eax, edx);
3448 __ shrl(edx, Immediate(31));
3449 __ addl(edx, eax);
3450
3451 if (instruction->IsRem()) {
3452 __ movl(eax, numerator);
3453 __ imull(edx, Immediate(imm));
3454 __ subl(eax, edx);
3455 __ movl(edx, eax);
3456 } else {
3457 __ movl(eax, edx);
3458 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003459 } else {
3460 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3461
3462 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3463
3464 CpuRegister rax = eax;
3465 CpuRegister rdx = edx;
3466
3467 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3468
3469 // Save the numerator.
3470 __ movq(numerator, rax);
3471
3472 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003473 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475 // RDX:RAX = magic * numerator
3476 __ imulq(numerator);
3477
3478 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003479 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 __ addq(rdx, numerator);
3481 } else if (imm < 0 && magic > 0) {
3482 // RDX -= numerator
3483 __ subq(rdx, numerator);
3484 }
3485
3486 // Shift if needed.
3487 if (shift != 0) {
3488 __ sarq(rdx, Immediate(shift));
3489 }
3490
3491 // RDX += 1 if RDX < 0
3492 __ movq(rax, rdx);
3493 __ shrq(rdx, Immediate(63));
3494 __ addq(rdx, rax);
3495
3496 if (instruction->IsRem()) {
3497 __ movq(rax, numerator);
3498
3499 if (IsInt<32>(imm)) {
3500 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3501 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003502 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 }
3504
3505 __ subq(rax, rdx);
3506 __ movq(rdx, rax);
3507 } else {
3508 __ movq(rax, rdx);
3509 }
3510 }
3511}
3512
Calin Juravlebacfec32014-11-14 15:54:36 +00003513void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3514 DCHECK(instruction->IsDiv() || instruction->IsRem());
3515 Primitive::Type type = instruction->GetResultType();
3516 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3517
3518 bool is_div = instruction->IsDiv();
3519 LocationSummary* locations = instruction->GetLocations();
3520
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3522 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003523
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003526
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003528 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 if (imm == 0) {
3531 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3532 } else if (imm == 1 || imm == -1) {
3533 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003534 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003535 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 } else {
3537 DCHECK(imm <= -2 || imm >= 2);
3538 GenerateDivRemWithAnyConstant(instruction);
3539 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003541 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003543 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003545
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3547 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3548 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3549 // so it's safe to just use negl instead of more complex comparisons.
3550 if (type == Primitive::kPrimInt) {
3551 __ cmpl(second_reg, Immediate(-1));
3552 __ j(kEqual, slow_path->GetEntryLabel());
3553 // edx:eax <- sign-extended of eax
3554 __ cdq();
3555 // eax = quotient, edx = remainder
3556 __ idivl(second_reg);
3557 } else {
3558 __ cmpq(second_reg, Immediate(-1));
3559 __ j(kEqual, slow_path->GetEntryLabel());
3560 // rdx:rax <- sign-extended of rax
3561 __ cqo();
3562 // rax = quotient, rdx = remainder
3563 __ idivq(second_reg);
3564 }
3565 __ Bind(slow_path->GetExitLabel());
3566 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003567}
3568
Calin Juravle7c4954d2014-10-28 16:57:40 +00003569void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3570 LocationSummary* locations =
3571 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3572 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 case Primitive::kPrimInt:
3574 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003575 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003577 locations->SetOut(Location::SameAsFirstInput());
3578 // Intel uses edx:eax as the dividend.
3579 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3581 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3582 // output and request another temp.
3583 if (div->InputAt(1)->IsConstant()) {
3584 locations->AddTemp(Location::RequiresRegister());
3585 }
Calin Juravled0d48522014-11-04 16:40:20 +00003586 break;
3587 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003588
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589 case Primitive::kPrimFloat:
3590 case Primitive::kPrimDouble: {
3591 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003592 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003593 locations->SetOut(Location::SameAsFirstInput());
3594 break;
3595 }
3596
3597 default:
3598 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3599 }
3600}
3601
3602void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3603 LocationSummary* locations = div->GetLocations();
3604 Location first = locations->InAt(0);
3605 Location second = locations->InAt(1);
3606 DCHECK(first.Equals(locations->Out()));
3607
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003608 Primitive::Type type = div->GetResultType();
3609 switch (type) {
3610 case Primitive::kPrimInt:
3611 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003613 break;
3614 }
3615
Calin Juravle7c4954d2014-10-28 16:57:40 +00003616 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003617 if (second.IsFpuRegister()) {
3618 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3619 } else if (second.IsConstant()) {
3620 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003621 codegen_->LiteralFloatAddress(
3622 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003623 } else {
3624 DCHECK(second.IsStackSlot());
3625 __ divss(first.AsFpuRegister<XmmRegister>(),
3626 Address(CpuRegister(RSP), second.GetStackIndex()));
3627 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003628 break;
3629 }
3630
3631 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003632 if (second.IsFpuRegister()) {
3633 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3634 } else if (second.IsConstant()) {
3635 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003636 codegen_->LiteralDoubleAddress(
3637 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003638 } else {
3639 DCHECK(second.IsDoubleStackSlot());
3640 __ divsd(first.AsFpuRegister<XmmRegister>(),
3641 Address(CpuRegister(RSP), second.GetStackIndex()));
3642 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003643 break;
3644 }
3645
3646 default:
3647 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3648 }
3649}
3650
Calin Juravlebacfec32014-11-14 15:54:36 +00003651void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003652 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003653 LocationSummary* locations =
3654 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003655
3656 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 case Primitive::kPrimInt:
3658 case Primitive::kPrimLong: {
3659 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003660 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003661 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3662 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003663 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3664 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3665 // output and request another temp.
3666 if (rem->InputAt(1)->IsConstant()) {
3667 locations->AddTemp(Location::RequiresRegister());
3668 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003669 break;
3670 }
3671
3672 case Primitive::kPrimFloat:
3673 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003674 locations->SetInAt(0, Location::Any());
3675 locations->SetInAt(1, Location::Any());
3676 locations->SetOut(Location::RequiresFpuRegister());
3677 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 break;
3679 }
3680
3681 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003682 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003683 }
3684}
3685
3686void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3687 Primitive::Type type = rem->GetResultType();
3688 switch (type) {
3689 case Primitive::kPrimInt:
3690 case Primitive::kPrimLong: {
3691 GenerateDivRemIntegral(rem);
3692 break;
3693 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003694 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003695 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003696 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003697 break;
3698 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003699 default:
3700 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3701 }
3702}
3703
Calin Juravled0d48522014-11-04 16:40:20 +00003704void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003705 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3706 ? LocationSummary::kCallOnSlowPath
3707 : LocationSummary::kNoCall;
3708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003709 locations->SetInAt(0, Location::Any());
3710 if (instruction->HasUses()) {
3711 locations->SetOut(Location::SameAsFirstInput());
3712 }
3713}
3714
3715void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003716 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003717 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3718 codegen_->AddSlowPath(slow_path);
3719
3720 LocationSummary* locations = instruction->GetLocations();
3721 Location value = locations->InAt(0);
3722
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003723 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003724 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003725 case Primitive::kPrimByte:
3726 case Primitive::kPrimChar:
3727 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003728 case Primitive::kPrimInt: {
3729 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003731 __ j(kEqual, slow_path->GetEntryLabel());
3732 } else if (value.IsStackSlot()) {
3733 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3734 __ j(kEqual, slow_path->GetEntryLabel());
3735 } else {
3736 DCHECK(value.IsConstant()) << value;
3737 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3738 __ jmp(slow_path->GetEntryLabel());
3739 }
3740 }
3741 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003742 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003743 case Primitive::kPrimLong: {
3744 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003746 __ j(kEqual, slow_path->GetEntryLabel());
3747 } else if (value.IsDoubleStackSlot()) {
3748 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3749 __ j(kEqual, slow_path->GetEntryLabel());
3750 } else {
3751 DCHECK(value.IsConstant()) << value;
3752 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3753 __ jmp(slow_path->GetEntryLabel());
3754 }
3755 }
3756 break;
3757 }
3758 default:
3759 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003760 }
Calin Juravled0d48522014-11-04 16:40:20 +00003761}
3762
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3764 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3765
3766 LocationSummary* locations =
3767 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3768
3769 switch (op->GetResultType()) {
3770 case Primitive::kPrimInt:
3771 case Primitive::kPrimLong: {
3772 locations->SetInAt(0, Location::RequiresRegister());
3773 // The shift count needs to be in CL.
3774 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3775 locations->SetOut(Location::SameAsFirstInput());
3776 break;
3777 }
3778 default:
3779 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3780 }
3781}
3782
3783void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3784 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3785
3786 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788 Location second = locations->InAt(1);
3789
3790 switch (op->GetResultType()) {
3791 case Primitive::kPrimInt: {
3792 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794 if (op->IsShl()) {
3795 __ shll(first_reg, second_reg);
3796 } else if (op->IsShr()) {
3797 __ sarl(first_reg, second_reg);
3798 } else {
3799 __ shrl(first_reg, second_reg);
3800 }
3801 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003802 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003803 if (op->IsShl()) {
3804 __ shll(first_reg, imm);
3805 } else if (op->IsShr()) {
3806 __ sarl(first_reg, imm);
3807 } else {
3808 __ shrl(first_reg, imm);
3809 }
3810 }
3811 break;
3812 }
3813 case Primitive::kPrimLong: {
3814 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816 if (op->IsShl()) {
3817 __ shlq(first_reg, second_reg);
3818 } else if (op->IsShr()) {
3819 __ sarq(first_reg, second_reg);
3820 } else {
3821 __ shrq(first_reg, second_reg);
3822 }
3823 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003824 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003825 if (op->IsShl()) {
3826 __ shlq(first_reg, imm);
3827 } else if (op->IsShr()) {
3828 __ sarq(first_reg, imm);
3829 } else {
3830 __ shrq(first_reg, imm);
3831 }
3832 }
3833 break;
3834 }
3835 default:
3836 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003837 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 }
3839}
3840
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003841void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3842 LocationSummary* locations =
3843 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3844
3845 switch (ror->GetResultType()) {
3846 case Primitive::kPrimInt:
3847 case Primitive::kPrimLong: {
3848 locations->SetInAt(0, Location::RequiresRegister());
3849 // The shift count needs to be in CL (unless it is a constant).
3850 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3851 locations->SetOut(Location::SameAsFirstInput());
3852 break;
3853 }
3854 default:
3855 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3856 UNREACHABLE();
3857 }
3858}
3859
3860void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3861 LocationSummary* locations = ror->GetLocations();
3862 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3863 Location second = locations->InAt(1);
3864
3865 switch (ror->GetResultType()) {
3866 case Primitive::kPrimInt:
3867 if (second.IsRegister()) {
3868 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3869 __ rorl(first_reg, second_reg);
3870 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003871 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003872 __ rorl(first_reg, imm);
3873 }
3874 break;
3875 case Primitive::kPrimLong:
3876 if (second.IsRegister()) {
3877 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3878 __ rorq(first_reg, second_reg);
3879 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003880 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003881 __ rorq(first_reg, imm);
3882 }
3883 break;
3884 default:
3885 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3886 UNREACHABLE();
3887 }
3888}
3889
Calin Juravle9aec02f2014-11-18 23:06:35 +00003890void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3891 HandleShift(shl);
3892}
3893
3894void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3895 HandleShift(shl);
3896}
3897
3898void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3899 HandleShift(shr);
3900}
3901
3902void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3903 HandleShift(shr);
3904}
3905
3906void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3907 HandleShift(ushr);
3908}
3909
3910void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3911 HandleShift(ushr);
3912}
3913
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003914void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003915 LocationSummary* locations =
3916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003917 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003918 if (instruction->IsStringAlloc()) {
3919 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3920 } else {
3921 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3922 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3923 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003924 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003925}
3926
3927void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003928 // Note: if heap poisoning is enabled, the entry point takes cares
3929 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003930 if (instruction->IsStringAlloc()) {
3931 // String is allocated through StringFactory. Call NewEmptyString entry point.
3932 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3933 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3934 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3935 __ call(Address(temp, code_offset.SizeValue()));
3936 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3937 } else {
3938 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3939 instruction,
3940 instruction->GetDexPc(),
3941 nullptr);
3942 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3943 DCHECK(!codegen_->IsLeafMethod());
3944 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003945}
3946
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003947void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3950 InvokeRuntimeCallingConvention calling_convention;
3951 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003952 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003953 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003954 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003955}
3956
3957void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3958 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003959 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3960 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003961 // Note: if heap poisoning is enabled, the entry point takes cares
3962 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003963 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3964 instruction,
3965 instruction->GetDexPc(),
3966 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003967 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968
3969 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003970}
3971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003972void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003973 LocationSummary* locations =
3974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003975 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3976 if (location.IsStackSlot()) {
3977 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3978 } else if (location.IsDoubleStackSlot()) {
3979 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3980 }
3981 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003982}
3983
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003984void InstructionCodeGeneratorX86_64::VisitParameterValue(
3985 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003986 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003987}
3988
3989void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3990 LocationSummary* locations =
3991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3992 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3993}
3994
3995void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3996 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3997 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003998}
3999
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004000void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4001 LocationSummary* locations =
4002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4003 locations->SetInAt(0, Location::RequiresRegister());
4004 locations->SetOut(Location::RequiresRegister());
4005}
4006
4007void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4008 LocationSummary* locations = instruction->GetLocations();
4009 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004011 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4012 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4013 } else {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004014 __ movq(locations->Out().AsRegister<CpuRegister>(),
4015 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4016 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
4017 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
4018 instruction->GetIndex() % ImTable::kSize, kX86_64PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004019 }
4020 __ movq(locations->Out().AsRegister<CpuRegister>(),
4021 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4022}
4023
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004024void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004025 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004026 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004027 locations->SetInAt(0, Location::RequiresRegister());
4028 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004029}
4030
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004031void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4032 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004033 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4034 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004035 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004036 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004037 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004038 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004039 break;
4040
4041 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004042 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004043 break;
4044
4045 default:
4046 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004048}
4049
David Brazdil66d126e2015-04-03 16:02:44 +01004050void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4051 LocationSummary* locations =
4052 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4053 locations->SetInAt(0, Location::RequiresRegister());
4054 locations->SetOut(Location::SameAsFirstInput());
4055}
4056
4057void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004058 LocationSummary* locations = bool_not->GetLocations();
4059 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4060 locations->Out().AsRegister<CpuRegister>().AsRegister());
4061 Location out = locations->Out();
4062 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4063}
4064
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004065void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 LocationSummary* locations =
4067 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004068 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004069 locations->SetInAt(i, Location::Any());
4070 }
4071 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004072}
4073
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004074void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004075 LOG(FATAL) << "Unimplemented";
4076}
4077
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004078void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004079 /*
4080 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004081 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004082 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4083 */
4084 switch (kind) {
4085 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004086 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004087 break;
4088 }
4089 case MemBarrierKind::kAnyStore:
4090 case MemBarrierKind::kLoadAny:
4091 case MemBarrierKind::kStoreStore: {
4092 // nop
4093 break;
4094 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004095 case MemBarrierKind::kNTStoreStore:
4096 // Non-Temporal Store/Store needs an explicit fence.
4097 MemoryFence(/* non-temporal */ true);
4098 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004099 }
4100}
4101
4102void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4103 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4104
Roland Levillain0d5a2812015-11-13 10:07:31 +00004105 bool object_field_get_with_read_barrier =
4106 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004107 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004108 new (GetGraph()->GetArena()) LocationSummary(instruction,
4109 object_field_get_with_read_barrier ?
4110 LocationSummary::kCallOnSlowPath :
4111 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004112 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004113 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4114 locations->SetOut(Location::RequiresFpuRegister());
4115 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004116 // The output overlaps for an object field get when read barriers
4117 // are enabled: we do not want the move to overwrite the object's
4118 // location, as we need it to emit the read barrier.
4119 locations->SetOut(
4120 Location::RequiresRegister(),
4121 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004122 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004123 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4124 // We need a temporary register for the read barrier marking slow
4125 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4126 locations->AddTemp(Location::RequiresRegister());
4127 }
Calin Juravle52c48962014-12-16 17:02:57 +00004128}
4129
4130void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4131 const FieldInfo& field_info) {
4132 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4133
4134 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004135 Location base_loc = locations->InAt(0);
4136 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004137 Location out = locations->Out();
4138 bool is_volatile = field_info.IsVolatile();
4139 Primitive::Type field_type = field_info.GetFieldType();
4140 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4141
4142 switch (field_type) {
4143 case Primitive::kPrimBoolean: {
4144 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4145 break;
4146 }
4147
4148 case Primitive::kPrimByte: {
4149 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4150 break;
4151 }
4152
4153 case Primitive::kPrimShort: {
4154 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4155 break;
4156 }
4157
4158 case Primitive::kPrimChar: {
4159 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4160 break;
4161 }
4162
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004163 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004164 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4165 break;
4166 }
4167
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004168 case Primitive::kPrimNot: {
4169 // /* HeapReference<Object> */ out = *(base + offset)
4170 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4171 Location temp_loc = locations->GetTemp(0);
4172 // Note that a potential implicit null check is handled in this
4173 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4174 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4175 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4176 if (is_volatile) {
4177 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4178 }
4179 } else {
4180 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4181 codegen_->MaybeRecordImplicitNullCheck(instruction);
4182 if (is_volatile) {
4183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4184 }
4185 // If read barriers are enabled, emit read barriers other than
4186 // Baker's using a slow path (and also unpoison the loaded
4187 // reference, if heap poisoning is enabled).
4188 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4189 }
4190 break;
4191 }
4192
Calin Juravle52c48962014-12-16 17:02:57 +00004193 case Primitive::kPrimLong: {
4194 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4195 break;
4196 }
4197
4198 case Primitive::kPrimFloat: {
4199 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4200 break;
4201 }
4202
4203 case Primitive::kPrimDouble: {
4204 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4205 break;
4206 }
4207
4208 case Primitive::kPrimVoid:
4209 LOG(FATAL) << "Unreachable type " << field_type;
4210 UNREACHABLE();
4211 }
4212
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004213 if (field_type == Primitive::kPrimNot) {
4214 // Potential implicit null checks, in the case of reference
4215 // fields, are handled in the previous switch statement.
4216 } else {
4217 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004218 }
Roland Levillain4d027112015-07-01 15:41:14 +01004219
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004220 if (is_volatile) {
4221 if (field_type == Primitive::kPrimNot) {
4222 // Memory barriers, in the case of references, are also handled
4223 // in the previous switch statement.
4224 } else {
4225 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4226 }
Roland Levillain4d027112015-07-01 15:41:14 +01004227 }
Calin Juravle52c48962014-12-16 17:02:57 +00004228}
4229
4230void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4231 const FieldInfo& field_info) {
4232 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4233
4234 LocationSummary* locations =
4235 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004236 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004237 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004238 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004239 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004240
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004241 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004242 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004243 if (is_volatile) {
4244 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4245 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4246 } else {
4247 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4248 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004249 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004250 if (is_volatile) {
4251 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4252 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4253 } else {
4254 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4255 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004256 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004257 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004258 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004259 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004260 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004261 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4262 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004263 locations->AddTemp(Location::RequiresRegister());
4264 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265}
4266
Calin Juravle52c48962014-12-16 17:02:57 +00004267void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004268 const FieldInfo& field_info,
4269 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004270 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4271
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004273 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4274 Location value = locations->InAt(1);
4275 bool is_volatile = field_info.IsVolatile();
4276 Primitive::Type field_type = field_info.GetFieldType();
4277 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4278
4279 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004280 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004281 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004282
Mark Mendellea5af682015-10-22 17:35:49 -04004283 bool maybe_record_implicit_null_check_done = false;
4284
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285 switch (field_type) {
4286 case Primitive::kPrimBoolean:
4287 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004288 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004289 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004290 __ movb(Address(base, offset), Immediate(v));
4291 } else {
4292 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4293 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004294 break;
4295 }
4296
4297 case Primitive::kPrimShort:
4298 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004299 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004300 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004301 __ movw(Address(base, offset), Immediate(v));
4302 } else {
4303 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4304 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004305 break;
4306 }
4307
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004308 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004309 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004310 if (value.IsConstant()) {
4311 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004312 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4313 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4314 // Note: if heap poisoning is enabled, no need to poison
4315 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004316 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004317 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004318 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4319 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4320 __ movl(temp, value.AsRegister<CpuRegister>());
4321 __ PoisonHeapReference(temp);
4322 __ movl(Address(base, offset), temp);
4323 } else {
4324 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4325 }
Mark Mendell40741f32015-04-20 22:10:34 -04004326 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004327 break;
4328 }
4329
4330 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004331 if (value.IsConstant()) {
4332 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004333 codegen_->MoveInt64ToAddress(Address(base, offset),
4334 Address(base, offset + sizeof(int32_t)),
4335 v,
4336 instruction);
4337 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004338 } else {
4339 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341 break;
4342 }
4343
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004344 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004345 if (value.IsConstant()) {
4346 int32_t v =
4347 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4348 __ movl(Address(base, offset), Immediate(v));
4349 } else {
4350 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4351 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004352 break;
4353 }
4354
4355 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004356 if (value.IsConstant()) {
4357 int64_t v =
4358 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4359 codegen_->MoveInt64ToAddress(Address(base, offset),
4360 Address(base, offset + sizeof(int32_t)),
4361 v,
4362 instruction);
4363 maybe_record_implicit_null_check_done = true;
4364 } else {
4365 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4366 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004367 break;
4368 }
4369
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370 case Primitive::kPrimVoid:
4371 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004372 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373 }
Calin Juravle52c48962014-12-16 17:02:57 +00004374
Mark Mendellea5af682015-10-22 17:35:49 -04004375 if (!maybe_record_implicit_null_check_done) {
4376 codegen_->MaybeRecordImplicitNullCheck(instruction);
4377 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004378
4379 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4380 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4381 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004382 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004383 }
4384
Calin Juravle52c48962014-12-16 17:02:57 +00004385 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004386 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004387 }
4388}
4389
4390void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4391 HandleFieldSet(instruction, instruction->GetFieldInfo());
4392}
4393
4394void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004395 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004396}
4397
4398void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004399 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400}
4401
4402void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004403 HandleFieldGet(instruction, instruction->GetFieldInfo());
4404}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405
Calin Juravle52c48962014-12-16 17:02:57 +00004406void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4407 HandleFieldGet(instruction);
4408}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004409
Calin Juravle52c48962014-12-16 17:02:57 +00004410void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4411 HandleFieldGet(instruction, instruction->GetFieldInfo());
4412}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004413
Calin Juravle52c48962014-12-16 17:02:57 +00004414void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4415 HandleFieldSet(instruction, instruction->GetFieldInfo());
4416}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004417
Calin Juravle52c48962014-12-16 17:02:57 +00004418void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004419 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420}
4421
Calin Juravlee460d1d2015-09-29 04:52:17 +01004422void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4423 HUnresolvedInstanceFieldGet* instruction) {
4424 FieldAccessCallingConventionX86_64 calling_convention;
4425 codegen_->CreateUnresolvedFieldLocationSummary(
4426 instruction, instruction->GetFieldType(), calling_convention);
4427}
4428
4429void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4430 HUnresolvedInstanceFieldGet* instruction) {
4431 FieldAccessCallingConventionX86_64 calling_convention;
4432 codegen_->GenerateUnresolvedFieldAccess(instruction,
4433 instruction->GetFieldType(),
4434 instruction->GetFieldIndex(),
4435 instruction->GetDexPc(),
4436 calling_convention);
4437}
4438
4439void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4440 HUnresolvedInstanceFieldSet* instruction) {
4441 FieldAccessCallingConventionX86_64 calling_convention;
4442 codegen_->CreateUnresolvedFieldLocationSummary(
4443 instruction, instruction->GetFieldType(), calling_convention);
4444}
4445
4446void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4447 HUnresolvedInstanceFieldSet* instruction) {
4448 FieldAccessCallingConventionX86_64 calling_convention;
4449 codegen_->GenerateUnresolvedFieldAccess(instruction,
4450 instruction->GetFieldType(),
4451 instruction->GetFieldIndex(),
4452 instruction->GetDexPc(),
4453 calling_convention);
4454}
4455
4456void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4457 HUnresolvedStaticFieldGet* instruction) {
4458 FieldAccessCallingConventionX86_64 calling_convention;
4459 codegen_->CreateUnresolvedFieldLocationSummary(
4460 instruction, instruction->GetFieldType(), calling_convention);
4461}
4462
4463void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4464 HUnresolvedStaticFieldGet* instruction) {
4465 FieldAccessCallingConventionX86_64 calling_convention;
4466 codegen_->GenerateUnresolvedFieldAccess(instruction,
4467 instruction->GetFieldType(),
4468 instruction->GetFieldIndex(),
4469 instruction->GetDexPc(),
4470 calling_convention);
4471}
4472
4473void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4474 HUnresolvedStaticFieldSet* instruction) {
4475 FieldAccessCallingConventionX86_64 calling_convention;
4476 codegen_->CreateUnresolvedFieldLocationSummary(
4477 instruction, instruction->GetFieldType(), calling_convention);
4478}
4479
4480void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4481 HUnresolvedStaticFieldSet* instruction) {
4482 FieldAccessCallingConventionX86_64 calling_convention;
4483 codegen_->GenerateUnresolvedFieldAccess(instruction,
4484 instruction->GetFieldType(),
4485 instruction->GetFieldIndex(),
4486 instruction->GetDexPc(),
4487 calling_convention);
4488}
4489
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004490void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004491 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4492 ? LocationSummary::kCallOnSlowPath
4493 : LocationSummary::kNoCall;
4494 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4495 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004496 ? Location::RequiresRegister()
4497 : Location::Any();
4498 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004499 if (instruction->HasUses()) {
4500 locations->SetOut(Location::SameAsFirstInput());
4501 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502}
4503
Calin Juravle2ae48182016-03-16 14:05:09 +00004504void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4505 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004506 return;
4507 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004508 LocationSummary* locations = instruction->GetLocations();
4509 Location obj = locations->InAt(0);
4510
4511 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004512 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004513}
4514
Calin Juravle2ae48182016-03-16 14:05:09 +00004515void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004516 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004517 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004518
4519 LocationSummary* locations = instruction->GetLocations();
4520 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521
4522 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004523 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004524 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004525 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004526 } else {
4527 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004528 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004529 __ jmp(slow_path->GetEntryLabel());
4530 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531 }
4532 __ j(kEqual, slow_path->GetEntryLabel());
4533}
4534
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004535void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004536 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004537}
4538
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004540 bool object_array_get_with_read_barrier =
4541 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004542 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004543 new (GetGraph()->GetArena()) LocationSummary(instruction,
4544 object_array_get_with_read_barrier ?
4545 LocationSummary::kCallOnSlowPath :
4546 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004547 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004548 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004549 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4551 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004552 // The output overlaps for an object array get when read barriers
4553 // are enabled: we do not want the move to overwrite the array's
4554 // location, as we need it to emit the read barrier.
4555 locations->SetOut(
4556 Location::RequiresRegister(),
4557 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004558 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004559 // We need a temporary register for the read barrier marking slow
4560 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4561 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4562 locations->AddTemp(Location::RequiresRegister());
4563 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004564}
4565
4566void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4567 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004568 Location obj_loc = locations->InAt(0);
4569 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004571 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004572 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004574 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004575 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004577 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 if (index.IsConstant()) {
4579 __ movzxb(out, Address(obj,
4580 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4581 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004582 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 }
4584 break;
4585 }
4586
4587 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004588 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 if (index.IsConstant()) {
4590 __ movsxb(out, Address(obj,
4591 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4592 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004593 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 }
4595 break;
4596 }
4597
4598 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004599 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 if (index.IsConstant()) {
4601 __ movsxw(out, Address(obj,
4602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4603 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004604 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 }
4606 break;
4607 }
4608
4609 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004610 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 if (index.IsConstant()) {
4612 __ movzxw(out, Address(obj,
4613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4614 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004615 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 }
4617 break;
4618 }
4619
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004620 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004621 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 if (index.IsConstant()) {
4623 __ movl(out, Address(obj,
4624 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4625 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004626 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 }
4628 break;
4629 }
4630
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004631 case Primitive::kPrimNot: {
4632 static_assert(
4633 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4634 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004635 // /* HeapReference<Object> */ out =
4636 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4637 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4638 Location temp = locations->GetTemp(0);
4639 // Note that a potential implicit null check is handled in this
4640 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4641 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4642 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4643 } else {
4644 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4645 if (index.IsConstant()) {
4646 uint32_t offset =
4647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4648 __ movl(out, Address(obj, offset));
4649 codegen_->MaybeRecordImplicitNullCheck(instruction);
4650 // If read barriers are enabled, emit read barriers other than
4651 // Baker's using a slow path (and also unpoison the loaded
4652 // reference, if heap poisoning is enabled).
4653 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4654 } else {
4655 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4656 codegen_->MaybeRecordImplicitNullCheck(instruction);
4657 // If read barriers are enabled, emit read barriers other than
4658 // Baker's using a slow path (and also unpoison the loaded
4659 // reference, if heap poisoning is enabled).
4660 codegen_->MaybeGenerateReadBarrierSlow(
4661 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4662 }
4663 }
4664 break;
4665 }
4666
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004668 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004669 if (index.IsConstant()) {
4670 __ movq(out, Address(obj,
4671 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4672 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004673 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004674 }
4675 break;
4676 }
4677
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004678 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004679 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004680 if (index.IsConstant()) {
4681 __ movss(out, Address(obj,
4682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4683 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004684 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004685 }
4686 break;
4687 }
4688
4689 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004690 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004691 if (index.IsConstant()) {
4692 __ movsd(out, Address(obj,
4693 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4694 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004695 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004696 }
4697 break;
4698 }
4699
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004701 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004702 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 }
Roland Levillain4d027112015-07-01 15:41:14 +01004704
4705 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004706 // Potential implicit null checks, in the case of reference
4707 // arrays, are handled in the previous switch statement.
4708 } else {
4709 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004710 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711}
4712
4713void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004714 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004715
4716 bool needs_write_barrier =
4717 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004718 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004719 bool object_array_set_with_read_barrier =
4720 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004721
Nicolas Geoffray39468442014-09-02 15:17:15 +01004722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004723 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004724 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004725 LocationSummary::kCallOnSlowPath :
4726 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004727
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004728 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004729 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4730 if (Primitive::IsFloatingPointType(value_type)) {
4731 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004732 } else {
4733 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4734 }
4735
4736 if (needs_write_barrier) {
4737 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004738
4739 // This first temporary register is possibly used for heap
4740 // reference poisoning and/or read barrier emission too.
4741 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004742 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744}
4745
4746void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4747 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004748 Location array_loc = locations->InAt(0);
4749 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004751 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004752 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004753 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004754 bool needs_write_barrier =
4755 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004756 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4757 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4758 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759
4760 switch (value_type) {
4761 case Primitive::kPrimBoolean:
4762 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4764 Address address = index.IsConstant()
4765 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4766 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4767 if (value.IsRegister()) {
4768 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004769 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004770 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004771 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004772 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 break;
4774 }
4775
4776 case Primitive::kPrimShort:
4777 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004778 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4779 Address address = index.IsConstant()
4780 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4781 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4782 if (value.IsRegister()) {
4783 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004785 DCHECK(value.IsConstant()) << value;
4786 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004788 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004789 break;
4790 }
4791
4792 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4794 Address address = index.IsConstant()
4795 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4796 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004797
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004798 if (!value.IsRegister()) {
4799 // Just setting null.
4800 DCHECK(instruction->InputAt(2)->IsNullConstant());
4801 DCHECK(value.IsConstant()) << value;
4802 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004805 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004807 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808
4809 DCHECK(needs_write_barrier);
4810 CpuRegister register_value = value.AsRegister<CpuRegister>();
4811 NearLabel done, not_null, do_put;
4812 SlowPathCode* slow_path = nullptr;
4813 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004814 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004815 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4816 codegen_->AddSlowPath(slow_path);
4817 if (instruction->GetValueCanBeNull()) {
4818 __ testl(register_value, register_value);
4819 __ j(kNotEqual, &not_null);
4820 __ movl(address, Immediate(0));
4821 codegen_->MaybeRecordImplicitNullCheck(instruction);
4822 __ jmp(&done);
4823 __ Bind(&not_null);
4824 }
4825
Roland Levillain0d5a2812015-11-13 10:07:31 +00004826 if (kEmitCompilerReadBarrier) {
4827 // When read barriers are enabled, the type checking
4828 // instrumentation requires two read barriers:
4829 //
4830 // __ movl(temp2, temp);
4831 // // /* HeapReference<Class> */ temp = temp->component_type_
4832 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004833 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004834 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4835 //
4836 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4837 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004838 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004839 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4840 //
4841 // __ cmpl(temp, temp2);
4842 //
4843 // However, the second read barrier may trash `temp`, as it
4844 // is a temporary register, and as such would not be saved
4845 // along with live registers before calling the runtime (nor
4846 // restored afterwards). So in this case, we bail out and
4847 // delegate the work to the array set slow path.
4848 //
4849 // TODO: Extend the register allocator to support a new
4850 // "(locally) live temp" location so as to avoid always
4851 // going into the slow path when read barriers are enabled.
4852 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004854 // /* HeapReference<Class> */ temp = array->klass_
4855 __ movl(temp, Address(array, class_offset));
4856 codegen_->MaybeRecordImplicitNullCheck(instruction);
4857 __ MaybeUnpoisonHeapReference(temp);
4858
4859 // /* HeapReference<Class> */ temp = temp->component_type_
4860 __ movl(temp, Address(temp, component_offset));
4861 // If heap poisoning is enabled, no need to unpoison `temp`
4862 // nor the object reference in `register_value->klass`, as
4863 // we are comparing two poisoned references.
4864 __ cmpl(temp, Address(register_value, class_offset));
4865
4866 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4867 __ j(kEqual, &do_put);
4868 // If heap poisoning is enabled, the `temp` reference has
4869 // not been unpoisoned yet; unpoison it now.
4870 __ MaybeUnpoisonHeapReference(temp);
4871
4872 // /* HeapReference<Class> */ temp = temp->super_class_
4873 __ movl(temp, Address(temp, super_offset));
4874 // If heap poisoning is enabled, no need to unpoison
4875 // `temp`, as we are comparing against null below.
4876 __ testl(temp, temp);
4877 __ j(kNotEqual, slow_path->GetEntryLabel());
4878 __ Bind(&do_put);
4879 } else {
4880 __ j(kNotEqual, slow_path->GetEntryLabel());
4881 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 }
4883 }
4884
4885 if (kPoisonHeapReferences) {
4886 __ movl(temp, register_value);
4887 __ PoisonHeapReference(temp);
4888 __ movl(address, temp);
4889 } else {
4890 __ movl(address, register_value);
4891 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004892 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 codegen_->MaybeRecordImplicitNullCheck(instruction);
4894 }
4895
4896 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4897 codegen_->MarkGCCard(
4898 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4899 __ Bind(&done);
4900
4901 if (slow_path != nullptr) {
4902 __ Bind(slow_path->GetExitLabel());
4903 }
4904
4905 break;
4906 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004907
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004908 case Primitive::kPrimInt: {
4909 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4910 Address address = index.IsConstant()
4911 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4912 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4913 if (value.IsRegister()) {
4914 __ movl(address, value.AsRegister<CpuRegister>());
4915 } else {
4916 DCHECK(value.IsConstant()) << value;
4917 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4918 __ movl(address, Immediate(v));
4919 }
4920 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 break;
4922 }
4923
4924 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004925 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4926 Address address = index.IsConstant()
4927 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4928 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4929 if (value.IsRegister()) {
4930 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004933 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004934 Address address_high = index.IsConstant()
4935 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4936 offset + sizeof(int32_t))
4937 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4938 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004939 }
4940 break;
4941 }
4942
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004943 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004944 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4945 Address address = index.IsConstant()
4946 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4947 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004948 if (value.IsFpuRegister()) {
4949 __ movss(address, value.AsFpuRegister<XmmRegister>());
4950 } else {
4951 DCHECK(value.IsConstant());
4952 int32_t v =
4953 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4954 __ movl(address, Immediate(v));
4955 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004956 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004957 break;
4958 }
4959
4960 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004961 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4962 Address address = index.IsConstant()
4963 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4964 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004965 if (value.IsFpuRegister()) {
4966 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4967 codegen_->MaybeRecordImplicitNullCheck(instruction);
4968 } else {
4969 int64_t v =
4970 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4971 Address address_high = index.IsConstant()
4972 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4973 offset + sizeof(int32_t))
4974 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4975 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4976 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004977 break;
4978 }
4979
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004980 case Primitive::kPrimVoid:
4981 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004982 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983 }
4984}
4985
4986void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004987 LocationSummary* locations =
4988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004989 locations->SetInAt(0, Location::RequiresRegister());
4990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004991}
4992
4993void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4994 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004995 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004996 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4997 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004998 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004999 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005000}
5001
5002void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005003 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5004 ? LocationSummary::kCallOnSlowPath
5005 : LocationSummary::kNoCall;
5006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005007 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005008 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005009 if (instruction->HasUses()) {
5010 locations->SetOut(Location::SameAsFirstInput());
5011 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005012}
5013
5014void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5015 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005016 Location index_loc = locations->InAt(0);
5017 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005018 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005019 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020
Mark Mendell99dbd682015-04-22 16:18:52 -04005021 if (length_loc.IsConstant()) {
5022 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5023 if (index_loc.IsConstant()) {
5024 // BCE will remove the bounds check if we are guarenteed to pass.
5025 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5026 if (index < 0 || index >= length) {
5027 codegen_->AddSlowPath(slow_path);
5028 __ jmp(slow_path->GetEntryLabel());
5029 } else {
5030 // Some optimization after BCE may have generated this, and we should not
5031 // generate a bounds check if it is a valid range.
5032 }
5033 return;
5034 }
5035
5036 // We have to reverse the jump condition because the length is the constant.
5037 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5038 __ cmpl(index_reg, Immediate(length));
5039 codegen_->AddSlowPath(slow_path);
5040 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005041 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005042 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5043 if (index_loc.IsConstant()) {
5044 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5045 __ cmpl(length, Immediate(value));
5046 } else {
5047 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5048 }
5049 codegen_->AddSlowPath(slow_path);
5050 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005051 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052}
5053
5054void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5055 CpuRegister card,
5056 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005057 CpuRegister value,
5058 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005059 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005060 if (value_can_be_null) {
5061 __ testl(value, value);
5062 __ j(kEqual, &is_null);
5063 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005064 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5065 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 __ movq(temp, object);
5067 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005068 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005069 if (value_can_be_null) {
5070 __ Bind(&is_null);
5071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072}
5073
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005074void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005075 LOG(FATAL) << "Unimplemented";
5076}
5077
5078void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005079 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5080}
5081
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005082void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5084}
5085
5086void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005087 HBasicBlock* block = instruction->GetBlock();
5088 if (block->GetLoopInformation() != nullptr) {
5089 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5090 // The back edge will generate the suspend check.
5091 return;
5092 }
5093 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5094 // The goto will generate the suspend check.
5095 return;
5096 }
5097 GenerateSuspendCheck(instruction, nullptr);
5098}
5099
5100void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5101 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005102 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005103 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5104 if (slow_path == nullptr) {
5105 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5106 instruction->SetSlowPath(slow_path);
5107 codegen_->AddSlowPath(slow_path);
5108 if (successor != nullptr) {
5109 DCHECK(successor->IsLoopHeader());
5110 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5111 }
5112 } else {
5113 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5114 }
5115
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005116 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5117 /* no_rip */ true),
5118 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005119 if (successor == nullptr) {
5120 __ j(kNotEqual, slow_path->GetEntryLabel());
5121 __ Bind(slow_path->GetReturnLabel());
5122 } else {
5123 __ j(kEqual, codegen_->GetLabelOf(successor));
5124 __ jmp(slow_path->GetEntryLabel());
5125 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005126}
5127
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005128X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5129 return codegen_->GetAssembler();
5130}
5131
5132void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005133 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005134 Location source = move->GetSource();
5135 Location destination = move->GetDestination();
5136
5137 if (source.IsRegister()) {
5138 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005140 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005141 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143 } else {
5144 DCHECK(destination.IsDoubleStackSlot());
5145 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005146 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005147 }
5148 } else if (source.IsStackSlot()) {
5149 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005150 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005151 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005152 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005154 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005155 } else {
5156 DCHECK(destination.IsStackSlot());
5157 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5158 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5159 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005160 } else if (source.IsDoubleStackSlot()) {
5161 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005162 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005163 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005164 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005165 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5166 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005167 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005168 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005169 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5170 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5171 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005172 } else if (source.IsConstant()) {
5173 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005174 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5175 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005176 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005177 if (value == 0) {
5178 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5179 } else {
5180 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5181 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005182 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005183 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005184 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005185 }
5186 } else if (constant->IsLongConstant()) {
5187 int64_t value = constant->AsLongConstant()->GetValue();
5188 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005189 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005190 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005191 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005192 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005193 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005194 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005195 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005196 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005197 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005198 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005199 } else {
5200 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005201 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005202 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5203 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005204 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005206 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005207 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005208 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005209 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005210 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 } else {
5212 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005213 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005215 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005216 } else if (source.IsFpuRegister()) {
5217 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 } else if (destination.IsStackSlot()) {
5220 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005221 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005222 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005223 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005224 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005225 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005226 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005227 }
5228}
5229
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005230void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005231 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005232 __ movl(Address(CpuRegister(RSP), mem), reg);
5233 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005234}
5235
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005236void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005237 ScratchRegisterScope ensure_scratch(
5238 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5239
5240 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5241 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5242 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5243 Address(CpuRegister(RSP), mem2 + stack_offset));
5244 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5245 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5246 CpuRegister(ensure_scratch.GetRegister()));
5247}
5248
Mark Mendell8a1c7282015-06-29 15:41:28 -04005249void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5250 __ movq(CpuRegister(TMP), reg1);
5251 __ movq(reg1, reg2);
5252 __ movq(reg2, CpuRegister(TMP));
5253}
5254
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005255void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5256 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5257 __ movq(Address(CpuRegister(RSP), mem), reg);
5258 __ movq(reg, CpuRegister(TMP));
5259}
5260
5261void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5262 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005263 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005264
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005265 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5266 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5267 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5268 Address(CpuRegister(RSP), mem2 + stack_offset));
5269 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5270 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5271 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005272}
5273
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005274void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5275 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5276 __ movss(Address(CpuRegister(RSP), mem), reg);
5277 __ movd(reg, CpuRegister(TMP));
5278}
5279
5280void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5281 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5282 __ movsd(Address(CpuRegister(RSP), mem), reg);
5283 __ movd(reg, CpuRegister(TMP));
5284}
5285
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005286void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005287 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005288 Location source = move->GetSource();
5289 Location destination = move->GetDestination();
5290
5291 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005292 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005293 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005294 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005295 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005296 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005297 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005298 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5299 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005300 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005301 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005302 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005303 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5304 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005306 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5307 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5308 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005309 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005310 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005312 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005313 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005314 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005315 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005316 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005317 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005318 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005319 }
5320}
5321
5322
5323void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5324 __ pushq(CpuRegister(reg));
5325}
5326
5327
5328void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5329 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005330}
5331
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005332void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005333 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005334 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5335 Immediate(mirror::Class::kStatusInitialized));
5336 __ j(kLess, slow_path->GetEntryLabel());
5337 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005338 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005339}
5340
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005341HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5342 HLoadClass::LoadKind desired_class_load_kind) {
5343 if (kEmitCompilerReadBarrier) {
5344 switch (desired_class_load_kind) {
5345 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5346 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5347 case HLoadClass::LoadKind::kBootImageAddress:
5348 // TODO: Implement for read barrier.
5349 return HLoadClass::LoadKind::kDexCacheViaMethod;
5350 default:
5351 break;
5352 }
5353 }
5354 switch (desired_class_load_kind) {
5355 case HLoadClass::LoadKind::kReferrersClass:
5356 break;
5357 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5358 DCHECK(!GetCompilerOptions().GetCompilePic());
5359 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5360 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5361 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5362 DCHECK(GetCompilerOptions().GetCompilePic());
5363 break;
5364 case HLoadClass::LoadKind::kBootImageAddress:
5365 break;
5366 case HLoadClass::LoadKind::kDexCacheAddress:
5367 DCHECK(Runtime::Current()->UseJitCompilation());
5368 break;
5369 case HLoadClass::LoadKind::kDexCachePcRelative:
5370 DCHECK(!Runtime::Current()->UseJitCompilation());
5371 break;
5372 case HLoadClass::LoadKind::kDexCacheViaMethod:
5373 break;
5374 }
5375 return desired_class_load_kind;
5376}
5377
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005378void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005379 if (cls->NeedsAccessCheck()) {
5380 InvokeRuntimeCallingConvention calling_convention;
5381 CodeGenerator::CreateLoadClassLocationSummary(
5382 cls,
5383 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5384 Location::RegisterLocation(RAX),
5385 /* code_generator_supports_read_barrier */ true);
5386 return;
5387 }
5388
5389 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5390 ? LocationSummary::kCallOnSlowPath
5391 : LocationSummary::kNoCall;
5392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5393 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5394 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5395 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5396 locations->SetInAt(0, Location::RequiresRegister());
5397 }
5398 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005399}
5400
5401void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005402 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005403 if (cls->NeedsAccessCheck()) {
5404 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5405 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5406 cls,
5407 cls->GetDexPc(),
5408 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005409 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005410 return;
5411 }
5412
Roland Levillain0d5a2812015-11-13 10:07:31 +00005413 Location out_loc = locations->Out();
5414 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005415
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005416 bool generate_null_check = false;
5417 switch (cls->GetLoadKind()) {
5418 case HLoadClass::LoadKind::kReferrersClass: {
5419 DCHECK(!cls->CanCallRuntime());
5420 DCHECK(!cls->MustGenerateClinitCheck());
5421 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5422 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5423 GenerateGcRootFieldLoad(
5424 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5425 break;
5426 }
5427 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5428 DCHECK(!kEmitCompilerReadBarrier);
5429 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5430 codegen_->RecordTypePatch(cls);
5431 break;
5432 case HLoadClass::LoadKind::kBootImageAddress: {
5433 DCHECK(!kEmitCompilerReadBarrier);
5434 DCHECK_NE(cls->GetAddress(), 0u);
5435 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5436 __ movl(out, Immediate(address)); // Zero-extended.
5437 codegen_->RecordSimplePatch();
5438 break;
5439 }
5440 case HLoadClass::LoadKind::kDexCacheAddress: {
5441 DCHECK_NE(cls->GetAddress(), 0u);
5442 // /* GcRoot<mirror::Class> */ out = *address
5443 if (IsUint<32>(cls->GetAddress())) {
5444 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5445 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005446 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005447 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5448 __ movq(out, Immediate(cls->GetAddress()));
5449 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005450 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005451 generate_null_check = !cls->IsInDexCache();
5452 break;
5453 }
5454 case HLoadClass::LoadKind::kDexCachePcRelative: {
5455 uint32_t offset = cls->GetDexCacheElementOffset();
5456 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5457 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5458 /* no_rip */ false);
5459 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5460 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5461 generate_null_check = !cls->IsInDexCache();
5462 break;
5463 }
5464 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5465 // /* GcRoot<mirror::Class>[] */ out =
5466 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5467 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5468 __ movq(out,
5469 Address(current_method,
5470 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5471 // /* GcRoot<mirror::Class> */ out = out[type_index]
5472 GenerateGcRootFieldLoad(
5473 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5474 generate_null_check = !cls->IsInDexCache();
5475 break;
5476 }
5477 default:
5478 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5479 UNREACHABLE();
5480 }
5481
5482 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5483 DCHECK(cls->CanCallRuntime());
5484 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5485 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5486 codegen_->AddSlowPath(slow_path);
5487 if (generate_null_check) {
5488 __ testl(out, out);
5489 __ j(kEqual, slow_path->GetEntryLabel());
5490 }
5491 if (cls->MustGenerateClinitCheck()) {
5492 GenerateClassInitializationCheck(slow_path, out);
5493 } else {
5494 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005495 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005496 }
5497}
5498
5499void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5500 LocationSummary* locations =
5501 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5502 locations->SetInAt(0, Location::RequiresRegister());
5503 if (check->HasUses()) {
5504 locations->SetOut(Location::SameAsFirstInput());
5505 }
5506}
5507
5508void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005509 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005510 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005511 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005512 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005513 GenerateClassInitializationCheck(slow_path,
5514 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515}
5516
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005517HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5518 HLoadString::LoadKind desired_string_load_kind) {
5519 if (kEmitCompilerReadBarrier) {
5520 switch (desired_string_load_kind) {
5521 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5522 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5523 case HLoadString::LoadKind::kBootImageAddress:
5524 // TODO: Implement for read barrier.
5525 return HLoadString::LoadKind::kDexCacheViaMethod;
5526 default:
5527 break;
5528 }
5529 }
5530 switch (desired_string_load_kind) {
5531 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5532 DCHECK(!GetCompilerOptions().GetCompilePic());
5533 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5534 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5535 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5536 DCHECK(GetCompilerOptions().GetCompilePic());
5537 break;
5538 case HLoadString::LoadKind::kBootImageAddress:
5539 break;
5540 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005541 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005542 break;
5543 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005544 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005545 break;
5546 case HLoadString::LoadKind::kDexCacheViaMethod:
5547 break;
5548 }
5549 return desired_string_load_kind;
5550}
5551
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005552void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005553 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005554 ? LocationSummary::kCallOnSlowPath
5555 : LocationSummary::kNoCall;
5556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005557 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5558 locations->SetInAt(0, Location::RequiresRegister());
5559 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005560 locations->SetOut(Location::RequiresRegister());
5561}
5562
5563void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005564 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005565 Location out_loc = locations->Out();
5566 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005568 switch (load->GetLoadKind()) {
5569 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5570 DCHECK(!kEmitCompilerReadBarrier);
5571 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5572 codegen_->RecordStringPatch(load);
5573 return; // No dex cache slow path.
5574 }
5575 case HLoadString::LoadKind::kBootImageAddress: {
5576 DCHECK(!kEmitCompilerReadBarrier);
5577 DCHECK_NE(load->GetAddress(), 0u);
5578 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5579 __ movl(out, Immediate(address)); // Zero-extended.
5580 codegen_->RecordSimplePatch();
5581 return; // No dex cache slow path.
5582 }
5583 case HLoadString::LoadKind::kDexCacheAddress: {
5584 DCHECK_NE(load->GetAddress(), 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005585 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005586 if (IsUint<32>(load->GetAddress())) {
5587 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5588 GenerateGcRootFieldLoad(load, out_loc, address);
5589 } else {
5590 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5591 __ movq(out, Immediate(load->GetAddress()));
5592 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5593 }
5594 break;
5595 }
5596 case HLoadString::LoadKind::kDexCachePcRelative: {
5597 uint32_t offset = load->GetDexCacheElementOffset();
5598 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5599 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5600 /* no_rip */ false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005601 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005602 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5603 break;
5604 }
5605 case HLoadString::LoadKind::kDexCacheViaMethod: {
5606 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5607
5608 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5609 GenerateGcRootFieldLoad(
5610 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5611 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5612 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5613 // /* GcRoot<mirror::String> */ out = out[string_index]
5614 GenerateGcRootFieldLoad(
5615 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5616 break;
5617 }
5618 default:
5619 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5620 UNREACHABLE();
5621 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005622
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005623 if (!load->IsInDexCache()) {
5624 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5625 codegen_->AddSlowPath(slow_path);
5626 __ testl(out, out);
5627 __ j(kEqual, slow_path->GetEntryLabel());
5628 __ Bind(slow_path->GetExitLabel());
5629 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005630}
5631
David Brazdilcb1c0552015-08-04 16:22:25 +01005632static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005633 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5634 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005635}
5636
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005637void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5638 LocationSummary* locations =
5639 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5640 locations->SetOut(Location::RequiresRegister());
5641}
5642
5643void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005644 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5645}
5646
5647void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5648 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5649}
5650
5651void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5652 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005653}
5654
5655void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5656 LocationSummary* locations =
5657 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5658 InvokeRuntimeCallingConvention calling_convention;
5659 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5660}
5661
5662void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005663 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5664 instruction,
5665 instruction->GetDexPc(),
5666 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005667 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005668}
5669
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005670static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5671 return kEmitCompilerReadBarrier &&
5672 (kUseBakerReadBarrier ||
5673 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5674 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5675 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5676}
5677
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005678void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005679 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005680 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5681 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005682 case TypeCheckKind::kExactCheck:
5683 case TypeCheckKind::kAbstractClassCheck:
5684 case TypeCheckKind::kClassHierarchyCheck:
5685 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005686 call_kind =
5687 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005688 break;
5689 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005690 case TypeCheckKind::kUnresolvedCheck:
5691 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005692 call_kind = LocationSummary::kCallOnSlowPath;
5693 break;
5694 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005695
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005696 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005697 locations->SetInAt(0, Location::RequiresRegister());
5698 locations->SetInAt(1, Location::Any());
5699 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5700 locations->SetOut(Location::RequiresRegister());
5701 // When read barriers are enabled, we need a temporary register for
5702 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005703 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005704 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005706}
5707
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005708void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005709 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005710 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005711 Location obj_loc = locations->InAt(0);
5712 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005713 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005714 Location out_loc = locations->Out();
5715 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005716 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005717 locations->GetTemp(0) :
5718 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005719 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5721 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5722 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005723 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005725
5726 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005727 // Avoid null check if we know obj is not null.
5728 if (instruction->MustDoNullCheck()) {
5729 __ testl(obj, obj);
5730 __ j(kEqual, &zero);
5731 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005732
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005734 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005736 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 case TypeCheckKind::kExactCheck: {
5738 if (cls.IsRegister()) {
5739 __ cmpl(out, cls.AsRegister<CpuRegister>());
5740 } else {
5741 DCHECK(cls.IsStackSlot()) << cls;
5742 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5743 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005744 if (zero.IsLinked()) {
5745 // Classes must be equal for the instanceof to succeed.
5746 __ j(kNotEqual, &zero);
5747 __ movl(out, Immediate(1));
5748 __ jmp(&done);
5749 } else {
5750 __ setcc(kEqual, out);
5751 // setcc only sets the low byte.
5752 __ andl(out, Immediate(1));
5753 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 break;
5755 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005757 case TypeCheckKind::kAbstractClassCheck: {
5758 // If the class is abstract, we eagerly fetch the super class of the
5759 // object to avoid doing a comparison we know will fail.
5760 NearLabel loop, success;
5761 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005762 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005763 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 __ testl(out, out);
5765 // If `out` is null, we use it for the result, and jump to `done`.
5766 __ j(kEqual, &done);
5767 if (cls.IsRegister()) {
5768 __ cmpl(out, cls.AsRegister<CpuRegister>());
5769 } else {
5770 DCHECK(cls.IsStackSlot()) << cls;
5771 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5772 }
5773 __ j(kNotEqual, &loop);
5774 __ movl(out, Immediate(1));
5775 if (zero.IsLinked()) {
5776 __ jmp(&done);
5777 }
5778 break;
5779 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 case TypeCheckKind::kClassHierarchyCheck: {
5782 // Walk over the class hierarchy to find a match.
5783 NearLabel loop, success;
5784 __ Bind(&loop);
5785 if (cls.IsRegister()) {
5786 __ cmpl(out, cls.AsRegister<CpuRegister>());
5787 } else {
5788 DCHECK(cls.IsStackSlot()) << cls;
5789 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5790 }
5791 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005793 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 __ testl(out, out);
5795 __ j(kNotEqual, &loop);
5796 // If `out` is null, we use it for the result, and jump to `done`.
5797 __ jmp(&done);
5798 __ Bind(&success);
5799 __ movl(out, Immediate(1));
5800 if (zero.IsLinked()) {
5801 __ jmp(&done);
5802 }
5803 break;
5804 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005807 // Do an exact check.
5808 NearLabel exact_check;
5809 if (cls.IsRegister()) {
5810 __ cmpl(out, cls.AsRegister<CpuRegister>());
5811 } else {
5812 DCHECK(cls.IsStackSlot()) << cls;
5813 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5814 }
5815 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005818 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 __ testl(out, out);
5820 // If `out` is null, we use it for the result, and jump to `done`.
5821 __ j(kEqual, &done);
5822 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5823 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005824 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825 __ movl(out, Immediate(1));
5826 __ jmp(&done);
5827 break;
5828 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005829
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830 case TypeCheckKind::kArrayCheck: {
5831 if (cls.IsRegister()) {
5832 __ cmpl(out, cls.AsRegister<CpuRegister>());
5833 } else {
5834 DCHECK(cls.IsStackSlot()) << cls;
5835 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5836 }
5837 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005838 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5839 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 codegen_->AddSlowPath(slow_path);
5841 __ j(kNotEqual, slow_path->GetEntryLabel());
5842 __ movl(out, Immediate(1));
5843 if (zero.IsLinked()) {
5844 __ jmp(&done);
5845 }
5846 break;
5847 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005848
Calin Juravle98893e12015-10-02 21:05:03 +01005849 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005850 case TypeCheckKind::kInterfaceCheck: {
5851 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005852 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005853 // cases.
5854 //
5855 // We cannot directly call the InstanceofNonTrivial runtime
5856 // entry point without resorting to a type checking slow path
5857 // here (i.e. by calling InvokeRuntime directly), as it would
5858 // require to assign fixed registers for the inputs of this
5859 // HInstanceOf instruction (following the runtime calling
5860 // convention), which might be cluttered by the potential first
5861 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005862 //
5863 // TODO: Introduce a new runtime entry point taking the object
5864 // to test (instead of its class) as argument, and let it deal
5865 // with the read barrier issues. This will let us refactor this
5866 // case of the `switch` code as it was previously (with a direct
5867 // call to the runtime not using a type checking slow path).
5868 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869 DCHECK(locations->OnlyCallsOnSlowPath());
5870 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5871 /* is_fatal */ false);
5872 codegen_->AddSlowPath(slow_path);
5873 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874 if (zero.IsLinked()) {
5875 __ jmp(&done);
5876 }
5877 break;
5878 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005879 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005880
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005882 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 __ xorl(out, out);
5884 }
5885
5886 if (done.IsLinked()) {
5887 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005888 }
5889
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005890 if (slow_path != nullptr) {
5891 __ Bind(slow_path->GetExitLabel());
5892 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005893}
5894
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005895void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5897 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005898 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5899 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 case TypeCheckKind::kExactCheck:
5901 case TypeCheckKind::kAbstractClassCheck:
5902 case TypeCheckKind::kClassHierarchyCheck:
5903 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005904 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5905 LocationSummary::kCallOnSlowPath :
5906 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907 break;
5908 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909 case TypeCheckKind::kUnresolvedCheck:
5910 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005911 call_kind = LocationSummary::kCallOnSlowPath;
5912 break;
5913 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5915 locations->SetInAt(0, Location::RequiresRegister());
5916 locations->SetInAt(1, Location::Any());
5917 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5918 locations->AddTemp(Location::RequiresRegister());
5919 // When read barriers are enabled, we need an additional temporary
5920 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005921 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005924}
5925
5926void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005927 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005928 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005929 Location obj_loc = locations->InAt(0);
5930 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005931 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932 Location temp_loc = locations->GetTemp(0);
5933 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005934 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005935 locations->GetTemp(1) :
5936 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5938 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5939 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5940 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 bool is_type_check_slow_path_fatal =
5943 (type_check_kind == TypeCheckKind::kExactCheck ||
5944 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5945 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5946 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5947 !instruction->CanThrowIntoCatchBlock();
5948 SlowPathCode* type_check_slow_path =
5949 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5950 is_type_check_slow_path_fatal);
5951 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005952
Roland Levillain0d5a2812015-11-13 10:07:31 +00005953 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005954 case TypeCheckKind::kExactCheck:
5955 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005956 NearLabel done;
5957 // Avoid null check if we know obj is not null.
5958 if (instruction->MustDoNullCheck()) {
5959 __ testl(obj, obj);
5960 __ j(kEqual, &done);
5961 }
5962
5963 // /* HeapReference<Class> */ temp = obj->klass_
5964 GenerateReferenceLoadTwoRegisters(
5965 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5966
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967 if (cls.IsRegister()) {
5968 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5969 } else {
5970 DCHECK(cls.IsStackSlot()) << cls;
5971 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5972 }
5973 // Jump to slow path for throwing the exception or doing a
5974 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005976 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 break;
5978 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005979
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005981 NearLabel done;
5982 // Avoid null check if we know obj is not null.
5983 if (instruction->MustDoNullCheck()) {
5984 __ testl(obj, obj);
5985 __ j(kEqual, &done);
5986 }
5987
5988 // /* HeapReference<Class> */ temp = obj->klass_
5989 GenerateReferenceLoadTwoRegisters(
5990 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5991
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 // If the class is abstract, we eagerly fetch the super class of the
5993 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005997 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998
5999 // If the class reference currently in `temp` is not null, jump
6000 // to the `compare_classes` label to compare it with the checked
6001 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006002 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006003 __ j(kNotEqual, &compare_classes);
6004 // Otherwise, jump to the slow path to throw the exception.
6005 //
6006 // But before, move back the object's class into `temp` before
6007 // going into the slow path, as it has been overwritten in the
6008 // meantime.
6009 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006010 GenerateReferenceLoadTwoRegisters(
6011 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012 __ jmp(type_check_slow_path->GetEntryLabel());
6013
6014 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006015 if (cls.IsRegister()) {
6016 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6017 } else {
6018 DCHECK(cls.IsStackSlot()) << cls;
6019 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6020 }
6021 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006022 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006023 break;
6024 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006026 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006027 NearLabel done;
6028 // Avoid null check if we know obj is not null.
6029 if (instruction->MustDoNullCheck()) {
6030 __ testl(obj, obj);
6031 __ j(kEqual, &done);
6032 }
6033
6034 // /* HeapReference<Class> */ temp = obj->klass_
6035 GenerateReferenceLoadTwoRegisters(
6036 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6037
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006038 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006039 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006040 __ Bind(&loop);
6041 if (cls.IsRegister()) {
6042 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6043 } else {
6044 DCHECK(cls.IsStackSlot()) << cls;
6045 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6046 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006047 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006048
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006050 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006051
6052 // If the class reference currently in `temp` is not null, jump
6053 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006054 __ testl(temp, temp);
6055 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056 // Otherwise, jump to the slow path to throw the exception.
6057 //
6058 // But before, move back the object's class into `temp` before
6059 // going into the slow path, as it has been overwritten in the
6060 // meantime.
6061 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006062 GenerateReferenceLoadTwoRegisters(
6063 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006064 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006065 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006066 break;
6067 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006069 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006070 // We cannot use a NearLabel here, as its range might be too
6071 // short in some cases when read barriers are enabled. This has
6072 // been observed for instance when the code emitted for this
6073 // case uses high x86-64 registers (R8-R15).
6074 Label done;
6075 // Avoid null check if we know obj is not null.
6076 if (instruction->MustDoNullCheck()) {
6077 __ testl(obj, obj);
6078 __ j(kEqual, &done);
6079 }
6080
6081 // /* HeapReference<Class> */ temp = obj->klass_
6082 GenerateReferenceLoadTwoRegisters(
6083 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6084
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006085 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006087 if (cls.IsRegister()) {
6088 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6089 } else {
6090 DCHECK(cls.IsStackSlot()) << cls;
6091 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6092 }
6093 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006094
6095 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006097 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098
6099 // If the component type is not null (i.e. the object is indeed
6100 // an array), jump to label `check_non_primitive_component_type`
6101 // to further check that this component type is not a primitive
6102 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006103 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 __ j(kNotEqual, &check_non_primitive_component_type);
6105 // Otherwise, jump to the slow path to throw the exception.
6106 //
6107 // But before, move back the object's class into `temp` before
6108 // going into the slow path, as it has been overwritten in the
6109 // meantime.
6110 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006111 GenerateReferenceLoadTwoRegisters(
6112 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006113 __ jmp(type_check_slow_path->GetEntryLabel());
6114
6115 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006116 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117 __ j(kEqual, &done);
6118 // Same comment as above regarding `temp` and the slow path.
6119 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006120 GenerateReferenceLoadTwoRegisters(
6121 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006123 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124 break;
6125 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006126
Calin Juravle98893e12015-10-02 21:05:03 +01006127 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006128 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006129 NearLabel done;
6130 // Avoid null check if we know obj is not null.
6131 if (instruction->MustDoNullCheck()) {
6132 __ testl(obj, obj);
6133 __ j(kEqual, &done);
6134 }
6135
6136 // /* HeapReference<Class> */ temp = obj->klass_
6137 GenerateReferenceLoadTwoRegisters(
6138 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6139
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006140 // We always go into the type check slow path for the unresolved
6141 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142 //
6143 // We cannot directly call the CheckCast runtime entry point
6144 // without resorting to a type checking slow path here (i.e. by
6145 // calling InvokeRuntime directly), as it would require to
6146 // assign fixed registers for the inputs of this HInstanceOf
6147 // instruction (following the runtime calling convention), which
6148 // might be cluttered by the potential first read barrier
6149 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006150 //
6151 // TODO: Introduce a new runtime entry point taking the object
6152 // to test (instead of its class) as argument, and let it deal
6153 // with the read barrier issues. This will let us refactor this
6154 // case of the `switch` code as it was previously (with a direct
6155 // call to the runtime not using a type checking slow path).
6156 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006158 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 break;
6160 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006163}
6164
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006165void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6166 LocationSummary* locations =
6167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6168 InvokeRuntimeCallingConvention calling_convention;
6169 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6170}
6171
6172void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006173 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6174 : QUICK_ENTRY_POINT(pUnlockObject),
6175 instruction,
6176 instruction->GetDexPc(),
6177 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006178 if (instruction->IsEnter()) {
6179 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6180 } else {
6181 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6182 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006183}
6184
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006185void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6186void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6187void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6188
6189void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6190 LocationSummary* locations =
6191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6192 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6193 || instruction->GetResultType() == Primitive::kPrimLong);
6194 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006195 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006196 locations->SetOut(Location::SameAsFirstInput());
6197}
6198
6199void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6200 HandleBitwiseOperation(instruction);
6201}
6202
6203void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6204 HandleBitwiseOperation(instruction);
6205}
6206
6207void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6208 HandleBitwiseOperation(instruction);
6209}
6210
6211void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6212 LocationSummary* locations = instruction->GetLocations();
6213 Location first = locations->InAt(0);
6214 Location second = locations->InAt(1);
6215 DCHECK(first.Equals(locations->Out()));
6216
6217 if (instruction->GetResultType() == Primitive::kPrimInt) {
6218 if (second.IsRegister()) {
6219 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006220 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006221 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006222 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006223 } else {
6224 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006225 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006226 }
6227 } else if (second.IsConstant()) {
6228 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6229 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006230 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006231 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006232 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006233 } else {
6234 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006235 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006236 }
6237 } else {
6238 Address address(CpuRegister(RSP), second.GetStackIndex());
6239 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006240 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006241 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006242 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006243 } else {
6244 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006245 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006246 }
6247 }
6248 } else {
6249 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006250 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6251 bool second_is_constant = false;
6252 int64_t value = 0;
6253 if (second.IsConstant()) {
6254 second_is_constant = true;
6255 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006256 }
Mark Mendell40741f32015-04-20 22:10:34 -04006257 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006258
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006259 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006260 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006261 if (is_int32_value) {
6262 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6263 } else {
6264 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6265 }
6266 } else if (second.IsDoubleStackSlot()) {
6267 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006268 } else {
6269 __ andq(first_reg, second.AsRegister<CpuRegister>());
6270 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006271 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006272 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006273 if (is_int32_value) {
6274 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6275 } else {
6276 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6277 }
6278 } else if (second.IsDoubleStackSlot()) {
6279 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006280 } else {
6281 __ orq(first_reg, second.AsRegister<CpuRegister>());
6282 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006283 } else {
6284 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006285 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006286 if (is_int32_value) {
6287 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6288 } else {
6289 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6290 }
6291 } else if (second.IsDoubleStackSlot()) {
6292 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006293 } else {
6294 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6295 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006296 }
6297 }
6298}
6299
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006300void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6301 Location out,
6302 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006303 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006304 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6305 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006306 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006307 if (kUseBakerReadBarrier) {
6308 // Load with fast path based Baker's read barrier.
6309 // /* HeapReference<Object> */ out = *(out + offset)
6310 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006311 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006312 } else {
6313 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006314 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006315 // in the following move operation, as we will need it for the
6316 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006317 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006318 // /* HeapReference<Object> */ out = *(out + offset)
6319 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006320 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006321 }
6322 } else {
6323 // Plain load with no read barrier.
6324 // /* HeapReference<Object> */ out = *(out + offset)
6325 __ movl(out_reg, Address(out_reg, offset));
6326 __ MaybeUnpoisonHeapReference(out_reg);
6327 }
6328}
6329
6330void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6331 Location out,
6332 Location obj,
6333 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006334 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006335 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6336 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6337 if (kEmitCompilerReadBarrier) {
6338 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006339 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006340 // Load with fast path based Baker's read barrier.
6341 // /* HeapReference<Object> */ out = *(obj + offset)
6342 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006343 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006344 } else {
6345 // Load with slow path based read barrier.
6346 // /* HeapReference<Object> */ out = *(obj + offset)
6347 __ movl(out_reg, Address(obj_reg, offset));
6348 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6349 }
6350 } else {
6351 // Plain load with no read barrier.
6352 // /* HeapReference<Object> */ out = *(obj + offset)
6353 __ movl(out_reg, Address(obj_reg, offset));
6354 __ MaybeUnpoisonHeapReference(out_reg);
6355 }
6356}
6357
6358void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6359 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006360 const Address& address,
6361 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006362 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6363 if (kEmitCompilerReadBarrier) {
6364 if (kUseBakerReadBarrier) {
6365 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6366 // Baker's read barrier are used:
6367 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006368 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006369 // if (Thread::Current()->GetIsGcMarking()) {
6370 // root = ReadBarrier::Mark(root)
6371 // }
6372
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006373 // /* GcRoot<mirror::Object> */ root = *address
6374 __ movl(root_reg, address);
6375 if (fixup_label != nullptr) {
6376 __ Bind(fixup_label);
6377 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006378 static_assert(
6379 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6380 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6381 "have different sizes.");
6382 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6383 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6384 "have different sizes.");
6385
6386 // Slow path used to mark the GC root `root`.
6387 SlowPathCode* slow_path =
6388 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6389 codegen_->AddSlowPath(slow_path);
6390
6391 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6392 /* no_rip */ true),
6393 Immediate(0));
6394 __ j(kNotEqual, slow_path->GetEntryLabel());
6395 __ Bind(slow_path->GetExitLabel());
6396 } else {
6397 // GC root loaded through a slow path for read barriers other
6398 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006399 // /* GcRoot<mirror::Object>* */ root = address
6400 __ leaq(root_reg, address);
6401 if (fixup_label != nullptr) {
6402 __ Bind(fixup_label);
6403 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006404 // /* mirror::Object* */ root = root->Read()
6405 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6406 }
6407 } else {
6408 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006409 // /* GcRoot<mirror::Object> */ root = *address
6410 __ movl(root_reg, address);
6411 if (fixup_label != nullptr) {
6412 __ Bind(fixup_label);
6413 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006414 // Note that GC roots are not affected by heap poisoning, thus we
6415 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006416 }
6417}
6418
6419void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6420 Location ref,
6421 CpuRegister obj,
6422 uint32_t offset,
6423 Location temp,
6424 bool needs_null_check) {
6425 DCHECK(kEmitCompilerReadBarrier);
6426 DCHECK(kUseBakerReadBarrier);
6427
6428 // /* HeapReference<Object> */ ref = *(obj + offset)
6429 Address src(obj, offset);
6430 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6431}
6432
6433void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6434 Location ref,
6435 CpuRegister obj,
6436 uint32_t data_offset,
6437 Location index,
6438 Location temp,
6439 bool needs_null_check) {
6440 DCHECK(kEmitCompilerReadBarrier);
6441 DCHECK(kUseBakerReadBarrier);
6442
Roland Levillain3d312422016-06-23 13:53:42 +01006443 static_assert(
6444 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6445 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006446 // /* HeapReference<Object> */ ref =
6447 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6448 Address src = index.IsConstant() ?
6449 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6450 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6451 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6452}
6453
6454void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6455 Location ref,
6456 CpuRegister obj,
6457 const Address& src,
6458 Location temp,
6459 bool needs_null_check) {
6460 DCHECK(kEmitCompilerReadBarrier);
6461 DCHECK(kUseBakerReadBarrier);
6462
6463 // In slow path based read barriers, the read barrier call is
6464 // inserted after the original load. However, in fast path based
6465 // Baker's read barriers, we need to perform the load of
6466 // mirror::Object::monitor_ *before* the original reference load.
6467 // This load-load ordering is required by the read barrier.
6468 // The fast path/slow path (for Baker's algorithm) should look like:
6469 //
6470 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6471 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6472 // HeapReference<Object> ref = *src; // Original reference load.
6473 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6474 // if (is_gray) {
6475 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6476 // }
6477 //
6478 // Note: the original implementation in ReadBarrier::Barrier is
6479 // slightly more complex as:
6480 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006481 // the high-bits of rb_state, which are expected to be all zeroes
6482 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6483 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006484 // - it performs additional checks that we do not do here for
6485 // performance reasons.
6486
6487 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6488 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6489 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6490
6491 // /* int32_t */ monitor = obj->monitor_
6492 __ movl(temp_reg, Address(obj, monitor_offset));
6493 if (needs_null_check) {
6494 MaybeRecordImplicitNullCheck(instruction);
6495 }
6496 // /* LockWord */ lock_word = LockWord(monitor)
6497 static_assert(sizeof(LockWord) == sizeof(int32_t),
6498 "art::LockWord and int32_t have different sizes.");
6499 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6500 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6501 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6502 static_assert(
6503 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6504 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6505
6506 // Load fence to prevent load-load reordering.
6507 // Note that this is a no-op, thanks to the x86-64 memory model.
6508 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6509
6510 // The actual reference load.
6511 // /* HeapReference<Object> */ ref = *src
6512 __ movl(ref_reg, src);
6513
6514 // Object* ref = ref_addr->AsMirrorPtr()
6515 __ MaybeUnpoisonHeapReference(ref_reg);
6516
6517 // Slow path used to mark the object `ref` when it is gray.
6518 SlowPathCode* slow_path =
6519 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6520 AddSlowPath(slow_path);
6521
6522 // if (rb_state == ReadBarrier::gray_ptr_)
6523 // ref = ReadBarrier::Mark(ref);
6524 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6525 __ j(kEqual, slow_path->GetEntryLabel());
6526 __ Bind(slow_path->GetExitLabel());
6527}
6528
6529void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6530 Location out,
6531 Location ref,
6532 Location obj,
6533 uint32_t offset,
6534 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006535 DCHECK(kEmitCompilerReadBarrier);
6536
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006537 // Insert a slow path based read barrier *after* the reference load.
6538 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 // If heap poisoning is enabled, the unpoisoning of the loaded
6540 // reference will be carried out by the runtime within the slow
6541 // path.
6542 //
6543 // Note that `ref` currently does not get unpoisoned (when heap
6544 // poisoning is enabled), which is alright as the `ref` argument is
6545 // not used by the artReadBarrierSlow entry point.
6546 //
6547 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6548 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6549 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6550 AddSlowPath(slow_path);
6551
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 __ jmp(slow_path->GetEntryLabel());
6553 __ Bind(slow_path->GetExitLabel());
6554}
6555
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006556void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6557 Location out,
6558 Location ref,
6559 Location obj,
6560 uint32_t offset,
6561 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006563 // Baker's read barriers shall be handled by the fast path
6564 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6565 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566 // If heap poisoning is enabled, unpoisoning will be taken care of
6567 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006568 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006569 } else if (kPoisonHeapReferences) {
6570 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6571 }
6572}
6573
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006574void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6575 Location out,
6576 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577 DCHECK(kEmitCompilerReadBarrier);
6578
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006579 // Insert a slow path based read barrier *after* the GC root load.
6580 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581 // Note that GC roots are not affected by heap poisoning, so we do
6582 // not need to do anything special for this here.
6583 SlowPathCode* slow_path =
6584 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6585 AddSlowPath(slow_path);
6586
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 __ jmp(slow_path->GetEntryLabel());
6588 __ Bind(slow_path->GetExitLabel());
6589}
6590
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006591void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006592 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006593 LOG(FATAL) << "Unreachable";
6594}
6595
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006596void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006597 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006598 LOG(FATAL) << "Unreachable";
6599}
6600
Mark Mendellfe57faa2015-09-18 09:26:15 -04006601// Simple implementation of packed switch - generate cascaded compare/jumps.
6602void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6603 LocationSummary* locations =
6604 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6605 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006606 locations->AddTemp(Location::RequiresRegister());
6607 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006608}
6609
6610void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6611 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006612 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006613 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006614 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6615 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6616 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006617 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6618
6619 // Should we generate smaller inline compare/jumps?
6620 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6621 // Figure out the correct compare values and jump conditions.
6622 // Handle the first compare/branch as a special case because it might
6623 // jump to the default case.
6624 DCHECK_GT(num_entries, 2u);
6625 Condition first_condition;
6626 uint32_t index;
6627 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6628 if (lower_bound != 0) {
6629 first_condition = kLess;
6630 __ cmpl(value_reg_in, Immediate(lower_bound));
6631 __ j(first_condition, codegen_->GetLabelOf(default_block));
6632 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6633
6634 index = 1;
6635 } else {
6636 // Handle all the compare/jumps below.
6637 first_condition = kBelow;
6638 index = 0;
6639 }
6640
6641 // Handle the rest of the compare/jumps.
6642 for (; index + 1 < num_entries; index += 2) {
6643 int32_t compare_to_value = lower_bound + index + 1;
6644 __ cmpl(value_reg_in, Immediate(compare_to_value));
6645 // Jump to successors[index] if value < case_value[index].
6646 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6647 // Jump to successors[index + 1] if value == case_value[index + 1].
6648 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6649 }
6650
6651 if (index != num_entries) {
6652 // There are an odd number of entries. Handle the last one.
6653 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006654 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006655 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6656 }
6657
6658 // And the default for any other value.
6659 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6660 __ jmp(codegen_->GetLabelOf(default_block));
6661 }
6662 return;
6663 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006664
6665 // Remove the bias, if needed.
6666 Register value_reg_out = value_reg_in.AsRegister();
6667 if (lower_bound != 0) {
6668 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6669 value_reg_out = temp_reg.AsRegister();
6670 }
6671 CpuRegister value_reg(value_reg_out);
6672
6673 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006674 __ cmpl(value_reg, Immediate(num_entries - 1));
6675 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006676
Mark Mendell9c86b482015-09-18 13:36:07 -04006677 // We are in the range of the table.
6678 // Load the address of the jump table in the constant area.
6679 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006680
Mark Mendell9c86b482015-09-18 13:36:07 -04006681 // Load the (signed) offset from the jump table.
6682 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6683
6684 // Add the offset to the address of the table base.
6685 __ addq(temp_reg, base_reg);
6686
6687 // And jump.
6688 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006689}
6690
Aart Bikc5d47542016-01-27 17:00:35 -08006691void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6692 if (value == 0) {
6693 __ xorl(dest, dest);
6694 } else {
6695 __ movl(dest, Immediate(value));
6696 }
6697}
6698
Mark Mendell92e83bf2015-05-07 11:25:03 -04006699void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6700 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006701 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006702 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006703 } else if (IsUint<32>(value)) {
6704 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006705 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6706 } else {
6707 __ movq(dest, Immediate(value));
6708 }
6709}
6710
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006711void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6712 if (value == 0) {
6713 __ xorps(dest, dest);
6714 } else {
6715 __ movss(dest, LiteralInt32Address(value));
6716 }
6717}
6718
6719void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6720 if (value == 0) {
6721 __ xorpd(dest, dest);
6722 } else {
6723 __ movsd(dest, LiteralInt64Address(value));
6724 }
6725}
6726
6727void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6728 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6729}
6730
6731void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6732 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6733}
6734
Aart Bika19616e2016-02-01 18:57:58 -08006735void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6736 if (value == 0) {
6737 __ testl(dest, dest);
6738 } else {
6739 __ cmpl(dest, Immediate(value));
6740 }
6741}
6742
6743void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6744 if (IsInt<32>(value)) {
6745 if (value == 0) {
6746 __ testq(dest, dest);
6747 } else {
6748 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6749 }
6750 } else {
6751 // Value won't fit in an int.
6752 __ cmpq(dest, LiteralInt64Address(value));
6753 }
6754}
6755
Mark Mendellcfa410b2015-05-25 16:02:44 -04006756void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6757 DCHECK(dest.IsDoubleStackSlot());
6758 if (IsInt<32>(value)) {
6759 // Can move directly as an int32 constant.
6760 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6761 Immediate(static_cast<int32_t>(value)));
6762 } else {
6763 Load64BitValue(CpuRegister(TMP), value);
6764 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6765 }
6766}
6767
Mark Mendell9c86b482015-09-18 13:36:07 -04006768/**
6769 * Class to handle late fixup of offsets into constant area.
6770 */
6771class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6772 public:
6773 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6774 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6775
6776 protected:
6777 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6778
6779 CodeGeneratorX86_64* codegen_;
6780
6781 private:
6782 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6783 // Patch the correct offset for the instruction. We use the address of the
6784 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6785 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6786 int32_t relative_position = constant_offset - pos;
6787
6788 // Patch in the right value.
6789 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6790 }
6791
6792 // Location in constant area that the fixup refers to.
6793 size_t offset_into_constant_area_;
6794};
6795
6796/**
6797 t * Class to handle late fixup of offsets to a jump table that will be created in the
6798 * constant area.
6799 */
6800class JumpTableRIPFixup : public RIPFixup {
6801 public:
6802 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6803 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6804
6805 void CreateJumpTable() {
6806 X86_64Assembler* assembler = codegen_->GetAssembler();
6807
6808 // Ensure that the reference to the jump table has the correct offset.
6809 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6810 SetOffset(offset_in_constant_table);
6811
6812 // Compute the offset from the start of the function to this jump table.
6813 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6814
6815 // Populate the jump table with the correct values for the jump table.
6816 int32_t num_entries = switch_instr_->GetNumEntries();
6817 HBasicBlock* block = switch_instr_->GetBlock();
6818 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6819 // The value that we want is the target offset - the position of the table.
6820 for (int32_t i = 0; i < num_entries; i++) {
6821 HBasicBlock* b = successors[i];
6822 Label* l = codegen_->GetLabelOf(b);
6823 DCHECK(l->IsBound());
6824 int32_t offset_to_block = l->Position() - current_table_offset;
6825 assembler->AppendInt32(offset_to_block);
6826 }
6827 }
6828
6829 private:
6830 const HPackedSwitch* switch_instr_;
6831};
6832
Mark Mendellf55c3e02015-03-26 21:07:46 -04006833void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6834 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006835 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006836 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6837 // 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 -04006838 assembler->Align(4, 0);
6839 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006840
6841 // Populate any jump tables.
6842 for (auto jump_table : fixups_to_jump_tables_) {
6843 jump_table->CreateJumpTable();
6844 }
6845
6846 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006847 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006848 }
6849
6850 // And finish up.
6851 CodeGenerator::Finalize(allocator);
6852}
6853
Mark Mendellf55c3e02015-03-26 21:07:46 -04006854Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6855 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6856 return Address::RIP(fixup);
6857}
6858
6859Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6860 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6861 return Address::RIP(fixup);
6862}
6863
6864Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6865 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6866 return Address::RIP(fixup);
6867}
6868
6869Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6870 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6871 return Address::RIP(fixup);
6872}
6873
Andreas Gampe85b62f22015-09-09 13:15:38 -07006874// TODO: trg as memory.
6875void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6876 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006877 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006878 return;
6879 }
6880
6881 DCHECK_NE(type, Primitive::kPrimVoid);
6882
6883 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6884 if (trg.Equals(return_loc)) {
6885 return;
6886 }
6887
6888 // Let the parallel move resolver take care of all of this.
6889 HParallelMove parallel_move(GetGraph()->GetArena());
6890 parallel_move.AddMove(return_loc, trg, type, nullptr);
6891 GetMoveResolver()->EmitNativeCode(&parallel_move);
6892}
6893
Mark Mendell9c86b482015-09-18 13:36:07 -04006894Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6895 // Create a fixup to be used to create and address the jump table.
6896 JumpTableRIPFixup* table_fixup =
6897 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6898
6899 // We have to populate the jump tables.
6900 fixups_to_jump_tables_.push_back(table_fixup);
6901 return Address::RIP(table_fixup);
6902}
6903
Mark Mendellea5af682015-10-22 17:35:49 -04006904void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6905 const Address& addr_high,
6906 int64_t v,
6907 HInstruction* instruction) {
6908 if (IsInt<32>(v)) {
6909 int32_t v_32 = v;
6910 __ movq(addr_low, Immediate(v_32));
6911 MaybeRecordImplicitNullCheck(instruction);
6912 } else {
6913 // Didn't fit in a register. Do it in pieces.
6914 int32_t low_v = Low32Bits(v);
6915 int32_t high_v = High32Bits(v);
6916 __ movl(addr_low, Immediate(low_v));
6917 MaybeRecordImplicitNullCheck(instruction);
6918 __ movl(addr_high, Immediate(high_v));
6919 }
6920}
6921
Roland Levillain4d027112015-07-01 15:41:14 +01006922#undef __
6923
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006924} // namespace x86_64
6925} // namespace art