blob: a8da5f2ea50f854dab1565850c7fec483d26f691 [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);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000207 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
208 instruction_,
209 instruction_->GetDexPc(),
210 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000211 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212 }
213
Alexandre Rames8158f282015-08-07 10:26:17 +0100214 bool IsFatal() const OVERRIDE { return true; }
215
Alexandre Rames9931f312015-06-19 14:47:01 +0100216 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100218 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100219 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
220};
221
Andreas Gampe85b62f22015-09-09 13:15:38 -0700222class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 LoadClassSlowPathX86_64(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
227 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000228 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
230 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000234 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000237 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000241 x86_64_codegen->InvokeRuntime(do_clinit_ ?
242 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
243 QUICK_ENTRY_POINT(pInitializeType),
244 at_,
245 dex_pc_,
246 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000247 if (do_clinit_) {
248 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
249 } else {
250 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
251 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255 if (out.IsValid()) {
256 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000257 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 }
259
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100261 __ jmp(GetExitLabel());
262 }
263
Alexandre Rames9931f312015-06-19 14:47:01 +0100264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
265
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100266 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000267 // The class this slow path will load.
268 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100269
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100281};
282
Andreas Gampe85b62f22015-09-09 13:15:38 -0700283class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000286
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290
Roland Levillain0d5a2812015-11-13 10:07:31 +0000291 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000293 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000298 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000302 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000303 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000304 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000305 __ jmp(GetExitLabel());
306 }
307
Alexandre Rames9931f312015-06-19 14:47:01 +0100308 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
309
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000310 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000311 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
312};
313
Andreas Gampe85b62f22015-09-09 13:15:38 -0700314class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
322 : locations->Out();
323 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 DCHECK(instruction_->IsCheckCast()
325 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Roland Levillain0d5a2812015-11-13 10:07:31 +0000327 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 if (!is_fatal_) {
331 SaveLiveRegisters(codegen, locations);
332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
334 // We're moving two locations to locations that could overlap, so we need a parallel
335 // move resolver.
336 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000337 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100338 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000339 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100340 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100342 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
343 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000346 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
347 instruction_,
348 dex_pc,
349 this);
350 CheckEntrypointTypes<
351 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352 } else {
353 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000354 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
355 instruction_,
356 dex_pc,
357 this);
358 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 if (!is_fatal_) {
362 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000363 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 RestoreLiveRegisters(codegen, locations);
367 __ jmp(GetExitLabel());
368 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000369 }
370
Alexandre Rames9931f312015-06-19 14:47:01 +0100371 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
372
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 bool IsFatal() const OVERRIDE { return is_fatal_; }
374
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377
378 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
379};
380
Andreas Gampe85b62f22015-09-09 13:15:38 -0700381class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 public:
Aart Bik42249c32016-01-07 15:33:50 -0800383 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000387 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000390 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800391 instruction_,
392 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000393 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
401};
402
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100403class ArraySetSlowPathX86_64 : public SlowPathCode {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConvention calling_convention;
413 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
414 parallel_move.AddMove(
415 locations->InAt(0),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
417 Primitive::kPrimNot,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(1),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
422 Primitive::kPrimInt,
423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(2),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
427 Primitive::kPrimNot,
428 nullptr);
429 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
430
Roland Levillain0d5a2812015-11-13 10:07:31 +0000431 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
432 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
433 instruction_,
434 instruction_->GetDexPc(),
435 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000436 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 RestoreLiveRegisters(codegen, locations);
438 __ jmp(GetExitLabel());
439 }
440
441 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
442
443 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
445};
446
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000447// Slow path marking an object during a read barrier.
448class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
449 public:
450 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000451 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000452 DCHECK(kEmitCompilerReadBarrier);
453 }
454
455 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
456
457 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
458 LocationSummary* locations = instruction_->GetLocations();
459 Register reg_out = out_.AsRegister<Register>();
460 DCHECK(locations->CanCall());
461 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
462 DCHECK(instruction_->IsInstanceFieldGet() ||
463 instruction_->IsStaticFieldGet() ||
464 instruction_->IsArrayGet() ||
465 instruction_->IsLoadClass() ||
466 instruction_->IsLoadString() ||
467 instruction_->IsInstanceOf() ||
468 instruction_->IsCheckCast())
469 << "Unexpected instruction in read barrier marking slow path: "
470 << instruction_->DebugName();
471
472 __ Bind(GetEntryLabel());
473 SaveLiveRegisters(codegen, locations);
474
475 InvokeRuntimeCallingConvention calling_convention;
476 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
477 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
478 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
479 instruction_,
480 instruction_->GetDexPc(),
481 this);
482 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
483 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
484
485 RestoreLiveRegisters(codegen, locations);
486 __ jmp(GetExitLabel());
487 }
488
489 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000490 const Location out_;
491 const Location obj_;
492
493 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
494};
495
Roland Levillain0d5a2812015-11-13 10:07:31 +0000496// Slow path generating a read barrier for a heap reference.
497class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
498 public:
499 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
500 Location out,
501 Location ref,
502 Location obj,
503 uint32_t offset,
504 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000505 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000506 out_(out),
507 ref_(ref),
508 obj_(obj),
509 offset_(offset),
510 index_(index) {
511 DCHECK(kEmitCompilerReadBarrier);
512 // If `obj` is equal to `out` or `ref`, it means the initial
513 // object has been overwritten by (or after) the heap object
514 // reference load to be instrumented, e.g.:
515 //
516 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000517 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000518 //
519 // In that case, we have lost the information about the original
520 // object, and the emitted read barrier cannot work properly.
521 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
522 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
523}
524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
526 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
527 LocationSummary* locations = instruction_->GetLocations();
528 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
529 DCHECK(locations->CanCall());
530 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
531 DCHECK(!instruction_->IsInvoke() ||
532 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000533 instruction_->GetLocations()->Intrinsified()))
534 << "Unexpected instruction in read barrier for heap reference slow path: "
535 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000536
537 __ Bind(GetEntryLabel());
538 SaveLiveRegisters(codegen, locations);
539
540 // We may have to change the index's value, but as `index_` is a
541 // constant member (like other "inputs" of this slow path),
542 // introduce a copy of it, `index`.
543 Location index = index_;
544 if (index_.IsValid()) {
545 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
546 if (instruction_->IsArrayGet()) {
547 // Compute real offset and store it in index_.
548 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
549 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
550 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
551 // We are about to change the value of `index_reg` (see the
552 // calls to art::x86_64::X86_64Assembler::shll and
553 // art::x86_64::X86_64Assembler::AddImmediate below), but it
554 // has not been saved by the previous call to
555 // art::SlowPathCode::SaveLiveRegisters, as it is a
556 // callee-save register --
557 // art::SlowPathCode::SaveLiveRegisters does not consider
558 // callee-save registers, as it has been designed with the
559 // assumption that callee-save registers are supposed to be
560 // handled by the called function. So, as a callee-save
561 // register, `index_reg` _would_ eventually be saved onto
562 // the stack, but it would be too late: we would have
563 // changed its value earlier. Therefore, we manually save
564 // it here into another freely available register,
565 // `free_reg`, chosen of course among the caller-save
566 // registers (as a callee-save `free_reg` register would
567 // exhibit the same problem).
568 //
569 // Note we could have requested a temporary register from
570 // the register allocator instead; but we prefer not to, as
571 // this is a slow path, and we know we can find a
572 // caller-save register that is available.
573 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
574 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
575 index_reg = free_reg;
576 index = Location::RegisterLocation(index_reg);
577 } else {
578 // The initial register stored in `index_` has already been
579 // saved in the call to art::SlowPathCode::SaveLiveRegisters
580 // (as it is not a callee-save register), so we can freely
581 // use it.
582 }
583 // Shifting the index value contained in `index_reg` by the
584 // scale factor (2) cannot overflow in practice, as the
585 // runtime is unable to allocate object arrays with a size
586 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
587 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
588 static_assert(
589 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
590 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
591 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
592 } else {
593 DCHECK(instruction_->IsInvoke());
594 DCHECK(instruction_->GetLocations()->Intrinsified());
595 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
596 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
597 << instruction_->AsInvoke()->GetIntrinsic();
598 DCHECK_EQ(offset_, 0U);
599 DCHECK(index_.IsRegister());
600 }
601 }
602
603 // We're moving two or three locations to locations that could
604 // overlap, so we need a parallel move resolver.
605 InvokeRuntimeCallingConvention calling_convention;
606 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
607 parallel_move.AddMove(ref_,
608 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
609 Primitive::kPrimNot,
610 nullptr);
611 parallel_move.AddMove(obj_,
612 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
613 Primitive::kPrimNot,
614 nullptr);
615 if (index.IsValid()) {
616 parallel_move.AddMove(index,
617 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
618 Primitive::kPrimInt,
619 nullptr);
620 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
621 } else {
622 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
623 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
624 }
625 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
626 instruction_,
627 instruction_->GetDexPc(),
628 this);
629 CheckEntrypointTypes<
630 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
631 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
632
633 RestoreLiveRegisters(codegen, locations);
634 __ jmp(GetExitLabel());
635 }
636
637 const char* GetDescription() const OVERRIDE {
638 return "ReadBarrierForHeapReferenceSlowPathX86_64";
639 }
640
641 private:
642 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
643 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
644 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
645 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
646 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
647 return static_cast<CpuRegister>(i);
648 }
649 }
650 // We shall never fail to find a free caller-save register, as
651 // there are more than two core caller-save registers on x86-64
652 // (meaning it is possible to find one which is different from
653 // `ref` and `obj`).
654 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
655 LOG(FATAL) << "Could not find a free caller-save register";
656 UNREACHABLE();
657 }
658
Roland Levillain0d5a2812015-11-13 10:07:31 +0000659 const Location out_;
660 const Location ref_;
661 const Location obj_;
662 const uint32_t offset_;
663 // An additional location containing an index to an array.
664 // Only used for HArrayGet and the UnsafeGetObject &
665 // UnsafeGetObjectVolatile intrinsics.
666 const Location index_;
667
668 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
669};
670
671// Slow path generating a read barrier for a GC root.
672class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
673 public:
674 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000675 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000676 DCHECK(kEmitCompilerReadBarrier);
677 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
680 LocationSummary* locations = instruction_->GetLocations();
681 DCHECK(locations->CanCall());
682 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000683 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
684 << "Unexpected instruction in read barrier for GC root slow path: "
685 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000686
687 __ Bind(GetEntryLabel());
688 SaveLiveRegisters(codegen, locations);
689
690 InvokeRuntimeCallingConvention calling_convention;
691 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
692 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
693 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
694 instruction_,
695 instruction_->GetDexPc(),
696 this);
697 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
698 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
699
700 RestoreLiveRegisters(codegen, locations);
701 __ jmp(GetExitLabel());
702 }
703
704 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
705
706 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707 const Location out_;
708 const Location root_;
709
710 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
711};
712
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100713#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700714// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
715#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100716
Roland Levillain4fa13f62015-07-06 18:11:54 +0100717inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700718 switch (cond) {
719 case kCondEQ: return kEqual;
720 case kCondNE: return kNotEqual;
721 case kCondLT: return kLess;
722 case kCondLE: return kLessEqual;
723 case kCondGT: return kGreater;
724 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700725 case kCondB: return kBelow;
726 case kCondBE: return kBelowEqual;
727 case kCondA: return kAbove;
728 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700729 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 LOG(FATAL) << "Unreachable";
731 UNREACHABLE();
732}
733
Aart Bike9f37602015-10-09 11:15:55 -0700734// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100735inline Condition X86_64FPCondition(IfCondition cond) {
736 switch (cond) {
737 case kCondEQ: return kEqual;
738 case kCondNE: return kNotEqual;
739 case kCondLT: return kBelow;
740 case kCondLE: return kBelowEqual;
741 case kCondGT: return kAbove;
742 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700743 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 };
745 LOG(FATAL) << "Unreachable";
746 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700747}
748
Vladimir Markodc151b22015-10-15 18:02:30 +0100749HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
750 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
751 MethodReference target_method ATTRIBUTE_UNUSED) {
752 switch (desired_dispatch_info.code_ptr_location) {
753 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
754 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
755 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
756 return HInvokeStaticOrDirect::DispatchInfo {
757 desired_dispatch_info.method_load_kind,
758 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
759 desired_dispatch_info.method_load_data,
760 0u
761 };
762 default:
763 return desired_dispatch_info;
764 }
765}
766
Serguei Katkov288c7a82016-05-16 11:53:15 +0600767Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
768 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800769 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000770 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
771 switch (invoke->GetMethodLoadKind()) {
772 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
773 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000774 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000775 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000776 break;
777 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000778 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000779 break;
780 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
781 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
782 break;
783 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
784 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
785 method_patches_.emplace_back(invoke->GetTargetMethod());
786 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
787 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000788 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000789 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000790 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000791 // Bind a new fixup label at the end of the "movl" insn.
792 uint32_t offset = invoke->GetDexCacheArrayOffset();
793 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000794 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000795 }
Vladimir Marko58155012015-08-19 12:49:41 +0000796 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000797 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000798 Register method_reg;
799 CpuRegister reg = temp.AsRegister<CpuRegister>();
800 if (current_method.IsRegister()) {
801 method_reg = current_method.AsRegister<Register>();
802 } else {
803 DCHECK(invoke->GetLocations()->Intrinsified());
804 DCHECK(!current_method.IsValid());
805 method_reg = reg.AsRegister();
806 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
807 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000808 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100809 __ movq(reg,
810 Address(CpuRegister(method_reg),
811 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100812 // temp = temp[index_in_cache];
813 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
814 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000815 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
816 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100817 }
Vladimir Marko58155012015-08-19 12:49:41 +0000818 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600819 return callee_method;
820}
821
822void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
823 Location temp) {
824 // All registers are assumed to be correctly set up.
825 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000826
827 switch (invoke->GetCodePtrLocation()) {
828 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
829 __ call(&frame_entry_label_);
830 break;
831 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
832 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
833 Label* label = &relative_call_patches_.back().label;
834 __ call(label); // Bind to the patch label, override at link time.
835 __ Bind(label); // Bind the label at the end of the "call" insn.
836 break;
837 }
838 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
839 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100840 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
841 LOG(FATAL) << "Unsupported";
842 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000843 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
844 // (callee_method + offset_of_quick_compiled_code)()
845 __ call(Address(callee_method.AsRegister<CpuRegister>(),
846 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
847 kX86_64WordSize).SizeValue()));
848 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000849 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800850
851 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800852}
853
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000854void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
855 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
856 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
857 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000858
859 // Use the calling convention instead of the location of the receiver, as
860 // intrinsics may have put the receiver in a different register. In the intrinsics
861 // slow path, the arguments have been moved to the right place, so here we are
862 // guaranteed that the receiver is the first register of the calling convention.
863 InvokeDexCallingConvention calling_convention;
864 Register receiver = calling_convention.GetRegisterAt(0);
865
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000866 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000867 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000868 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000869 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000870 // Instead of simply (possibly) unpoisoning `temp` here, we should
871 // emit a read barrier for the previous class reference load.
872 // However this is not required in practice, as this is an
873 // intermediate/temporary reference and because the current
874 // concurrent copying collector keeps the from-space memory
875 // intact/accessible until the end of the marking phase (the
876 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000877 __ MaybeUnpoisonHeapReference(temp);
878 // temp = temp->GetMethodAt(method_offset);
879 __ movq(temp, Address(temp, method_offset));
880 // call temp->GetEntryPoint();
881 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
882 kX86_64WordSize).SizeValue()));
883}
884
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000885void CodeGeneratorX86_64::RecordSimplePatch() {
886 if (GetCompilerOptions().GetIncludePatchInformation()) {
887 simple_patches_.emplace_back();
888 __ Bind(&simple_patches_.back());
889 }
890}
891
892void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
893 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
894 __ Bind(&string_patches_.back().label);
895}
896
897Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
898 uint32_t element_offset) {
899 // Add a patch entry and return the label.
900 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
901 return &pc_relative_dex_cache_patches_.back().label;
902}
903
Vladimir Marko58155012015-08-19 12:49:41 +0000904void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
905 DCHECK(linker_patches->empty());
906 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000907 method_patches_.size() +
908 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000909 pc_relative_dex_cache_patches_.size() +
910 simple_patches_.size() +
911 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000912 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000913 // The label points to the end of the "movl" insn but the literal offset for method
914 // patch needs to point to the embedded constant which occupies the last 4 bytes.
915 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000916 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000917 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000918 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
919 info.target_method.dex_file,
920 info.target_method.dex_method_index));
921 }
922 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000923 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000924 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
925 info.target_method.dex_file,
926 info.target_method.dex_method_index));
927 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000928 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
929 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000930 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
931 &info.target_dex_file,
932 info.label.Position(),
933 info.element_offset));
934 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000935 for (const Label& label : simple_patches_) {
936 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
937 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
938 }
939 for (const StringPatchInfo<Label>& info : string_patches_) {
940 // These are always PC-relative, see GetSupportedLoadStringKind().
941 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
942 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
943 &info.dex_file,
944 info.label.Position(),
945 info.string_index));
946 }
Vladimir Marko58155012015-08-19 12:49:41 +0000947}
948
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100950 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100951}
952
953void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100954 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100955}
956
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100957size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
958 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
959 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100960}
961
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100962size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
963 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
964 return kX86_64WordSize;
965}
966
967size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
968 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
969 return kX86_64WordSize;
970}
971
972size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
973 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
974 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100975}
976
Calin Juravle175dc732015-08-25 15:42:32 +0100977void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
978 HInstruction* instruction,
979 uint32_t dex_pc,
980 SlowPathCode* slow_path) {
981 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
982 instruction,
983 dex_pc,
984 slow_path);
985}
986
987void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100988 HInstruction* instruction,
989 uint32_t dex_pc,
990 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100991 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000992 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100993 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100994}
995
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000996static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000997// Use a fake return address register to mimic Quick.
998static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400999CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001000 const X86_64InstructionSetFeatures& isa_features,
1001 const CompilerOptions& compiler_options,
1002 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001003 : CodeGenerator(graph,
1004 kNumberOfCpuRegisters,
1005 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001006 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001007 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1008 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001009 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001010 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1011 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001012 compiler_options,
1013 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001014 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001015 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001016 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001017 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001018 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001019 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001020 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001021 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1022 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001023 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001024 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1025 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001026 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001027 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1028}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001030InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1031 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001032 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033 assembler_(codegen->GetAssembler()),
1034 codegen_(codegen) {}
1035
David Brazdil58282f42016-01-14 12:45:10 +00001036void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001038 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001039
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001040 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001041 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042}
1043
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001044static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001045 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001046}
David Srbecky9d8606d2015-04-12 09:35:32 +01001047
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001048static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001049 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001050}
1051
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001053 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001054 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001055 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001056 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001058
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001059 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001060 __ testq(CpuRegister(RAX), Address(
1061 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001062 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001063 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001064
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001065 if (HasEmptyFrame()) {
1066 return;
1067 }
1068
Nicolas Geoffray98893962015-01-21 12:32:32 +00001069 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001071 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001072 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1074 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001075 }
1076 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001077
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001078 int adjust = GetFrameSize() - GetCoreSpillSize();
1079 __ subq(CpuRegister(RSP), Immediate(adjust));
1080 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001081 uint32_t xmm_spill_location = GetFpuSpillStart();
1082 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001083
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001084 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1085 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1087 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1088 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001089 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001090 }
1091
Mathieu Chartiere401d142015-04-22 13:56:20 -07001092 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001093 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094}
1095
1096void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001097 __ cfi().RememberState();
1098 if (!HasEmptyFrame()) {
1099 uint32_t xmm_spill_location = GetFpuSpillStart();
1100 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1101 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1102 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1103 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1104 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1105 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1106 }
1107 }
1108
1109 int adjust = GetFrameSize() - GetCoreSpillSize();
1110 __ addq(CpuRegister(RSP), Immediate(adjust));
1111 __ cfi().AdjustCFAOffset(-adjust);
1112
1113 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1114 Register reg = kCoreCalleeSaves[i];
1115 if (allocated_registers_.ContainsCoreRegister(reg)) {
1116 __ popq(CpuRegister(reg));
1117 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1118 __ cfi().Restore(DWARFReg(reg));
1119 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001120 }
1121 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001122 __ ret();
1123 __ cfi().RestoreState();
1124 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125}
1126
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001127void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1128 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129}
1130
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131void CodeGeneratorX86_64::Move(Location destination, Location source) {
1132 if (source.Equals(destination)) {
1133 return;
1134 }
1135 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001136 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001138 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001139 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001140 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001142 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1143 } else if (source.IsConstant()) {
1144 HConstant* constant = source.GetConstant();
1145 if (constant->IsLongConstant()) {
1146 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1147 } else {
1148 Load32BitValue(dest, GetInt32ValueOf(constant));
1149 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150 } else {
1151 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001152 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001153 }
1154 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001155 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001157 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001159 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1160 } else if (source.IsConstant()) {
1161 HConstant* constant = source.GetConstant();
1162 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1163 if (constant->IsFloatConstant()) {
1164 Load32BitValue(dest, static_cast<int32_t>(value));
1165 } else {
1166 Load64BitValue(dest, value);
1167 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001169 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 } else {
1171 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001172 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 }
1174 } else if (destination.IsStackSlot()) {
1175 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001177 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001178 } else if (source.IsFpuRegister()) {
1179 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001180 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001181 } else if (source.IsConstant()) {
1182 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001183 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001184 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001186 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001187 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1188 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001189 }
1190 } else {
1191 DCHECK(destination.IsDoubleStackSlot());
1192 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001193 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 } else if (source.IsFpuRegister()) {
1196 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001197 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001198 } else if (source.IsConstant()) {
1199 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001200 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001201 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001202 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001203 } else {
1204 DCHECK(constant->IsLongConstant());
1205 value = constant->AsLongConstant()->GetValue();
1206 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001207 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208 } else {
1209 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001210 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1211 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212 }
1213 }
1214}
1215
Calin Juravle175dc732015-08-25 15:42:32 +01001216void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1217 DCHECK(location.IsRegister());
1218 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1219}
1220
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221void CodeGeneratorX86_64::MoveLocation(
1222 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1223 Move(dst, src);
1224}
1225
1226void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1227 if (location.IsRegister()) {
1228 locations->AddTemp(location);
1229 } else {
1230 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1231 }
1232}
1233
David Brazdilfc6a86a2015-06-26 10:33:45 +00001234void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001235 DCHECK(!successor->IsExitBlock());
1236
1237 HBasicBlock* block = got->GetBlock();
1238 HInstruction* previous = got->GetPrevious();
1239
1240 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001241 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001242 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1243 return;
1244 }
1245
1246 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1247 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1248 }
1249 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250 __ jmp(codegen_->GetLabelOf(successor));
1251 }
1252}
1253
David Brazdilfc6a86a2015-06-26 10:33:45 +00001254void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1255 got->SetLocations(nullptr);
1256}
1257
1258void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1259 HandleGoto(got, got->GetSuccessor());
1260}
1261
1262void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1263 try_boundary->SetLocations(nullptr);
1264}
1265
1266void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1267 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1268 if (!successor->IsExitBlock()) {
1269 HandleGoto(try_boundary, successor);
1270 }
1271}
1272
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1274 exit->SetLocations(nullptr);
1275}
1276
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001277void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001278}
1279
Mark Mendell152408f2015-12-31 12:28:50 -05001280template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001281void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001282 LabelType* true_label,
1283 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001284 if (cond->IsFPConditionTrueIfNaN()) {
1285 __ j(kUnordered, true_label);
1286 } else if (cond->IsFPConditionFalseIfNaN()) {
1287 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001289 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001290}
1291
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001292void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001293 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001294
Mark Mendellc4701932015-04-10 13:18:51 -04001295 Location left = locations->InAt(0);
1296 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001297 Primitive::Type type = condition->InputAt(0)->GetType();
1298 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001299 case Primitive::kPrimBoolean:
1300 case Primitive::kPrimByte:
1301 case Primitive::kPrimChar:
1302 case Primitive::kPrimShort:
1303 case Primitive::kPrimInt:
1304 case Primitive::kPrimNot: {
1305 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1306 if (right.IsConstant()) {
1307 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1308 if (value == 0) {
1309 __ testl(left_reg, left_reg);
1310 } else {
1311 __ cmpl(left_reg, Immediate(value));
1312 }
1313 } else if (right.IsStackSlot()) {
1314 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1315 } else {
1316 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1317 }
1318 break;
1319 }
Mark Mendellc4701932015-04-10 13:18:51 -04001320 case Primitive::kPrimLong: {
1321 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1322 if (right.IsConstant()) {
1323 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001324 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001325 } else if (right.IsDoubleStackSlot()) {
1326 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1327 } else {
1328 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1329 }
Mark Mendellc4701932015-04-10 13:18:51 -04001330 break;
1331 }
1332 case Primitive::kPrimFloat: {
1333 if (right.IsFpuRegister()) {
1334 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1335 } else if (right.IsConstant()) {
1336 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1337 codegen_->LiteralFloatAddress(
1338 right.GetConstant()->AsFloatConstant()->GetValue()));
1339 } else {
1340 DCHECK(right.IsStackSlot());
1341 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1342 Address(CpuRegister(RSP), right.GetStackIndex()));
1343 }
Mark Mendellc4701932015-04-10 13:18:51 -04001344 break;
1345 }
1346 case Primitive::kPrimDouble: {
1347 if (right.IsFpuRegister()) {
1348 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1349 } else if (right.IsConstant()) {
1350 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1351 codegen_->LiteralDoubleAddress(
1352 right.GetConstant()->AsDoubleConstant()->GetValue()));
1353 } else {
1354 DCHECK(right.IsDoubleStackSlot());
1355 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1356 Address(CpuRegister(RSP), right.GetStackIndex()));
1357 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001358 break;
1359 }
1360 default:
1361 LOG(FATAL) << "Unexpected condition type " << type;
1362 }
1363}
1364
1365template<class LabelType>
1366void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1367 LabelType* true_target_in,
1368 LabelType* false_target_in) {
1369 // Generated branching requires both targets to be explicit. If either of the
1370 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1371 LabelType fallthrough_target;
1372 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1373 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1374
1375 // Generate the comparison to set the CC.
1376 GenerateCompareTest(condition);
1377
1378 // Now generate the correct jump(s).
1379 Primitive::Type type = condition->InputAt(0)->GetType();
1380 switch (type) {
1381 case Primitive::kPrimLong: {
1382 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1383 break;
1384 }
1385 case Primitive::kPrimFloat: {
1386 GenerateFPJumps(condition, true_target, false_target);
1387 break;
1388 }
1389 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001390 GenerateFPJumps(condition, true_target, false_target);
1391 break;
1392 }
1393 default:
1394 LOG(FATAL) << "Unexpected condition type " << type;
1395 }
1396
David Brazdil0debae72015-11-12 18:37:00 +00001397 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001398 __ jmp(false_target);
1399 }
David Brazdil0debae72015-11-12 18:37:00 +00001400
1401 if (fallthrough_target.IsLinked()) {
1402 __ Bind(&fallthrough_target);
1403 }
Mark Mendellc4701932015-04-10 13:18:51 -04001404}
1405
David Brazdil0debae72015-11-12 18:37:00 +00001406static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1407 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1408 // are set only strictly before `branch`. We can't use the eflags on long
1409 // conditions if they are materialized due to the complex branching.
1410 return cond->IsCondition() &&
1411 cond->GetNext() == branch &&
1412 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1413}
1414
Mark Mendell152408f2015-12-31 12:28:50 -05001415template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001416void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001417 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001418 LabelType* true_target,
1419 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001420 HInstruction* cond = instruction->InputAt(condition_input_index);
1421
1422 if (true_target == nullptr && false_target == nullptr) {
1423 // Nothing to do. The code always falls through.
1424 return;
1425 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001426 // Constant condition, statically compared against "true" (integer value 1).
1427 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001428 if (true_target != nullptr) {
1429 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001430 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001431 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001432 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001433 if (false_target != nullptr) {
1434 __ jmp(false_target);
1435 }
1436 }
1437 return;
1438 }
1439
1440 // The following code generates these patterns:
1441 // (1) true_target == nullptr && false_target != nullptr
1442 // - opposite condition true => branch to false_target
1443 // (2) true_target != nullptr && false_target == nullptr
1444 // - condition true => branch to true_target
1445 // (3) true_target != nullptr && false_target != nullptr
1446 // - condition true => branch to true_target
1447 // - branch to false_target
1448 if (IsBooleanValueOrMaterializedCondition(cond)) {
1449 if (AreEflagsSetFrom(cond, instruction)) {
1450 if (true_target == nullptr) {
1451 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1452 } else {
1453 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1454 }
1455 } else {
1456 // Materialized condition, compare against 0.
1457 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1458 if (lhs.IsRegister()) {
1459 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1460 } else {
1461 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1462 }
1463 if (true_target == nullptr) {
1464 __ j(kEqual, false_target);
1465 } else {
1466 __ j(kNotEqual, true_target);
1467 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001468 }
1469 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001470 // Condition has not been materialized, use its inputs as the
1471 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001472 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001473
David Brazdil0debae72015-11-12 18:37:00 +00001474 // If this is a long or FP comparison that has been folded into
1475 // the HCondition, generate the comparison directly.
1476 Primitive::Type type = condition->InputAt(0)->GetType();
1477 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1478 GenerateCompareTestAndBranch(condition, true_target, false_target);
1479 return;
1480 }
1481
1482 Location lhs = condition->GetLocations()->InAt(0);
1483 Location rhs = condition->GetLocations()->InAt(1);
1484 if (rhs.IsRegister()) {
1485 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1486 } else if (rhs.IsConstant()) {
1487 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001488 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001489 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001490 __ cmpl(lhs.AsRegister<CpuRegister>(),
1491 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1492 }
1493 if (true_target == nullptr) {
1494 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1495 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001496 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001497 }
Dave Allison20dfc792014-06-16 20:44:29 -07001498 }
David Brazdil0debae72015-11-12 18:37:00 +00001499
1500 // If neither branch falls through (case 3), the conditional branch to `true_target`
1501 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1502 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001503 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001504 }
1505}
1506
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001507void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1509 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001510 locations->SetInAt(0, Location::Any());
1511 }
1512}
1513
1514void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001515 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1516 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1517 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1518 nullptr : codegen_->GetLabelOf(true_successor);
1519 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1520 nullptr : codegen_->GetLabelOf(false_successor);
1521 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001522}
1523
1524void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1525 LocationSummary* locations = new (GetGraph()->GetArena())
1526 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001527 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001528 locations->SetInAt(0, Location::Any());
1529 }
1530}
1531
1532void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001533 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001534 GenerateTestAndBranch<Label>(deoptimize,
1535 /* condition_input_index */ 0,
1536 slow_path->GetEntryLabel(),
1537 /* false_target */ nullptr);
1538}
1539
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001540static bool SelectCanUseCMOV(HSelect* select) {
1541 // There are no conditional move instructions for XMMs.
1542 if (Primitive::IsFloatingPointType(select->GetType())) {
1543 return false;
1544 }
1545
1546 // A FP condition doesn't generate the single CC that we need.
1547 HInstruction* condition = select->GetCondition();
1548 if (condition->IsCondition() &&
1549 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1550 return false;
1551 }
1552
1553 // We can generate a CMOV for this Select.
1554 return true;
1555}
1556
David Brazdil74eb1b22015-12-14 11:44:01 +00001557void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1559 if (Primitive::IsFloatingPointType(select->GetType())) {
1560 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001561 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001562 } else {
1563 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001564 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001565 if (select->InputAt(1)->IsConstant()) {
1566 locations->SetInAt(1, Location::RequiresRegister());
1567 } else {
1568 locations->SetInAt(1, Location::Any());
1569 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001570 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001571 locations->SetInAt(1, Location::Any());
1572 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001573 }
1574 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1575 locations->SetInAt(2, Location::RequiresRegister());
1576 }
1577 locations->SetOut(Location::SameAsFirstInput());
1578}
1579
1580void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1581 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001582 if (SelectCanUseCMOV(select)) {
1583 // If both the condition and the source types are integer, we can generate
1584 // a CMOV to implement Select.
1585 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001586 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001587 DCHECK(locations->InAt(0).Equals(locations->Out()));
1588
1589 HInstruction* select_condition = select->GetCondition();
1590 Condition cond = kNotEqual;
1591
1592 // Figure out how to test the 'condition'.
1593 if (select_condition->IsCondition()) {
1594 HCondition* condition = select_condition->AsCondition();
1595 if (!condition->IsEmittedAtUseSite()) {
1596 // This was a previously materialized condition.
1597 // Can we use the existing condition code?
1598 if (AreEflagsSetFrom(condition, select)) {
1599 // Materialization was the previous instruction. Condition codes are right.
1600 cond = X86_64IntegerCondition(condition->GetCondition());
1601 } else {
1602 // No, we have to recreate the condition code.
1603 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1604 __ testl(cond_reg, cond_reg);
1605 }
1606 } else {
1607 GenerateCompareTest(condition);
1608 cond = X86_64IntegerCondition(condition->GetCondition());
1609 }
1610 } else {
1611 // Must be a boolean condition, which needs to be compared to 0.
1612 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1613 __ testl(cond_reg, cond_reg);
1614 }
1615
1616 // If the condition is true, overwrite the output, which already contains false.
1617 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001618 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1619 if (value_true_loc.IsRegister()) {
1620 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1621 } else {
1622 __ cmov(cond,
1623 value_false,
1624 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1625 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001626 } else {
1627 NearLabel false_target;
1628 GenerateTestAndBranch<NearLabel>(select,
1629 /* condition_input_index */ 2,
1630 /* true_target */ nullptr,
1631 &false_target);
1632 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1633 __ Bind(&false_target);
1634 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001635}
1636
David Srbecky0cf44932015-12-09 14:09:59 +00001637void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1638 new (GetGraph()->GetArena()) LocationSummary(info);
1639}
1640
David Srbeckyd28f4a02016-03-14 17:14:24 +00001641void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1642 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001643}
1644
1645void CodeGeneratorX86_64::GenerateNop() {
1646 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001647}
1648
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001649void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001650 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001651 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001652 // Handle the long/FP comparisons made in instruction simplification.
1653 switch (cond->InputAt(0)->GetType()) {
1654 case Primitive::kPrimLong:
1655 locations->SetInAt(0, Location::RequiresRegister());
1656 locations->SetInAt(1, Location::Any());
1657 break;
1658 case Primitive::kPrimFloat:
1659 case Primitive::kPrimDouble:
1660 locations->SetInAt(0, Location::RequiresFpuRegister());
1661 locations->SetInAt(1, Location::Any());
1662 break;
1663 default:
1664 locations->SetInAt(0, Location::RequiresRegister());
1665 locations->SetInAt(1, Location::Any());
1666 break;
1667 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001668 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001669 locations->SetOut(Location::RequiresRegister());
1670 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671}
1672
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001674 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001675 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001676 }
Mark Mendellc4701932015-04-10 13:18:51 -04001677
1678 LocationSummary* locations = cond->GetLocations();
1679 Location lhs = locations->InAt(0);
1680 Location rhs = locations->InAt(1);
1681 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001682 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001683
1684 switch (cond->InputAt(0)->GetType()) {
1685 default:
1686 // Integer case.
1687
1688 // Clear output register: setcc only sets the low byte.
1689 __ xorl(reg, reg);
1690
1691 if (rhs.IsRegister()) {
1692 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1693 } else if (rhs.IsConstant()) {
1694 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001695 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001696 } else {
1697 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1698 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001699 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001700 return;
1701 case Primitive::kPrimLong:
1702 // Clear output register: setcc only sets the low byte.
1703 __ xorl(reg, reg);
1704
1705 if (rhs.IsRegister()) {
1706 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1707 } else if (rhs.IsConstant()) {
1708 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001709 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001710 } else {
1711 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1712 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001713 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001714 return;
1715 case Primitive::kPrimFloat: {
1716 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1717 if (rhs.IsConstant()) {
1718 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1719 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1720 } else if (rhs.IsStackSlot()) {
1721 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1722 } else {
1723 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1724 }
1725 GenerateFPJumps(cond, &true_label, &false_label);
1726 break;
1727 }
1728 case Primitive::kPrimDouble: {
1729 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1730 if (rhs.IsConstant()) {
1731 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1732 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1733 } else if (rhs.IsDoubleStackSlot()) {
1734 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1735 } else {
1736 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1737 }
1738 GenerateFPJumps(cond, &true_label, &false_label);
1739 break;
1740 }
1741 }
1742
1743 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001744 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001745
Roland Levillain4fa13f62015-07-06 18:11:54 +01001746 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001747 __ Bind(&false_label);
1748 __ xorl(reg, reg);
1749 __ jmp(&done_label);
1750
Roland Levillain4fa13f62015-07-06 18:11:54 +01001751 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001752 __ Bind(&true_label);
1753 __ movl(reg, Immediate(1));
1754 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001803}
1804
Aart Bike9f37602015-10-09 11:15:55 -07001805void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001807}
1808
1809void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001811}
1812
1813void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001815}
1816
1817void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
1825void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
1829void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001831}
1832
1833void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001835}
1836
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001837void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001838 LocationSummary* locations =
1839 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001840 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001841 case Primitive::kPrimBoolean:
1842 case Primitive::kPrimByte:
1843 case Primitive::kPrimShort:
1844 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001845 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001846 case Primitive::kPrimLong: {
1847 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001848 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1850 break;
1851 }
1852 case Primitive::kPrimFloat:
1853 case Primitive::kPrimDouble: {
1854 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001855 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001856 locations->SetOut(Location::RequiresRegister());
1857 break;
1858 }
1859 default:
1860 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1861 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001862}
1863
1864void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001865 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001867 Location left = locations->InAt(0);
1868 Location right = locations->InAt(1);
1869
Mark Mendell0c9497d2015-08-21 09:30:05 -04001870 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001871 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001872 Condition less_cond = kLess;
1873
Calin Juravleddb7df22014-11-25 20:56:51 +00001874 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001875 case Primitive::kPrimBoolean:
1876 case Primitive::kPrimByte:
1877 case Primitive::kPrimShort:
1878 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001879 case Primitive::kPrimInt: {
1880 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1881 if (right.IsConstant()) {
1882 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1883 codegen_->Compare32BitValue(left_reg, value);
1884 } else if (right.IsStackSlot()) {
1885 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1886 } else {
1887 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1888 }
1889 break;
1890 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001891 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001892 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1893 if (right.IsConstant()) {
1894 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001895 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001896 } else if (right.IsDoubleStackSlot()) {
1897 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001898 } else {
1899 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1900 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001901 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001902 }
1903 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001904 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1905 if (right.IsConstant()) {
1906 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1907 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1908 } else if (right.IsStackSlot()) {
1909 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1910 } else {
1911 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1912 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001914 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001915 break;
1916 }
1917 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001918 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1919 if (right.IsConstant()) {
1920 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1921 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1922 } else if (right.IsDoubleStackSlot()) {
1923 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1924 } else {
1925 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1926 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001927 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001928 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001929 break;
1930 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001931 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001932 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001933 }
Aart Bika19616e2016-02-01 18:57:58 -08001934
Calin Juravleddb7df22014-11-25 20:56:51 +00001935 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001936 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001937 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001938
Calin Juravle91debbc2014-11-26 19:01:09 +00001939 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001940 __ movl(out, Immediate(1));
1941 __ jmp(&done);
1942
1943 __ Bind(&less);
1944 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001945
1946 __ Bind(&done);
1947}
1948
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001949void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001950 LocationSummary* locations =
1951 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001952 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001953}
1954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001955void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001956 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001957}
1958
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001959void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1960 LocationSummary* locations =
1961 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1962 locations->SetOut(Location::ConstantLocation(constant));
1963}
1964
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001965void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001966 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001967}
1968
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001969void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001970 LocationSummary* locations =
1971 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001972 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001973}
1974
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001975void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001976 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977}
1978
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001979void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1980 LocationSummary* locations =
1981 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1982 locations->SetOut(Location::ConstantLocation(constant));
1983}
1984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001985void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001986 // Will be generated at use site.
1987}
1988
1989void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1990 LocationSummary* locations =
1991 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1992 locations->SetOut(Location::ConstantLocation(constant));
1993}
1994
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001995void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1996 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001997 // Will be generated at use site.
1998}
1999
Calin Juravle27df7582015-04-17 19:12:31 +01002000void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2001 memory_barrier->SetLocations(nullptr);
2002}
2003
2004void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002005 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002006}
2007
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2009 ret->SetLocations(nullptr);
2010}
2011
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002012void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002013 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014}
2015
2016void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 LocationSummary* locations =
2018 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002019 switch (ret->InputAt(0)->GetType()) {
2020 case Primitive::kPrimBoolean:
2021 case Primitive::kPrimByte:
2022 case Primitive::kPrimChar:
2023 case Primitive::kPrimShort:
2024 case Primitive::kPrimInt:
2025 case Primitive::kPrimNot:
2026 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002027 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002028 break;
2029
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002030 case Primitive::kPrimFloat:
2031 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002032 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002033 break;
2034
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002035 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002036 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002038}
2039
2040void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2041 if (kIsDebugBuild) {
2042 switch (ret->InputAt(0)->GetType()) {
2043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte:
2045 case Primitive::kPrimChar:
2046 case Primitive::kPrimShort:
2047 case Primitive::kPrimInt:
2048 case Primitive::kPrimNot:
2049 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002051 break;
2052
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002053 case Primitive::kPrimFloat:
2054 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002056 XMM0);
2057 break;
2058
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002060 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002061 }
2062 }
2063 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002064}
2065
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002066Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2067 switch (type) {
2068 case Primitive::kPrimBoolean:
2069 case Primitive::kPrimByte:
2070 case Primitive::kPrimChar:
2071 case Primitive::kPrimShort:
2072 case Primitive::kPrimInt:
2073 case Primitive::kPrimNot:
2074 case Primitive::kPrimLong:
2075 return Location::RegisterLocation(RAX);
2076
2077 case Primitive::kPrimVoid:
2078 return Location::NoLocation();
2079
2080 case Primitive::kPrimDouble:
2081 case Primitive::kPrimFloat:
2082 return Location::FpuRegisterLocation(XMM0);
2083 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002084
2085 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002086}
2087
2088Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2089 return Location::RegisterLocation(kMethodRegisterArgument);
2090}
2091
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002092Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002093 switch (type) {
2094 case Primitive::kPrimBoolean:
2095 case Primitive::kPrimByte:
2096 case Primitive::kPrimChar:
2097 case Primitive::kPrimShort:
2098 case Primitive::kPrimInt:
2099 case Primitive::kPrimNot: {
2100 uint32_t index = gp_index_++;
2101 stack_index_++;
2102 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002103 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104 } else {
2105 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2106 }
2107 }
2108
2109 case Primitive::kPrimLong: {
2110 uint32_t index = gp_index_;
2111 stack_index_ += 2;
2112 if (index < calling_convention.GetNumberOfRegisters()) {
2113 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002114 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002115 } else {
2116 gp_index_ += 2;
2117 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2118 }
2119 }
2120
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002122 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002123 stack_index_++;
2124 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002125 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002126 } else {
2127 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2128 }
2129 }
2130
2131 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002132 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002133 stack_index_ += 2;
2134 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002136 } else {
2137 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2138 }
2139 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002140
2141 case Primitive::kPrimVoid:
2142 LOG(FATAL) << "Unexpected parameter type " << type;
2143 break;
2144 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002145 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002146}
2147
Calin Juravle175dc732015-08-25 15:42:32 +01002148void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2149 // The trampoline uses the same calling convention as dex calling conventions,
2150 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2151 // the method_idx.
2152 HandleInvoke(invoke);
2153}
2154
2155void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2156 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2157}
2158
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002159void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002160 // Explicit clinit checks triggered by static invokes must have been pruned by
2161 // art::PrepareForRegisterAllocation.
2162 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002163
Mark Mendellfb8d2792015-03-31 22:16:59 -04002164 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002165 if (intrinsic.TryDispatch(invoke)) {
2166 return;
2167 }
2168
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002169 HandleInvoke(invoke);
2170}
2171
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002172static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2173 if (invoke->GetLocations()->Intrinsified()) {
2174 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2175 intrinsic.Dispatch(invoke);
2176 return true;
2177 }
2178 return false;
2179}
2180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002181void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002182 // Explicit clinit checks triggered by static invokes must have been pruned by
2183 // art::PrepareForRegisterAllocation.
2184 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002185
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002186 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2187 return;
2188 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002189
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002190 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002191 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002192 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002193 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002194}
2195
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002196void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002197 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002198 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199}
2200
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002201void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002202 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002203 if (intrinsic.TryDispatch(invoke)) {
2204 return;
2205 }
2206
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207 HandleInvoke(invoke);
2208}
2209
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002210void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2212 return;
2213 }
2214
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002215 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002216 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002217 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002218}
2219
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002220void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2221 HandleInvoke(invoke);
2222 // Add the hidden argument.
2223 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2224}
2225
2226void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2227 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002228 LocationSummary* locations = invoke->GetLocations();
2229 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2230 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002231 Location receiver = locations->InAt(0);
2232 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2233
Roland Levillain0d5a2812015-11-13 10:07:31 +00002234 // Set the hidden argument. This is safe to do this here, as RAX
2235 // won't be modified thereafter, before the `call` instruction.
2236 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002237 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239 if (receiver.IsStackSlot()) {
2240 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002241 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242 __ movl(temp, Address(temp, class_offset));
2243 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002247 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002248 // Instead of simply (possibly) unpoisoning `temp` here, we should
2249 // emit a read barrier for the previous class reference load.
2250 // However this is not required in practice, as this is an
2251 // intermediate/temporary reference and because the current
2252 // concurrent copying collector keeps the from-space memory
2253 // intact/accessible until the end of the marking phase (the
2254 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002255 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03002256 // temp = temp->GetAddressOfIMT()
2257 __ movq(temp,
2258 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2259 // temp = temp->GetImtEntryAt(method_offset);
2260 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2261 invoke->GetImtIndex() % ImTable::kSize, kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002263 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 __ call(Address(temp,
2266 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267
2268 DCHECK(!codegen_->IsLeafMethod());
2269 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2270}
2271
Roland Levillain88cb1752014-10-20 16:36:47 +01002272void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2273 LocationSummary* locations =
2274 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2275 switch (neg->GetResultType()) {
2276 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002277 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002278 locations->SetInAt(0, Location::RequiresRegister());
2279 locations->SetOut(Location::SameAsFirstInput());
2280 break;
2281
Roland Levillain88cb1752014-10-20 16:36:47 +01002282 case Primitive::kPrimFloat:
2283 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002284 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002285 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002286 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002287 break;
2288
2289 default:
2290 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2291 }
2292}
2293
2294void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2295 LocationSummary* locations = neg->GetLocations();
2296 Location out = locations->Out();
2297 Location in = locations->InAt(0);
2298 switch (neg->GetResultType()) {
2299 case Primitive::kPrimInt:
2300 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 break;
2304
2305 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002306 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002307 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002309 break;
2310
Roland Levillain5368c212014-11-27 15:03:41 +00002311 case Primitive::kPrimFloat: {
2312 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002313 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002314 // Implement float negation with an exclusive or with value
2315 // 0x80000000 (mask for bit 31, representing the sign of a
2316 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002317 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002318 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002319 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002320 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002321
Roland Levillain5368c212014-11-27 15:03:41 +00002322 case Primitive::kPrimDouble: {
2323 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002324 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002325 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002326 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002327 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002328 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002330 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002331 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002332
2333 default:
2334 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2335 }
2336}
2337
Roland Levillaindff1f282014-11-05 14:15:05 +00002338void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2339 LocationSummary* locations =
2340 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2341 Primitive::Type result_type = conversion->GetResultType();
2342 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002343 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002344
David Brazdilb2bd1c52015-03-25 11:17:37 +00002345 // The Java language does not allow treating boolean as an integral type but
2346 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002347
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002349 case Primitive::kPrimByte:
2350 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002351 case Primitive::kPrimLong:
2352 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002353 case Primitive::kPrimBoolean:
2354 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002355 case Primitive::kPrimShort:
2356 case Primitive::kPrimInt:
2357 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002358 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002359 locations->SetInAt(0, Location::Any());
2360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2361 break;
2362
2363 default:
2364 LOG(FATAL) << "Unexpected type conversion from " << input_type
2365 << " to " << result_type;
2366 }
2367 break;
2368
Roland Levillain01a8d712014-11-14 16:27:39 +00002369 case Primitive::kPrimShort:
2370 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002371 case Primitive::kPrimLong:
2372 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002373 case Primitive::kPrimBoolean:
2374 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002375 case Primitive::kPrimByte:
2376 case Primitive::kPrimInt:
2377 case Primitive::kPrimChar:
2378 // Processing a Dex `int-to-short' instruction.
2379 locations->SetInAt(0, Location::Any());
2380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 }
2387 break;
2388
Roland Levillain946e1432014-11-11 17:35:19 +00002389 case Primitive::kPrimInt:
2390 switch (input_type) {
2391 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002392 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002393 locations->SetInAt(0, Location::Any());
2394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2395 break;
2396
2397 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002398 // Processing a Dex `float-to-int' instruction.
2399 locations->SetInAt(0, Location::RequiresFpuRegister());
2400 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002401 break;
2402
Roland Levillain946e1432014-11-11 17:35:19 +00002403 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002404 // Processing a Dex `double-to-int' instruction.
2405 locations->SetInAt(0, Location::RequiresFpuRegister());
2406 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002407 break;
2408
2409 default:
2410 LOG(FATAL) << "Unexpected type conversion from " << input_type
2411 << " to " << result_type;
2412 }
2413 break;
2414
Roland Levillaindff1f282014-11-05 14:15:05 +00002415 case Primitive::kPrimLong:
2416 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002417 case Primitive::kPrimBoolean:
2418 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002419 case Primitive::kPrimByte:
2420 case Primitive::kPrimShort:
2421 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002422 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002423 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 // TODO: We would benefit from a (to-be-implemented)
2425 // Location::RegisterOrStackSlot requirement for this input.
2426 locations->SetInAt(0, Location::RequiresRegister());
2427 locations->SetOut(Location::RequiresRegister());
2428 break;
2429
2430 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002431 // Processing a Dex `float-to-long' instruction.
2432 locations->SetInAt(0, Location::RequiresFpuRegister());
2433 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002434 break;
2435
Roland Levillaindff1f282014-11-05 14:15:05 +00002436 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002437 // Processing a Dex `double-to-long' instruction.
2438 locations->SetInAt(0, Location::RequiresFpuRegister());
2439 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002440 break;
2441
2442 default:
2443 LOG(FATAL) << "Unexpected type conversion from " << input_type
2444 << " to " << result_type;
2445 }
2446 break;
2447
Roland Levillain981e4542014-11-14 11:47:14 +00002448 case Primitive::kPrimChar:
2449 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002450 case Primitive::kPrimLong:
2451 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002452 case Primitive::kPrimBoolean:
2453 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002454 case Primitive::kPrimByte:
2455 case Primitive::kPrimShort:
2456 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `int-to-char' instruction.
2458 locations->SetInAt(0, Location::Any());
2459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2460 break;
2461
2462 default:
2463 LOG(FATAL) << "Unexpected type conversion from " << input_type
2464 << " to " << result_type;
2465 }
2466 break;
2467
Roland Levillaindff1f282014-11-05 14:15:05 +00002468 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002469 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002470 case Primitive::kPrimBoolean:
2471 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002472 case Primitive::kPrimByte:
2473 case Primitive::kPrimShort:
2474 case Primitive::kPrimInt:
2475 case Primitive::kPrimChar:
2476 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002477 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002478 locations->SetOut(Location::RequiresFpuRegister());
2479 break;
2480
2481 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002482 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002483 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002484 locations->SetOut(Location::RequiresFpuRegister());
2485 break;
2486
Roland Levillaincff13742014-11-17 14:32:17 +00002487 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002488 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002489 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002490 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002491 break;
2492
2493 default:
2494 LOG(FATAL) << "Unexpected type conversion from " << input_type
2495 << " to " << result_type;
2496 };
2497 break;
2498
Roland Levillaindff1f282014-11-05 14:15:05 +00002499 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002500 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002501 case Primitive::kPrimBoolean:
2502 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002503 case Primitive::kPrimByte:
2504 case Primitive::kPrimShort:
2505 case Primitive::kPrimInt:
2506 case Primitive::kPrimChar:
2507 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002508 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002509 locations->SetOut(Location::RequiresFpuRegister());
2510 break;
2511
2512 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002513 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002514 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002515 locations->SetOut(Location::RequiresFpuRegister());
2516 break;
2517
Roland Levillaincff13742014-11-17 14:32:17 +00002518 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002519 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002520 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002521 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected type conversion from " << input_type
2526 << " to " << result_type;
2527 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 break;
2529
2530 default:
2531 LOG(FATAL) << "Unexpected type conversion from " << input_type
2532 << " to " << result_type;
2533 }
2534}
2535
2536void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2537 LocationSummary* locations = conversion->GetLocations();
2538 Location out = locations->Out();
2539 Location in = locations->InAt(0);
2540 Primitive::Type result_type = conversion->GetResultType();
2541 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002542 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002544 case Primitive::kPrimByte:
2545 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002546 case Primitive::kPrimLong:
2547 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002548 case Primitive::kPrimBoolean:
2549 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002550 case Primitive::kPrimShort:
2551 case Primitive::kPrimInt:
2552 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002553 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002554 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002555 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002556 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002558 Address(CpuRegister(RSP), in.GetStackIndex()));
2559 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002560 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002561 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002562 }
2563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unexpected type conversion from " << input_type
2567 << " to " << result_type;
2568 }
2569 break;
2570
Roland Levillain01a8d712014-11-14 16:27:39 +00002571 case Primitive::kPrimShort:
2572 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002573 case Primitive::kPrimLong:
2574 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002575 case Primitive::kPrimBoolean:
2576 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002577 case Primitive::kPrimByte:
2578 case Primitive::kPrimInt:
2579 case Primitive::kPrimChar:
2580 // Processing a Dex `int-to-short' instruction.
2581 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002583 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002585 Address(CpuRegister(RSP), in.GetStackIndex()));
2586 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002587 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002588 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002589 }
2590 break;
2591
2592 default:
2593 LOG(FATAL) << "Unexpected type conversion from " << input_type
2594 << " to " << result_type;
2595 }
2596 break;
2597
Roland Levillain946e1432014-11-11 17:35:19 +00002598 case Primitive::kPrimInt:
2599 switch (input_type) {
2600 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002601 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002602 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002604 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002606 Address(CpuRegister(RSP), in.GetStackIndex()));
2607 } else {
2608 DCHECK(in.IsConstant());
2609 DCHECK(in.GetConstant()->IsLongConstant());
2610 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002612 }
2613 break;
2614
Roland Levillain3f8f9362014-12-02 17:45:01 +00002615 case Primitive::kPrimFloat: {
2616 // Processing a Dex `float-to-int' instruction.
2617 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2618 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002619 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002620
2621 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002622 // if input >= (float)INT_MAX goto done
2623 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002624 __ j(kAboveEqual, &done);
2625 // if input == NaN goto nan
2626 __ j(kUnordered, &nan);
2627 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002628 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002629 __ jmp(&done);
2630 __ Bind(&nan);
2631 // output = 0
2632 __ xorl(output, output);
2633 __ Bind(&done);
2634 break;
2635 }
2636
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002637 case Primitive::kPrimDouble: {
2638 // Processing a Dex `double-to-int' instruction.
2639 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2640 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002641 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002642
2643 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002644 // if input >= (double)INT_MAX goto done
2645 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002646 __ j(kAboveEqual, &done);
2647 // if input == NaN goto nan
2648 __ j(kUnordered, &nan);
2649 // output = double-to-int-truncate(input)
2650 __ cvttsd2si(output, input);
2651 __ jmp(&done);
2652 __ Bind(&nan);
2653 // output = 0
2654 __ xorl(output, output);
2655 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002656 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002657 }
Roland Levillain946e1432014-11-11 17:35:19 +00002658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663 break;
2664
Roland Levillaindff1f282014-11-05 14:15:05 +00002665 case Primitive::kPrimLong:
2666 switch (input_type) {
2667 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002668 case Primitive::kPrimBoolean:
2669 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002670 case Primitive::kPrimByte:
2671 case Primitive::kPrimShort:
2672 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002673 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002674 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002675 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002676 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002677 break;
2678
Roland Levillain624279f2014-12-04 11:54:28 +00002679 case Primitive::kPrimFloat: {
2680 // Processing a Dex `float-to-long' instruction.
2681 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2682 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002683 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002684
Mark Mendell92e83bf2015-05-07 11:25:03 -04002685 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002686 // if input >= (float)LONG_MAX goto done
2687 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002688 __ j(kAboveEqual, &done);
2689 // if input == NaN goto nan
2690 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002691 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002692 __ cvttss2si(output, input, true);
2693 __ jmp(&done);
2694 __ Bind(&nan);
2695 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002696 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002697 __ Bind(&done);
2698 break;
2699 }
2700
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002701 case Primitive::kPrimDouble: {
2702 // Processing a Dex `double-to-long' instruction.
2703 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2704 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002705 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002706
Mark Mendell92e83bf2015-05-07 11:25:03 -04002707 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002708 // if input >= (double)LONG_MAX goto done
2709 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002710 __ j(kAboveEqual, &done);
2711 // if input == NaN goto nan
2712 __ j(kUnordered, &nan);
2713 // output = double-to-long-truncate(input)
2714 __ cvttsd2si(output, input, true);
2715 __ jmp(&done);
2716 __ Bind(&nan);
2717 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002718 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002719 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002720 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002721 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002722
2723 default:
2724 LOG(FATAL) << "Unexpected type conversion from " << input_type
2725 << " to " << result_type;
2726 }
2727 break;
2728
Roland Levillain981e4542014-11-14 11:47:14 +00002729 case Primitive::kPrimChar:
2730 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002731 case Primitive::kPrimLong:
2732 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002733 case Primitive::kPrimBoolean:
2734 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002735 case Primitive::kPrimByte:
2736 case Primitive::kPrimShort:
2737 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002738 // Processing a Dex `int-to-char' instruction.
2739 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002740 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002741 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002743 Address(CpuRegister(RSP), in.GetStackIndex()));
2744 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002745 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002746 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002747 }
2748 break;
2749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
2753 }
2754 break;
2755
Roland Levillaindff1f282014-11-05 14:15:05 +00002756 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002757 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002758 case Primitive::kPrimBoolean:
2759 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002760 case Primitive::kPrimByte:
2761 case Primitive::kPrimShort:
2762 case Primitive::kPrimInt:
2763 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002764 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002765 if (in.IsRegister()) {
2766 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2767 } else if (in.IsConstant()) {
2768 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2769 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002770 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002771 } else {
2772 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2773 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2774 }
Roland Levillaincff13742014-11-17 14:32:17 +00002775 break;
2776
2777 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002778 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002779 if (in.IsRegister()) {
2780 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2781 } else if (in.IsConstant()) {
2782 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2783 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002784 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002785 } else {
2786 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2787 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2788 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002789 break;
2790
Roland Levillaincff13742014-11-17 14:32:17 +00002791 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002792 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002793 if (in.IsFpuRegister()) {
2794 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2795 } else if (in.IsConstant()) {
2796 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2797 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002798 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002799 } else {
2800 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2801 Address(CpuRegister(RSP), in.GetStackIndex()));
2802 }
Roland Levillaincff13742014-11-17 14:32:17 +00002803 break;
2804
2805 default:
2806 LOG(FATAL) << "Unexpected type conversion from " << input_type
2807 << " to " << result_type;
2808 };
2809 break;
2810
Roland Levillaindff1f282014-11-05 14:15:05 +00002811 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002812 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002813 case Primitive::kPrimBoolean:
2814 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002815 case Primitive::kPrimByte:
2816 case Primitive::kPrimShort:
2817 case Primitive::kPrimInt:
2818 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002819 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002820 if (in.IsRegister()) {
2821 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2822 } else if (in.IsConstant()) {
2823 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2824 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002825 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002826 } else {
2827 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2828 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2829 }
Roland Levillaincff13742014-11-17 14:32:17 +00002830 break;
2831
2832 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002833 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002834 if (in.IsRegister()) {
2835 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2836 } else if (in.IsConstant()) {
2837 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2838 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002839 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002840 } else {
2841 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2842 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2843 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002844 break;
2845
Roland Levillaincff13742014-11-17 14:32:17 +00002846 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002847 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002848 if (in.IsFpuRegister()) {
2849 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2850 } else if (in.IsConstant()) {
2851 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2852 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002853 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002854 } else {
2855 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2856 Address(CpuRegister(RSP), in.GetStackIndex()));
2857 }
Roland Levillaincff13742014-11-17 14:32:17 +00002858 break;
2859
2860 default:
2861 LOG(FATAL) << "Unexpected type conversion from " << input_type
2862 << " to " << result_type;
2863 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002864 break;
2865
2866 default:
2867 LOG(FATAL) << "Unexpected type conversion from " << input_type
2868 << " to " << result_type;
2869 }
2870}
2871
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002872void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002873 LocationSummary* locations =
2874 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002875 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002876 case Primitive::kPrimInt: {
2877 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002878 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002880 break;
2881 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002882
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002883 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002884 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002885 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002886 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002888 break;
2889 }
2890
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002891 case Primitive::kPrimDouble:
2892 case Primitive::kPrimFloat: {
2893 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002894 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002895 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002897 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002898
2899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002900 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002901 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002902}
2903
2904void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2905 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002906 Location first = locations->InAt(0);
2907 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002908 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002909
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002910 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002911 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002912 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002913 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2914 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002915 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2916 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002917 } else {
2918 __ leal(out.AsRegister<CpuRegister>(), Address(
2919 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2920 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002921 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002922 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2923 __ addl(out.AsRegister<CpuRegister>(),
2924 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2925 } else {
2926 __ leal(out.AsRegister<CpuRegister>(), Address(
2927 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2928 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002929 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002930 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002931 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002932 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002933 break;
2934 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002936 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002937 if (second.IsRegister()) {
2938 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2939 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002940 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2941 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002942 } else {
2943 __ leaq(out.AsRegister<CpuRegister>(), Address(
2944 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2945 }
2946 } else {
2947 DCHECK(second.IsConstant());
2948 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2949 int32_t int32_value = Low32Bits(value);
2950 DCHECK_EQ(int32_value, value);
2951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2952 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2953 } else {
2954 __ leaq(out.AsRegister<CpuRegister>(), Address(
2955 first.AsRegister<CpuRegister>(), int32_value));
2956 }
2957 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002958 break;
2959 }
2960
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002961 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002962 if (second.IsFpuRegister()) {
2963 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2964 } else if (second.IsConstant()) {
2965 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002966 codegen_->LiteralFloatAddress(
2967 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002968 } else {
2969 DCHECK(second.IsStackSlot());
2970 __ addss(first.AsFpuRegister<XmmRegister>(),
2971 Address(CpuRegister(RSP), second.GetStackIndex()));
2972 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002973 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 }
2975
2976 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002977 if (second.IsFpuRegister()) {
2978 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2979 } else if (second.IsConstant()) {
2980 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002981 codegen_->LiteralDoubleAddress(
2982 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002983 } else {
2984 DCHECK(second.IsDoubleStackSlot());
2985 __ addsd(first.AsFpuRegister<XmmRegister>(),
2986 Address(CpuRegister(RSP), second.GetStackIndex()));
2987 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 break;
2989 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002990
2991 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002992 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993 }
2994}
2995
2996void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002997 LocationSummary* locations =
2998 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002999 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003000 case Primitive::kPrimInt: {
3001 locations->SetInAt(0, Location::RequiresRegister());
3002 locations->SetInAt(1, Location::Any());
3003 locations->SetOut(Location::SameAsFirstInput());
3004 break;
3005 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003006 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003007 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003008 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003009 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003010 break;
3011 }
Calin Juravle11351682014-10-23 15:38:15 +01003012 case Primitive::kPrimFloat:
3013 case Primitive::kPrimDouble: {
3014 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003015 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003016 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003017 break;
Calin Juravle11351682014-10-23 15:38:15 +01003018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019 default:
Calin Juravle11351682014-10-23 15:38:15 +01003020 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003021 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003022}
3023
3024void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3025 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003026 Location first = locations->InAt(0);
3027 Location second = locations->InAt(1);
3028 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003029 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003030 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003031 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003032 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003033 } else if (second.IsConstant()) {
3034 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003035 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003036 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003038 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003039 break;
3040 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003041 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003042 if (second.IsConstant()) {
3043 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3044 DCHECK(IsInt<32>(value));
3045 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3046 } else {
3047 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3048 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049 break;
3050 }
3051
Calin Juravle11351682014-10-23 15:38:15 +01003052 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003053 if (second.IsFpuRegister()) {
3054 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3055 } else if (second.IsConstant()) {
3056 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003057 codegen_->LiteralFloatAddress(
3058 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003059 } else {
3060 DCHECK(second.IsStackSlot());
3061 __ subss(first.AsFpuRegister<XmmRegister>(),
3062 Address(CpuRegister(RSP), second.GetStackIndex()));
3063 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003064 break;
Calin Juravle11351682014-10-23 15:38:15 +01003065 }
3066
3067 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003068 if (second.IsFpuRegister()) {
3069 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3070 } else if (second.IsConstant()) {
3071 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003072 codegen_->LiteralDoubleAddress(
3073 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003074 } else {
3075 DCHECK(second.IsDoubleStackSlot());
3076 __ subsd(first.AsFpuRegister<XmmRegister>(),
3077 Address(CpuRegister(RSP), second.GetStackIndex()));
3078 }
Calin Juravle11351682014-10-23 15:38:15 +01003079 break;
3080 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003081
3082 default:
Calin Juravle11351682014-10-23 15:38:15 +01003083 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003084 }
3085}
3086
Calin Juravle34bacdf2014-10-07 20:23:36 +01003087void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3088 LocationSummary* locations =
3089 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3090 switch (mul->GetResultType()) {
3091 case Primitive::kPrimInt: {
3092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003094 if (mul->InputAt(1)->IsIntConstant()) {
3095 // Can use 3 operand multiply.
3096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3097 } else {
3098 locations->SetOut(Location::SameAsFirstInput());
3099 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003100 break;
3101 }
3102 case Primitive::kPrimLong: {
3103 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003104 locations->SetInAt(1, Location::Any());
3105 if (mul->InputAt(1)->IsLongConstant() &&
3106 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003107 // Can use 3 operand multiply.
3108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3109 } else {
3110 locations->SetOut(Location::SameAsFirstInput());
3111 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003112 break;
3113 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003114 case Primitive::kPrimFloat:
3115 case Primitive::kPrimDouble: {
3116 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003117 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003119 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003120 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003121
3122 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003123 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003124 }
3125}
3126
3127void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3128 LocationSummary* locations = mul->GetLocations();
3129 Location first = locations->InAt(0);
3130 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003131 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003132 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003133 case Primitive::kPrimInt:
3134 // The constant may have ended up in a register, so test explicitly to avoid
3135 // problems where the output may not be the same as the first operand.
3136 if (mul->InputAt(1)->IsIntConstant()) {
3137 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3138 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3139 } else if (second.IsRegister()) {
3140 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003141 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003143 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003144 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003145 __ imull(first.AsRegister<CpuRegister>(),
3146 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003147 }
3148 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003149 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003150 // The constant may have ended up in a register, so test explicitly to avoid
3151 // problems where the output may not be the same as the first operand.
3152 if (mul->InputAt(1)->IsLongConstant()) {
3153 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3154 if (IsInt<32>(value)) {
3155 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3156 Immediate(static_cast<int32_t>(value)));
3157 } else {
3158 // Have to use the constant area.
3159 DCHECK(first.Equals(out));
3160 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3161 }
3162 } else if (second.IsRegister()) {
3163 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003164 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003165 } else {
3166 DCHECK(second.IsDoubleStackSlot());
3167 DCHECK(first.Equals(out));
3168 __ imulq(first.AsRegister<CpuRegister>(),
3169 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003170 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 break;
3172 }
3173
Calin Juravleb5bfa962014-10-21 18:02:24 +01003174 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003175 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003176 if (second.IsFpuRegister()) {
3177 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3178 } else if (second.IsConstant()) {
3179 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003180 codegen_->LiteralFloatAddress(
3181 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003182 } else {
3183 DCHECK(second.IsStackSlot());
3184 __ mulss(first.AsFpuRegister<XmmRegister>(),
3185 Address(CpuRegister(RSP), second.GetStackIndex()));
3186 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003188 }
3189
3190 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003191 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003192 if (second.IsFpuRegister()) {
3193 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3194 } else if (second.IsConstant()) {
3195 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003196 codegen_->LiteralDoubleAddress(
3197 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003198 } else {
3199 DCHECK(second.IsDoubleStackSlot());
3200 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3201 Address(CpuRegister(RSP), second.GetStackIndex()));
3202 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 break;
3204 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003205
3206 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003207 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208 }
3209}
3210
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003211void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3212 uint32_t stack_adjustment, bool is_float) {
3213 if (source.IsStackSlot()) {
3214 DCHECK(is_float);
3215 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3216 } else if (source.IsDoubleStackSlot()) {
3217 DCHECK(!is_float);
3218 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3219 } else {
3220 // Write the value to the temporary location on the stack and load to FP stack.
3221 if (is_float) {
3222 Location stack_temp = Location::StackSlot(temp_offset);
3223 codegen_->Move(stack_temp, source);
3224 __ flds(Address(CpuRegister(RSP), temp_offset));
3225 } else {
3226 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3227 codegen_->Move(stack_temp, source);
3228 __ fldl(Address(CpuRegister(RSP), temp_offset));
3229 }
3230 }
3231}
3232
3233void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3234 Primitive::Type type = rem->GetResultType();
3235 bool is_float = type == Primitive::kPrimFloat;
3236 size_t elem_size = Primitive::ComponentSize(type);
3237 LocationSummary* locations = rem->GetLocations();
3238 Location first = locations->InAt(0);
3239 Location second = locations->InAt(1);
3240 Location out = locations->Out();
3241
3242 // Create stack space for 2 elements.
3243 // TODO: enhance register allocator to ask for stack temporaries.
3244 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3245
3246 // Load the values to the FP stack in reverse order, using temporaries if needed.
3247 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3248 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3249
3250 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003251 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003252 __ Bind(&retry);
3253 __ fprem();
3254
3255 // Move FP status to AX.
3256 __ fstsw();
3257
3258 // And see if the argument reduction is complete. This is signaled by the
3259 // C2 FPU flag bit set to 0.
3260 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3261 __ j(kNotEqual, &retry);
3262
3263 // We have settled on the final value. Retrieve it into an XMM register.
3264 // Store FP top of stack to real stack.
3265 if (is_float) {
3266 __ fsts(Address(CpuRegister(RSP), 0));
3267 } else {
3268 __ fstl(Address(CpuRegister(RSP), 0));
3269 }
3270
3271 // Pop the 2 items from the FP stack.
3272 __ fucompp();
3273
3274 // Load the value from the stack into an XMM register.
3275 DCHECK(out.IsFpuRegister()) << out;
3276 if (is_float) {
3277 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3278 } else {
3279 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3280 }
3281
3282 // And remove the temporary stack space we allocated.
3283 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3284}
3285
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003286void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3287 DCHECK(instruction->IsDiv() || instruction->IsRem());
3288
3289 LocationSummary* locations = instruction->GetLocations();
3290 Location second = locations->InAt(1);
3291 DCHECK(second.IsConstant());
3292
3293 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3294 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003295 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003296
3297 DCHECK(imm == 1 || imm == -1);
3298
3299 switch (instruction->GetResultType()) {
3300 case Primitive::kPrimInt: {
3301 if (instruction->IsRem()) {
3302 __ xorl(output_register, output_register);
3303 } else {
3304 __ movl(output_register, input_register);
3305 if (imm == -1) {
3306 __ negl(output_register);
3307 }
3308 }
3309 break;
3310 }
3311
3312 case Primitive::kPrimLong: {
3313 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003314 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315 } else {
3316 __ movq(output_register, input_register);
3317 if (imm == -1) {
3318 __ negq(output_register);
3319 }
3320 }
3321 break;
3322 }
3323
3324 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003325 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003326 }
3327}
3328
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003329void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003330 LocationSummary* locations = instruction->GetLocations();
3331 Location second = locations->InAt(1);
3332
3333 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3334 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3335
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003336 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003337 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3338 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003339
3340 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3341
3342 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003343 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 __ testl(numerator, numerator);
3345 __ cmov(kGreaterEqual, tmp, numerator);
3346 int shift = CTZ(imm);
3347 __ sarl(tmp, Immediate(shift));
3348
3349 if (imm < 0) {
3350 __ negl(tmp);
3351 }
3352
3353 __ movl(output_register, tmp);
3354 } else {
3355 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3356 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3357
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003358 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 __ addq(rdx, numerator);
3360 __ testq(numerator, numerator);
3361 __ cmov(kGreaterEqual, rdx, numerator);
3362 int shift = CTZ(imm);
3363 __ sarq(rdx, Immediate(shift));
3364
3365 if (imm < 0) {
3366 __ negq(rdx);
3367 }
3368
3369 __ movq(output_register, rdx);
3370 }
3371}
3372
3373void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3374 DCHECK(instruction->IsDiv() || instruction->IsRem());
3375
3376 LocationSummary* locations = instruction->GetLocations();
3377 Location second = locations->InAt(1);
3378
3379 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3380 : locations->GetTemp(0).AsRegister<CpuRegister>();
3381 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3382 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3383 : locations->Out().AsRegister<CpuRegister>();
3384 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3385
3386 DCHECK_EQ(RAX, eax.AsRegister());
3387 DCHECK_EQ(RDX, edx.AsRegister());
3388 if (instruction->IsDiv()) {
3389 DCHECK_EQ(RAX, out.AsRegister());
3390 } else {
3391 DCHECK_EQ(RDX, out.AsRegister());
3392 }
3393
3394 int64_t magic;
3395 int shift;
3396
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003397 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003398 if (instruction->GetResultType() == Primitive::kPrimInt) {
3399 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3400
3401 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3402
3403 __ movl(numerator, eax);
3404
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003405 __ movl(eax, Immediate(magic));
3406 __ imull(numerator);
3407
3408 if (imm > 0 && magic < 0) {
3409 __ addl(edx, numerator);
3410 } else if (imm < 0 && magic > 0) {
3411 __ subl(edx, numerator);
3412 }
3413
3414 if (shift != 0) {
3415 __ sarl(edx, Immediate(shift));
3416 }
3417
3418 __ movl(eax, edx);
3419 __ shrl(edx, Immediate(31));
3420 __ addl(edx, eax);
3421
3422 if (instruction->IsRem()) {
3423 __ movl(eax, numerator);
3424 __ imull(edx, Immediate(imm));
3425 __ subl(eax, edx);
3426 __ movl(edx, eax);
3427 } else {
3428 __ movl(eax, edx);
3429 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 } else {
3431 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3432
3433 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3434
3435 CpuRegister rax = eax;
3436 CpuRegister rdx = edx;
3437
3438 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3439
3440 // Save the numerator.
3441 __ movq(numerator, rax);
3442
3443 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003444 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445
3446 // RDX:RAX = magic * numerator
3447 __ imulq(numerator);
3448
3449 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003450 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 __ addq(rdx, numerator);
3452 } else if (imm < 0 && magic > 0) {
3453 // RDX -= numerator
3454 __ subq(rdx, numerator);
3455 }
3456
3457 // Shift if needed.
3458 if (shift != 0) {
3459 __ sarq(rdx, Immediate(shift));
3460 }
3461
3462 // RDX += 1 if RDX < 0
3463 __ movq(rax, rdx);
3464 __ shrq(rdx, Immediate(63));
3465 __ addq(rdx, rax);
3466
3467 if (instruction->IsRem()) {
3468 __ movq(rax, numerator);
3469
3470 if (IsInt<32>(imm)) {
3471 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3472 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003473 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474 }
3475
3476 __ subq(rax, rdx);
3477 __ movq(rdx, rax);
3478 } else {
3479 __ movq(rax, rdx);
3480 }
3481 }
3482}
3483
Calin Juravlebacfec32014-11-14 15:54:36 +00003484void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3485 DCHECK(instruction->IsDiv() || instruction->IsRem());
3486 Primitive::Type type = instruction->GetResultType();
3487 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3488
3489 bool is_div = instruction->IsDiv();
3490 LocationSummary* locations = instruction->GetLocations();
3491
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003492 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3493 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003494
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003499 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003500
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 if (imm == 0) {
3502 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3503 } else if (imm == 1 || imm == -1) {
3504 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003505 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003506 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507 } else {
3508 DCHECK(imm <= -2 || imm >= 2);
3509 GenerateDivRemWithAnyConstant(instruction);
3510 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003511 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003512 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003514 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003515 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003516
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3518 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3519 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3520 // so it's safe to just use negl instead of more complex comparisons.
3521 if (type == Primitive::kPrimInt) {
3522 __ cmpl(second_reg, Immediate(-1));
3523 __ j(kEqual, slow_path->GetEntryLabel());
3524 // edx:eax <- sign-extended of eax
3525 __ cdq();
3526 // eax = quotient, edx = remainder
3527 __ idivl(second_reg);
3528 } else {
3529 __ cmpq(second_reg, Immediate(-1));
3530 __ j(kEqual, slow_path->GetEntryLabel());
3531 // rdx:rax <- sign-extended of rax
3532 __ cqo();
3533 // rax = quotient, rdx = remainder
3534 __ idivq(second_reg);
3535 }
3536 __ Bind(slow_path->GetExitLabel());
3537 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003538}
3539
Calin Juravle7c4954d2014-10-28 16:57:40 +00003540void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3541 LocationSummary* locations =
3542 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3543 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003544 case Primitive::kPrimInt:
3545 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003546 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003547 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003548 locations->SetOut(Location::SameAsFirstInput());
3549 // Intel uses edx:eax as the dividend.
3550 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003551 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3552 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3553 // output and request another temp.
3554 if (div->InputAt(1)->IsConstant()) {
3555 locations->AddTemp(Location::RequiresRegister());
3556 }
Calin Juravled0d48522014-11-04 16:40:20 +00003557 break;
3558 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003559
Calin Juravle7c4954d2014-10-28 16:57:40 +00003560 case Primitive::kPrimFloat:
3561 case Primitive::kPrimDouble: {
3562 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003563 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003564 locations->SetOut(Location::SameAsFirstInput());
3565 break;
3566 }
3567
3568 default:
3569 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3570 }
3571}
3572
3573void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3574 LocationSummary* locations = div->GetLocations();
3575 Location first = locations->InAt(0);
3576 Location second = locations->InAt(1);
3577 DCHECK(first.Equals(locations->Out()));
3578
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003579 Primitive::Type type = div->GetResultType();
3580 switch (type) {
3581 case Primitive::kPrimInt:
3582 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003583 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003584 break;
3585 }
3586
Calin Juravle7c4954d2014-10-28 16:57:40 +00003587 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003588 if (second.IsFpuRegister()) {
3589 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3590 } else if (second.IsConstant()) {
3591 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003592 codegen_->LiteralFloatAddress(
3593 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003594 } else {
3595 DCHECK(second.IsStackSlot());
3596 __ divss(first.AsFpuRegister<XmmRegister>(),
3597 Address(CpuRegister(RSP), second.GetStackIndex()));
3598 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003599 break;
3600 }
3601
3602 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003603 if (second.IsFpuRegister()) {
3604 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3605 } else if (second.IsConstant()) {
3606 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003607 codegen_->LiteralDoubleAddress(
3608 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003609 } else {
3610 DCHECK(second.IsDoubleStackSlot());
3611 __ divsd(first.AsFpuRegister<XmmRegister>(),
3612 Address(CpuRegister(RSP), second.GetStackIndex()));
3613 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003614 break;
3615 }
3616
3617 default:
3618 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3619 }
3620}
3621
Calin Juravlebacfec32014-11-14 15:54:36 +00003622void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003623 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003624 LocationSummary* locations =
3625 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003626
3627 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003628 case Primitive::kPrimInt:
3629 case Primitive::kPrimLong: {
3630 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003631 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003632 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3633 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003634 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3635 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3636 // output and request another temp.
3637 if (rem->InputAt(1)->IsConstant()) {
3638 locations->AddTemp(Location::RequiresRegister());
3639 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003640 break;
3641 }
3642
3643 case Primitive::kPrimFloat:
3644 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003645 locations->SetInAt(0, Location::Any());
3646 locations->SetInAt(1, Location::Any());
3647 locations->SetOut(Location::RequiresFpuRegister());
3648 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003649 break;
3650 }
3651
3652 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003653 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003654 }
3655}
3656
3657void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3658 Primitive::Type type = rem->GetResultType();
3659 switch (type) {
3660 case Primitive::kPrimInt:
3661 case Primitive::kPrimLong: {
3662 GenerateDivRemIntegral(rem);
3663 break;
3664 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003665 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003666 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003667 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003668 break;
3669 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003670 default:
3671 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3672 }
3673}
3674
Calin Juravled0d48522014-11-04 16:40:20 +00003675void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003676 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3677 ? LocationSummary::kCallOnSlowPath
3678 : LocationSummary::kNoCall;
3679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003680 locations->SetInAt(0, Location::Any());
3681 if (instruction->HasUses()) {
3682 locations->SetOut(Location::SameAsFirstInput());
3683 }
3684}
3685
3686void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003687 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003688 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3689 codegen_->AddSlowPath(slow_path);
3690
3691 LocationSummary* locations = instruction->GetLocations();
3692 Location value = locations->InAt(0);
3693
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003694 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003695 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003696 case Primitive::kPrimByte:
3697 case Primitive::kPrimChar:
3698 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003699 case Primitive::kPrimInt: {
3700 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003702 __ j(kEqual, slow_path->GetEntryLabel());
3703 } else if (value.IsStackSlot()) {
3704 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3705 __ j(kEqual, slow_path->GetEntryLabel());
3706 } else {
3707 DCHECK(value.IsConstant()) << value;
3708 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3709 __ jmp(slow_path->GetEntryLabel());
3710 }
3711 }
3712 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003713 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003714 case Primitive::kPrimLong: {
3715 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003716 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003717 __ j(kEqual, slow_path->GetEntryLabel());
3718 } else if (value.IsDoubleStackSlot()) {
3719 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3720 __ j(kEqual, slow_path->GetEntryLabel());
3721 } else {
3722 DCHECK(value.IsConstant()) << value;
3723 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3724 __ jmp(slow_path->GetEntryLabel());
3725 }
3726 }
3727 break;
3728 }
3729 default:
3730 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003731 }
Calin Juravled0d48522014-11-04 16:40:20 +00003732}
3733
Calin Juravle9aec02f2014-11-18 23:06:35 +00003734void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3735 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3736
3737 LocationSummary* locations =
3738 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3739
3740 switch (op->GetResultType()) {
3741 case Primitive::kPrimInt:
3742 case Primitive::kPrimLong: {
3743 locations->SetInAt(0, Location::RequiresRegister());
3744 // The shift count needs to be in CL.
3745 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3746 locations->SetOut(Location::SameAsFirstInput());
3747 break;
3748 }
3749 default:
3750 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3751 }
3752}
3753
3754void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3755 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3756
3757 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 Location second = locations->InAt(1);
3760
3761 switch (op->GetResultType()) {
3762 case Primitive::kPrimInt: {
3763 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003764 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003765 if (op->IsShl()) {
3766 __ shll(first_reg, second_reg);
3767 } else if (op->IsShr()) {
3768 __ sarl(first_reg, second_reg);
3769 } else {
3770 __ shrl(first_reg, second_reg);
3771 }
3772 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003773 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003774 if (op->IsShl()) {
3775 __ shll(first_reg, imm);
3776 } else if (op->IsShr()) {
3777 __ sarl(first_reg, imm);
3778 } else {
3779 __ shrl(first_reg, imm);
3780 }
3781 }
3782 break;
3783 }
3784 case Primitive::kPrimLong: {
3785 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003787 if (op->IsShl()) {
3788 __ shlq(first_reg, second_reg);
3789 } else if (op->IsShr()) {
3790 __ sarq(first_reg, second_reg);
3791 } else {
3792 __ shrq(first_reg, second_reg);
3793 }
3794 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003795 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796 if (op->IsShl()) {
3797 __ shlq(first_reg, imm);
3798 } else if (op->IsShr()) {
3799 __ sarq(first_reg, imm);
3800 } else {
3801 __ shrq(first_reg, imm);
3802 }
3803 }
3804 break;
3805 }
3806 default:
3807 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003808 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003809 }
3810}
3811
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003812void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3813 LocationSummary* locations =
3814 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3815
3816 switch (ror->GetResultType()) {
3817 case Primitive::kPrimInt:
3818 case Primitive::kPrimLong: {
3819 locations->SetInAt(0, Location::RequiresRegister());
3820 // The shift count needs to be in CL (unless it is a constant).
3821 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3822 locations->SetOut(Location::SameAsFirstInput());
3823 break;
3824 }
3825 default:
3826 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3827 UNREACHABLE();
3828 }
3829}
3830
3831void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3832 LocationSummary* locations = ror->GetLocations();
3833 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3834 Location second = locations->InAt(1);
3835
3836 switch (ror->GetResultType()) {
3837 case Primitive::kPrimInt:
3838 if (second.IsRegister()) {
3839 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3840 __ rorl(first_reg, second_reg);
3841 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003842 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003843 __ rorl(first_reg, imm);
3844 }
3845 break;
3846 case Primitive::kPrimLong:
3847 if (second.IsRegister()) {
3848 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3849 __ rorq(first_reg, second_reg);
3850 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003851 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003852 __ rorq(first_reg, imm);
3853 }
3854 break;
3855 default:
3856 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3857 UNREACHABLE();
3858 }
3859}
3860
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3862 HandleShift(shl);
3863}
3864
3865void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3866 HandleShift(shl);
3867}
3868
3869void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3870 HandleShift(shr);
3871}
3872
3873void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3874 HandleShift(shr);
3875}
3876
3877void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3878 HandleShift(ushr);
3879}
3880
3881void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3882 HandleShift(ushr);
3883}
3884
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003885void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003886 LocationSummary* locations =
3887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003888 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003889 if (instruction->IsStringAlloc()) {
3890 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3891 } else {
3892 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3893 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3894 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003895 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003896}
3897
3898void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003899 // Note: if heap poisoning is enabled, the entry point takes cares
3900 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003901 if (instruction->IsStringAlloc()) {
3902 // String is allocated through StringFactory. Call NewEmptyString entry point.
3903 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3904 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3905 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3906 __ call(Address(temp, code_offset.SizeValue()));
3907 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3908 } else {
3909 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3910 instruction,
3911 instruction->GetDexPc(),
3912 nullptr);
3913 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3914 DCHECK(!codegen_->IsLeafMethod());
3915 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003916}
3917
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003918void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3919 LocationSummary* locations =
3920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3921 InvokeRuntimeCallingConvention calling_convention;
3922 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003923 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003924 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003925 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003926}
3927
3928void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3929 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003930 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3931 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003932 // Note: if heap poisoning is enabled, the entry point takes cares
3933 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003934 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3935 instruction,
3936 instruction->GetDexPc(),
3937 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003938 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003939
3940 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003941}
3942
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003943void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003944 LocationSummary* locations =
3945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003946 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3947 if (location.IsStackSlot()) {
3948 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3949 } else if (location.IsDoubleStackSlot()) {
3950 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3951 }
3952 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003953}
3954
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003955void InstructionCodeGeneratorX86_64::VisitParameterValue(
3956 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003957 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003958}
3959
3960void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3963 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3964}
3965
3966void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3967 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3968 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003969}
3970
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003971void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3974 locations->SetInAt(0, Location::RequiresRegister());
3975 locations->SetOut(Location::RequiresRegister());
3976}
3977
3978void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3979 LocationSummary* locations = instruction->GetLocations();
3980 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003981 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003982 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3983 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
3984 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03003985 __ movq(locations->Out().AsRegister<CpuRegister>(),
3986 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3987 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
3988 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3989 instruction->GetIndex() % ImTable::kSize, kX86_64PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003990 }
3991 __ movq(locations->Out().AsRegister<CpuRegister>(),
3992 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
3993}
3994
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003995void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003996 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003997 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003998 locations->SetInAt(0, Location::RequiresRegister());
3999 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004000}
4001
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004002void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4003 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004004 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4005 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004006 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004007 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004008 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004009 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004010 break;
4011
4012 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004014 break;
4015
4016 default:
4017 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004019}
4020
David Brazdil66d126e2015-04-03 16:02:44 +01004021void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4022 LocationSummary* locations =
4023 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4024 locations->SetInAt(0, Location::RequiresRegister());
4025 locations->SetOut(Location::SameAsFirstInput());
4026}
4027
4028void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004029 LocationSummary* locations = bool_not->GetLocations();
4030 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4031 locations->Out().AsRegister<CpuRegister>().AsRegister());
4032 Location out = locations->Out();
4033 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4034}
4035
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004036void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004037 LocationSummary* locations =
4038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004039 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004040 locations->SetInAt(i, Location::Any());
4041 }
4042 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004043}
4044
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004045void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004046 LOG(FATAL) << "Unimplemented";
4047}
4048
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004049void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004050 /*
4051 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004052 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004053 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4054 */
4055 switch (kind) {
4056 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004057 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004058 break;
4059 }
4060 case MemBarrierKind::kAnyStore:
4061 case MemBarrierKind::kLoadAny:
4062 case MemBarrierKind::kStoreStore: {
4063 // nop
4064 break;
4065 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004066 case MemBarrierKind::kNTStoreStore:
4067 // Non-Temporal Store/Store needs an explicit fence.
4068 MemoryFence(/* non-temporal */ true);
4069 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004070 }
4071}
4072
4073void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4074 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4075
Roland Levillain0d5a2812015-11-13 10:07:31 +00004076 bool object_field_get_with_read_barrier =
4077 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004078 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004079 new (GetGraph()->GetArena()) LocationSummary(instruction,
4080 object_field_get_with_read_barrier ?
4081 LocationSummary::kCallOnSlowPath :
4082 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004083 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004084 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4085 locations->SetOut(Location::RequiresFpuRegister());
4086 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004087 // The output overlaps for an object field get when read barriers
4088 // are enabled: we do not want the move to overwrite the object's
4089 // location, as we need it to emit the read barrier.
4090 locations->SetOut(
4091 Location::RequiresRegister(),
4092 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004093 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004094 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4095 // We need a temporary register for the read barrier marking slow
4096 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4097 locations->AddTemp(Location::RequiresRegister());
4098 }
Calin Juravle52c48962014-12-16 17:02:57 +00004099}
4100
4101void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4102 const FieldInfo& field_info) {
4103 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4104
4105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004106 Location base_loc = locations->InAt(0);
4107 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004108 Location out = locations->Out();
4109 bool is_volatile = field_info.IsVolatile();
4110 Primitive::Type field_type = field_info.GetFieldType();
4111 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4112
4113 switch (field_type) {
4114 case Primitive::kPrimBoolean: {
4115 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4116 break;
4117 }
4118
4119 case Primitive::kPrimByte: {
4120 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4121 break;
4122 }
4123
4124 case Primitive::kPrimShort: {
4125 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4126 break;
4127 }
4128
4129 case Primitive::kPrimChar: {
4130 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4131 break;
4132 }
4133
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004134 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004135 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4136 break;
4137 }
4138
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004139 case Primitive::kPrimNot: {
4140 // /* HeapReference<Object> */ out = *(base + offset)
4141 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4142 Location temp_loc = locations->GetTemp(0);
4143 // Note that a potential implicit null check is handled in this
4144 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4145 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4146 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4147 if (is_volatile) {
4148 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4149 }
4150 } else {
4151 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4152 codegen_->MaybeRecordImplicitNullCheck(instruction);
4153 if (is_volatile) {
4154 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4155 }
4156 // If read barriers are enabled, emit read barriers other than
4157 // Baker's using a slow path (and also unpoison the loaded
4158 // reference, if heap poisoning is enabled).
4159 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4160 }
4161 break;
4162 }
4163
Calin Juravle52c48962014-12-16 17:02:57 +00004164 case Primitive::kPrimLong: {
4165 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4166 break;
4167 }
4168
4169 case Primitive::kPrimFloat: {
4170 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4171 break;
4172 }
4173
4174 case Primitive::kPrimDouble: {
4175 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4176 break;
4177 }
4178
4179 case Primitive::kPrimVoid:
4180 LOG(FATAL) << "Unreachable type " << field_type;
4181 UNREACHABLE();
4182 }
4183
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004184 if (field_type == Primitive::kPrimNot) {
4185 // Potential implicit null checks, in the case of reference
4186 // fields, are handled in the previous switch statement.
4187 } else {
4188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004189 }
Roland Levillain4d027112015-07-01 15:41:14 +01004190
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004191 if (is_volatile) {
4192 if (field_type == Primitive::kPrimNot) {
4193 // Memory barriers, in the case of references, are also handled
4194 // in the previous switch statement.
4195 } else {
4196 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4197 }
Roland Levillain4d027112015-07-01 15:41:14 +01004198 }
Calin Juravle52c48962014-12-16 17:02:57 +00004199}
4200
4201void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4202 const FieldInfo& field_info) {
4203 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4204
4205 LocationSummary* locations =
4206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004207 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004208 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004210 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004211
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004212 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004213 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004214 if (is_volatile) {
4215 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4216 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4217 } else {
4218 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4219 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004220 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004221 if (is_volatile) {
4222 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4223 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4224 } else {
4225 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4226 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004227 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004228 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004229 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004230 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004231 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004232 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4233 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004234 locations->AddTemp(Location::RequiresRegister());
4235 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236}
4237
Calin Juravle52c48962014-12-16 17:02:57 +00004238void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004239 const FieldInfo& field_info,
4240 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004241 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4242
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004243 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004244 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4245 Location value = locations->InAt(1);
4246 bool is_volatile = field_info.IsVolatile();
4247 Primitive::Type field_type = field_info.GetFieldType();
4248 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4249
4250 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004251 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004252 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253
Mark Mendellea5af682015-10-22 17:35:49 -04004254 bool maybe_record_implicit_null_check_done = false;
4255
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004256 switch (field_type) {
4257 case Primitive::kPrimBoolean:
4258 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004259 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004260 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004261 __ movb(Address(base, offset), Immediate(v));
4262 } else {
4263 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4264 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265 break;
4266 }
4267
4268 case Primitive::kPrimShort:
4269 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004270 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004271 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004272 __ movw(Address(base, offset), Immediate(v));
4273 } else {
4274 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4275 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004276 break;
4277 }
4278
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004281 if (value.IsConstant()) {
4282 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004283 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4284 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4285 // Note: if heap poisoning is enabled, no need to poison
4286 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004287 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004288 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004289 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4290 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4291 __ movl(temp, value.AsRegister<CpuRegister>());
4292 __ PoisonHeapReference(temp);
4293 __ movl(Address(base, offset), temp);
4294 } else {
4295 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4296 }
Mark Mendell40741f32015-04-20 22:10:34 -04004297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298 break;
4299 }
4300
4301 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004302 if (value.IsConstant()) {
4303 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004304 codegen_->MoveInt64ToAddress(Address(base, offset),
4305 Address(base, offset + sizeof(int32_t)),
4306 v,
4307 instruction);
4308 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004309 } else {
4310 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4311 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312 break;
4313 }
4314
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004315 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004316 if (value.IsConstant()) {
4317 int32_t v =
4318 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4319 __ movl(Address(base, offset), Immediate(v));
4320 } else {
4321 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4322 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004323 break;
4324 }
4325
4326 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004327 if (value.IsConstant()) {
4328 int64_t v =
4329 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4330 codegen_->MoveInt64ToAddress(Address(base, offset),
4331 Address(base, offset + sizeof(int32_t)),
4332 v,
4333 instruction);
4334 maybe_record_implicit_null_check_done = true;
4335 } else {
4336 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4337 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004338 break;
4339 }
4340
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341 case Primitive::kPrimVoid:
4342 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004343 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344 }
Calin Juravle52c48962014-12-16 17:02:57 +00004345
Mark Mendellea5af682015-10-22 17:35:49 -04004346 if (!maybe_record_implicit_null_check_done) {
4347 codegen_->MaybeRecordImplicitNullCheck(instruction);
4348 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004349
4350 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4351 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4352 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004353 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004354 }
4355
Calin Juravle52c48962014-12-16 17:02:57 +00004356 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004357 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004358 }
4359}
4360
4361void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4362 HandleFieldSet(instruction, instruction->GetFieldInfo());
4363}
4364
4365void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004366 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367}
4368
4369void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004370 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371}
4372
4373void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004374 HandleFieldGet(instruction, instruction->GetFieldInfo());
4375}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
Calin Juravle52c48962014-12-16 17:02:57 +00004377void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4378 HandleFieldGet(instruction);
4379}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380
Calin Juravle52c48962014-12-16 17:02:57 +00004381void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4382 HandleFieldGet(instruction, instruction->GetFieldInfo());
4383}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384
Calin Juravle52c48962014-12-16 17:02:57 +00004385void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4386 HandleFieldSet(instruction, instruction->GetFieldInfo());
4387}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004388
Calin Juravle52c48962014-12-16 17:02:57 +00004389void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004390 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391}
4392
Calin Juravlee460d1d2015-09-29 04:52:17 +01004393void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4394 HUnresolvedInstanceFieldGet* instruction) {
4395 FieldAccessCallingConventionX86_64 calling_convention;
4396 codegen_->CreateUnresolvedFieldLocationSummary(
4397 instruction, instruction->GetFieldType(), calling_convention);
4398}
4399
4400void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4401 HUnresolvedInstanceFieldGet* instruction) {
4402 FieldAccessCallingConventionX86_64 calling_convention;
4403 codegen_->GenerateUnresolvedFieldAccess(instruction,
4404 instruction->GetFieldType(),
4405 instruction->GetFieldIndex(),
4406 instruction->GetDexPc(),
4407 calling_convention);
4408}
4409
4410void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4411 HUnresolvedInstanceFieldSet* instruction) {
4412 FieldAccessCallingConventionX86_64 calling_convention;
4413 codegen_->CreateUnresolvedFieldLocationSummary(
4414 instruction, instruction->GetFieldType(), calling_convention);
4415}
4416
4417void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4418 HUnresolvedInstanceFieldSet* instruction) {
4419 FieldAccessCallingConventionX86_64 calling_convention;
4420 codegen_->GenerateUnresolvedFieldAccess(instruction,
4421 instruction->GetFieldType(),
4422 instruction->GetFieldIndex(),
4423 instruction->GetDexPc(),
4424 calling_convention);
4425}
4426
4427void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4428 HUnresolvedStaticFieldGet* instruction) {
4429 FieldAccessCallingConventionX86_64 calling_convention;
4430 codegen_->CreateUnresolvedFieldLocationSummary(
4431 instruction, instruction->GetFieldType(), calling_convention);
4432}
4433
4434void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4435 HUnresolvedStaticFieldGet* instruction) {
4436 FieldAccessCallingConventionX86_64 calling_convention;
4437 codegen_->GenerateUnresolvedFieldAccess(instruction,
4438 instruction->GetFieldType(),
4439 instruction->GetFieldIndex(),
4440 instruction->GetDexPc(),
4441 calling_convention);
4442}
4443
4444void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4445 HUnresolvedStaticFieldSet* instruction) {
4446 FieldAccessCallingConventionX86_64 calling_convention;
4447 codegen_->CreateUnresolvedFieldLocationSummary(
4448 instruction, instruction->GetFieldType(), calling_convention);
4449}
4450
4451void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4452 HUnresolvedStaticFieldSet* instruction) {
4453 FieldAccessCallingConventionX86_64 calling_convention;
4454 codegen_->GenerateUnresolvedFieldAccess(instruction,
4455 instruction->GetFieldType(),
4456 instruction->GetFieldIndex(),
4457 instruction->GetDexPc(),
4458 calling_convention);
4459}
4460
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004462 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4463 ? LocationSummary::kCallOnSlowPath
4464 : LocationSummary::kNoCall;
4465 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4466 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004467 ? Location::RequiresRegister()
4468 : Location::Any();
4469 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004470 if (instruction->HasUses()) {
4471 locations->SetOut(Location::SameAsFirstInput());
4472 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004473}
4474
Calin Juravle2ae48182016-03-16 14:05:09 +00004475void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4476 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004477 return;
4478 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004479 LocationSummary* locations = instruction->GetLocations();
4480 Location obj = locations->InAt(0);
4481
4482 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004483 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004484}
4485
Calin Juravle2ae48182016-03-16 14:05:09 +00004486void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004487 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004488 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489
4490 LocationSummary* locations = instruction->GetLocations();
4491 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492
4493 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004494 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004495 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004496 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004497 } else {
4498 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004499 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004500 __ jmp(slow_path->GetEntryLabel());
4501 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502 }
4503 __ j(kEqual, slow_path->GetEntryLabel());
4504}
4505
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004506void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004507 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004508}
4509
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004510void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004511 bool object_array_get_with_read_barrier =
4512 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004513 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004514 new (GetGraph()->GetArena()) LocationSummary(instruction,
4515 object_array_get_with_read_barrier ?
4516 LocationSummary::kCallOnSlowPath :
4517 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004518 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004519 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004520 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4521 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4522 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004523 // The output overlaps for an object array get when read barriers
4524 // are enabled: we do not want the move to overwrite the array's
4525 // location, as we need it to emit the read barrier.
4526 locations->SetOut(
4527 Location::RequiresRegister(),
4528 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004529 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004530 // We need a temporary register for the read barrier marking slow
4531 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4532 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4533 locations->AddTemp(Location::RequiresRegister());
4534 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535}
4536
4537void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4538 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004539 Location obj_loc = locations->InAt(0);
4540 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004542 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004544 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004545 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004546 case Primitive::kPrimBoolean: {
4547 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004548 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 if (index.IsConstant()) {
4550 __ movzxb(out, Address(obj,
4551 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4552 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004553 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004554 }
4555 break;
4556 }
4557
4558 case Primitive::kPrimByte: {
4559 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004560 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004561 if (index.IsConstant()) {
4562 __ movsxb(out, Address(obj,
4563 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4564 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004565 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566 }
4567 break;
4568 }
4569
4570 case Primitive::kPrimShort: {
4571 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004572 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573 if (index.IsConstant()) {
4574 __ movsxw(out, Address(obj,
4575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4576 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004577 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 }
4579 break;
4580 }
4581
4582 case Primitive::kPrimChar: {
4583 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004584 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004585 if (index.IsConstant()) {
4586 __ movzxw(out, Address(obj,
4587 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4588 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004589 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 }
4591 break;
4592 }
4593
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004594 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004596 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 if (index.IsConstant()) {
4598 __ movl(out, Address(obj,
4599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4600 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004601 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602 }
4603 break;
4604 }
4605
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004606 case Primitive::kPrimNot: {
4607 static_assert(
4608 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4609 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4610 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4611 // /* HeapReference<Object> */ out =
4612 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4613 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4614 Location temp = locations->GetTemp(0);
4615 // Note that a potential implicit null check is handled in this
4616 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4617 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4618 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4619 } else {
4620 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4621 if (index.IsConstant()) {
4622 uint32_t offset =
4623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4624 __ movl(out, Address(obj, offset));
4625 codegen_->MaybeRecordImplicitNullCheck(instruction);
4626 // If read barriers are enabled, emit read barriers other than
4627 // Baker's using a slow path (and also unpoison the loaded
4628 // reference, if heap poisoning is enabled).
4629 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4630 } else {
4631 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4632 codegen_->MaybeRecordImplicitNullCheck(instruction);
4633 // If read barriers are enabled, emit read barriers other than
4634 // Baker's using a slow path (and also unpoison the loaded
4635 // reference, if heap poisoning is enabled).
4636 codegen_->MaybeGenerateReadBarrierSlow(
4637 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4638 }
4639 }
4640 break;
4641 }
4642
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 case Primitive::kPrimLong: {
4644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004645 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004646 if (index.IsConstant()) {
4647 __ movq(out, Address(obj,
4648 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4649 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004650 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 }
4652 break;
4653 }
4654
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004655 case Primitive::kPrimFloat: {
4656 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004657 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004658 if (index.IsConstant()) {
4659 __ movss(out, Address(obj,
4660 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4661 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004662 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004663 }
4664 break;
4665 }
4666
4667 case Primitive::kPrimDouble: {
4668 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004669 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004670 if (index.IsConstant()) {
4671 __ movsd(out, Address(obj,
4672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4673 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004674 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004675 }
4676 break;
4677 }
4678
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004679 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004680 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004681 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682 }
Roland Levillain4d027112015-07-01 15:41:14 +01004683
4684 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004685 // Potential implicit null checks, in the case of reference
4686 // arrays, are handled in the previous switch statement.
4687 } else {
4688 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004689 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004690}
4691
4692void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004693 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004694
4695 bool needs_write_barrier =
4696 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004697 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004698 bool object_array_set_with_read_barrier =
4699 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004700
Nicolas Geoffray39468442014-09-02 15:17:15 +01004701 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004702 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004703 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004704 LocationSummary::kCallOnSlowPath :
4705 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004706
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004707 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004708 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4709 if (Primitive::IsFloatingPointType(value_type)) {
4710 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004711 } else {
4712 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4713 }
4714
4715 if (needs_write_barrier) {
4716 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004717
4718 // This first temporary register is possibly used for heap
4719 // reference poisoning and/or read barrier emission too.
4720 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004721 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004722 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004723}
4724
4725void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4726 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004727 Location array_loc = locations->InAt(0);
4728 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004730 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004731 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004732 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004733 bool needs_write_barrier =
4734 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004735 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4736 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4737 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738
4739 switch (value_type) {
4740 case Primitive::kPrimBoolean:
4741 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004742 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4743 Address address = index.IsConstant()
4744 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4745 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4746 if (value.IsRegister()) {
4747 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004748 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004749 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004751 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 break;
4753 }
4754
4755 case Primitive::kPrimShort:
4756 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004757 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4758 Address address = index.IsConstant()
4759 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4760 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4761 if (value.IsRegister()) {
4762 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004763 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 DCHECK(value.IsConstant()) << value;
4765 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 break;
4769 }
4770
4771 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004772 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4773 Address address = index.IsConstant()
4774 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4775 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004776
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004777 if (!value.IsRegister()) {
4778 // Just setting null.
4779 DCHECK(instruction->InputAt(2)->IsNullConstant());
4780 DCHECK(value.IsConstant()) << value;
4781 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004782 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004783 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004784 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004785 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004786 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787
4788 DCHECK(needs_write_barrier);
4789 CpuRegister register_value = value.AsRegister<CpuRegister>();
4790 NearLabel done, not_null, do_put;
4791 SlowPathCode* slow_path = nullptr;
4792 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004793 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4795 codegen_->AddSlowPath(slow_path);
4796 if (instruction->GetValueCanBeNull()) {
4797 __ testl(register_value, register_value);
4798 __ j(kNotEqual, &not_null);
4799 __ movl(address, Immediate(0));
4800 codegen_->MaybeRecordImplicitNullCheck(instruction);
4801 __ jmp(&done);
4802 __ Bind(&not_null);
4803 }
4804
Roland Levillain0d5a2812015-11-13 10:07:31 +00004805 if (kEmitCompilerReadBarrier) {
4806 // When read barriers are enabled, the type checking
4807 // instrumentation requires two read barriers:
4808 //
4809 // __ movl(temp2, temp);
4810 // // /* HeapReference<Class> */ temp = temp->component_type_
4811 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004812 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004813 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4814 //
4815 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4816 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004817 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004818 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4819 //
4820 // __ cmpl(temp, temp2);
4821 //
4822 // However, the second read barrier may trash `temp`, as it
4823 // is a temporary register, and as such would not be saved
4824 // along with live registers before calling the runtime (nor
4825 // restored afterwards). So in this case, we bail out and
4826 // delegate the work to the array set slow path.
4827 //
4828 // TODO: Extend the register allocator to support a new
4829 // "(locally) live temp" location so as to avoid always
4830 // going into the slow path when read barriers are enabled.
4831 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004833 // /* HeapReference<Class> */ temp = array->klass_
4834 __ movl(temp, Address(array, class_offset));
4835 codegen_->MaybeRecordImplicitNullCheck(instruction);
4836 __ MaybeUnpoisonHeapReference(temp);
4837
4838 // /* HeapReference<Class> */ temp = temp->component_type_
4839 __ movl(temp, Address(temp, component_offset));
4840 // If heap poisoning is enabled, no need to unpoison `temp`
4841 // nor the object reference in `register_value->klass`, as
4842 // we are comparing two poisoned references.
4843 __ cmpl(temp, Address(register_value, class_offset));
4844
4845 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4846 __ j(kEqual, &do_put);
4847 // If heap poisoning is enabled, the `temp` reference has
4848 // not been unpoisoned yet; unpoison it now.
4849 __ MaybeUnpoisonHeapReference(temp);
4850
4851 // /* HeapReference<Class> */ temp = temp->super_class_
4852 __ movl(temp, Address(temp, super_offset));
4853 // If heap poisoning is enabled, no need to unpoison
4854 // `temp`, as we are comparing against null below.
4855 __ testl(temp, temp);
4856 __ j(kNotEqual, slow_path->GetEntryLabel());
4857 __ Bind(&do_put);
4858 } else {
4859 __ j(kNotEqual, slow_path->GetEntryLabel());
4860 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 }
4862 }
4863
4864 if (kPoisonHeapReferences) {
4865 __ movl(temp, register_value);
4866 __ PoisonHeapReference(temp);
4867 __ movl(address, temp);
4868 } else {
4869 __ movl(address, register_value);
4870 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004871 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004872 codegen_->MaybeRecordImplicitNullCheck(instruction);
4873 }
4874
4875 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4876 codegen_->MarkGCCard(
4877 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4878 __ Bind(&done);
4879
4880 if (slow_path != nullptr) {
4881 __ Bind(slow_path->GetExitLabel());
4882 }
4883
4884 break;
4885 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004886
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 case Primitive::kPrimInt: {
4888 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4889 Address address = index.IsConstant()
4890 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4891 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4892 if (value.IsRegister()) {
4893 __ movl(address, value.AsRegister<CpuRegister>());
4894 } else {
4895 DCHECK(value.IsConstant()) << value;
4896 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4897 __ movl(address, Immediate(v));
4898 }
4899 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 break;
4901 }
4902
4903 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004904 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4905 Address address = index.IsConstant()
4906 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4907 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4908 if (value.IsRegister()) {
4909 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004910 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004912 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004913 Address address_high = index.IsConstant()
4914 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4915 offset + sizeof(int32_t))
4916 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4917 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 }
4919 break;
4920 }
4921
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004922 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004923 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4924 Address address = index.IsConstant()
4925 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4926 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004927 if (value.IsFpuRegister()) {
4928 __ movss(address, value.AsFpuRegister<XmmRegister>());
4929 } else {
4930 DCHECK(value.IsConstant());
4931 int32_t v =
4932 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4933 __ movl(address, Immediate(v));
4934 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004935 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004936 break;
4937 }
4938
4939 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004940 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4941 Address address = index.IsConstant()
4942 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4943 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004944 if (value.IsFpuRegister()) {
4945 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4946 codegen_->MaybeRecordImplicitNullCheck(instruction);
4947 } else {
4948 int64_t v =
4949 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4950 Address address_high = index.IsConstant()
4951 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4952 offset + sizeof(int32_t))
4953 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4954 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4955 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004956 break;
4957 }
4958
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004959 case Primitive::kPrimVoid:
4960 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004961 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004962 }
4963}
4964
4965void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004966 LocationSummary* locations =
4967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004968 locations->SetInAt(0, Location::RequiresRegister());
4969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970}
4971
4972void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4973 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004974 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004975 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4976 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004977 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004978 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004979}
4980
4981void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004982 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4983 ? LocationSummary::kCallOnSlowPath
4984 : LocationSummary::kNoCall;
4985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004986 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004987 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004988 if (instruction->HasUses()) {
4989 locations->SetOut(Location::SameAsFirstInput());
4990 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004991}
4992
4993void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4994 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004995 Location index_loc = locations->InAt(0);
4996 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004997 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004998 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999
Mark Mendell99dbd682015-04-22 16:18:52 -04005000 if (length_loc.IsConstant()) {
5001 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5002 if (index_loc.IsConstant()) {
5003 // BCE will remove the bounds check if we are guarenteed to pass.
5004 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5005 if (index < 0 || index >= length) {
5006 codegen_->AddSlowPath(slow_path);
5007 __ jmp(slow_path->GetEntryLabel());
5008 } else {
5009 // Some optimization after BCE may have generated this, and we should not
5010 // generate a bounds check if it is a valid range.
5011 }
5012 return;
5013 }
5014
5015 // We have to reverse the jump condition because the length is the constant.
5016 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5017 __ cmpl(index_reg, Immediate(length));
5018 codegen_->AddSlowPath(slow_path);
5019 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005020 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005021 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5022 if (index_loc.IsConstant()) {
5023 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5024 __ cmpl(length, Immediate(value));
5025 } else {
5026 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5027 }
5028 codegen_->AddSlowPath(slow_path);
5029 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005030 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031}
5032
5033void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5034 CpuRegister card,
5035 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005036 CpuRegister value,
5037 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005038 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005039 if (value_can_be_null) {
5040 __ testl(value, value);
5041 __ j(kEqual, &is_null);
5042 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005043 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5044 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045 __ movq(temp, object);
5046 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005047 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005048 if (value_can_be_null) {
5049 __ Bind(&is_null);
5050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051}
5052
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005053void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005054 LOG(FATAL) << "Unimplemented";
5055}
5056
5057void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005058 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5059}
5060
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005061void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5063}
5064
5065void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005066 HBasicBlock* block = instruction->GetBlock();
5067 if (block->GetLoopInformation() != nullptr) {
5068 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5069 // The back edge will generate the suspend check.
5070 return;
5071 }
5072 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5073 // The goto will generate the suspend check.
5074 return;
5075 }
5076 GenerateSuspendCheck(instruction, nullptr);
5077}
5078
5079void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5080 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005081 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005082 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5083 if (slow_path == nullptr) {
5084 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5085 instruction->SetSlowPath(slow_path);
5086 codegen_->AddSlowPath(slow_path);
5087 if (successor != nullptr) {
5088 DCHECK(successor->IsLoopHeader());
5089 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5090 }
5091 } else {
5092 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5093 }
5094
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005095 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5096 /* no_rip */ true),
5097 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005098 if (successor == nullptr) {
5099 __ j(kNotEqual, slow_path->GetEntryLabel());
5100 __ Bind(slow_path->GetReturnLabel());
5101 } else {
5102 __ j(kEqual, codegen_->GetLabelOf(successor));
5103 __ jmp(slow_path->GetEntryLabel());
5104 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005105}
5106
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005107X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5108 return codegen_->GetAssembler();
5109}
5110
5111void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005112 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005113 Location source = move->GetSource();
5114 Location destination = move->GetDestination();
5115
5116 if (source.IsRegister()) {
5117 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005118 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005119 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005120 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005122 } else {
5123 DCHECK(destination.IsDoubleStackSlot());
5124 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005125 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005126 }
5127 } else if (source.IsStackSlot()) {
5128 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005130 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005131 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005132 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005133 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005134 } else {
5135 DCHECK(destination.IsStackSlot());
5136 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5137 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5138 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005139 } else if (source.IsDoubleStackSlot()) {
5140 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005141 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005142 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005143 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005144 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5145 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005146 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005147 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005148 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5149 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5150 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005151 } else if (source.IsConstant()) {
5152 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005153 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5154 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005155 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005156 if (value == 0) {
5157 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5158 } else {
5159 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5160 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005161 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005162 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005163 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005164 }
5165 } else if (constant->IsLongConstant()) {
5166 int64_t value = constant->AsLongConstant()->GetValue();
5167 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005168 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005169 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005170 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005171 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005172 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005173 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005174 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005175 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005176 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005177 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005178 } else {
5179 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005180 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005181 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5182 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005183 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005185 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005186 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005187 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005188 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005189 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005190 } else {
5191 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005192 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005193 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005194 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005195 } else if (source.IsFpuRegister()) {
5196 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005197 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005198 } else if (destination.IsStackSlot()) {
5199 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005200 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005201 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005202 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005204 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005206 }
5207}
5208
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005209void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005210 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005211 __ movl(Address(CpuRegister(RSP), mem), reg);
5212 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005213}
5214
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005215void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005216 ScratchRegisterScope ensure_scratch(
5217 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5218
5219 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5220 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5221 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5222 Address(CpuRegister(RSP), mem2 + stack_offset));
5223 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5224 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5225 CpuRegister(ensure_scratch.GetRegister()));
5226}
5227
Mark Mendell8a1c7282015-06-29 15:41:28 -04005228void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5229 __ movq(CpuRegister(TMP), reg1);
5230 __ movq(reg1, reg2);
5231 __ movq(reg2, CpuRegister(TMP));
5232}
5233
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005234void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5235 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5236 __ movq(Address(CpuRegister(RSP), mem), reg);
5237 __ movq(reg, CpuRegister(TMP));
5238}
5239
5240void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5241 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005242 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005243
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005244 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5245 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5246 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5247 Address(CpuRegister(RSP), mem2 + stack_offset));
5248 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5249 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5250 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005251}
5252
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005253void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5254 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5255 __ movss(Address(CpuRegister(RSP), mem), reg);
5256 __ movd(reg, CpuRegister(TMP));
5257}
5258
5259void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5260 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5261 __ movsd(Address(CpuRegister(RSP), mem), reg);
5262 __ movd(reg, CpuRegister(TMP));
5263}
5264
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005265void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005266 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005267 Location source = move->GetSource();
5268 Location destination = move->GetDestination();
5269
5270 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005271 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005272 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005273 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005274 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005275 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005276 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005277 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5278 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005279 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005280 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005281 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005282 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5283 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5286 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5287 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005289 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005290 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005291 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005296 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005297 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005298 }
5299}
5300
5301
5302void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5303 __ pushq(CpuRegister(reg));
5304}
5305
5306
5307void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5308 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005309}
5310
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005311void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005312 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005313 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5314 Immediate(mirror::Class::kStatusInitialized));
5315 __ j(kLess, slow_path->GetEntryLabel());
5316 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005317 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005318}
5319
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005320void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005321 InvokeRuntimeCallingConvention calling_convention;
5322 CodeGenerator::CreateLoadClassLocationSummary(
5323 cls,
5324 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005325 Location::RegisterLocation(RAX),
5326 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005327}
5328
5329void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005330 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005331 if (cls->NeedsAccessCheck()) {
5332 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5333 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5334 cls,
5335 cls->GetDexPc(),
5336 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005337 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005338 return;
5339 }
5340
Roland Levillain0d5a2812015-11-13 10:07:31 +00005341 Location out_loc = locations->Out();
5342 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005343 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005344
Calin Juravle580b6092015-10-06 17:35:58 +01005345 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005346 DCHECK(!cls->CanCallRuntime());
5347 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005348 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5349 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005350 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005351 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005352 // /* GcRoot<mirror::Class>[] */ out =
5353 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5354 __ movq(out, Address(current_method,
5355 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005356 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005357 GenerateGcRootFieldLoad(
5358 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01005359
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005360 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5361 DCHECK(cls->CanCallRuntime());
5362 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5363 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5364 codegen_->AddSlowPath(slow_path);
5365 if (!cls->IsInDexCache()) {
5366 __ testl(out, out);
5367 __ j(kEqual, slow_path->GetEntryLabel());
5368 }
5369 if (cls->MustGenerateClinitCheck()) {
5370 GenerateClassInitializationCheck(slow_path, out);
5371 } else {
5372 __ Bind(slow_path->GetExitLabel());
5373 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005374 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005375 }
5376}
5377
5378void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5379 LocationSummary* locations =
5380 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5381 locations->SetInAt(0, Location::RequiresRegister());
5382 if (check->HasUses()) {
5383 locations->SetOut(Location::SameAsFirstInput());
5384 }
5385}
5386
5387void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005388 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005389 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005390 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005391 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005392 GenerateClassInitializationCheck(slow_path,
5393 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005394}
5395
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005396HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5397 HLoadString::LoadKind desired_string_load_kind) {
5398 if (kEmitCompilerReadBarrier) {
5399 switch (desired_string_load_kind) {
5400 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5401 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5402 case HLoadString::LoadKind::kBootImageAddress:
5403 // TODO: Implement for read barrier.
5404 return HLoadString::LoadKind::kDexCacheViaMethod;
5405 default:
5406 break;
5407 }
5408 }
5409 switch (desired_string_load_kind) {
5410 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5411 DCHECK(!GetCompilerOptions().GetCompilePic());
5412 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5413 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5414 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5415 DCHECK(GetCompilerOptions().GetCompilePic());
5416 break;
5417 case HLoadString::LoadKind::kBootImageAddress:
5418 break;
5419 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005420 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005421 break;
5422 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005423 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005424 break;
5425 case HLoadString::LoadKind::kDexCacheViaMethod:
5426 break;
5427 }
5428 return desired_string_load_kind;
5429}
5430
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005431void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005432 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005433 ? LocationSummary::kCallOnSlowPath
5434 : LocationSummary::kNoCall;
5435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005436 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5437 locations->SetInAt(0, Location::RequiresRegister());
5438 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005439 locations->SetOut(Location::RequiresRegister());
5440}
5441
5442void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005443 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005444 Location out_loc = locations->Out();
5445 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005446
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005447 switch (load->GetLoadKind()) {
5448 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5449 DCHECK(!kEmitCompilerReadBarrier);
5450 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5451 codegen_->RecordStringPatch(load);
5452 return; // No dex cache slow path.
5453 }
5454 case HLoadString::LoadKind::kBootImageAddress: {
5455 DCHECK(!kEmitCompilerReadBarrier);
5456 DCHECK_NE(load->GetAddress(), 0u);
5457 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5458 __ movl(out, Immediate(address)); // Zero-extended.
5459 codegen_->RecordSimplePatch();
5460 return; // No dex cache slow path.
5461 }
5462 case HLoadString::LoadKind::kDexCacheAddress: {
5463 DCHECK_NE(load->GetAddress(), 0u);
5464 if (IsUint<32>(load->GetAddress())) {
5465 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5466 GenerateGcRootFieldLoad(load, out_loc, address);
5467 } else {
5468 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5469 __ movq(out, Immediate(load->GetAddress()));
5470 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5471 }
5472 break;
5473 }
5474 case HLoadString::LoadKind::kDexCachePcRelative: {
5475 uint32_t offset = load->GetDexCacheElementOffset();
5476 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5477 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5478 /* no_rip */ false);
5479 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5480 break;
5481 }
5482 case HLoadString::LoadKind::kDexCacheViaMethod: {
5483 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5484
5485 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5486 GenerateGcRootFieldLoad(
5487 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5488 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5489 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5490 // /* GcRoot<mirror::String> */ out = out[string_index]
5491 GenerateGcRootFieldLoad(
5492 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5493 break;
5494 }
5495 default:
5496 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5497 UNREACHABLE();
5498 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005500 if (!load->IsInDexCache()) {
5501 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5502 codegen_->AddSlowPath(slow_path);
5503 __ testl(out, out);
5504 __ j(kEqual, slow_path->GetEntryLabel());
5505 __ Bind(slow_path->GetExitLabel());
5506 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005507}
5508
David Brazdilcb1c0552015-08-04 16:22:25 +01005509static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005510 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5511 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005512}
5513
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005514void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5515 LocationSummary* locations =
5516 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5517 locations->SetOut(Location::RequiresRegister());
5518}
5519
5520void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005521 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5522}
5523
5524void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5525 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5526}
5527
5528void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5529 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005530}
5531
5532void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5533 LocationSummary* locations =
5534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5535 InvokeRuntimeCallingConvention calling_convention;
5536 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5537}
5538
5539void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005540 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5541 instruction,
5542 instruction->GetDexPc(),
5543 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005544 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005545}
5546
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005547static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5548 return kEmitCompilerReadBarrier &&
5549 (kUseBakerReadBarrier ||
5550 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5551 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5552 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5553}
5554
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005555void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005556 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005557 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5558 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005559 case TypeCheckKind::kExactCheck:
5560 case TypeCheckKind::kAbstractClassCheck:
5561 case TypeCheckKind::kClassHierarchyCheck:
5562 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005563 call_kind =
5564 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565 break;
5566 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567 case TypeCheckKind::kUnresolvedCheck:
5568 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 call_kind = LocationSummary::kCallOnSlowPath;
5570 break;
5571 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005572
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005574 locations->SetInAt(0, Location::RequiresRegister());
5575 locations->SetInAt(1, Location::Any());
5576 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5577 locations->SetOut(Location::RequiresRegister());
5578 // When read barriers are enabled, we need a temporary register for
5579 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005580 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005581 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005582 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005583}
5584
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005585void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005586 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005587 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005588 Location obj_loc = locations->InAt(0);
5589 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005590 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005591 Location out_loc = locations->Out();
5592 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005593 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005594 locations->GetTemp(0) :
5595 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005596 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005597 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5598 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5599 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005600 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005602
5603 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005604 // Avoid null check if we know obj is not null.
5605 if (instruction->MustDoNullCheck()) {
5606 __ testl(obj, obj);
5607 __ j(kEqual, &zero);
5608 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005609
Roland Levillain0d5a2812015-11-13 10:07:31 +00005610 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005611 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005613 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005614 case TypeCheckKind::kExactCheck: {
5615 if (cls.IsRegister()) {
5616 __ cmpl(out, cls.AsRegister<CpuRegister>());
5617 } else {
5618 DCHECK(cls.IsStackSlot()) << cls;
5619 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5620 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005621 if (zero.IsLinked()) {
5622 // Classes must be equal for the instanceof to succeed.
5623 __ j(kNotEqual, &zero);
5624 __ movl(out, Immediate(1));
5625 __ jmp(&done);
5626 } else {
5627 __ setcc(kEqual, out);
5628 // setcc only sets the low byte.
5629 __ andl(out, Immediate(1));
5630 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005631 break;
5632 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005633
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005634 case TypeCheckKind::kAbstractClassCheck: {
5635 // If the class is abstract, we eagerly fetch the super class of the
5636 // object to avoid doing a comparison we know will fail.
5637 NearLabel loop, success;
5638 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005639 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005640 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 __ testl(out, out);
5642 // If `out` is null, we use it for the result, and jump to `done`.
5643 __ j(kEqual, &done);
5644 if (cls.IsRegister()) {
5645 __ cmpl(out, cls.AsRegister<CpuRegister>());
5646 } else {
5647 DCHECK(cls.IsStackSlot()) << cls;
5648 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5649 }
5650 __ j(kNotEqual, &loop);
5651 __ movl(out, Immediate(1));
5652 if (zero.IsLinked()) {
5653 __ jmp(&done);
5654 }
5655 break;
5656 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005657
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005658 case TypeCheckKind::kClassHierarchyCheck: {
5659 // Walk over the class hierarchy to find a match.
5660 NearLabel loop, success;
5661 __ Bind(&loop);
5662 if (cls.IsRegister()) {
5663 __ cmpl(out, cls.AsRegister<CpuRegister>());
5664 } else {
5665 DCHECK(cls.IsStackSlot()) << cls;
5666 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5667 }
5668 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005669 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005670 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 __ testl(out, out);
5672 __ j(kNotEqual, &loop);
5673 // If `out` is null, we use it for the result, and jump to `done`.
5674 __ jmp(&done);
5675 __ Bind(&success);
5676 __ movl(out, Immediate(1));
5677 if (zero.IsLinked()) {
5678 __ jmp(&done);
5679 }
5680 break;
5681 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005682
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005684 // Do an exact check.
5685 NearLabel exact_check;
5686 if (cls.IsRegister()) {
5687 __ cmpl(out, cls.AsRegister<CpuRegister>());
5688 } else {
5689 DCHECK(cls.IsStackSlot()) << cls;
5690 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5691 }
5692 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005694 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005695 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 __ testl(out, out);
5697 // If `out` is null, we use it for the result, and jump to `done`.
5698 __ j(kEqual, &done);
5699 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5700 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005701 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702 __ movl(out, Immediate(1));
5703 __ jmp(&done);
5704 break;
5705 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005707 case TypeCheckKind::kArrayCheck: {
5708 if (cls.IsRegister()) {
5709 __ cmpl(out, cls.AsRegister<CpuRegister>());
5710 } else {
5711 DCHECK(cls.IsStackSlot()) << cls;
5712 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5713 }
5714 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005715 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5716 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717 codegen_->AddSlowPath(slow_path);
5718 __ j(kNotEqual, slow_path->GetEntryLabel());
5719 __ movl(out, Immediate(1));
5720 if (zero.IsLinked()) {
5721 __ jmp(&done);
5722 }
5723 break;
5724 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005725
Calin Juravle98893e12015-10-02 21:05:03 +01005726 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005727 case TypeCheckKind::kInterfaceCheck: {
5728 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005729 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005730 // cases.
5731 //
5732 // We cannot directly call the InstanceofNonTrivial runtime
5733 // entry point without resorting to a type checking slow path
5734 // here (i.e. by calling InvokeRuntime directly), as it would
5735 // require to assign fixed registers for the inputs of this
5736 // HInstanceOf instruction (following the runtime calling
5737 // convention), which might be cluttered by the potential first
5738 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005739 //
5740 // TODO: Introduce a new runtime entry point taking the object
5741 // to test (instead of its class) as argument, and let it deal
5742 // with the read barrier issues. This will let us refactor this
5743 // case of the `switch` code as it was previously (with a direct
5744 // call to the runtime not using a type checking slow path).
5745 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 DCHECK(locations->OnlyCallsOnSlowPath());
5747 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5748 /* is_fatal */ false);
5749 codegen_->AddSlowPath(slow_path);
5750 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 if (zero.IsLinked()) {
5752 __ jmp(&done);
5753 }
5754 break;
5755 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005756 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005758 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005759 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005760 __ xorl(out, out);
5761 }
5762
5763 if (done.IsLinked()) {
5764 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005765 }
5766
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005767 if (slow_path != nullptr) {
5768 __ Bind(slow_path->GetExitLabel());
5769 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005770}
5771
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005772void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5774 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005775 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5776 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 case TypeCheckKind::kExactCheck:
5778 case TypeCheckKind::kAbstractClassCheck:
5779 case TypeCheckKind::kClassHierarchyCheck:
5780 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5782 LocationSummary::kCallOnSlowPath :
5783 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005784 break;
5785 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 case TypeCheckKind::kUnresolvedCheck:
5787 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788 call_kind = LocationSummary::kCallOnSlowPath;
5789 break;
5790 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005791 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5792 locations->SetInAt(0, Location::RequiresRegister());
5793 locations->SetInAt(1, Location::Any());
5794 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5795 locations->AddTemp(Location::RequiresRegister());
5796 // When read barriers are enabled, we need an additional temporary
5797 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005798 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005799 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005800 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005801}
5802
5803void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005804 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005805 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 Location obj_loc = locations->InAt(0);
5807 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005808 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809 Location temp_loc = locations->GetTemp(0);
5810 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005811 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005812 locations->GetTemp(1) :
5813 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5815 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5816 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5817 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 bool is_type_check_slow_path_fatal =
5820 (type_check_kind == TypeCheckKind::kExactCheck ||
5821 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5822 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5823 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5824 !instruction->CanThrowIntoCatchBlock();
5825 SlowPathCode* type_check_slow_path =
5826 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5827 is_type_check_slow_path_fatal);
5828 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829
Roland Levillain0d5a2812015-11-13 10:07:31 +00005830 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 case TypeCheckKind::kExactCheck:
5832 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005833 NearLabel done;
5834 // Avoid null check if we know obj is not null.
5835 if (instruction->MustDoNullCheck()) {
5836 __ testl(obj, obj);
5837 __ j(kEqual, &done);
5838 }
5839
5840 // /* HeapReference<Class> */ temp = obj->klass_
5841 GenerateReferenceLoadTwoRegisters(
5842 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 if (cls.IsRegister()) {
5845 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5846 } else {
5847 DCHECK(cls.IsStackSlot()) << cls;
5848 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5849 }
5850 // Jump to slow path for throwing the exception or doing a
5851 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005852 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005853 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005854 break;
5855 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005857 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005858 NearLabel done;
5859 // Avoid null check if we know obj is not null.
5860 if (instruction->MustDoNullCheck()) {
5861 __ testl(obj, obj);
5862 __ j(kEqual, &done);
5863 }
5864
5865 // /* HeapReference<Class> */ temp = obj->klass_
5866 GenerateReferenceLoadTwoRegisters(
5867 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5868
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869 // If the class is abstract, we eagerly fetch the super class of the
5870 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005871 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005873 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005874 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005875
5876 // If the class reference currently in `temp` is not null, jump
5877 // to the `compare_classes` label to compare it with the checked
5878 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 __ j(kNotEqual, &compare_classes);
5881 // Otherwise, jump to the slow path to throw the exception.
5882 //
5883 // But before, move back the object's class into `temp` before
5884 // going into the slow path, as it has been overwritten in the
5885 // meantime.
5886 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005887 GenerateReferenceLoadTwoRegisters(
5888 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005889 __ jmp(type_check_slow_path->GetEntryLabel());
5890
5891 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005892 if (cls.IsRegister()) {
5893 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5894 } else {
5895 DCHECK(cls.IsStackSlot()) << cls;
5896 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5897 }
5898 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005899 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 break;
5901 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005903 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005904 NearLabel done;
5905 // Avoid null check if we know obj is not null.
5906 if (instruction->MustDoNullCheck()) {
5907 __ testl(obj, obj);
5908 __ j(kEqual, &done);
5909 }
5910
5911 // /* HeapReference<Class> */ temp = obj->klass_
5912 GenerateReferenceLoadTwoRegisters(
5913 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5914
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005916 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 __ Bind(&loop);
5918 if (cls.IsRegister()) {
5919 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5920 } else {
5921 DCHECK(cls.IsStackSlot()) << cls;
5922 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5923 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005924 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925
Roland Levillain0d5a2812015-11-13 10:07:31 +00005926 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005927 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928
5929 // If the class reference currently in `temp` is not null, jump
5930 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 __ testl(temp, temp);
5932 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933 // Otherwise, jump to the slow path to throw the exception.
5934 //
5935 // But before, move back the object's class into `temp` before
5936 // going into the slow path, as it has been overwritten in the
5937 // meantime.
5938 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005939 GenerateReferenceLoadTwoRegisters(
5940 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005941 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005942 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 break;
5944 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005945
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005947 // We cannot use a NearLabel here, as its range might be too
5948 // short in some cases when read barriers are enabled. This has
5949 // been observed for instance when the code emitted for this
5950 // case uses high x86-64 registers (R8-R15).
5951 Label done;
5952 // Avoid null check if we know obj is not null.
5953 if (instruction->MustDoNullCheck()) {
5954 __ testl(obj, obj);
5955 __ j(kEqual, &done);
5956 }
5957
5958 // /* HeapReference<Class> */ temp = obj->klass_
5959 GenerateReferenceLoadTwoRegisters(
5960 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5961
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005962 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005963 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005964 if (cls.IsRegister()) {
5965 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5966 } else {
5967 DCHECK(cls.IsStackSlot()) << cls;
5968 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5969 }
5970 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005971
5972 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005973 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005974 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975
5976 // If the component type is not null (i.e. the object is indeed
5977 // an array), jump to label `check_non_primitive_component_type`
5978 // to further check that this component type is not a primitive
5979 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981 __ j(kNotEqual, &check_non_primitive_component_type);
5982 // Otherwise, jump to the slow path to throw the exception.
5983 //
5984 // But before, move back the object's class into `temp` before
5985 // going into the slow path, as it has been overwritten in the
5986 // meantime.
5987 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005988 GenerateReferenceLoadTwoRegisters(
5989 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 __ jmp(type_check_slow_path->GetEntryLabel());
5991
5992 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 __ j(kEqual, &done);
5995 // Same comment as above regarding `temp` and the slow path.
5996 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005997 GenerateReferenceLoadTwoRegisters(
5998 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005999 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006000 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006001 break;
6002 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006003
Calin Juravle98893e12015-10-02 21:05:03 +01006004 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006006 NearLabel done;
6007 // Avoid null check if we know obj is not null.
6008 if (instruction->MustDoNullCheck()) {
6009 __ testl(obj, obj);
6010 __ j(kEqual, &done);
6011 }
6012
6013 // /* HeapReference<Class> */ temp = obj->klass_
6014 GenerateReferenceLoadTwoRegisters(
6015 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6016
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006017 // We always go into the type check slow path for the unresolved
6018 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006019 //
6020 // We cannot directly call the CheckCast runtime entry point
6021 // without resorting to a type checking slow path here (i.e. by
6022 // calling InvokeRuntime directly), as it would require to
6023 // assign fixed registers for the inputs of this HInstanceOf
6024 // instruction (following the runtime calling convention), which
6025 // might be cluttered by the potential first read barrier
6026 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006027 //
6028 // TODO: Introduce a new runtime entry point taking the object
6029 // to test (instead of its class) as argument, and let it deal
6030 // with the read barrier issues. This will let us refactor this
6031 // case of the `switch` code as it was previously (with a direct
6032 // call to the runtime not using a type checking slow path).
6033 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006034 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006035 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 break;
6037 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006038
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006040}
6041
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006042void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6043 LocationSummary* locations =
6044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6045 InvokeRuntimeCallingConvention calling_convention;
6046 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6047}
6048
6049void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006050 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6051 : QUICK_ENTRY_POINT(pUnlockObject),
6052 instruction,
6053 instruction->GetDexPc(),
6054 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006055 if (instruction->IsEnter()) {
6056 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6057 } else {
6058 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6059 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006060}
6061
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006062void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6063void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6064void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6065
6066void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6067 LocationSummary* locations =
6068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6069 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6070 || instruction->GetResultType() == Primitive::kPrimLong);
6071 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006072 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006073 locations->SetOut(Location::SameAsFirstInput());
6074}
6075
6076void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6077 HandleBitwiseOperation(instruction);
6078}
6079
6080void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6081 HandleBitwiseOperation(instruction);
6082}
6083
6084void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6085 HandleBitwiseOperation(instruction);
6086}
6087
6088void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6089 LocationSummary* locations = instruction->GetLocations();
6090 Location first = locations->InAt(0);
6091 Location second = locations->InAt(1);
6092 DCHECK(first.Equals(locations->Out()));
6093
6094 if (instruction->GetResultType() == Primitive::kPrimInt) {
6095 if (second.IsRegister()) {
6096 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006097 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006098 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006099 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006100 } else {
6101 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006102 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006103 }
6104 } else if (second.IsConstant()) {
6105 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6106 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006107 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006108 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006109 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006110 } else {
6111 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006112 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006113 }
6114 } else {
6115 Address address(CpuRegister(RSP), second.GetStackIndex());
6116 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006117 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006118 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006119 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006120 } else {
6121 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006122 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006123 }
6124 }
6125 } else {
6126 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006127 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6128 bool second_is_constant = false;
6129 int64_t value = 0;
6130 if (second.IsConstant()) {
6131 second_is_constant = true;
6132 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006133 }
Mark Mendell40741f32015-04-20 22:10:34 -04006134 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006135
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006136 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006137 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006138 if (is_int32_value) {
6139 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6140 } else {
6141 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6142 }
6143 } else if (second.IsDoubleStackSlot()) {
6144 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006145 } else {
6146 __ andq(first_reg, second.AsRegister<CpuRegister>());
6147 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006148 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006149 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006150 if (is_int32_value) {
6151 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6152 } else {
6153 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6154 }
6155 } else if (second.IsDoubleStackSlot()) {
6156 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006157 } else {
6158 __ orq(first_reg, second.AsRegister<CpuRegister>());
6159 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006160 } else {
6161 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006162 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006163 if (is_int32_value) {
6164 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6165 } else {
6166 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6167 }
6168 } else if (second.IsDoubleStackSlot()) {
6169 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006170 } else {
6171 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6172 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006173 }
6174 }
6175}
6176
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006177void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6178 Location out,
6179 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006180 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006181 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6182 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006183 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006184 if (kUseBakerReadBarrier) {
6185 // Load with fast path based Baker's read barrier.
6186 // /* HeapReference<Object> */ out = *(out + offset)
6187 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006188 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006189 } else {
6190 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006191 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006192 // in the following move operation, as we will need it for the
6193 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006194 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006195 // /* HeapReference<Object> */ out = *(out + offset)
6196 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006197 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006198 }
6199 } else {
6200 // Plain load with no read barrier.
6201 // /* HeapReference<Object> */ out = *(out + offset)
6202 __ movl(out_reg, Address(out_reg, offset));
6203 __ MaybeUnpoisonHeapReference(out_reg);
6204 }
6205}
6206
6207void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6208 Location out,
6209 Location obj,
6210 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006211 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006212 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6213 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6214 if (kEmitCompilerReadBarrier) {
6215 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006216 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006217 // Load with fast path based Baker's read barrier.
6218 // /* HeapReference<Object> */ out = *(obj + offset)
6219 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006220 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006221 } else {
6222 // Load with slow path based read barrier.
6223 // /* HeapReference<Object> */ out = *(obj + offset)
6224 __ movl(out_reg, Address(obj_reg, offset));
6225 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6226 }
6227 } else {
6228 // Plain load with no read barrier.
6229 // /* HeapReference<Object> */ out = *(obj + offset)
6230 __ movl(out_reg, Address(obj_reg, offset));
6231 __ MaybeUnpoisonHeapReference(out_reg);
6232 }
6233}
6234
6235void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6236 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 const Address& address,
6238 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006239 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6240 if (kEmitCompilerReadBarrier) {
6241 if (kUseBakerReadBarrier) {
6242 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6243 // Baker's read barrier are used:
6244 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006245 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006246 // if (Thread::Current()->GetIsGcMarking()) {
6247 // root = ReadBarrier::Mark(root)
6248 // }
6249
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006250 // /* GcRoot<mirror::Object> */ root = *address
6251 __ movl(root_reg, address);
6252 if (fixup_label != nullptr) {
6253 __ Bind(fixup_label);
6254 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006255 static_assert(
6256 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6257 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6258 "have different sizes.");
6259 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6260 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6261 "have different sizes.");
6262
6263 // Slow path used to mark the GC root `root`.
6264 SlowPathCode* slow_path =
6265 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6266 codegen_->AddSlowPath(slow_path);
6267
6268 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6269 /* no_rip */ true),
6270 Immediate(0));
6271 __ j(kNotEqual, slow_path->GetEntryLabel());
6272 __ Bind(slow_path->GetExitLabel());
6273 } else {
6274 // GC root loaded through a slow path for read barriers other
6275 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006276 // /* GcRoot<mirror::Object>* */ root = address
6277 __ leaq(root_reg, address);
6278 if (fixup_label != nullptr) {
6279 __ Bind(fixup_label);
6280 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006281 // /* mirror::Object* */ root = root->Read()
6282 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6283 }
6284 } else {
6285 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 // /* GcRoot<mirror::Object> */ root = *address
6287 __ movl(root_reg, address);
6288 if (fixup_label != nullptr) {
6289 __ Bind(fixup_label);
6290 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006291 // Note that GC roots are not affected by heap poisoning, thus we
6292 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006293 }
6294}
6295
6296void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6297 Location ref,
6298 CpuRegister obj,
6299 uint32_t offset,
6300 Location temp,
6301 bool needs_null_check) {
6302 DCHECK(kEmitCompilerReadBarrier);
6303 DCHECK(kUseBakerReadBarrier);
6304
6305 // /* HeapReference<Object> */ ref = *(obj + offset)
6306 Address src(obj, offset);
6307 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6308}
6309
6310void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6311 Location ref,
6312 CpuRegister obj,
6313 uint32_t data_offset,
6314 Location index,
6315 Location temp,
6316 bool needs_null_check) {
6317 DCHECK(kEmitCompilerReadBarrier);
6318 DCHECK(kUseBakerReadBarrier);
6319
6320 // /* HeapReference<Object> */ ref =
6321 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6322 Address src = index.IsConstant() ?
6323 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6324 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6325 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6326}
6327
6328void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6329 Location ref,
6330 CpuRegister obj,
6331 const Address& src,
6332 Location temp,
6333 bool needs_null_check) {
6334 DCHECK(kEmitCompilerReadBarrier);
6335 DCHECK(kUseBakerReadBarrier);
6336
6337 // In slow path based read barriers, the read barrier call is
6338 // inserted after the original load. However, in fast path based
6339 // Baker's read barriers, we need to perform the load of
6340 // mirror::Object::monitor_ *before* the original reference load.
6341 // This load-load ordering is required by the read barrier.
6342 // The fast path/slow path (for Baker's algorithm) should look like:
6343 //
6344 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6345 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6346 // HeapReference<Object> ref = *src; // Original reference load.
6347 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6348 // if (is_gray) {
6349 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6350 // }
6351 //
6352 // Note: the original implementation in ReadBarrier::Barrier is
6353 // slightly more complex as:
6354 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006355 // the high-bits of rb_state, which are expected to be all zeroes
6356 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6357 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006358 // - it performs additional checks that we do not do here for
6359 // performance reasons.
6360
6361 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6362 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6363 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6364
6365 // /* int32_t */ monitor = obj->monitor_
6366 __ movl(temp_reg, Address(obj, monitor_offset));
6367 if (needs_null_check) {
6368 MaybeRecordImplicitNullCheck(instruction);
6369 }
6370 // /* LockWord */ lock_word = LockWord(monitor)
6371 static_assert(sizeof(LockWord) == sizeof(int32_t),
6372 "art::LockWord and int32_t have different sizes.");
6373 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6374 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6375 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6376 static_assert(
6377 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6378 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6379
6380 // Load fence to prevent load-load reordering.
6381 // Note that this is a no-op, thanks to the x86-64 memory model.
6382 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6383
6384 // The actual reference load.
6385 // /* HeapReference<Object> */ ref = *src
6386 __ movl(ref_reg, src);
6387
6388 // Object* ref = ref_addr->AsMirrorPtr()
6389 __ MaybeUnpoisonHeapReference(ref_reg);
6390
6391 // Slow path used to mark the object `ref` when it is gray.
6392 SlowPathCode* slow_path =
6393 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6394 AddSlowPath(slow_path);
6395
6396 // if (rb_state == ReadBarrier::gray_ptr_)
6397 // ref = ReadBarrier::Mark(ref);
6398 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6399 __ j(kEqual, slow_path->GetEntryLabel());
6400 __ Bind(slow_path->GetExitLabel());
6401}
6402
6403void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6404 Location out,
6405 Location ref,
6406 Location obj,
6407 uint32_t offset,
6408 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 DCHECK(kEmitCompilerReadBarrier);
6410
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006411 // Insert a slow path based read barrier *after* the reference load.
6412 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 // If heap poisoning is enabled, the unpoisoning of the loaded
6414 // reference will be carried out by the runtime within the slow
6415 // path.
6416 //
6417 // Note that `ref` currently does not get unpoisoned (when heap
6418 // poisoning is enabled), which is alright as the `ref` argument is
6419 // not used by the artReadBarrierSlow entry point.
6420 //
6421 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6422 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6423 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6424 AddSlowPath(slow_path);
6425
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 __ jmp(slow_path->GetEntryLabel());
6427 __ Bind(slow_path->GetExitLabel());
6428}
6429
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006430void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6431 Location out,
6432 Location ref,
6433 Location obj,
6434 uint32_t offset,
6435 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006437 // Baker's read barriers shall be handled by the fast path
6438 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6439 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006440 // If heap poisoning is enabled, unpoisoning will be taken care of
6441 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006442 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006443 } else if (kPoisonHeapReferences) {
6444 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6445 }
6446}
6447
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006448void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6449 Location out,
6450 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 DCHECK(kEmitCompilerReadBarrier);
6452
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006453 // Insert a slow path based read barrier *after* the GC root load.
6454 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 // Note that GC roots are not affected by heap poisoning, so we do
6456 // not need to do anything special for this here.
6457 SlowPathCode* slow_path =
6458 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6459 AddSlowPath(slow_path);
6460
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461 __ jmp(slow_path->GetEntryLabel());
6462 __ Bind(slow_path->GetExitLabel());
6463}
6464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006465void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006466 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006467 LOG(FATAL) << "Unreachable";
6468}
6469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006470void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006471 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006472 LOG(FATAL) << "Unreachable";
6473}
6474
Mark Mendellfe57faa2015-09-18 09:26:15 -04006475// Simple implementation of packed switch - generate cascaded compare/jumps.
6476void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6477 LocationSummary* locations =
6478 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6479 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006480 locations->AddTemp(Location::RequiresRegister());
6481 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006482}
6483
6484void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6485 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006486 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006487 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006488 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6489 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6490 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006491 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6492
6493 // Should we generate smaller inline compare/jumps?
6494 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6495 // Figure out the correct compare values and jump conditions.
6496 // Handle the first compare/branch as a special case because it might
6497 // jump to the default case.
6498 DCHECK_GT(num_entries, 2u);
6499 Condition first_condition;
6500 uint32_t index;
6501 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6502 if (lower_bound != 0) {
6503 first_condition = kLess;
6504 __ cmpl(value_reg_in, Immediate(lower_bound));
6505 __ j(first_condition, codegen_->GetLabelOf(default_block));
6506 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6507
6508 index = 1;
6509 } else {
6510 // Handle all the compare/jumps below.
6511 first_condition = kBelow;
6512 index = 0;
6513 }
6514
6515 // Handle the rest of the compare/jumps.
6516 for (; index + 1 < num_entries; index += 2) {
6517 int32_t compare_to_value = lower_bound + index + 1;
6518 __ cmpl(value_reg_in, Immediate(compare_to_value));
6519 // Jump to successors[index] if value < case_value[index].
6520 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6521 // Jump to successors[index + 1] if value == case_value[index + 1].
6522 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6523 }
6524
6525 if (index != num_entries) {
6526 // There are an odd number of entries. Handle the last one.
6527 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006528 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006529 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6530 }
6531
6532 // And the default for any other value.
6533 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6534 __ jmp(codegen_->GetLabelOf(default_block));
6535 }
6536 return;
6537 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006538
6539 // Remove the bias, if needed.
6540 Register value_reg_out = value_reg_in.AsRegister();
6541 if (lower_bound != 0) {
6542 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6543 value_reg_out = temp_reg.AsRegister();
6544 }
6545 CpuRegister value_reg(value_reg_out);
6546
6547 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006548 __ cmpl(value_reg, Immediate(num_entries - 1));
6549 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006550
Mark Mendell9c86b482015-09-18 13:36:07 -04006551 // We are in the range of the table.
6552 // Load the address of the jump table in the constant area.
6553 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006554
Mark Mendell9c86b482015-09-18 13:36:07 -04006555 // Load the (signed) offset from the jump table.
6556 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6557
6558 // Add the offset to the address of the table base.
6559 __ addq(temp_reg, base_reg);
6560
6561 // And jump.
6562 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006563}
6564
Aart Bikc5d47542016-01-27 17:00:35 -08006565void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6566 if (value == 0) {
6567 __ xorl(dest, dest);
6568 } else {
6569 __ movl(dest, Immediate(value));
6570 }
6571}
6572
Mark Mendell92e83bf2015-05-07 11:25:03 -04006573void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6574 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006575 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006576 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006577 } else if (IsUint<32>(value)) {
6578 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006579 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6580 } else {
6581 __ movq(dest, Immediate(value));
6582 }
6583}
6584
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006585void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6586 if (value == 0) {
6587 __ xorps(dest, dest);
6588 } else {
6589 __ movss(dest, LiteralInt32Address(value));
6590 }
6591}
6592
6593void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6594 if (value == 0) {
6595 __ xorpd(dest, dest);
6596 } else {
6597 __ movsd(dest, LiteralInt64Address(value));
6598 }
6599}
6600
6601void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6602 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6603}
6604
6605void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6606 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6607}
6608
Aart Bika19616e2016-02-01 18:57:58 -08006609void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6610 if (value == 0) {
6611 __ testl(dest, dest);
6612 } else {
6613 __ cmpl(dest, Immediate(value));
6614 }
6615}
6616
6617void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6618 if (IsInt<32>(value)) {
6619 if (value == 0) {
6620 __ testq(dest, dest);
6621 } else {
6622 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6623 }
6624 } else {
6625 // Value won't fit in an int.
6626 __ cmpq(dest, LiteralInt64Address(value));
6627 }
6628}
6629
Mark Mendellcfa410b2015-05-25 16:02:44 -04006630void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6631 DCHECK(dest.IsDoubleStackSlot());
6632 if (IsInt<32>(value)) {
6633 // Can move directly as an int32 constant.
6634 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6635 Immediate(static_cast<int32_t>(value)));
6636 } else {
6637 Load64BitValue(CpuRegister(TMP), value);
6638 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6639 }
6640}
6641
Mark Mendell9c86b482015-09-18 13:36:07 -04006642/**
6643 * Class to handle late fixup of offsets into constant area.
6644 */
6645class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6646 public:
6647 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6648 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6649
6650 protected:
6651 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6652
6653 CodeGeneratorX86_64* codegen_;
6654
6655 private:
6656 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6657 // Patch the correct offset for the instruction. We use the address of the
6658 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6659 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6660 int32_t relative_position = constant_offset - pos;
6661
6662 // Patch in the right value.
6663 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6664 }
6665
6666 // Location in constant area that the fixup refers to.
6667 size_t offset_into_constant_area_;
6668};
6669
6670/**
6671 t * Class to handle late fixup of offsets to a jump table that will be created in the
6672 * constant area.
6673 */
6674class JumpTableRIPFixup : public RIPFixup {
6675 public:
6676 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6677 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6678
6679 void CreateJumpTable() {
6680 X86_64Assembler* assembler = codegen_->GetAssembler();
6681
6682 // Ensure that the reference to the jump table has the correct offset.
6683 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6684 SetOffset(offset_in_constant_table);
6685
6686 // Compute the offset from the start of the function to this jump table.
6687 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6688
6689 // Populate the jump table with the correct values for the jump table.
6690 int32_t num_entries = switch_instr_->GetNumEntries();
6691 HBasicBlock* block = switch_instr_->GetBlock();
6692 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6693 // The value that we want is the target offset - the position of the table.
6694 for (int32_t i = 0; i < num_entries; i++) {
6695 HBasicBlock* b = successors[i];
6696 Label* l = codegen_->GetLabelOf(b);
6697 DCHECK(l->IsBound());
6698 int32_t offset_to_block = l->Position() - current_table_offset;
6699 assembler->AppendInt32(offset_to_block);
6700 }
6701 }
6702
6703 private:
6704 const HPackedSwitch* switch_instr_;
6705};
6706
Mark Mendellf55c3e02015-03-26 21:07:46 -04006707void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6708 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006709 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006710 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6711 // 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 -04006712 assembler->Align(4, 0);
6713 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006714
6715 // Populate any jump tables.
6716 for (auto jump_table : fixups_to_jump_tables_) {
6717 jump_table->CreateJumpTable();
6718 }
6719
6720 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006721 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006722 }
6723
6724 // And finish up.
6725 CodeGenerator::Finalize(allocator);
6726}
6727
Mark Mendellf55c3e02015-03-26 21:07:46 -04006728Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6729 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6730 return Address::RIP(fixup);
6731}
6732
6733Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6734 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6735 return Address::RIP(fixup);
6736}
6737
6738Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6739 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6740 return Address::RIP(fixup);
6741}
6742
6743Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6744 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6745 return Address::RIP(fixup);
6746}
6747
Andreas Gampe85b62f22015-09-09 13:15:38 -07006748// TODO: trg as memory.
6749void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6750 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006751 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006752 return;
6753 }
6754
6755 DCHECK_NE(type, Primitive::kPrimVoid);
6756
6757 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6758 if (trg.Equals(return_loc)) {
6759 return;
6760 }
6761
6762 // Let the parallel move resolver take care of all of this.
6763 HParallelMove parallel_move(GetGraph()->GetArena());
6764 parallel_move.AddMove(return_loc, trg, type, nullptr);
6765 GetMoveResolver()->EmitNativeCode(&parallel_move);
6766}
6767
Mark Mendell9c86b482015-09-18 13:36:07 -04006768Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6769 // Create a fixup to be used to create and address the jump table.
6770 JumpTableRIPFixup* table_fixup =
6771 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6772
6773 // We have to populate the jump tables.
6774 fixups_to_jump_tables_.push_back(table_fixup);
6775 return Address::RIP(table_fixup);
6776}
6777
Mark Mendellea5af682015-10-22 17:35:49 -04006778void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6779 const Address& addr_high,
6780 int64_t v,
6781 HInstruction* instruction) {
6782 if (IsInt<32>(v)) {
6783 int32_t v_32 = v;
6784 __ movq(addr_low, Immediate(v_32));
6785 MaybeRecordImplicitNullCheck(instruction);
6786 } else {
6787 // Didn't fit in a register. Do it in pieces.
6788 int32_t low_v = Low32Bits(v);
6789 int32_t high_v = High32Bits(v);
6790 __ movl(addr_low, Immediate(low_v));
6791 MaybeRecordImplicitNullCheck(instruction);
6792 __ movl(addr_high, Immediate(high_v));
6793 }
6794}
6795
Roland Levillain4d027112015-07-01 15:41:14 +01006796#undef __
6797
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006798} // namespace x86_64
6799} // namespace art