blob: 17532baee565ed677b92ebd24886150211989b3d [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() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100468 instruction_->IsCheckCast() ||
469 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
470 instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000471 << "Unexpected instruction in read barrier marking slow path: "
472 << instruction_->DebugName();
473
474 __ Bind(GetEntryLabel());
475 SaveLiveRegisters(codegen, locations);
476
477 InvokeRuntimeCallingConvention calling_convention;
478 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
479 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
480 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
481 instruction_,
482 instruction_->GetDexPc(),
483 this);
484 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
485 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
486
487 RestoreLiveRegisters(codegen, locations);
488 __ jmp(GetExitLabel());
489 }
490
491 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000492 const Location out_;
493 const Location obj_;
494
495 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
496};
497
Roland Levillain0d5a2812015-11-13 10:07:31 +0000498// Slow path generating a read barrier for a heap reference.
499class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
500 public:
501 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
502 Location out,
503 Location ref,
504 Location obj,
505 uint32_t offset,
506 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000507 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000508 out_(out),
509 ref_(ref),
510 obj_(obj),
511 offset_(offset),
512 index_(index) {
513 DCHECK(kEmitCompilerReadBarrier);
514 // If `obj` is equal to `out` or `ref`, it means the initial
515 // object has been overwritten by (or after) the heap object
516 // reference load to be instrumented, e.g.:
517 //
518 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000519 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000520 //
521 // In that case, we have lost the information about the original
522 // object, and the emitted read barrier cannot work properly.
523 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
524 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
525}
526
527 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
528 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
529 LocationSummary* locations = instruction_->GetLocations();
530 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
531 DCHECK(locations->CanCall());
532 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100533 DCHECK(instruction_->IsInstanceFieldGet() ||
534 instruction_->IsStaticFieldGet() ||
535 instruction_->IsArrayGet() ||
536 instruction_->IsInstanceOf() ||
537 instruction_->IsCheckCast() ||
538 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000539 instruction_->GetLocations()->Intrinsified()))
540 << "Unexpected instruction in read barrier for heap reference slow path: "
541 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000542
543 __ Bind(GetEntryLabel());
544 SaveLiveRegisters(codegen, locations);
545
546 // We may have to change the index's value, but as `index_` is a
547 // constant member (like other "inputs" of this slow path),
548 // introduce a copy of it, `index`.
549 Location index = index_;
550 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100551 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000552 if (instruction_->IsArrayGet()) {
553 // Compute real offset and store it in index_.
554 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
555 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
556 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
557 // We are about to change the value of `index_reg` (see the
558 // calls to art::x86_64::X86_64Assembler::shll and
559 // art::x86_64::X86_64Assembler::AddImmediate below), but it
560 // has not been saved by the previous call to
561 // art::SlowPathCode::SaveLiveRegisters, as it is a
562 // callee-save register --
563 // art::SlowPathCode::SaveLiveRegisters does not consider
564 // callee-save registers, as it has been designed with the
565 // assumption that callee-save registers are supposed to be
566 // handled by the called function. So, as a callee-save
567 // register, `index_reg` _would_ eventually be saved onto
568 // the stack, but it would be too late: we would have
569 // changed its value earlier. Therefore, we manually save
570 // it here into another freely available register,
571 // `free_reg`, chosen of course among the caller-save
572 // registers (as a callee-save `free_reg` register would
573 // exhibit the same problem).
574 //
575 // Note we could have requested a temporary register from
576 // the register allocator instead; but we prefer not to, as
577 // this is a slow path, and we know we can find a
578 // caller-save register that is available.
579 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
580 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
581 index_reg = free_reg;
582 index = Location::RegisterLocation(index_reg);
583 } else {
584 // The initial register stored in `index_` has already been
585 // saved in the call to art::SlowPathCode::SaveLiveRegisters
586 // (as it is not a callee-save register), so we can freely
587 // use it.
588 }
589 // Shifting the index value contained in `index_reg` by the
590 // scale factor (2) cannot overflow in practice, as the
591 // runtime is unable to allocate object arrays with a size
592 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
593 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
594 static_assert(
595 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
596 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
597 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
598 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100599 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
600 // intrinsics, `index_` is not shifted by a scale factor of 2
601 // (as in the case of ArrayGet), as it is actually an offset
602 // to an object field within an object.
603 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000604 DCHECK(instruction_->GetLocations()->Intrinsified());
605 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
606 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
607 << instruction_->AsInvoke()->GetIntrinsic();
608 DCHECK_EQ(offset_, 0U);
609 DCHECK(index_.IsRegister());
610 }
611 }
612
613 // We're moving two or three locations to locations that could
614 // overlap, so we need a parallel move resolver.
615 InvokeRuntimeCallingConvention calling_convention;
616 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
617 parallel_move.AddMove(ref_,
618 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
619 Primitive::kPrimNot,
620 nullptr);
621 parallel_move.AddMove(obj_,
622 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
623 Primitive::kPrimNot,
624 nullptr);
625 if (index.IsValid()) {
626 parallel_move.AddMove(index,
627 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
628 Primitive::kPrimInt,
629 nullptr);
630 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
631 } else {
632 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
633 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
634 }
635 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
636 instruction_,
637 instruction_->GetDexPc(),
638 this);
639 CheckEntrypointTypes<
640 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
641 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
642
643 RestoreLiveRegisters(codegen, locations);
644 __ jmp(GetExitLabel());
645 }
646
647 const char* GetDescription() const OVERRIDE {
648 return "ReadBarrierForHeapReferenceSlowPathX86_64";
649 }
650
651 private:
652 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
653 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
654 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
655 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
656 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
657 return static_cast<CpuRegister>(i);
658 }
659 }
660 // We shall never fail to find a free caller-save register, as
661 // there are more than two core caller-save registers on x86-64
662 // (meaning it is possible to find one which is different from
663 // `ref` and `obj`).
664 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
665 LOG(FATAL) << "Could not find a free caller-save register";
666 UNREACHABLE();
667 }
668
Roland Levillain0d5a2812015-11-13 10:07:31 +0000669 const Location out_;
670 const Location ref_;
671 const Location obj_;
672 const uint32_t offset_;
673 // An additional location containing an index to an array.
674 // Only used for HArrayGet and the UnsafeGetObject &
675 // UnsafeGetObjectVolatile intrinsics.
676 const Location index_;
677
678 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
679};
680
681// Slow path generating a read barrier for a GC root.
682class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
683 public:
684 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000685 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000686 DCHECK(kEmitCompilerReadBarrier);
687 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688
689 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
690 LocationSummary* locations = instruction_->GetLocations();
691 DCHECK(locations->CanCall());
692 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000693 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
694 << "Unexpected instruction in read barrier for GC root slow path: "
695 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000696
697 __ Bind(GetEntryLabel());
698 SaveLiveRegisters(codegen, locations);
699
700 InvokeRuntimeCallingConvention calling_convention;
701 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
702 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
703 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
704 instruction_,
705 instruction_->GetDexPc(),
706 this);
707 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
708 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
709
710 RestoreLiveRegisters(codegen, locations);
711 __ jmp(GetExitLabel());
712 }
713
714 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
715
716 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000717 const Location out_;
718 const Location root_;
719
720 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
721};
722
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100723#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700724// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
725#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100726
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700728 switch (cond) {
729 case kCondEQ: return kEqual;
730 case kCondNE: return kNotEqual;
731 case kCondLT: return kLess;
732 case kCondLE: return kLessEqual;
733 case kCondGT: return kGreater;
734 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700735 case kCondB: return kBelow;
736 case kCondBE: return kBelowEqual;
737 case kCondA: return kAbove;
738 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700739 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100740 LOG(FATAL) << "Unreachable";
741 UNREACHABLE();
742}
743
Aart Bike9f37602015-10-09 11:15:55 -0700744// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100745inline Condition X86_64FPCondition(IfCondition cond) {
746 switch (cond) {
747 case kCondEQ: return kEqual;
748 case kCondNE: return kNotEqual;
749 case kCondLT: return kBelow;
750 case kCondLE: return kBelowEqual;
751 case kCondGT: return kAbove;
752 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700753 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100754 };
755 LOG(FATAL) << "Unreachable";
756 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700757}
758
Vladimir Markodc151b22015-10-15 18:02:30 +0100759HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
760 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
761 MethodReference target_method ATTRIBUTE_UNUSED) {
762 switch (desired_dispatch_info.code_ptr_location) {
763 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
764 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
765 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
766 return HInvokeStaticOrDirect::DispatchInfo {
767 desired_dispatch_info.method_load_kind,
768 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
769 desired_dispatch_info.method_load_data,
770 0u
771 };
772 default:
773 return desired_dispatch_info;
774 }
775}
776
Serguei Katkov288c7a82016-05-16 11:53:15 +0600777Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
778 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800779 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000780 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
781 switch (invoke->GetMethodLoadKind()) {
782 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
783 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000784 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000785 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000786 break;
787 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000788 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000789 break;
790 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
791 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
792 break;
793 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
794 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
795 method_patches_.emplace_back(invoke->GetTargetMethod());
796 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
797 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000798 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000799 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000800 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000801 // Bind a new fixup label at the end of the "movl" insn.
802 uint32_t offset = invoke->GetDexCacheArrayOffset();
803 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000804 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000805 }
Vladimir Marko58155012015-08-19 12:49:41 +0000806 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000807 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000808 Register method_reg;
809 CpuRegister reg = temp.AsRegister<CpuRegister>();
810 if (current_method.IsRegister()) {
811 method_reg = current_method.AsRegister<Register>();
812 } else {
813 DCHECK(invoke->GetLocations()->Intrinsified());
814 DCHECK(!current_method.IsValid());
815 method_reg = reg.AsRegister();
816 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
817 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000818 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100819 __ movq(reg,
820 Address(CpuRegister(method_reg),
821 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100822 // temp = temp[index_in_cache];
823 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
824 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000825 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
826 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100827 }
Vladimir Marko58155012015-08-19 12:49:41 +0000828 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600829 return callee_method;
830}
831
832void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
833 Location temp) {
834 // All registers are assumed to be correctly set up.
835 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000836
837 switch (invoke->GetCodePtrLocation()) {
838 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
839 __ call(&frame_entry_label_);
840 break;
841 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
842 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
843 Label* label = &relative_call_patches_.back().label;
844 __ call(label); // Bind to the patch label, override at link time.
845 __ Bind(label); // Bind the label at the end of the "call" insn.
846 break;
847 }
848 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
849 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100850 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
851 LOG(FATAL) << "Unsupported";
852 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000853 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
854 // (callee_method + offset_of_quick_compiled_code)()
855 __ call(Address(callee_method.AsRegister<CpuRegister>(),
856 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
857 kX86_64WordSize).SizeValue()));
858 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000859 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800860
861 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800862}
863
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000864void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
865 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
866 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
867 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000868
869 // Use the calling convention instead of the location of the receiver, as
870 // intrinsics may have put the receiver in a different register. In the intrinsics
871 // slow path, the arguments have been moved to the right place, so here we are
872 // guaranteed that the receiver is the first register of the calling convention.
873 InvokeDexCallingConvention calling_convention;
874 Register receiver = calling_convention.GetRegisterAt(0);
875
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000876 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000877 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000878 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000879 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000880 // Instead of simply (possibly) unpoisoning `temp` here, we should
881 // emit a read barrier for the previous class reference load.
882 // However this is not required in practice, as this is an
883 // intermediate/temporary reference and because the current
884 // concurrent copying collector keeps the from-space memory
885 // intact/accessible until the end of the marking phase (the
886 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000887 __ MaybeUnpoisonHeapReference(temp);
888 // temp = temp->GetMethodAt(method_offset);
889 __ movq(temp, Address(temp, method_offset));
890 // call temp->GetEntryPoint();
891 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
892 kX86_64WordSize).SizeValue()));
893}
894
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000895void CodeGeneratorX86_64::RecordSimplePatch() {
896 if (GetCompilerOptions().GetIncludePatchInformation()) {
897 simple_patches_.emplace_back();
898 __ Bind(&simple_patches_.back());
899 }
900}
901
902void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
903 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
904 __ Bind(&string_patches_.back().label);
905}
906
907Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
908 uint32_t element_offset) {
909 // Add a patch entry and return the label.
910 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
911 return &pc_relative_dex_cache_patches_.back().label;
912}
913
Vladimir Marko58155012015-08-19 12:49:41 +0000914void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
915 DCHECK(linker_patches->empty());
916 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000917 method_patches_.size() +
918 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000919 pc_relative_dex_cache_patches_.size() +
920 simple_patches_.size() +
921 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000922 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000923 // The label points to the end of the "movl" insn but the literal offset for method
924 // patch needs to point to the embedded constant which occupies the last 4 bytes.
925 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000926 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000927 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000928 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
929 info.target_method.dex_file,
930 info.target_method.dex_method_index));
931 }
932 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000933 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000934 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
935 info.target_method.dex_file,
936 info.target_method.dex_method_index));
937 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000938 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
939 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000940 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
941 &info.target_dex_file,
942 info.label.Position(),
943 info.element_offset));
944 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000945 for (const Label& label : simple_patches_) {
946 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
947 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
948 }
949 for (const StringPatchInfo<Label>& info : string_patches_) {
950 // These are always PC-relative, see GetSupportedLoadStringKind().
951 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
952 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
953 &info.dex_file,
954 info.label.Position(),
955 info.string_index));
956 }
Vladimir Marko58155012015-08-19 12:49:41 +0000957}
958
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100960 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100961}
962
963void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100964 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100965}
966
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100967size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
968 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
969 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100970}
971
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100972size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
973 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
974 return kX86_64WordSize;
975}
976
977size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
978 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
979 return kX86_64WordSize;
980}
981
982size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
983 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
984 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100985}
986
Calin Juravle175dc732015-08-25 15:42:32 +0100987void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
988 HInstruction* instruction,
989 uint32_t dex_pc,
990 SlowPathCode* slow_path) {
991 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
992 instruction,
993 dex_pc,
994 slow_path);
995}
996
997void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100998 HInstruction* instruction,
999 uint32_t dex_pc,
1000 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001001 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001002 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +01001003 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +01001004}
1005
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001006static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001007// Use a fake return address register to mimic Quick.
1008static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001009CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001010 const X86_64InstructionSetFeatures& isa_features,
1011 const CompilerOptions& compiler_options,
1012 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001013 : CodeGenerator(graph,
1014 kNumberOfCpuRegisters,
1015 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001016 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001017 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1018 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001019 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001020 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1021 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001022 compiler_options,
1023 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001025 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001026 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001027 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001028 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001029 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001030 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001031 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1032 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001033 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001034 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1035 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001036 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001037 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1038}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001039
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001040InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1041 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001042 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001043 assembler_(codegen->GetAssembler()),
1044 codegen_(codegen) {}
1045
David Brazdil58282f42016-01-14 12:45:10 +00001046void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001047 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001048 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001049
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001050 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001051 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052}
1053
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001054static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001055 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056}
David Srbecky9d8606d2015-04-12 09:35:32 +01001057
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001059 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060}
1061
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001064 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001065 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001066 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001067 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001068
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001070 __ testq(CpuRegister(RAX), Address(
1071 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001072 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001073 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001074
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001075 if (HasEmptyFrame()) {
1076 return;
1077 }
1078
Nicolas Geoffray98893962015-01-21 12:32:32 +00001079 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001080 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001081 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001082 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1084 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001085 }
1086 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001087
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001088 int adjust = GetFrameSize() - GetCoreSpillSize();
1089 __ subq(CpuRegister(RSP), Immediate(adjust));
1090 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001091 uint32_t xmm_spill_location = GetFpuSpillStart();
1092 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001093
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001094 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1095 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001096 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1097 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1098 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001099 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001100 }
1101
Mathieu Chartiere401d142015-04-22 13:56:20 -07001102 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001103 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104}
1105
1106void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001107 __ cfi().RememberState();
1108 if (!HasEmptyFrame()) {
1109 uint32_t xmm_spill_location = GetFpuSpillStart();
1110 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1111 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1112 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1113 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1114 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1115 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1116 }
1117 }
1118
1119 int adjust = GetFrameSize() - GetCoreSpillSize();
1120 __ addq(CpuRegister(RSP), Immediate(adjust));
1121 __ cfi().AdjustCFAOffset(-adjust);
1122
1123 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1124 Register reg = kCoreCalleeSaves[i];
1125 if (allocated_registers_.ContainsCoreRegister(reg)) {
1126 __ popq(CpuRegister(reg));
1127 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1128 __ cfi().Restore(DWARFReg(reg));
1129 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001130 }
1131 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001132 __ ret();
1133 __ cfi().RestoreState();
1134 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135}
1136
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001137void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1138 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139}
1140
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141void CodeGeneratorX86_64::Move(Location destination, Location source) {
1142 if (source.Equals(destination)) {
1143 return;
1144 }
1145 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001146 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001148 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001150 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001152 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1153 } else if (source.IsConstant()) {
1154 HConstant* constant = source.GetConstant();
1155 if (constant->IsLongConstant()) {
1156 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1157 } else {
1158 Load32BitValue(dest, GetInt32ValueOf(constant));
1159 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160 } else {
1161 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001162 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 }
1164 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001165 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001167 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001169 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1170 } else if (source.IsConstant()) {
1171 HConstant* constant = source.GetConstant();
1172 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1173 if (constant->IsFloatConstant()) {
1174 Load32BitValue(dest, static_cast<int32_t>(value));
1175 } else {
1176 Load64BitValue(dest, value);
1177 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001178 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001179 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001180 } else {
1181 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001182 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001183 }
1184 } else if (destination.IsStackSlot()) {
1185 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001187 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001188 } else if (source.IsFpuRegister()) {
1189 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001191 } else if (source.IsConstant()) {
1192 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001193 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001194 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001196 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001197 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1198 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199 }
1200 } else {
1201 DCHECK(destination.IsDoubleStackSlot());
1202 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001204 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 } else if (source.IsFpuRegister()) {
1206 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001208 } else if (source.IsConstant()) {
1209 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001210 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001211 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001212 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001213 } else {
1214 DCHECK(constant->IsLongConstant());
1215 value = constant->AsLongConstant()->GetValue();
1216 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001217 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218 } else {
1219 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001220 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1221 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222 }
1223 }
1224}
1225
Calin Juravle175dc732015-08-25 15:42:32 +01001226void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1227 DCHECK(location.IsRegister());
1228 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1229}
1230
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231void CodeGeneratorX86_64::MoveLocation(
1232 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1233 Move(dst, src);
1234}
1235
1236void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1237 if (location.IsRegister()) {
1238 locations->AddTemp(location);
1239 } else {
1240 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1241 }
1242}
1243
David Brazdilfc6a86a2015-06-26 10:33:45 +00001244void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001245 DCHECK(!successor->IsExitBlock());
1246
1247 HBasicBlock* block = got->GetBlock();
1248 HInstruction* previous = got->GetPrevious();
1249
1250 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001251 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001252 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1253 return;
1254 }
1255
1256 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1257 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1258 }
1259 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 __ jmp(codegen_->GetLabelOf(successor));
1261 }
1262}
1263
David Brazdilfc6a86a2015-06-26 10:33:45 +00001264void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1265 got->SetLocations(nullptr);
1266}
1267
1268void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1269 HandleGoto(got, got->GetSuccessor());
1270}
1271
1272void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1273 try_boundary->SetLocations(nullptr);
1274}
1275
1276void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1277 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1278 if (!successor->IsExitBlock()) {
1279 HandleGoto(try_boundary, successor);
1280 }
1281}
1282
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001283void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1284 exit->SetLocations(nullptr);
1285}
1286
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001287void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288}
1289
Mark Mendell152408f2015-12-31 12:28:50 -05001290template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001291void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001292 LabelType* true_label,
1293 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001294 if (cond->IsFPConditionTrueIfNaN()) {
1295 __ j(kUnordered, true_label);
1296 } else if (cond->IsFPConditionFalseIfNaN()) {
1297 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001298 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001299 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001300}
1301
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001302void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001303 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001304
Mark Mendellc4701932015-04-10 13:18:51 -04001305 Location left = locations->InAt(0);
1306 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001307 Primitive::Type type = condition->InputAt(0)->GetType();
1308 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001309 case Primitive::kPrimBoolean:
1310 case Primitive::kPrimByte:
1311 case Primitive::kPrimChar:
1312 case Primitive::kPrimShort:
1313 case Primitive::kPrimInt:
1314 case Primitive::kPrimNot: {
1315 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1316 if (right.IsConstant()) {
1317 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1318 if (value == 0) {
1319 __ testl(left_reg, left_reg);
1320 } else {
1321 __ cmpl(left_reg, Immediate(value));
1322 }
1323 } else if (right.IsStackSlot()) {
1324 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1325 } else {
1326 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1327 }
1328 break;
1329 }
Mark Mendellc4701932015-04-10 13:18:51 -04001330 case Primitive::kPrimLong: {
1331 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1332 if (right.IsConstant()) {
1333 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001334 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001335 } else if (right.IsDoubleStackSlot()) {
1336 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1337 } else {
1338 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1339 }
Mark Mendellc4701932015-04-10 13:18:51 -04001340 break;
1341 }
1342 case Primitive::kPrimFloat: {
1343 if (right.IsFpuRegister()) {
1344 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1345 } else if (right.IsConstant()) {
1346 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1347 codegen_->LiteralFloatAddress(
1348 right.GetConstant()->AsFloatConstant()->GetValue()));
1349 } else {
1350 DCHECK(right.IsStackSlot());
1351 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1352 Address(CpuRegister(RSP), right.GetStackIndex()));
1353 }
Mark Mendellc4701932015-04-10 13:18:51 -04001354 break;
1355 }
1356 case Primitive::kPrimDouble: {
1357 if (right.IsFpuRegister()) {
1358 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1359 } else if (right.IsConstant()) {
1360 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1361 codegen_->LiteralDoubleAddress(
1362 right.GetConstant()->AsDoubleConstant()->GetValue()));
1363 } else {
1364 DCHECK(right.IsDoubleStackSlot());
1365 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1366 Address(CpuRegister(RSP), right.GetStackIndex()));
1367 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001368 break;
1369 }
1370 default:
1371 LOG(FATAL) << "Unexpected condition type " << type;
1372 }
1373}
1374
1375template<class LabelType>
1376void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1377 LabelType* true_target_in,
1378 LabelType* false_target_in) {
1379 // Generated branching requires both targets to be explicit. If either of the
1380 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1381 LabelType fallthrough_target;
1382 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1383 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1384
1385 // Generate the comparison to set the CC.
1386 GenerateCompareTest(condition);
1387
1388 // Now generate the correct jump(s).
1389 Primitive::Type type = condition->InputAt(0)->GetType();
1390 switch (type) {
1391 case Primitive::kPrimLong: {
1392 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1393 break;
1394 }
1395 case Primitive::kPrimFloat: {
1396 GenerateFPJumps(condition, true_target, false_target);
1397 break;
1398 }
1399 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001400 GenerateFPJumps(condition, true_target, false_target);
1401 break;
1402 }
1403 default:
1404 LOG(FATAL) << "Unexpected condition type " << type;
1405 }
1406
David Brazdil0debae72015-11-12 18:37:00 +00001407 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001408 __ jmp(false_target);
1409 }
David Brazdil0debae72015-11-12 18:37:00 +00001410
1411 if (fallthrough_target.IsLinked()) {
1412 __ Bind(&fallthrough_target);
1413 }
Mark Mendellc4701932015-04-10 13:18:51 -04001414}
1415
David Brazdil0debae72015-11-12 18:37:00 +00001416static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1417 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1418 // are set only strictly before `branch`. We can't use the eflags on long
1419 // conditions if they are materialized due to the complex branching.
1420 return cond->IsCondition() &&
1421 cond->GetNext() == branch &&
1422 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1423}
1424
Mark Mendell152408f2015-12-31 12:28:50 -05001425template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001426void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001427 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001428 LabelType* true_target,
1429 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001430 HInstruction* cond = instruction->InputAt(condition_input_index);
1431
1432 if (true_target == nullptr && false_target == nullptr) {
1433 // Nothing to do. The code always falls through.
1434 return;
1435 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001436 // Constant condition, statically compared against "true" (integer value 1).
1437 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001438 if (true_target != nullptr) {
1439 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001440 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001441 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001442 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001443 if (false_target != nullptr) {
1444 __ jmp(false_target);
1445 }
1446 }
1447 return;
1448 }
1449
1450 // The following code generates these patterns:
1451 // (1) true_target == nullptr && false_target != nullptr
1452 // - opposite condition true => branch to false_target
1453 // (2) true_target != nullptr && false_target == nullptr
1454 // - condition true => branch to true_target
1455 // (3) true_target != nullptr && false_target != nullptr
1456 // - condition true => branch to true_target
1457 // - branch to false_target
1458 if (IsBooleanValueOrMaterializedCondition(cond)) {
1459 if (AreEflagsSetFrom(cond, instruction)) {
1460 if (true_target == nullptr) {
1461 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1462 } else {
1463 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1464 }
1465 } else {
1466 // Materialized condition, compare against 0.
1467 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1468 if (lhs.IsRegister()) {
1469 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1470 } else {
1471 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1472 }
1473 if (true_target == nullptr) {
1474 __ j(kEqual, false_target);
1475 } else {
1476 __ j(kNotEqual, true_target);
1477 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001478 }
1479 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001480 // Condition has not been materialized, use its inputs as the
1481 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001482 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001483
David Brazdil0debae72015-11-12 18:37:00 +00001484 // If this is a long or FP comparison that has been folded into
1485 // the HCondition, generate the comparison directly.
1486 Primitive::Type type = condition->InputAt(0)->GetType();
1487 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1488 GenerateCompareTestAndBranch(condition, true_target, false_target);
1489 return;
1490 }
1491
1492 Location lhs = condition->GetLocations()->InAt(0);
1493 Location rhs = condition->GetLocations()->InAt(1);
1494 if (rhs.IsRegister()) {
1495 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1496 } else if (rhs.IsConstant()) {
1497 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001498 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001499 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001500 __ cmpl(lhs.AsRegister<CpuRegister>(),
1501 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1502 }
1503 if (true_target == nullptr) {
1504 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1505 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001506 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001507 }
Dave Allison20dfc792014-06-16 20:44:29 -07001508 }
David Brazdil0debae72015-11-12 18:37:00 +00001509
1510 // If neither branch falls through (case 3), the conditional branch to `true_target`
1511 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1512 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001513 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001514 }
1515}
1516
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1519 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520 locations->SetInAt(0, Location::Any());
1521 }
1522}
1523
1524void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001525 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1526 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1527 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1528 nullptr : codegen_->GetLabelOf(true_successor);
1529 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1530 nullptr : codegen_->GetLabelOf(false_successor);
1531 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001532}
1533
1534void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1535 LocationSummary* locations = new (GetGraph()->GetArena())
1536 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001537 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001538 locations->SetInAt(0, Location::Any());
1539 }
1540}
1541
1542void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001543 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001544 GenerateTestAndBranch<Label>(deoptimize,
1545 /* condition_input_index */ 0,
1546 slow_path->GetEntryLabel(),
1547 /* false_target */ nullptr);
1548}
1549
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001550static bool SelectCanUseCMOV(HSelect* select) {
1551 // There are no conditional move instructions for XMMs.
1552 if (Primitive::IsFloatingPointType(select->GetType())) {
1553 return false;
1554 }
1555
1556 // A FP condition doesn't generate the single CC that we need.
1557 HInstruction* condition = select->GetCondition();
1558 if (condition->IsCondition() &&
1559 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1560 return false;
1561 }
1562
1563 // We can generate a CMOV for this Select.
1564 return true;
1565}
1566
David Brazdil74eb1b22015-12-14 11:44:01 +00001567void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1569 if (Primitive::IsFloatingPointType(select->GetType())) {
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001571 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001572 } else {
1573 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001574 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001575 if (select->InputAt(1)->IsConstant()) {
1576 locations->SetInAt(1, Location::RequiresRegister());
1577 } else {
1578 locations->SetInAt(1, Location::Any());
1579 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001580 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001581 locations->SetInAt(1, Location::Any());
1582 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001583 }
1584 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1585 locations->SetInAt(2, Location::RequiresRegister());
1586 }
1587 locations->SetOut(Location::SameAsFirstInput());
1588}
1589
1590void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1591 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001592 if (SelectCanUseCMOV(select)) {
1593 // If both the condition and the source types are integer, we can generate
1594 // a CMOV to implement Select.
1595 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001596 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001597 DCHECK(locations->InAt(0).Equals(locations->Out()));
1598
1599 HInstruction* select_condition = select->GetCondition();
1600 Condition cond = kNotEqual;
1601
1602 // Figure out how to test the 'condition'.
1603 if (select_condition->IsCondition()) {
1604 HCondition* condition = select_condition->AsCondition();
1605 if (!condition->IsEmittedAtUseSite()) {
1606 // This was a previously materialized condition.
1607 // Can we use the existing condition code?
1608 if (AreEflagsSetFrom(condition, select)) {
1609 // Materialization was the previous instruction. Condition codes are right.
1610 cond = X86_64IntegerCondition(condition->GetCondition());
1611 } else {
1612 // No, we have to recreate the condition code.
1613 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1614 __ testl(cond_reg, cond_reg);
1615 }
1616 } else {
1617 GenerateCompareTest(condition);
1618 cond = X86_64IntegerCondition(condition->GetCondition());
1619 }
1620 } else {
1621 // Must be a boolean condition, which needs to be compared to 0.
1622 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1623 __ testl(cond_reg, cond_reg);
1624 }
1625
1626 // If the condition is true, overwrite the output, which already contains false.
1627 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001628 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1629 if (value_true_loc.IsRegister()) {
1630 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1631 } else {
1632 __ cmov(cond,
1633 value_false,
1634 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1635 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001636 } else {
1637 NearLabel false_target;
1638 GenerateTestAndBranch<NearLabel>(select,
1639 /* condition_input_index */ 2,
1640 /* true_target */ nullptr,
1641 &false_target);
1642 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1643 __ Bind(&false_target);
1644 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001645}
1646
David Srbecky0cf44932015-12-09 14:09:59 +00001647void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1648 new (GetGraph()->GetArena()) LocationSummary(info);
1649}
1650
David Srbeckyd28f4a02016-03-14 17:14:24 +00001651void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1652 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001653}
1654
1655void CodeGeneratorX86_64::GenerateNop() {
1656 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001657}
1658
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001659void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001660 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001661 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001662 // Handle the long/FP comparisons made in instruction simplification.
1663 switch (cond->InputAt(0)->GetType()) {
1664 case Primitive::kPrimLong:
1665 locations->SetInAt(0, Location::RequiresRegister());
1666 locations->SetInAt(1, Location::Any());
1667 break;
1668 case Primitive::kPrimFloat:
1669 case Primitive::kPrimDouble:
1670 locations->SetInAt(0, Location::RequiresFpuRegister());
1671 locations->SetInAt(1, Location::Any());
1672 break;
1673 default:
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::Any());
1676 break;
1677 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001678 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001679 locations->SetOut(Location::RequiresRegister());
1680 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001681}
1682
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001683void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001684 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001685 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001686 }
Mark Mendellc4701932015-04-10 13:18:51 -04001687
1688 LocationSummary* locations = cond->GetLocations();
1689 Location lhs = locations->InAt(0);
1690 Location rhs = locations->InAt(1);
1691 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001692 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001693
1694 switch (cond->InputAt(0)->GetType()) {
1695 default:
1696 // Integer case.
1697
1698 // Clear output register: setcc only sets the low byte.
1699 __ xorl(reg, reg);
1700
1701 if (rhs.IsRegister()) {
1702 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1703 } else if (rhs.IsConstant()) {
1704 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001705 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001706 } else {
1707 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1708 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001709 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001710 return;
1711 case Primitive::kPrimLong:
1712 // Clear output register: setcc only sets the low byte.
1713 __ xorl(reg, reg);
1714
1715 if (rhs.IsRegister()) {
1716 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1717 } else if (rhs.IsConstant()) {
1718 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001719 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001720 } else {
1721 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1722 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001723 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001724 return;
1725 case Primitive::kPrimFloat: {
1726 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1727 if (rhs.IsConstant()) {
1728 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1729 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1730 } else if (rhs.IsStackSlot()) {
1731 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1732 } else {
1733 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1734 }
1735 GenerateFPJumps(cond, &true_label, &false_label);
1736 break;
1737 }
1738 case Primitive::kPrimDouble: {
1739 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1740 if (rhs.IsConstant()) {
1741 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1742 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1743 } else if (rhs.IsDoubleStackSlot()) {
1744 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1745 } else {
1746 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1747 }
1748 GenerateFPJumps(cond, &true_label, &false_label);
1749 break;
1750 }
1751 }
1752
1753 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001754 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001755
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ Bind(&false_label);
1758 __ xorl(reg, reg);
1759 __ jmp(&done_label);
1760
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001762 __ Bind(&true_label);
1763 __ movl(reg, Immediate(1));
1764 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001813}
1814
Aart Bike9f37602015-10-09 11:15:55 -07001815void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001847void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001850 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001851 case Primitive::kPrimBoolean:
1852 case Primitive::kPrimByte:
1853 case Primitive::kPrimShort:
1854 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001855 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001856 case Primitive::kPrimLong: {
1857 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001858 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1860 break;
1861 }
1862 case Primitive::kPrimFloat:
1863 case Primitive::kPrimDouble: {
1864 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001865 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001866 locations->SetOut(Location::RequiresRegister());
1867 break;
1868 }
1869 default:
1870 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1871 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001872}
1873
1874void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001875 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001877 Location left = locations->InAt(0);
1878 Location right = locations->InAt(1);
1879
Mark Mendell0c9497d2015-08-21 09:30:05 -04001880 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001881 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001882 Condition less_cond = kLess;
1883
Calin Juravleddb7df22014-11-25 20:56:51 +00001884 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001885 case Primitive::kPrimBoolean:
1886 case Primitive::kPrimByte:
1887 case Primitive::kPrimShort:
1888 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001889 case Primitive::kPrimInt: {
1890 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1891 if (right.IsConstant()) {
1892 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1893 codegen_->Compare32BitValue(left_reg, value);
1894 } else if (right.IsStackSlot()) {
1895 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1896 } else {
1897 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1898 }
1899 break;
1900 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001901 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001902 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1903 if (right.IsConstant()) {
1904 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001905 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001906 } else if (right.IsDoubleStackSlot()) {
1907 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001908 } else {
1909 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1910 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001911 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001912 }
1913 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001914 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1915 if (right.IsConstant()) {
1916 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1917 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1918 } else if (right.IsStackSlot()) {
1919 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1920 } else {
1921 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1922 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001923 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001924 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001925 break;
1926 }
1927 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001928 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1929 if (right.IsConstant()) {
1930 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1931 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1932 } else if (right.IsDoubleStackSlot()) {
1933 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1934 } else {
1935 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1936 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001937 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001938 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001939 break;
1940 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001941 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001942 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001943 }
Aart Bika19616e2016-02-01 18:57:58 -08001944
Calin Juravleddb7df22014-11-25 20:56:51 +00001945 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001946 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001947 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001948
Calin Juravle91debbc2014-11-26 19:01:09 +00001949 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001950 __ movl(out, Immediate(1));
1951 __ jmp(&done);
1952
1953 __ Bind(&less);
1954 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001955
1956 __ Bind(&done);
1957}
1958
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001960 LocationSummary* locations =
1961 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001962 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001963}
1964
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001965void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001966 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967}
1968
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001969void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1970 LocationSummary* locations =
1971 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1972 locations->SetOut(Location::ConstantLocation(constant));
1973}
1974
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001975void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001976 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001977}
1978
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001979void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001980 LocationSummary* locations =
1981 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001982 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983}
1984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001985void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001986 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001987}
1988
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001989void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* 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::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001996 // Will be generated at use site.
1997}
1998
1999void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2000 LocationSummary* locations =
2001 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2002 locations->SetOut(Location::ConstantLocation(constant));
2003}
2004
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002005void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2006 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002007 // Will be generated at use site.
2008}
2009
Calin Juravle27df7582015-04-17 19:12:31 +01002010void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2011 memory_barrier->SetLocations(nullptr);
2012}
2013
2014void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002015 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002016}
2017
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002018void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2019 ret->SetLocations(nullptr);
2020}
2021
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002022void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002023 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002024}
2025
2026void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002027 LocationSummary* locations =
2028 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002029 switch (ret->InputAt(0)->GetType()) {
2030 case Primitive::kPrimBoolean:
2031 case Primitive::kPrimByte:
2032 case Primitive::kPrimChar:
2033 case Primitive::kPrimShort:
2034 case Primitive::kPrimInt:
2035 case Primitive::kPrimNot:
2036 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002037 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002038 break;
2039
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002040 case Primitive::kPrimFloat:
2041 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002042 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002043 break;
2044
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002045 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002046 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002048}
2049
2050void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2051 if (kIsDebugBuild) {
2052 switch (ret->InputAt(0)->GetType()) {
2053 case Primitive::kPrimBoolean:
2054 case Primitive::kPrimByte:
2055 case Primitive::kPrimChar:
2056 case Primitive::kPrimShort:
2057 case Primitive::kPrimInt:
2058 case Primitive::kPrimNot:
2059 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002061 break;
2062
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002063 case Primitive::kPrimFloat:
2064 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002065 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002066 XMM0);
2067 break;
2068
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002069 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002070 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002071 }
2072 }
2073 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002074}
2075
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002076Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2077 switch (type) {
2078 case Primitive::kPrimBoolean:
2079 case Primitive::kPrimByte:
2080 case Primitive::kPrimChar:
2081 case Primitive::kPrimShort:
2082 case Primitive::kPrimInt:
2083 case Primitive::kPrimNot:
2084 case Primitive::kPrimLong:
2085 return Location::RegisterLocation(RAX);
2086
2087 case Primitive::kPrimVoid:
2088 return Location::NoLocation();
2089
2090 case Primitive::kPrimDouble:
2091 case Primitive::kPrimFloat:
2092 return Location::FpuRegisterLocation(XMM0);
2093 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002094
2095 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002096}
2097
2098Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2099 return Location::RegisterLocation(kMethodRegisterArgument);
2100}
2101
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002102Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103 switch (type) {
2104 case Primitive::kPrimBoolean:
2105 case Primitive::kPrimByte:
2106 case Primitive::kPrimChar:
2107 case Primitive::kPrimShort:
2108 case Primitive::kPrimInt:
2109 case Primitive::kPrimNot: {
2110 uint32_t index = gp_index_++;
2111 stack_index_++;
2112 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002113 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002114 } else {
2115 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2116 }
2117 }
2118
2119 case Primitive::kPrimLong: {
2120 uint32_t index = gp_index_;
2121 stack_index_ += 2;
2122 if (index < calling_convention.GetNumberOfRegisters()) {
2123 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125 } else {
2126 gp_index_ += 2;
2127 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2128 }
2129 }
2130
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002132 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002133 stack_index_++;
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::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2138 }
2139 }
2140
2141 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002142 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 stack_index_ += 2;
2144 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002145 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002146 } else {
2147 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2148 }
2149 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150
2151 case Primitive::kPrimVoid:
2152 LOG(FATAL) << "Unexpected parameter type " << type;
2153 break;
2154 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002155 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002156}
2157
Calin Juravle175dc732015-08-25 15:42:32 +01002158void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2159 // The trampoline uses the same calling convention as dex calling conventions,
2160 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2161 // the method_idx.
2162 HandleInvoke(invoke);
2163}
2164
2165void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2166 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2167}
2168
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002169void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002170 // Explicit clinit checks triggered by static invokes must have been pruned by
2171 // art::PrepareForRegisterAllocation.
2172 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002173
Mark Mendellfb8d2792015-03-31 22:16:59 -04002174 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002175 if (intrinsic.TryDispatch(invoke)) {
2176 return;
2177 }
2178
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002179 HandleInvoke(invoke);
2180}
2181
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002182static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2183 if (invoke->GetLocations()->Intrinsified()) {
2184 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2185 intrinsic.Dispatch(invoke);
2186 return true;
2187 }
2188 return false;
2189}
2190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002191void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002192 // Explicit clinit checks triggered by static invokes must have been pruned by
2193 // art::PrepareForRegisterAllocation.
2194 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002195
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002196 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2197 return;
2198 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002199
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002200 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002201 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002202 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002203 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204}
2205
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002206void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002207 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002208 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002209}
2210
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002211void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002212 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002213 if (intrinsic.TryDispatch(invoke)) {
2214 return;
2215 }
2216
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002217 HandleInvoke(invoke);
2218}
2219
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002220void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002221 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2222 return;
2223 }
2224
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002225 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002226 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002227 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228}
2229
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2231 HandleInvoke(invoke);
2232 // Add the hidden argument.
2233 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2234}
2235
2236void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2237 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002238 LocationSummary* locations = invoke->GetLocations();
2239 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2240 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002241 Location receiver = locations->InAt(0);
2242 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2243
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 // Set the hidden argument. This is safe to do this here, as RAX
2245 // won't be modified thereafter, before the `call` instruction.
2246 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002247 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249 if (receiver.IsStackSlot()) {
2250 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 __ movl(temp, Address(temp, class_offset));
2253 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002254 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002257 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // Instead of simply (possibly) unpoisoning `temp` here, we should
2259 // emit a read barrier for the previous class reference load.
2260 // However this is not required in practice, as this is an
2261 // intermediate/temporary reference and because the current
2262 // concurrent copying collector keeps the from-space memory
2263 // intact/accessible until the end of the marking phase (the
2264 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002265 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03002266 // temp = temp->GetAddressOfIMT()
2267 __ movq(temp,
2268 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2269 // temp = temp->GetImtEntryAt(method_offset);
2270 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07002271 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002272 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002273 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002275 __ call(Address(temp,
2276 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277
2278 DCHECK(!codegen_->IsLeafMethod());
2279 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2280}
2281
Roland Levillain88cb1752014-10-20 16:36:47 +01002282void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2283 LocationSummary* locations =
2284 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2285 switch (neg->GetResultType()) {
2286 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002287 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002288 locations->SetInAt(0, Location::RequiresRegister());
2289 locations->SetOut(Location::SameAsFirstInput());
2290 break;
2291
Roland Levillain88cb1752014-10-20 16:36:47 +01002292 case Primitive::kPrimFloat:
2293 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002294 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002295 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002296 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002297 break;
2298
2299 default:
2300 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2301 }
2302}
2303
2304void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2305 LocationSummary* locations = neg->GetLocations();
2306 Location out = locations->Out();
2307 Location in = locations->InAt(0);
2308 switch (neg->GetResultType()) {
2309 case Primitive::kPrimInt:
2310 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002311 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002313 break;
2314
2315 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002316 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002317 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002318 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002319 break;
2320
Roland Levillain5368c212014-11-27 15:03:41 +00002321 case Primitive::kPrimFloat: {
2322 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002323 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002324 // Implement float negation with an exclusive or with value
2325 // 0x80000000 (mask for bit 31, representing the sign of a
2326 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002327 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002329 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002330 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002331
Roland Levillain5368c212014-11-27 15:03:41 +00002332 case Primitive::kPrimDouble: {
2333 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002334 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002335 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002336 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002337 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002338 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002339 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002340 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002341 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002342
2343 default:
2344 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2345 }
2346}
2347
Roland Levillaindff1f282014-11-05 14:15:05 +00002348void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2349 LocationSummary* locations =
2350 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2351 Primitive::Type result_type = conversion->GetResultType();
2352 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002353 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002354
David Brazdilb2bd1c52015-03-25 11:17:37 +00002355 // The Java language does not allow treating boolean as an integral type but
2356 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002357
Roland Levillaindff1f282014-11-05 14:15:05 +00002358 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002359 case Primitive::kPrimByte:
2360 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002361 case Primitive::kPrimLong:
2362 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002363 case Primitive::kPrimBoolean:
2364 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002365 case Primitive::kPrimShort:
2366 case Primitive::kPrimInt:
2367 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002368 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002369 locations->SetInAt(0, Location::Any());
2370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected type conversion from " << input_type
2375 << " to " << result_type;
2376 }
2377 break;
2378
Roland Levillain01a8d712014-11-14 16:27:39 +00002379 case Primitive::kPrimShort:
2380 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002381 case Primitive::kPrimLong:
2382 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002383 case Primitive::kPrimBoolean:
2384 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002385 case Primitive::kPrimByte:
2386 case Primitive::kPrimInt:
2387 case Primitive::kPrimChar:
2388 // Processing a Dex `int-to-short' instruction.
2389 locations->SetInAt(0, Location::Any());
2390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2391 break;
2392
2393 default:
2394 LOG(FATAL) << "Unexpected type conversion from " << input_type
2395 << " to " << result_type;
2396 }
2397 break;
2398
Roland Levillain946e1432014-11-11 17:35:19 +00002399 case Primitive::kPrimInt:
2400 switch (input_type) {
2401 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002402 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002403 locations->SetInAt(0, Location::Any());
2404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2405 break;
2406
2407 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002408 // Processing a Dex `float-to-int' instruction.
2409 locations->SetInAt(0, Location::RequiresFpuRegister());
2410 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002411 break;
2412
Roland Levillain946e1432014-11-11 17:35:19 +00002413 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002414 // Processing a Dex `double-to-int' instruction.
2415 locations->SetInAt(0, Location::RequiresFpuRegister());
2416 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002417 break;
2418
2419 default:
2420 LOG(FATAL) << "Unexpected type conversion from " << input_type
2421 << " to " << result_type;
2422 }
2423 break;
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425 case Primitive::kPrimLong:
2426 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002427 case Primitive::kPrimBoolean:
2428 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002429 case Primitive::kPrimByte:
2430 case Primitive::kPrimShort:
2431 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002432 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002433 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 // TODO: We would benefit from a (to-be-implemented)
2435 // Location::RegisterOrStackSlot requirement for this input.
2436 locations->SetInAt(0, Location::RequiresRegister());
2437 locations->SetOut(Location::RequiresRegister());
2438 break;
2439
2440 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002441 // Processing a Dex `float-to-long' instruction.
2442 locations->SetInAt(0, Location::RequiresFpuRegister());
2443 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002444 break;
2445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002447 // Processing a Dex `double-to-long' instruction.
2448 locations->SetInAt(0, Location::RequiresFpuRegister());
2449 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002450 break;
2451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 }
2456 break;
2457
Roland Levillain981e4542014-11-14 11:47:14 +00002458 case Primitive::kPrimChar:
2459 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002460 case Primitive::kPrimLong:
2461 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002462 case Primitive::kPrimBoolean:
2463 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002464 case Primitive::kPrimByte:
2465 case Primitive::kPrimShort:
2466 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002467 // Processing a Dex `int-to-char' instruction.
2468 locations->SetInAt(0, Location::Any());
2469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2470 break;
2471
2472 default:
2473 LOG(FATAL) << "Unexpected type conversion from " << input_type
2474 << " to " << result_type;
2475 }
2476 break;
2477
Roland Levillaindff1f282014-11-05 14:15:05 +00002478 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002479 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002480 case Primitive::kPrimBoolean:
2481 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002482 case Primitive::kPrimByte:
2483 case Primitive::kPrimShort:
2484 case Primitive::kPrimInt:
2485 case Primitive::kPrimChar:
2486 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002487 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002488 locations->SetOut(Location::RequiresFpuRegister());
2489 break;
2490
2491 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002492 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002493 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002494 locations->SetOut(Location::RequiresFpuRegister());
2495 break;
2496
Roland Levillaincff13742014-11-17 14:32:17 +00002497 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002498 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002499 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002500 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002501 break;
2502
2503 default:
2504 LOG(FATAL) << "Unexpected type conversion from " << input_type
2505 << " to " << result_type;
2506 };
2507 break;
2508
Roland Levillaindff1f282014-11-05 14:15:05 +00002509 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002510 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002511 case Primitive::kPrimBoolean:
2512 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002513 case Primitive::kPrimByte:
2514 case Primitive::kPrimShort:
2515 case Primitive::kPrimInt:
2516 case Primitive::kPrimChar:
2517 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002518 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002519 locations->SetOut(Location::RequiresFpuRegister());
2520 break;
2521
2522 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002523 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002524 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002525 locations->SetOut(Location::RequiresFpuRegister());
2526 break;
2527
Roland Levillaincff13742014-11-17 14:32:17 +00002528 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002529 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002530 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002531 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002532 break;
2533
2534 default:
2535 LOG(FATAL) << "Unexpected type conversion from " << input_type
2536 << " to " << result_type;
2537 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 break;
2539
2540 default:
2541 LOG(FATAL) << "Unexpected type conversion from " << input_type
2542 << " to " << result_type;
2543 }
2544}
2545
2546void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2547 LocationSummary* locations = conversion->GetLocations();
2548 Location out = locations->Out();
2549 Location in = locations->InAt(0);
2550 Primitive::Type result_type = conversion->GetResultType();
2551 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002552 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002553 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002554 case Primitive::kPrimByte:
2555 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002556 case Primitive::kPrimLong:
2557 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002558 case Primitive::kPrimBoolean:
2559 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002560 case Primitive::kPrimShort:
2561 case Primitive::kPrimInt:
2562 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002563 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002564 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002566 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002567 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002568 Address(CpuRegister(RSP), in.GetStackIndex()));
2569 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002570 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002571 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002572 }
2573 break;
2574
2575 default:
2576 LOG(FATAL) << "Unexpected type conversion from " << input_type
2577 << " to " << result_type;
2578 }
2579 break;
2580
Roland Levillain01a8d712014-11-14 16:27:39 +00002581 case Primitive::kPrimShort:
2582 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002583 case Primitive::kPrimLong:
2584 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002585 case Primitive::kPrimBoolean:
2586 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002587 case Primitive::kPrimByte:
2588 case Primitive::kPrimInt:
2589 case Primitive::kPrimChar:
2590 // Processing a Dex `int-to-short' instruction.
2591 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002593 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002595 Address(CpuRegister(RSP), in.GetStackIndex()));
2596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002598 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002599 }
2600 break;
2601
2602 default:
2603 LOG(FATAL) << "Unexpected type conversion from " << input_type
2604 << " to " << result_type;
2605 }
2606 break;
2607
Roland Levillain946e1432014-11-11 17:35:19 +00002608 case Primitive::kPrimInt:
2609 switch (input_type) {
2610 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002611 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002612 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002614 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002615 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002616 Address(CpuRegister(RSP), in.GetStackIndex()));
2617 } else {
2618 DCHECK(in.IsConstant());
2619 DCHECK(in.GetConstant()->IsLongConstant());
2620 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002622 }
2623 break;
2624
Roland Levillain3f8f9362014-12-02 17:45:01 +00002625 case Primitive::kPrimFloat: {
2626 // Processing a Dex `float-to-int' instruction.
2627 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2628 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002629 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002630
2631 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002632 // if input >= (float)INT_MAX goto done
2633 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002634 __ j(kAboveEqual, &done);
2635 // if input == NaN goto nan
2636 __ j(kUnordered, &nan);
2637 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002638 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002639 __ jmp(&done);
2640 __ Bind(&nan);
2641 // output = 0
2642 __ xorl(output, output);
2643 __ Bind(&done);
2644 break;
2645 }
2646
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002647 case Primitive::kPrimDouble: {
2648 // Processing a Dex `double-to-int' instruction.
2649 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2650 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002651 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002652
2653 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002654 // if input >= (double)INT_MAX goto done
2655 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002656 __ j(kAboveEqual, &done);
2657 // if input == NaN goto nan
2658 __ j(kUnordered, &nan);
2659 // output = double-to-int-truncate(input)
2660 __ cvttsd2si(output, input);
2661 __ jmp(&done);
2662 __ Bind(&nan);
2663 // output = 0
2664 __ xorl(output, output);
2665 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002666 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002667 }
Roland Levillain946e1432014-11-11 17:35:19 +00002668
2669 default:
2670 LOG(FATAL) << "Unexpected type conversion from " << input_type
2671 << " to " << result_type;
2672 }
2673 break;
2674
Roland Levillaindff1f282014-11-05 14:15:05 +00002675 case Primitive::kPrimLong:
2676 switch (input_type) {
2677 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002678 case Primitive::kPrimBoolean:
2679 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002680 case Primitive::kPrimByte:
2681 case Primitive::kPrimShort:
2682 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002683 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002684 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002685 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002687 break;
2688
Roland Levillain624279f2014-12-04 11:54:28 +00002689 case Primitive::kPrimFloat: {
2690 // Processing a Dex `float-to-long' instruction.
2691 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2692 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002693 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002694
Mark Mendell92e83bf2015-05-07 11:25:03 -04002695 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002696 // if input >= (float)LONG_MAX goto done
2697 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002698 __ j(kAboveEqual, &done);
2699 // if input == NaN goto nan
2700 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002701 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002702 __ cvttss2si(output, input, true);
2703 __ jmp(&done);
2704 __ Bind(&nan);
2705 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002706 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002707 __ Bind(&done);
2708 break;
2709 }
2710
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002711 case Primitive::kPrimDouble: {
2712 // Processing a Dex `double-to-long' instruction.
2713 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2714 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002715 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002716
Mark Mendell92e83bf2015-05-07 11:25:03 -04002717 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002718 // if input >= (double)LONG_MAX goto done
2719 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002720 __ j(kAboveEqual, &done);
2721 // if input == NaN goto nan
2722 __ j(kUnordered, &nan);
2723 // output = double-to-long-truncate(input)
2724 __ cvttsd2si(output, input, true);
2725 __ jmp(&done);
2726 __ Bind(&nan);
2727 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002728 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002729 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002730 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002731 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002732
2733 default:
2734 LOG(FATAL) << "Unexpected type conversion from " << input_type
2735 << " to " << result_type;
2736 }
2737 break;
2738
Roland Levillain981e4542014-11-14 11:47:14 +00002739 case Primitive::kPrimChar:
2740 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002741 case Primitive::kPrimLong:
2742 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002743 case Primitive::kPrimBoolean:
2744 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002745 case Primitive::kPrimByte:
2746 case Primitive::kPrimShort:
2747 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002748 // Processing a Dex `int-to-char' instruction.
2749 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002750 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002751 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002752 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002753 Address(CpuRegister(RSP), in.GetStackIndex()));
2754 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002756 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002757 }
2758 break;
2759
2760 default:
2761 LOG(FATAL) << "Unexpected type conversion from " << input_type
2762 << " to " << result_type;
2763 }
2764 break;
2765
Roland Levillaindff1f282014-11-05 14:15:05 +00002766 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002767 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002768 case Primitive::kPrimBoolean:
2769 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002770 case Primitive::kPrimByte:
2771 case Primitive::kPrimShort:
2772 case Primitive::kPrimInt:
2773 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002774 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002775 if (in.IsRegister()) {
2776 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2777 } else if (in.IsConstant()) {
2778 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2779 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002780 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002781 } else {
2782 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2783 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2784 }
Roland Levillaincff13742014-11-17 14:32:17 +00002785 break;
2786
2787 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002788 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002789 if (in.IsRegister()) {
2790 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2791 } else if (in.IsConstant()) {
2792 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2793 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002794 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002795 } else {
2796 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2797 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2798 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002799 break;
2800
Roland Levillaincff13742014-11-17 14:32:17 +00002801 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002802 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002803 if (in.IsFpuRegister()) {
2804 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2805 } else if (in.IsConstant()) {
2806 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2807 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002808 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002809 } else {
2810 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2811 Address(CpuRegister(RSP), in.GetStackIndex()));
2812 }
Roland Levillaincff13742014-11-17 14:32:17 +00002813 break;
2814
2815 default:
2816 LOG(FATAL) << "Unexpected type conversion from " << input_type
2817 << " to " << result_type;
2818 };
2819 break;
2820
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002822 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002823 case Primitive::kPrimBoolean:
2824 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002825 case Primitive::kPrimByte:
2826 case Primitive::kPrimShort:
2827 case Primitive::kPrimInt:
2828 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002829 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002830 if (in.IsRegister()) {
2831 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2832 } else if (in.IsConstant()) {
2833 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2834 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002835 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002836 } else {
2837 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2838 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2839 }
Roland Levillaincff13742014-11-17 14:32:17 +00002840 break;
2841
2842 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002843 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002844 if (in.IsRegister()) {
2845 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2846 } else if (in.IsConstant()) {
2847 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2848 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002849 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002850 } else {
2851 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2852 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2853 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002854 break;
2855
Roland Levillaincff13742014-11-17 14:32:17 +00002856 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002857 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002858 if (in.IsFpuRegister()) {
2859 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2860 } else if (in.IsConstant()) {
2861 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2862 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002863 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002864 } else {
2865 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2866 Address(CpuRegister(RSP), in.GetStackIndex()));
2867 }
Roland Levillaincff13742014-11-17 14:32:17 +00002868 break;
2869
2870 default:
2871 LOG(FATAL) << "Unexpected type conversion from " << input_type
2872 << " to " << result_type;
2873 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002874 break;
2875
2876 default:
2877 LOG(FATAL) << "Unexpected type conversion from " << input_type
2878 << " to " << result_type;
2879 }
2880}
2881
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002882void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002883 LocationSummary* locations =
2884 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002885 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002886 case Primitive::kPrimInt: {
2887 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002888 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002890 break;
2891 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002892
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002893 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002894 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002895 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002896 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002897 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002898 break;
2899 }
2900
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002901 case Primitive::kPrimDouble:
2902 case Primitive::kPrimFloat: {
2903 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002904 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002905 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002906 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002908
2909 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002910 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002911 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002912}
2913
2914void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2915 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002916 Location first = locations->InAt(0);
2917 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002918 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002919
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002920 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002921 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002922 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002923 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2924 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002925 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2926 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002927 } else {
2928 __ leal(out.AsRegister<CpuRegister>(), Address(
2929 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2930 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002931 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002932 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2933 __ addl(out.AsRegister<CpuRegister>(),
2934 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2935 } else {
2936 __ leal(out.AsRegister<CpuRegister>(), Address(
2937 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2938 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002939 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002940 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002942 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002943 break;
2944 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002945
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002946 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002947 if (second.IsRegister()) {
2948 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2949 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002950 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2951 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002952 } else {
2953 __ leaq(out.AsRegister<CpuRegister>(), Address(
2954 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2955 }
2956 } else {
2957 DCHECK(second.IsConstant());
2958 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2959 int32_t int32_value = Low32Bits(value);
2960 DCHECK_EQ(int32_value, value);
2961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2962 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2963 } else {
2964 __ leaq(out.AsRegister<CpuRegister>(), Address(
2965 first.AsRegister<CpuRegister>(), int32_value));
2966 }
2967 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002968 break;
2969 }
2970
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002971 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002972 if (second.IsFpuRegister()) {
2973 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2974 } else if (second.IsConstant()) {
2975 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002976 codegen_->LiteralFloatAddress(
2977 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002978 } else {
2979 DCHECK(second.IsStackSlot());
2980 __ addss(first.AsFpuRegister<XmmRegister>(),
2981 Address(CpuRegister(RSP), second.GetStackIndex()));
2982 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002983 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002984 }
2985
2986 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002987 if (second.IsFpuRegister()) {
2988 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2989 } else if (second.IsConstant()) {
2990 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002991 codegen_->LiteralDoubleAddress(
2992 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002993 } else {
2994 DCHECK(second.IsDoubleStackSlot());
2995 __ addsd(first.AsFpuRegister<XmmRegister>(),
2996 Address(CpuRegister(RSP), second.GetStackIndex()));
2997 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 break;
2999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003000
3001 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003002 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003003 }
3004}
3005
3006void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003007 LocationSummary* locations =
3008 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003009 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003010 case Primitive::kPrimInt: {
3011 locations->SetInAt(0, Location::RequiresRegister());
3012 locations->SetInAt(1, Location::Any());
3013 locations->SetOut(Location::SameAsFirstInput());
3014 break;
3015 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003016 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003017 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003018 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003019 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003020 break;
3021 }
Calin Juravle11351682014-10-23 15:38:15 +01003022 case Primitive::kPrimFloat:
3023 case Primitive::kPrimDouble: {
3024 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003025 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003026 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003027 break;
Calin Juravle11351682014-10-23 15:38:15 +01003028 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003029 default:
Calin Juravle11351682014-10-23 15:38:15 +01003030 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003031 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003032}
3033
3034void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3035 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003036 Location first = locations->InAt(0);
3037 Location second = locations->InAt(1);
3038 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003040 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003041 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003043 } else if (second.IsConstant()) {
3044 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003046 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003048 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003049 break;
3050 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003051 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003052 if (second.IsConstant()) {
3053 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3054 DCHECK(IsInt<32>(value));
3055 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3056 } else {
3057 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3058 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059 break;
3060 }
3061
Calin Juravle11351682014-10-23 15:38:15 +01003062 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003063 if (second.IsFpuRegister()) {
3064 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3065 } else if (second.IsConstant()) {
3066 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003067 codegen_->LiteralFloatAddress(
3068 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003069 } else {
3070 DCHECK(second.IsStackSlot());
3071 __ subss(first.AsFpuRegister<XmmRegister>(),
3072 Address(CpuRegister(RSP), second.GetStackIndex()));
3073 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 break;
Calin Juravle11351682014-10-23 15:38:15 +01003075 }
3076
3077 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003078 if (second.IsFpuRegister()) {
3079 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3080 } else if (second.IsConstant()) {
3081 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003082 codegen_->LiteralDoubleAddress(
3083 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003084 } else {
3085 DCHECK(second.IsDoubleStackSlot());
3086 __ subsd(first.AsFpuRegister<XmmRegister>(),
3087 Address(CpuRegister(RSP), second.GetStackIndex()));
3088 }
Calin Juravle11351682014-10-23 15:38:15 +01003089 break;
3090 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003091
3092 default:
Calin Juravle11351682014-10-23 15:38:15 +01003093 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003094 }
3095}
3096
Calin Juravle34bacdf2014-10-07 20:23:36 +01003097void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3098 LocationSummary* locations =
3099 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3100 switch (mul->GetResultType()) {
3101 case Primitive::kPrimInt: {
3102 locations->SetInAt(0, Location::RequiresRegister());
3103 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003104 if (mul->InputAt(1)->IsIntConstant()) {
3105 // Can use 3 operand multiply.
3106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3107 } else {
3108 locations->SetOut(Location::SameAsFirstInput());
3109 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110 break;
3111 }
3112 case Primitive::kPrimLong: {
3113 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003114 locations->SetInAt(1, Location::Any());
3115 if (mul->InputAt(1)->IsLongConstant() &&
3116 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003117 // Can use 3 operand multiply.
3118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3119 } else {
3120 locations->SetOut(Location::SameAsFirstInput());
3121 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003122 break;
3123 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003124 case Primitive::kPrimFloat:
3125 case Primitive::kPrimDouble: {
3126 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003127 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003130 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131
3132 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003133 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134 }
3135}
3136
3137void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3138 LocationSummary* locations = mul->GetLocations();
3139 Location first = locations->InAt(0);
3140 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003141 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003143 case Primitive::kPrimInt:
3144 // The constant may have ended up in a register, so test explicitly to avoid
3145 // problems where the output may not be the same as the first operand.
3146 if (mul->InputAt(1)->IsIntConstant()) {
3147 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3148 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3149 } else if (second.IsRegister()) {
3150 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003151 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003152 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003153 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003154 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003155 __ imull(first.AsRegister<CpuRegister>(),
3156 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157 }
3158 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003159 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003160 // The constant may have ended up in a register, so test explicitly to avoid
3161 // problems where the output may not be the same as the first operand.
3162 if (mul->InputAt(1)->IsLongConstant()) {
3163 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3164 if (IsInt<32>(value)) {
3165 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3166 Immediate(static_cast<int32_t>(value)));
3167 } else {
3168 // Have to use the constant area.
3169 DCHECK(first.Equals(out));
3170 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3171 }
3172 } else if (second.IsRegister()) {
3173 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003174 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003175 } else {
3176 DCHECK(second.IsDoubleStackSlot());
3177 DCHECK(first.Equals(out));
3178 __ imulq(first.AsRegister<CpuRegister>(),
3179 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 break;
3182 }
3183
Calin Juravleb5bfa962014-10-21 18:02:24 +01003184 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003185 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003186 if (second.IsFpuRegister()) {
3187 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3188 } else if (second.IsConstant()) {
3189 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003190 codegen_->LiteralFloatAddress(
3191 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003192 } else {
3193 DCHECK(second.IsStackSlot());
3194 __ mulss(first.AsFpuRegister<XmmRegister>(),
3195 Address(CpuRegister(RSP), second.GetStackIndex()));
3196 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003198 }
3199
3200 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003201 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003202 if (second.IsFpuRegister()) {
3203 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3204 } else if (second.IsConstant()) {
3205 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003206 codegen_->LiteralDoubleAddress(
3207 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003208 } else {
3209 DCHECK(second.IsDoubleStackSlot());
3210 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3211 Address(CpuRegister(RSP), second.GetStackIndex()));
3212 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003213 break;
3214 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215
3216 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003217 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218 }
3219}
3220
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003221void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3222 uint32_t stack_adjustment, bool is_float) {
3223 if (source.IsStackSlot()) {
3224 DCHECK(is_float);
3225 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3226 } else if (source.IsDoubleStackSlot()) {
3227 DCHECK(!is_float);
3228 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3229 } else {
3230 // Write the value to the temporary location on the stack and load to FP stack.
3231 if (is_float) {
3232 Location stack_temp = Location::StackSlot(temp_offset);
3233 codegen_->Move(stack_temp, source);
3234 __ flds(Address(CpuRegister(RSP), temp_offset));
3235 } else {
3236 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3237 codegen_->Move(stack_temp, source);
3238 __ fldl(Address(CpuRegister(RSP), temp_offset));
3239 }
3240 }
3241}
3242
3243void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3244 Primitive::Type type = rem->GetResultType();
3245 bool is_float = type == Primitive::kPrimFloat;
3246 size_t elem_size = Primitive::ComponentSize(type);
3247 LocationSummary* locations = rem->GetLocations();
3248 Location first = locations->InAt(0);
3249 Location second = locations->InAt(1);
3250 Location out = locations->Out();
3251
3252 // Create stack space for 2 elements.
3253 // TODO: enhance register allocator to ask for stack temporaries.
3254 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3255
3256 // Load the values to the FP stack in reverse order, using temporaries if needed.
3257 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3258 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3259
3260 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003261 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003262 __ Bind(&retry);
3263 __ fprem();
3264
3265 // Move FP status to AX.
3266 __ fstsw();
3267
3268 // And see if the argument reduction is complete. This is signaled by the
3269 // C2 FPU flag bit set to 0.
3270 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3271 __ j(kNotEqual, &retry);
3272
3273 // We have settled on the final value. Retrieve it into an XMM register.
3274 // Store FP top of stack to real stack.
3275 if (is_float) {
3276 __ fsts(Address(CpuRegister(RSP), 0));
3277 } else {
3278 __ fstl(Address(CpuRegister(RSP), 0));
3279 }
3280
3281 // Pop the 2 items from the FP stack.
3282 __ fucompp();
3283
3284 // Load the value from the stack into an XMM register.
3285 DCHECK(out.IsFpuRegister()) << out;
3286 if (is_float) {
3287 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3288 } else {
3289 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3290 }
3291
3292 // And remove the temporary stack space we allocated.
3293 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3294}
3295
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003296void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3297 DCHECK(instruction->IsDiv() || instruction->IsRem());
3298
3299 LocationSummary* locations = instruction->GetLocations();
3300 Location second = locations->InAt(1);
3301 DCHECK(second.IsConstant());
3302
3303 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3304 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003305 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003306
3307 DCHECK(imm == 1 || imm == -1);
3308
3309 switch (instruction->GetResultType()) {
3310 case Primitive::kPrimInt: {
3311 if (instruction->IsRem()) {
3312 __ xorl(output_register, output_register);
3313 } else {
3314 __ movl(output_register, input_register);
3315 if (imm == -1) {
3316 __ negl(output_register);
3317 }
3318 }
3319 break;
3320 }
3321
3322 case Primitive::kPrimLong: {
3323 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003324 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003325 } else {
3326 __ movq(output_register, input_register);
3327 if (imm == -1) {
3328 __ negq(output_register);
3329 }
3330 }
3331 break;
3332 }
3333
3334 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003335 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003336 }
3337}
3338
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003339void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003340 LocationSummary* locations = instruction->GetLocations();
3341 Location second = locations->InAt(1);
3342
3343 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3344 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3345
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003346 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003347 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3348 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349
3350 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3351
3352 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003353 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003354 __ testl(numerator, numerator);
3355 __ cmov(kGreaterEqual, tmp, numerator);
3356 int shift = CTZ(imm);
3357 __ sarl(tmp, Immediate(shift));
3358
3359 if (imm < 0) {
3360 __ negl(tmp);
3361 }
3362
3363 __ movl(output_register, tmp);
3364 } else {
3365 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3366 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3367
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003368 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369 __ addq(rdx, numerator);
3370 __ testq(numerator, numerator);
3371 __ cmov(kGreaterEqual, rdx, numerator);
3372 int shift = CTZ(imm);
3373 __ sarq(rdx, Immediate(shift));
3374
3375 if (imm < 0) {
3376 __ negq(rdx);
3377 }
3378
3379 __ movq(output_register, rdx);
3380 }
3381}
3382
3383void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3384 DCHECK(instruction->IsDiv() || instruction->IsRem());
3385
3386 LocationSummary* locations = instruction->GetLocations();
3387 Location second = locations->InAt(1);
3388
3389 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3390 : locations->GetTemp(0).AsRegister<CpuRegister>();
3391 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3392 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3393 : locations->Out().AsRegister<CpuRegister>();
3394 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3395
3396 DCHECK_EQ(RAX, eax.AsRegister());
3397 DCHECK_EQ(RDX, edx.AsRegister());
3398 if (instruction->IsDiv()) {
3399 DCHECK_EQ(RAX, out.AsRegister());
3400 } else {
3401 DCHECK_EQ(RDX, out.AsRegister());
3402 }
3403
3404 int64_t magic;
3405 int shift;
3406
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003407 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003408 if (instruction->GetResultType() == Primitive::kPrimInt) {
3409 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3410
3411 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3412
3413 __ movl(numerator, eax);
3414
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415 __ movl(eax, Immediate(magic));
3416 __ imull(numerator);
3417
3418 if (imm > 0 && magic < 0) {
3419 __ addl(edx, numerator);
3420 } else if (imm < 0 && magic > 0) {
3421 __ subl(edx, numerator);
3422 }
3423
3424 if (shift != 0) {
3425 __ sarl(edx, Immediate(shift));
3426 }
3427
3428 __ movl(eax, edx);
3429 __ shrl(edx, Immediate(31));
3430 __ addl(edx, eax);
3431
3432 if (instruction->IsRem()) {
3433 __ movl(eax, numerator);
3434 __ imull(edx, Immediate(imm));
3435 __ subl(eax, edx);
3436 __ movl(edx, eax);
3437 } else {
3438 __ movl(eax, edx);
3439 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 } else {
3441 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3442
3443 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3444
3445 CpuRegister rax = eax;
3446 CpuRegister rdx = edx;
3447
3448 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3449
3450 // Save the numerator.
3451 __ movq(numerator, rax);
3452
3453 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003454 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455
3456 // RDX:RAX = magic * numerator
3457 __ imulq(numerator);
3458
3459 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003460 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003461 __ addq(rdx, numerator);
3462 } else if (imm < 0 && magic > 0) {
3463 // RDX -= numerator
3464 __ subq(rdx, numerator);
3465 }
3466
3467 // Shift if needed.
3468 if (shift != 0) {
3469 __ sarq(rdx, Immediate(shift));
3470 }
3471
3472 // RDX += 1 if RDX < 0
3473 __ movq(rax, rdx);
3474 __ shrq(rdx, Immediate(63));
3475 __ addq(rdx, rax);
3476
3477 if (instruction->IsRem()) {
3478 __ movq(rax, numerator);
3479
3480 if (IsInt<32>(imm)) {
3481 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3482 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003483 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003484 }
3485
3486 __ subq(rax, rdx);
3487 __ movq(rdx, rax);
3488 } else {
3489 __ movq(rax, rdx);
3490 }
3491 }
3492}
3493
Calin Juravlebacfec32014-11-14 15:54:36 +00003494void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3495 DCHECK(instruction->IsDiv() || instruction->IsRem());
3496 Primitive::Type type = instruction->GetResultType();
3497 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3498
3499 bool is_div = instruction->IsDiv();
3500 LocationSummary* locations = instruction->GetLocations();
3501
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3503 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003504
Roland Levillain271ab9c2014-11-27 15:23:57 +00003505 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003507
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003509 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003510
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511 if (imm == 0) {
3512 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3513 } else if (imm == 1 || imm == -1) {
3514 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003515 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003516 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 } else {
3518 DCHECK(imm <= -2 || imm >= 2);
3519 GenerateDivRemWithAnyConstant(instruction);
3520 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003521 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003522 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003524 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003526
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3528 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3529 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3530 // so it's safe to just use negl instead of more complex comparisons.
3531 if (type == Primitive::kPrimInt) {
3532 __ cmpl(second_reg, Immediate(-1));
3533 __ j(kEqual, slow_path->GetEntryLabel());
3534 // edx:eax <- sign-extended of eax
3535 __ cdq();
3536 // eax = quotient, edx = remainder
3537 __ idivl(second_reg);
3538 } else {
3539 __ cmpq(second_reg, Immediate(-1));
3540 __ j(kEqual, slow_path->GetEntryLabel());
3541 // rdx:rax <- sign-extended of rax
3542 __ cqo();
3543 // rax = quotient, rdx = remainder
3544 __ idivq(second_reg);
3545 }
3546 __ Bind(slow_path->GetExitLabel());
3547 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003548}
3549
Calin Juravle7c4954d2014-10-28 16:57:40 +00003550void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3551 LocationSummary* locations =
3552 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3553 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003554 case Primitive::kPrimInt:
3555 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003556 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003558 locations->SetOut(Location::SameAsFirstInput());
3559 // Intel uses edx:eax as the dividend.
3560 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3562 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3563 // output and request another temp.
3564 if (div->InputAt(1)->IsConstant()) {
3565 locations->AddTemp(Location::RequiresRegister());
3566 }
Calin Juravled0d48522014-11-04 16:40:20 +00003567 break;
3568 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003569
Calin Juravle7c4954d2014-10-28 16:57:40 +00003570 case Primitive::kPrimFloat:
3571 case Primitive::kPrimDouble: {
3572 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003573 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003574 locations->SetOut(Location::SameAsFirstInput());
3575 break;
3576 }
3577
3578 default:
3579 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3580 }
3581}
3582
3583void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3584 LocationSummary* locations = div->GetLocations();
3585 Location first = locations->InAt(0);
3586 Location second = locations->InAt(1);
3587 DCHECK(first.Equals(locations->Out()));
3588
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003589 Primitive::Type type = div->GetResultType();
3590 switch (type) {
3591 case Primitive::kPrimInt:
3592 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003594 break;
3595 }
3596
Calin Juravle7c4954d2014-10-28 16:57:40 +00003597 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003598 if (second.IsFpuRegister()) {
3599 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3600 } else if (second.IsConstant()) {
3601 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003602 codegen_->LiteralFloatAddress(
3603 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003604 } else {
3605 DCHECK(second.IsStackSlot());
3606 __ divss(first.AsFpuRegister<XmmRegister>(),
3607 Address(CpuRegister(RSP), second.GetStackIndex()));
3608 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003609 break;
3610 }
3611
3612 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003613 if (second.IsFpuRegister()) {
3614 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3615 } else if (second.IsConstant()) {
3616 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003617 codegen_->LiteralDoubleAddress(
3618 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003619 } else {
3620 DCHECK(second.IsDoubleStackSlot());
3621 __ divsd(first.AsFpuRegister<XmmRegister>(),
3622 Address(CpuRegister(RSP), second.GetStackIndex()));
3623 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003624 break;
3625 }
3626
3627 default:
3628 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3629 }
3630}
3631
Calin Juravlebacfec32014-11-14 15:54:36 +00003632void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003633 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003634 LocationSummary* locations =
3635 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003636
3637 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 case Primitive::kPrimInt:
3639 case Primitive::kPrimLong: {
3640 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003641 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003642 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3643 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003644 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3645 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3646 // output and request another temp.
3647 if (rem->InputAt(1)->IsConstant()) {
3648 locations->AddTemp(Location::RequiresRegister());
3649 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 break;
3651 }
3652
3653 case Primitive::kPrimFloat:
3654 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003655 locations->SetInAt(0, Location::Any());
3656 locations->SetInAt(1, Location::Any());
3657 locations->SetOut(Location::RequiresFpuRegister());
3658 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003659 break;
3660 }
3661
3662 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003663 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003664 }
3665}
3666
3667void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3668 Primitive::Type type = rem->GetResultType();
3669 switch (type) {
3670 case Primitive::kPrimInt:
3671 case Primitive::kPrimLong: {
3672 GenerateDivRemIntegral(rem);
3673 break;
3674 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003675 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003676 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003677 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003678 break;
3679 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003680 default:
3681 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3682 }
3683}
3684
Calin Juravled0d48522014-11-04 16:40:20 +00003685void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003686 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3687 ? LocationSummary::kCallOnSlowPath
3688 : LocationSummary::kNoCall;
3689 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003690 locations->SetInAt(0, Location::Any());
3691 if (instruction->HasUses()) {
3692 locations->SetOut(Location::SameAsFirstInput());
3693 }
3694}
3695
3696void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003697 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003698 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3699 codegen_->AddSlowPath(slow_path);
3700
3701 LocationSummary* locations = instruction->GetLocations();
3702 Location value = locations->InAt(0);
3703
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003704 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003705 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003706 case Primitive::kPrimByte:
3707 case Primitive::kPrimChar:
3708 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003709 case Primitive::kPrimInt: {
3710 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003712 __ j(kEqual, slow_path->GetEntryLabel());
3713 } else if (value.IsStackSlot()) {
3714 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3715 __ j(kEqual, slow_path->GetEntryLabel());
3716 } else {
3717 DCHECK(value.IsConstant()) << value;
3718 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3719 __ jmp(slow_path->GetEntryLabel());
3720 }
3721 }
3722 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003723 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003724 case Primitive::kPrimLong: {
3725 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003727 __ j(kEqual, slow_path->GetEntryLabel());
3728 } else if (value.IsDoubleStackSlot()) {
3729 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3730 __ j(kEqual, slow_path->GetEntryLabel());
3731 } else {
3732 DCHECK(value.IsConstant()) << value;
3733 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3734 __ jmp(slow_path->GetEntryLabel());
3735 }
3736 }
3737 break;
3738 }
3739 default:
3740 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003741 }
Calin Juravled0d48522014-11-04 16:40:20 +00003742}
3743
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3745 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3746
3747 LocationSummary* locations =
3748 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3749
3750 switch (op->GetResultType()) {
3751 case Primitive::kPrimInt:
3752 case Primitive::kPrimLong: {
3753 locations->SetInAt(0, Location::RequiresRegister());
3754 // The shift count needs to be in CL.
3755 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3756 locations->SetOut(Location::SameAsFirstInput());
3757 break;
3758 }
3759 default:
3760 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3761 }
3762}
3763
3764void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3765 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3766
3767 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003768 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003769 Location second = locations->InAt(1);
3770
3771 switch (op->GetResultType()) {
3772 case Primitive::kPrimInt: {
3773 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775 if (op->IsShl()) {
3776 __ shll(first_reg, second_reg);
3777 } else if (op->IsShr()) {
3778 __ sarl(first_reg, second_reg);
3779 } else {
3780 __ shrl(first_reg, second_reg);
3781 }
3782 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003783 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003784 if (op->IsShl()) {
3785 __ shll(first_reg, imm);
3786 } else if (op->IsShr()) {
3787 __ sarl(first_reg, imm);
3788 } else {
3789 __ shrl(first_reg, imm);
3790 }
3791 }
3792 break;
3793 }
3794 case Primitive::kPrimLong: {
3795 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 if (op->IsShl()) {
3798 __ shlq(first_reg, second_reg);
3799 } else if (op->IsShr()) {
3800 __ sarq(first_reg, second_reg);
3801 } else {
3802 __ shrq(first_reg, second_reg);
3803 }
3804 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003805 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003806 if (op->IsShl()) {
3807 __ shlq(first_reg, imm);
3808 } else if (op->IsShr()) {
3809 __ sarq(first_reg, imm);
3810 } else {
3811 __ shrq(first_reg, imm);
3812 }
3813 }
3814 break;
3815 }
3816 default:
3817 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003818 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003819 }
3820}
3821
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003822void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3823 LocationSummary* locations =
3824 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3825
3826 switch (ror->GetResultType()) {
3827 case Primitive::kPrimInt:
3828 case Primitive::kPrimLong: {
3829 locations->SetInAt(0, Location::RequiresRegister());
3830 // The shift count needs to be in CL (unless it is a constant).
3831 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3832 locations->SetOut(Location::SameAsFirstInput());
3833 break;
3834 }
3835 default:
3836 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3837 UNREACHABLE();
3838 }
3839}
3840
3841void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3842 LocationSummary* locations = ror->GetLocations();
3843 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3844 Location second = locations->InAt(1);
3845
3846 switch (ror->GetResultType()) {
3847 case Primitive::kPrimInt:
3848 if (second.IsRegister()) {
3849 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3850 __ rorl(first_reg, second_reg);
3851 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003852 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003853 __ rorl(first_reg, imm);
3854 }
3855 break;
3856 case Primitive::kPrimLong:
3857 if (second.IsRegister()) {
3858 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3859 __ rorq(first_reg, second_reg);
3860 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003861 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003862 __ rorq(first_reg, imm);
3863 }
3864 break;
3865 default:
3866 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3867 UNREACHABLE();
3868 }
3869}
3870
Calin Juravle9aec02f2014-11-18 23:06:35 +00003871void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3872 HandleShift(shl);
3873}
3874
3875void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3876 HandleShift(shl);
3877}
3878
3879void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3880 HandleShift(shr);
3881}
3882
3883void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3884 HandleShift(shr);
3885}
3886
3887void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3888 HandleShift(ushr);
3889}
3890
3891void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3892 HandleShift(ushr);
3893}
3894
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003895void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003896 LocationSummary* locations =
3897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003898 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003899 if (instruction->IsStringAlloc()) {
3900 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3901 } else {
3902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3903 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3904 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003905 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003906}
3907
3908void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003909 // Note: if heap poisoning is enabled, the entry point takes cares
3910 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003911 if (instruction->IsStringAlloc()) {
3912 // String is allocated through StringFactory. Call NewEmptyString entry point.
3913 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3914 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3915 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3916 __ call(Address(temp, code_offset.SizeValue()));
3917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3918 } else {
3919 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3920 instruction,
3921 instruction->GetDexPc(),
3922 nullptr);
3923 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3924 DCHECK(!codegen_->IsLeafMethod());
3925 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003926}
3927
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003928void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3929 LocationSummary* locations =
3930 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3931 InvokeRuntimeCallingConvention calling_convention;
3932 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003933 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003934 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003935 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003936}
3937
3938void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3939 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003940 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3941 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003942 // Note: if heap poisoning is enabled, the entry point takes cares
3943 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003944 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3945 instruction,
3946 instruction->GetDexPc(),
3947 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003948 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003949
3950 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003951}
3952
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003953void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003954 LocationSummary* locations =
3955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003956 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3957 if (location.IsStackSlot()) {
3958 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3959 } else if (location.IsDoubleStackSlot()) {
3960 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3961 }
3962 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003963}
3964
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003965void InstructionCodeGeneratorX86_64::VisitParameterValue(
3966 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003967 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003968}
3969
3970void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3971 LocationSummary* locations =
3972 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3973 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3974}
3975
3976void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3977 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3978 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003979}
3980
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003981void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3982 LocationSummary* locations =
3983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3984 locations->SetInAt(0, Location::RequiresRegister());
3985 locations->SetOut(Location::RequiresRegister());
3986}
3987
3988void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3989 LocationSummary* locations = instruction->GetLocations();
3990 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003991 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003992 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3993 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
3994 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03003995 __ movq(locations->Out().AsRegister<CpuRegister>(),
3996 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3997 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
3998 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07003999 instruction->GetIndex(), kX86_64PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004000 }
4001 __ movq(locations->Out().AsRegister<CpuRegister>(),
4002 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4003}
4004
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004005void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004006 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004007 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004008 locations->SetInAt(0, Location::RequiresRegister());
4009 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004010}
4011
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004012void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4013 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004014 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4015 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004016 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004017 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004018 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004020 break;
4021
4022 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004024 break;
4025
4026 default:
4027 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4028 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004029}
4030
David Brazdil66d126e2015-04-03 16:02:44 +01004031void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4034 locations->SetInAt(0, Location::RequiresRegister());
4035 locations->SetOut(Location::SameAsFirstInput());
4036}
4037
4038void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004039 LocationSummary* locations = bool_not->GetLocations();
4040 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4041 locations->Out().AsRegister<CpuRegister>().AsRegister());
4042 Location out = locations->Out();
4043 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4044}
4045
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004046void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004047 LocationSummary* locations =
4048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004049 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004050 locations->SetInAt(i, Location::Any());
4051 }
4052 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004053}
4054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004055void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004056 LOG(FATAL) << "Unimplemented";
4057}
4058
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004059void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004060 /*
4061 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004062 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004063 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4064 */
4065 switch (kind) {
4066 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004067 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004068 break;
4069 }
4070 case MemBarrierKind::kAnyStore:
4071 case MemBarrierKind::kLoadAny:
4072 case MemBarrierKind::kStoreStore: {
4073 // nop
4074 break;
4075 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004076 case MemBarrierKind::kNTStoreStore:
4077 // Non-Temporal Store/Store needs an explicit fence.
4078 MemoryFence(/* non-temporal */ true);
4079 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004080 }
4081}
4082
4083void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4084 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4085
Roland Levillain0d5a2812015-11-13 10:07:31 +00004086 bool object_field_get_with_read_barrier =
4087 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004088 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004089 new (GetGraph()->GetArena()) LocationSummary(instruction,
4090 object_field_get_with_read_barrier ?
4091 LocationSummary::kCallOnSlowPath :
4092 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004093 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004094 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4095 locations->SetOut(Location::RequiresFpuRegister());
4096 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004097 // The output overlaps for an object field get when read barriers
4098 // are enabled: we do not want the move to overwrite the object's
4099 // location, as we need it to emit the read barrier.
4100 locations->SetOut(
4101 Location::RequiresRegister(),
4102 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004103 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004104 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4105 // We need a temporary register for the read barrier marking slow
4106 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4107 locations->AddTemp(Location::RequiresRegister());
4108 }
Calin Juravle52c48962014-12-16 17:02:57 +00004109}
4110
4111void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4112 const FieldInfo& field_info) {
4113 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4114
4115 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004116 Location base_loc = locations->InAt(0);
4117 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004118 Location out = locations->Out();
4119 bool is_volatile = field_info.IsVolatile();
4120 Primitive::Type field_type = field_info.GetFieldType();
4121 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4122
4123 switch (field_type) {
4124 case Primitive::kPrimBoolean: {
4125 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4126 break;
4127 }
4128
4129 case Primitive::kPrimByte: {
4130 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4131 break;
4132 }
4133
4134 case Primitive::kPrimShort: {
4135 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4136 break;
4137 }
4138
4139 case Primitive::kPrimChar: {
4140 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4141 break;
4142 }
4143
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004144 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004145 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4146 break;
4147 }
4148
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004149 case Primitive::kPrimNot: {
4150 // /* HeapReference<Object> */ out = *(base + offset)
4151 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4152 Location temp_loc = locations->GetTemp(0);
4153 // Note that a potential implicit null check is handled in this
4154 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4155 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4156 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4157 if (is_volatile) {
4158 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4159 }
4160 } else {
4161 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4162 codegen_->MaybeRecordImplicitNullCheck(instruction);
4163 if (is_volatile) {
4164 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4165 }
4166 // If read barriers are enabled, emit read barriers other than
4167 // Baker's using a slow path (and also unpoison the loaded
4168 // reference, if heap poisoning is enabled).
4169 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4170 }
4171 break;
4172 }
4173
Calin Juravle52c48962014-12-16 17:02:57 +00004174 case Primitive::kPrimLong: {
4175 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4176 break;
4177 }
4178
4179 case Primitive::kPrimFloat: {
4180 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4181 break;
4182 }
4183
4184 case Primitive::kPrimDouble: {
4185 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4186 break;
4187 }
4188
4189 case Primitive::kPrimVoid:
4190 LOG(FATAL) << "Unreachable type " << field_type;
4191 UNREACHABLE();
4192 }
4193
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004194 if (field_type == Primitive::kPrimNot) {
4195 // Potential implicit null checks, in the case of reference
4196 // fields, are handled in the previous switch statement.
4197 } else {
4198 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004199 }
Roland Levillain4d027112015-07-01 15:41:14 +01004200
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004201 if (is_volatile) {
4202 if (field_type == Primitive::kPrimNot) {
4203 // Memory barriers, in the case of references, are also handled
4204 // in the previous switch statement.
4205 } else {
4206 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4207 }
Roland Levillain4d027112015-07-01 15:41:14 +01004208 }
Calin Juravle52c48962014-12-16 17:02:57 +00004209}
4210
4211void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4212 const FieldInfo& field_info) {
4213 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4214
4215 LocationSummary* locations =
4216 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004217 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004218 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004219 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004220 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004221
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004222 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004223 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004224 if (is_volatile) {
4225 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4226 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4227 } else {
4228 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4229 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004230 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004231 if (is_volatile) {
4232 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4233 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4234 } else {
4235 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4236 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004237 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004238 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004239 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004240 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004241 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004242 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4243 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004244 locations->AddTemp(Location::RequiresRegister());
4245 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246}
4247
Calin Juravle52c48962014-12-16 17:02:57 +00004248void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004249 const FieldInfo& field_info,
4250 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4252
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004254 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4255 Location value = locations->InAt(1);
4256 bool is_volatile = field_info.IsVolatile();
4257 Primitive::Type field_type = field_info.GetFieldType();
4258 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4259
4260 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004261 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004262 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004263
Mark Mendellea5af682015-10-22 17:35:49 -04004264 bool maybe_record_implicit_null_check_done = false;
4265
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004266 switch (field_type) {
4267 case Primitive::kPrimBoolean:
4268 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004269 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004270 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004271 __ movb(Address(base, offset), Immediate(v));
4272 } else {
4273 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275 break;
4276 }
4277
4278 case Primitive::kPrimShort:
4279 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004280 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004281 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004282 __ movw(Address(base, offset), Immediate(v));
4283 } else {
4284 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4285 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286 break;
4287 }
4288
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004291 if (value.IsConstant()) {
4292 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004293 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4294 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4295 // Note: if heap poisoning is enabled, no need to poison
4296 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004297 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004298 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004299 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4300 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4301 __ movl(temp, value.AsRegister<CpuRegister>());
4302 __ PoisonHeapReference(temp);
4303 __ movl(Address(base, offset), temp);
4304 } else {
4305 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4306 }
Mark Mendell40741f32015-04-20 22:10:34 -04004307 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308 break;
4309 }
4310
4311 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004312 if (value.IsConstant()) {
4313 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004314 codegen_->MoveInt64ToAddress(Address(base, offset),
4315 Address(base, offset + sizeof(int32_t)),
4316 v,
4317 instruction);
4318 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004319 } else {
4320 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4321 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 break;
4323 }
4324
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004325 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004326 if (value.IsConstant()) {
4327 int32_t v =
4328 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4329 __ movl(Address(base, offset), Immediate(v));
4330 } else {
4331 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4332 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004333 break;
4334 }
4335
4336 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004337 if (value.IsConstant()) {
4338 int64_t v =
4339 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4340 codegen_->MoveInt64ToAddress(Address(base, offset),
4341 Address(base, offset + sizeof(int32_t)),
4342 v,
4343 instruction);
4344 maybe_record_implicit_null_check_done = true;
4345 } else {
4346 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4347 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004348 break;
4349 }
4350
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004351 case Primitive::kPrimVoid:
4352 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004353 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354 }
Calin Juravle52c48962014-12-16 17:02:57 +00004355
Mark Mendellea5af682015-10-22 17:35:49 -04004356 if (!maybe_record_implicit_null_check_done) {
4357 codegen_->MaybeRecordImplicitNullCheck(instruction);
4358 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004359
4360 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4361 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4362 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004363 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004364 }
4365
Calin Juravle52c48962014-12-16 17:02:57 +00004366 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004367 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004368 }
4369}
4370
4371void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4372 HandleFieldSet(instruction, instruction->GetFieldInfo());
4373}
4374
4375void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004376 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004377}
4378
4379void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004380 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004381}
4382
4383void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004384 HandleFieldGet(instruction, instruction->GetFieldInfo());
4385}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004386
Calin Juravle52c48962014-12-16 17:02:57 +00004387void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4388 HandleFieldGet(instruction);
4389}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390
Calin Juravle52c48962014-12-16 17:02:57 +00004391void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4392 HandleFieldGet(instruction, instruction->GetFieldInfo());
4393}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394
Calin Juravle52c48962014-12-16 17:02:57 +00004395void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4396 HandleFieldSet(instruction, instruction->GetFieldInfo());
4397}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004398
Calin Juravle52c48962014-12-16 17:02:57 +00004399void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004400 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401}
4402
Calin Juravlee460d1d2015-09-29 04:52:17 +01004403void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4404 HUnresolvedInstanceFieldGet* instruction) {
4405 FieldAccessCallingConventionX86_64 calling_convention;
4406 codegen_->CreateUnresolvedFieldLocationSummary(
4407 instruction, instruction->GetFieldType(), calling_convention);
4408}
4409
4410void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4411 HUnresolvedInstanceFieldGet* instruction) {
4412 FieldAccessCallingConventionX86_64 calling_convention;
4413 codegen_->GenerateUnresolvedFieldAccess(instruction,
4414 instruction->GetFieldType(),
4415 instruction->GetFieldIndex(),
4416 instruction->GetDexPc(),
4417 calling_convention);
4418}
4419
4420void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4421 HUnresolvedInstanceFieldSet* instruction) {
4422 FieldAccessCallingConventionX86_64 calling_convention;
4423 codegen_->CreateUnresolvedFieldLocationSummary(
4424 instruction, instruction->GetFieldType(), calling_convention);
4425}
4426
4427void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4428 HUnresolvedInstanceFieldSet* instruction) {
4429 FieldAccessCallingConventionX86_64 calling_convention;
4430 codegen_->GenerateUnresolvedFieldAccess(instruction,
4431 instruction->GetFieldType(),
4432 instruction->GetFieldIndex(),
4433 instruction->GetDexPc(),
4434 calling_convention);
4435}
4436
4437void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4438 HUnresolvedStaticFieldGet* instruction) {
4439 FieldAccessCallingConventionX86_64 calling_convention;
4440 codegen_->CreateUnresolvedFieldLocationSummary(
4441 instruction, instruction->GetFieldType(), calling_convention);
4442}
4443
4444void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4445 HUnresolvedStaticFieldGet* instruction) {
4446 FieldAccessCallingConventionX86_64 calling_convention;
4447 codegen_->GenerateUnresolvedFieldAccess(instruction,
4448 instruction->GetFieldType(),
4449 instruction->GetFieldIndex(),
4450 instruction->GetDexPc(),
4451 calling_convention);
4452}
4453
4454void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4455 HUnresolvedStaticFieldSet* instruction) {
4456 FieldAccessCallingConventionX86_64 calling_convention;
4457 codegen_->CreateUnresolvedFieldLocationSummary(
4458 instruction, instruction->GetFieldType(), calling_convention);
4459}
4460
4461void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4462 HUnresolvedStaticFieldSet* instruction) {
4463 FieldAccessCallingConventionX86_64 calling_convention;
4464 codegen_->GenerateUnresolvedFieldAccess(instruction,
4465 instruction->GetFieldType(),
4466 instruction->GetFieldIndex(),
4467 instruction->GetDexPc(),
4468 calling_convention);
4469}
4470
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004472 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4473 ? LocationSummary::kCallOnSlowPath
4474 : LocationSummary::kNoCall;
4475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4476 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004477 ? Location::RequiresRegister()
4478 : Location::Any();
4479 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004480 if (instruction->HasUses()) {
4481 locations->SetOut(Location::SameAsFirstInput());
4482 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483}
4484
Calin Juravle2ae48182016-03-16 14:05:09 +00004485void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4486 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004487 return;
4488 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004489 LocationSummary* locations = instruction->GetLocations();
4490 Location obj = locations->InAt(0);
4491
4492 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004493 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004494}
4495
Calin Juravle2ae48182016-03-16 14:05:09 +00004496void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004497 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004498 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004499
4500 LocationSummary* locations = instruction->GetLocations();
4501 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502
4503 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004504 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004505 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004507 } else {
4508 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004509 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004510 __ jmp(slow_path->GetEntryLabel());
4511 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004512 }
4513 __ j(kEqual, slow_path->GetEntryLabel());
4514}
4515
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004516void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004517 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004518}
4519
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004520void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004521 bool object_array_get_with_read_barrier =
4522 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004523 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004524 new (GetGraph()->GetArena()) LocationSummary(instruction,
4525 object_array_get_with_read_barrier ?
4526 LocationSummary::kCallOnSlowPath :
4527 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004528 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004529 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004530 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4531 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4532 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004533 // The output overlaps for an object array get when read barriers
4534 // are enabled: we do not want the move to overwrite the array's
4535 // location, as we need it to emit the read barrier.
4536 locations->SetOut(
4537 Location::RequiresRegister(),
4538 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004539 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004540 // We need a temporary register for the read barrier marking slow
4541 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4542 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4543 locations->AddTemp(Location::RequiresRegister());
4544 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545}
4546
4547void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4548 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004549 Location obj_loc = locations->InAt(0);
4550 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004551 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004552 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004553
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004554 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004555 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004556 case Primitive::kPrimBoolean: {
4557 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004558 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559 if (index.IsConstant()) {
4560 __ movzxb(out, Address(obj,
4561 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4562 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004563 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004564 }
4565 break;
4566 }
4567
4568 case Primitive::kPrimByte: {
4569 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004570 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571 if (index.IsConstant()) {
4572 __ movsxb(out, Address(obj,
4573 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4574 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004575 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 }
4577 break;
4578 }
4579
4580 case Primitive::kPrimShort: {
4581 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004582 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 if (index.IsConstant()) {
4584 __ movsxw(out, Address(obj,
4585 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4586 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004587 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 }
4589 break;
4590 }
4591
4592 case Primitive::kPrimChar: {
4593 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004594 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595 if (index.IsConstant()) {
4596 __ movzxw(out, Address(obj,
4597 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4598 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004599 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 }
4601 break;
4602 }
4603
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004604 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004606 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 if (index.IsConstant()) {
4608 __ movl(out, Address(obj,
4609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4610 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004611 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004612 }
4613 break;
4614 }
4615
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004616 case Primitive::kPrimNot: {
4617 static_assert(
4618 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4619 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4620 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4621 // /* HeapReference<Object> */ out =
4622 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4623 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4624 Location temp = locations->GetTemp(0);
4625 // Note that a potential implicit null check is handled in this
4626 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4627 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4628 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4629 } else {
4630 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4631 if (index.IsConstant()) {
4632 uint32_t offset =
4633 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4634 __ movl(out, Address(obj, offset));
4635 codegen_->MaybeRecordImplicitNullCheck(instruction);
4636 // If read barriers are enabled, emit read barriers other than
4637 // Baker's using a slow path (and also unpoison the loaded
4638 // reference, if heap poisoning is enabled).
4639 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4640 } else {
4641 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4642 codegen_->MaybeRecordImplicitNullCheck(instruction);
4643 // If read barriers are enabled, emit read barriers other than
4644 // Baker's using a slow path (and also unpoison the loaded
4645 // reference, if heap poisoning is enabled).
4646 codegen_->MaybeGenerateReadBarrierSlow(
4647 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4648 }
4649 }
4650 break;
4651 }
4652
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653 case Primitive::kPrimLong: {
4654 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004655 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 if (index.IsConstant()) {
4657 __ movq(out, Address(obj,
4658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4659 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004660 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 }
4662 break;
4663 }
4664
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004665 case Primitive::kPrimFloat: {
4666 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004667 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004668 if (index.IsConstant()) {
4669 __ movss(out, Address(obj,
4670 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4671 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004672 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004673 }
4674 break;
4675 }
4676
4677 case Primitive::kPrimDouble: {
4678 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004679 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004680 if (index.IsConstant()) {
4681 __ movsd(out, Address(obj,
4682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4683 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004684 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004685 }
4686 break;
4687 }
4688
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004690 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004691 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692 }
Roland Levillain4d027112015-07-01 15:41:14 +01004693
4694 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004695 // Potential implicit null checks, in the case of reference
4696 // arrays, are handled in the previous switch statement.
4697 } else {
4698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004699 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700}
4701
4702void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004703 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004704
4705 bool needs_write_barrier =
4706 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004707 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004708 bool object_array_set_with_read_barrier =
4709 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004710
Nicolas Geoffray39468442014-09-02 15:17:15 +01004711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004712 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004713 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004714 LocationSummary::kCallOnSlowPath :
4715 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004716
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004717 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004718 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4719 if (Primitive::IsFloatingPointType(value_type)) {
4720 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004721 } else {
4722 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4723 }
4724
4725 if (needs_write_barrier) {
4726 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004727
4728 // This first temporary register is possibly used for heap
4729 // reference poisoning and/or read barrier emission too.
4730 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004731 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733}
4734
4735void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4736 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004737 Location array_loc = locations->InAt(0);
4738 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004740 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004741 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004742 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004743 bool needs_write_barrier =
4744 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4746 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4747 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004748
4749 switch (value_type) {
4750 case Primitive::kPrimBoolean:
4751 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004752 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4753 Address address = index.IsConstant()
4754 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4755 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4756 if (value.IsRegister()) {
4757 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004761 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 break;
4763 }
4764
4765 case Primitive::kPrimShort:
4766 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004767 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4768 Address address = index.IsConstant()
4769 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4770 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4771 if (value.IsRegister()) {
4772 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774 DCHECK(value.IsConstant()) << value;
4775 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004776 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004777 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004778 break;
4779 }
4780
4781 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004782 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4783 Address address = index.IsConstant()
4784 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4785 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004786
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787 if (!value.IsRegister()) {
4788 // Just setting null.
4789 DCHECK(instruction->InputAt(2)->IsNullConstant());
4790 DCHECK(value.IsConstant()) << value;
4791 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004792 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004794 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004795 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004796 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797
4798 DCHECK(needs_write_barrier);
4799 CpuRegister register_value = value.AsRegister<CpuRegister>();
4800 NearLabel done, not_null, do_put;
4801 SlowPathCode* slow_path = nullptr;
4802 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004803 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4805 codegen_->AddSlowPath(slow_path);
4806 if (instruction->GetValueCanBeNull()) {
4807 __ testl(register_value, register_value);
4808 __ j(kNotEqual, &not_null);
4809 __ movl(address, Immediate(0));
4810 codegen_->MaybeRecordImplicitNullCheck(instruction);
4811 __ jmp(&done);
4812 __ Bind(&not_null);
4813 }
4814
Roland Levillain0d5a2812015-11-13 10:07:31 +00004815 if (kEmitCompilerReadBarrier) {
4816 // When read barriers are enabled, the type checking
4817 // instrumentation requires two read barriers:
4818 //
4819 // __ movl(temp2, temp);
4820 // // /* HeapReference<Class> */ temp = temp->component_type_
4821 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004822 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004823 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4824 //
4825 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4826 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004827 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004828 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4829 //
4830 // __ cmpl(temp, temp2);
4831 //
4832 // However, the second read barrier may trash `temp`, as it
4833 // is a temporary register, and as such would not be saved
4834 // along with live registers before calling the runtime (nor
4835 // restored afterwards). So in this case, we bail out and
4836 // delegate the work to the array set slow path.
4837 //
4838 // TODO: Extend the register allocator to support a new
4839 // "(locally) live temp" location so as to avoid always
4840 // going into the slow path when read barriers are enabled.
4841 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004842 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004843 // /* HeapReference<Class> */ temp = array->klass_
4844 __ movl(temp, Address(array, class_offset));
4845 codegen_->MaybeRecordImplicitNullCheck(instruction);
4846 __ MaybeUnpoisonHeapReference(temp);
4847
4848 // /* HeapReference<Class> */ temp = temp->component_type_
4849 __ movl(temp, Address(temp, component_offset));
4850 // If heap poisoning is enabled, no need to unpoison `temp`
4851 // nor the object reference in `register_value->klass`, as
4852 // we are comparing two poisoned references.
4853 __ cmpl(temp, Address(register_value, class_offset));
4854
4855 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4856 __ j(kEqual, &do_put);
4857 // If heap poisoning is enabled, the `temp` reference has
4858 // not been unpoisoned yet; unpoison it now.
4859 __ MaybeUnpoisonHeapReference(temp);
4860
4861 // /* HeapReference<Class> */ temp = temp->super_class_
4862 __ movl(temp, Address(temp, super_offset));
4863 // If heap poisoning is enabled, no need to unpoison
4864 // `temp`, as we are comparing against null below.
4865 __ testl(temp, temp);
4866 __ j(kNotEqual, slow_path->GetEntryLabel());
4867 __ Bind(&do_put);
4868 } else {
4869 __ j(kNotEqual, slow_path->GetEntryLabel());
4870 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004871 }
4872 }
4873
4874 if (kPoisonHeapReferences) {
4875 __ movl(temp, register_value);
4876 __ PoisonHeapReference(temp);
4877 __ movl(address, temp);
4878 } else {
4879 __ movl(address, register_value);
4880 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004881 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 codegen_->MaybeRecordImplicitNullCheck(instruction);
4883 }
4884
4885 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4886 codegen_->MarkGCCard(
4887 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4888 __ Bind(&done);
4889
4890 if (slow_path != nullptr) {
4891 __ Bind(slow_path->GetExitLabel());
4892 }
4893
4894 break;
4895 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004896
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004897 case Primitive::kPrimInt: {
4898 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4899 Address address = index.IsConstant()
4900 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4901 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4902 if (value.IsRegister()) {
4903 __ movl(address, value.AsRegister<CpuRegister>());
4904 } else {
4905 DCHECK(value.IsConstant()) << value;
4906 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4907 __ movl(address, Immediate(v));
4908 }
4909 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 break;
4911 }
4912
4913 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004914 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4915 Address address = index.IsConstant()
4916 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4917 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4918 if (value.IsRegister()) {
4919 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004920 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004922 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004923 Address address_high = index.IsConstant()
4924 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4925 offset + sizeof(int32_t))
4926 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4927 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 }
4929 break;
4930 }
4931
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004932 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004933 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4934 Address address = index.IsConstant()
4935 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4936 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004937 if (value.IsFpuRegister()) {
4938 __ movss(address, value.AsFpuRegister<XmmRegister>());
4939 } else {
4940 DCHECK(value.IsConstant());
4941 int32_t v =
4942 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4943 __ movl(address, Immediate(v));
4944 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004945 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004946 break;
4947 }
4948
4949 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004950 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4951 Address address = index.IsConstant()
4952 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4953 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004954 if (value.IsFpuRegister()) {
4955 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4956 codegen_->MaybeRecordImplicitNullCheck(instruction);
4957 } else {
4958 int64_t v =
4959 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4960 Address address_high = index.IsConstant()
4961 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4962 offset + sizeof(int32_t))
4963 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4964 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4965 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004966 break;
4967 }
4968
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969 case Primitive::kPrimVoid:
4970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004971 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004972 }
4973}
4974
4975void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004976 LocationSummary* locations =
4977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004978 locations->SetInAt(0, Location::RequiresRegister());
4979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004980}
4981
4982void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4983 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004984 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004985 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4986 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004987 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004988 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004989}
4990
4991void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004992 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4993 ? LocationSummary::kCallOnSlowPath
4994 : LocationSummary::kNoCall;
4995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004996 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004997 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004998 if (instruction->HasUses()) {
4999 locations->SetOut(Location::SameAsFirstInput());
5000 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001}
5002
5003void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5004 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005005 Location index_loc = locations->InAt(0);
5006 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005007 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005008 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009
Mark Mendell99dbd682015-04-22 16:18:52 -04005010 if (length_loc.IsConstant()) {
5011 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5012 if (index_loc.IsConstant()) {
5013 // BCE will remove the bounds check if we are guarenteed to pass.
5014 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5015 if (index < 0 || index >= length) {
5016 codegen_->AddSlowPath(slow_path);
5017 __ jmp(slow_path->GetEntryLabel());
5018 } else {
5019 // Some optimization after BCE may have generated this, and we should not
5020 // generate a bounds check if it is a valid range.
5021 }
5022 return;
5023 }
5024
5025 // We have to reverse the jump condition because the length is the constant.
5026 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5027 __ cmpl(index_reg, Immediate(length));
5028 codegen_->AddSlowPath(slow_path);
5029 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005030 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005031 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5032 if (index_loc.IsConstant()) {
5033 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5034 __ cmpl(length, Immediate(value));
5035 } else {
5036 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5037 }
5038 codegen_->AddSlowPath(slow_path);
5039 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041}
5042
5043void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5044 CpuRegister card,
5045 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005046 CpuRegister value,
5047 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005048 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005049 if (value_can_be_null) {
5050 __ testl(value, value);
5051 __ j(kEqual, &is_null);
5052 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005053 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5054 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055 __ movq(temp, object);
5056 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005057 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005058 if (value_can_be_null) {
5059 __ Bind(&is_null);
5060 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061}
5062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005063void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005064 LOG(FATAL) << "Unimplemented";
5065}
5066
5067void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005068 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5069}
5070
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005071void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5072 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5073}
5074
5075void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005076 HBasicBlock* block = instruction->GetBlock();
5077 if (block->GetLoopInformation() != nullptr) {
5078 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5079 // The back edge will generate the suspend check.
5080 return;
5081 }
5082 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5083 // The goto will generate the suspend check.
5084 return;
5085 }
5086 GenerateSuspendCheck(instruction, nullptr);
5087}
5088
5089void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5090 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005091 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005092 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5093 if (slow_path == nullptr) {
5094 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5095 instruction->SetSlowPath(slow_path);
5096 codegen_->AddSlowPath(slow_path);
5097 if (successor != nullptr) {
5098 DCHECK(successor->IsLoopHeader());
5099 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5100 }
5101 } else {
5102 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5103 }
5104
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005105 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5106 /* no_rip */ true),
5107 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005108 if (successor == nullptr) {
5109 __ j(kNotEqual, slow_path->GetEntryLabel());
5110 __ Bind(slow_path->GetReturnLabel());
5111 } else {
5112 __ j(kEqual, codegen_->GetLabelOf(successor));
5113 __ jmp(slow_path->GetEntryLabel());
5114 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005115}
5116
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005117X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5118 return codegen_->GetAssembler();
5119}
5120
5121void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005122 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005123 Location source = move->GetSource();
5124 Location destination = move->GetDestination();
5125
5126 if (source.IsRegister()) {
5127 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005128 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005129 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005130 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005132 } else {
5133 DCHECK(destination.IsDoubleStackSlot());
5134 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005135 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005136 }
5137 } else if (source.IsStackSlot()) {
5138 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005140 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005141 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005143 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005144 } else {
5145 DCHECK(destination.IsStackSlot());
5146 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5147 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5148 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005149 } else if (source.IsDoubleStackSlot()) {
5150 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005152 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005153 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005154 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5155 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005156 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005157 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005158 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5159 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5160 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005161 } else if (source.IsConstant()) {
5162 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005163 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5164 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005165 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005166 if (value == 0) {
5167 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5168 } else {
5169 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5170 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005171 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005172 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005173 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005174 }
5175 } else if (constant->IsLongConstant()) {
5176 int64_t value = constant->AsLongConstant()->GetValue();
5177 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005178 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005179 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005180 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005181 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005182 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005183 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005184 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005185 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005186 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005187 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005188 } else {
5189 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005190 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005191 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5192 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005193 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005194 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005195 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005196 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005198 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005199 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 } else {
5201 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005202 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005204 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 } else if (source.IsFpuRegister()) {
5206 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005208 } else if (destination.IsStackSlot()) {
5209 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005212 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005213 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005216 }
5217}
5218
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005219void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005221 __ movl(Address(CpuRegister(RSP), mem), reg);
5222 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005223}
5224
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005225void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005226 ScratchRegisterScope ensure_scratch(
5227 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5228
5229 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5230 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5231 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5232 Address(CpuRegister(RSP), mem2 + stack_offset));
5233 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5234 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5235 CpuRegister(ensure_scratch.GetRegister()));
5236}
5237
Mark Mendell8a1c7282015-06-29 15:41:28 -04005238void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5239 __ movq(CpuRegister(TMP), reg1);
5240 __ movq(reg1, reg2);
5241 __ movq(reg2, CpuRegister(TMP));
5242}
5243
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005244void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5245 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5246 __ movq(Address(CpuRegister(RSP), mem), reg);
5247 __ movq(reg, CpuRegister(TMP));
5248}
5249
5250void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5251 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005252 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005253
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005254 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5255 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5256 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5257 Address(CpuRegister(RSP), mem2 + stack_offset));
5258 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5259 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5260 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005261}
5262
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005263void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5264 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5265 __ movss(Address(CpuRegister(RSP), mem), reg);
5266 __ movd(reg, CpuRegister(TMP));
5267}
5268
5269void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5270 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5271 __ movsd(Address(CpuRegister(RSP), mem), reg);
5272 __ movd(reg, CpuRegister(TMP));
5273}
5274
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005275void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005276 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005277 Location source = move->GetSource();
5278 Location destination = move->GetDestination();
5279
5280 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005281 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005282 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005283 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005284 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005286 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005287 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5288 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005289 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005290 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005291 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005292 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5293 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5296 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5297 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005301 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005305 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005306 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005307 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005308 }
5309}
5310
5311
5312void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5313 __ pushq(CpuRegister(reg));
5314}
5315
5316
5317void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5318 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005319}
5320
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005321void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005322 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005323 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5324 Immediate(mirror::Class::kStatusInitialized));
5325 __ j(kLess, slow_path->GetEntryLabel());
5326 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005327 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005328}
5329
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005330void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005331 InvokeRuntimeCallingConvention calling_convention;
5332 CodeGenerator::CreateLoadClassLocationSummary(
5333 cls,
5334 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335 Location::RegisterLocation(RAX),
5336 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005337}
5338
5339void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005340 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005341 if (cls->NeedsAccessCheck()) {
5342 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5343 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5344 cls,
5345 cls->GetDexPc(),
5346 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005347 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005348 return;
5349 }
5350
Roland Levillain0d5a2812015-11-13 10:07:31 +00005351 Location out_loc = locations->Out();
5352 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005353 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354
Calin Juravle580b6092015-10-06 17:35:58 +01005355 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005356 DCHECK(!cls->CanCallRuntime());
5357 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005358 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5359 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005360 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005361 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005362 // /* GcRoot<mirror::Class>[] */ out =
5363 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5364 __ movq(out, Address(current_method,
5365 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005366 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005367 GenerateGcRootFieldLoad(
5368 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01005369
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005370 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5371 DCHECK(cls->CanCallRuntime());
5372 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5373 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5374 codegen_->AddSlowPath(slow_path);
5375 if (!cls->IsInDexCache()) {
5376 __ testl(out, out);
5377 __ j(kEqual, slow_path->GetEntryLabel());
5378 }
5379 if (cls->MustGenerateClinitCheck()) {
5380 GenerateClassInitializationCheck(slow_path, out);
5381 } else {
5382 __ Bind(slow_path->GetExitLabel());
5383 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005384 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385 }
5386}
5387
5388void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5389 LocationSummary* locations =
5390 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5391 locations->SetInAt(0, Location::RequiresRegister());
5392 if (check->HasUses()) {
5393 locations->SetOut(Location::SameAsFirstInput());
5394 }
5395}
5396
5397void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005398 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005399 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005400 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005401 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005402 GenerateClassInitializationCheck(slow_path,
5403 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005404}
5405
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005406HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5407 HLoadString::LoadKind desired_string_load_kind) {
5408 if (kEmitCompilerReadBarrier) {
5409 switch (desired_string_load_kind) {
5410 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5411 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5412 case HLoadString::LoadKind::kBootImageAddress:
5413 // TODO: Implement for read barrier.
5414 return HLoadString::LoadKind::kDexCacheViaMethod;
5415 default:
5416 break;
5417 }
5418 }
5419 switch (desired_string_load_kind) {
5420 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5421 DCHECK(!GetCompilerOptions().GetCompilePic());
5422 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5423 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5424 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5425 DCHECK(GetCompilerOptions().GetCompilePic());
5426 break;
5427 case HLoadString::LoadKind::kBootImageAddress:
5428 break;
5429 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005430 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005431 break;
5432 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005433 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005434 break;
5435 case HLoadString::LoadKind::kDexCacheViaMethod:
5436 break;
5437 }
5438 return desired_string_load_kind;
5439}
5440
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005441void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005442 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005443 ? LocationSummary::kCallOnSlowPath
5444 : LocationSummary::kNoCall;
5445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005446 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5447 locations->SetInAt(0, Location::RequiresRegister());
5448 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005449 locations->SetOut(Location::RequiresRegister());
5450}
5451
5452void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005453 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005454 Location out_loc = locations->Out();
5455 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005456
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005457 switch (load->GetLoadKind()) {
5458 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5459 DCHECK(!kEmitCompilerReadBarrier);
5460 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5461 codegen_->RecordStringPatch(load);
5462 return; // No dex cache slow path.
5463 }
5464 case HLoadString::LoadKind::kBootImageAddress: {
5465 DCHECK(!kEmitCompilerReadBarrier);
5466 DCHECK_NE(load->GetAddress(), 0u);
5467 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5468 __ movl(out, Immediate(address)); // Zero-extended.
5469 codegen_->RecordSimplePatch();
5470 return; // No dex cache slow path.
5471 }
5472 case HLoadString::LoadKind::kDexCacheAddress: {
5473 DCHECK_NE(load->GetAddress(), 0u);
5474 if (IsUint<32>(load->GetAddress())) {
5475 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5476 GenerateGcRootFieldLoad(load, out_loc, address);
5477 } else {
5478 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5479 __ movq(out, Immediate(load->GetAddress()));
5480 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5481 }
5482 break;
5483 }
5484 case HLoadString::LoadKind::kDexCachePcRelative: {
5485 uint32_t offset = load->GetDexCacheElementOffset();
5486 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5487 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5488 /* no_rip */ false);
5489 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5490 break;
5491 }
5492 case HLoadString::LoadKind::kDexCacheViaMethod: {
5493 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5494
5495 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5496 GenerateGcRootFieldLoad(
5497 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5498 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5499 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5500 // /* GcRoot<mirror::String> */ out = out[string_index]
5501 GenerateGcRootFieldLoad(
5502 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5503 break;
5504 }
5505 default:
5506 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5507 UNREACHABLE();
5508 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005509
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005510 if (!load->IsInDexCache()) {
5511 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5512 codegen_->AddSlowPath(slow_path);
5513 __ testl(out, out);
5514 __ j(kEqual, slow_path->GetEntryLabel());
5515 __ Bind(slow_path->GetExitLabel());
5516 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005517}
5518
David Brazdilcb1c0552015-08-04 16:22:25 +01005519static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005520 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5521 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005522}
5523
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005524void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5525 LocationSummary* locations =
5526 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5527 locations->SetOut(Location::RequiresRegister());
5528}
5529
5530void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005531 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5532}
5533
5534void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5535 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5536}
5537
5538void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5539 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005540}
5541
5542void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5543 LocationSummary* locations =
5544 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5545 InvokeRuntimeCallingConvention calling_convention;
5546 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5547}
5548
5549void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005550 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5551 instruction,
5552 instruction->GetDexPc(),
5553 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005554 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005555}
5556
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005557static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5558 return kEmitCompilerReadBarrier &&
5559 (kUseBakerReadBarrier ||
5560 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5561 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5562 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5563}
5564
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005565void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005566 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5568 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 case TypeCheckKind::kExactCheck:
5570 case TypeCheckKind::kAbstractClassCheck:
5571 case TypeCheckKind::kClassHierarchyCheck:
5572 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005573 call_kind =
5574 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005575 break;
5576 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005577 case TypeCheckKind::kUnresolvedCheck:
5578 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005579 call_kind = LocationSummary::kCallOnSlowPath;
5580 break;
5581 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005582
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005584 locations->SetInAt(0, Location::RequiresRegister());
5585 locations->SetInAt(1, Location::Any());
5586 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5587 locations->SetOut(Location::RequiresRegister());
5588 // When read barriers are enabled, we need a temporary register for
5589 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005590 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005591 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005592 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005593}
5594
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005595void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005596 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005597 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005598 Location obj_loc = locations->InAt(0);
5599 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005600 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005601 Location out_loc = locations->Out();
5602 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005603 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005604 locations->GetTemp(0) :
5605 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005606 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005607 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5608 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5609 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005610 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005611 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005612
5613 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005614 // Avoid null check if we know obj is not null.
5615 if (instruction->MustDoNullCheck()) {
5616 __ testl(obj, obj);
5617 __ j(kEqual, &zero);
5618 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005621 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005623 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005624 case TypeCheckKind::kExactCheck: {
5625 if (cls.IsRegister()) {
5626 __ cmpl(out, cls.AsRegister<CpuRegister>());
5627 } else {
5628 DCHECK(cls.IsStackSlot()) << cls;
5629 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5630 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005631 if (zero.IsLinked()) {
5632 // Classes must be equal for the instanceof to succeed.
5633 __ j(kNotEqual, &zero);
5634 __ movl(out, Immediate(1));
5635 __ jmp(&done);
5636 } else {
5637 __ setcc(kEqual, out);
5638 // setcc only sets the low byte.
5639 __ andl(out, Immediate(1));
5640 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 break;
5642 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kAbstractClassCheck: {
5645 // If the class is abstract, we eagerly fetch the super class of the
5646 // object to avoid doing a comparison we know will fail.
5647 NearLabel loop, success;
5648 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005649 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005650 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005651 __ testl(out, out);
5652 // If `out` is null, we use it for the result, and jump to `done`.
5653 __ j(kEqual, &done);
5654 if (cls.IsRegister()) {
5655 __ cmpl(out, cls.AsRegister<CpuRegister>());
5656 } else {
5657 DCHECK(cls.IsStackSlot()) << cls;
5658 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5659 }
5660 __ j(kNotEqual, &loop);
5661 __ movl(out, Immediate(1));
5662 if (zero.IsLinked()) {
5663 __ jmp(&done);
5664 }
5665 break;
5666 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005667
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005668 case TypeCheckKind::kClassHierarchyCheck: {
5669 // Walk over the class hierarchy to find a match.
5670 NearLabel loop, success;
5671 __ Bind(&loop);
5672 if (cls.IsRegister()) {
5673 __ cmpl(out, cls.AsRegister<CpuRegister>());
5674 } else {
5675 DCHECK(cls.IsStackSlot()) << cls;
5676 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5677 }
5678 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005679 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005680 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 __ testl(out, out);
5682 __ j(kNotEqual, &loop);
5683 // If `out` is null, we use it for the result, and jump to `done`.
5684 __ jmp(&done);
5685 __ Bind(&success);
5686 __ movl(out, Immediate(1));
5687 if (zero.IsLinked()) {
5688 __ jmp(&done);
5689 }
5690 break;
5691 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005692
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005693 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005694 // Do an exact check.
5695 NearLabel exact_check;
5696 if (cls.IsRegister()) {
5697 __ cmpl(out, cls.AsRegister<CpuRegister>());
5698 } else {
5699 DCHECK(cls.IsStackSlot()) << cls;
5700 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5701 }
5702 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005703 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005704 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005705 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005706 __ testl(out, out);
5707 // If `out` is null, we use it for the result, and jump to `done`.
5708 __ j(kEqual, &done);
5709 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5710 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005711 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005712 __ movl(out, Immediate(1));
5713 __ jmp(&done);
5714 break;
5715 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005716
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717 case TypeCheckKind::kArrayCheck: {
5718 if (cls.IsRegister()) {
5719 __ cmpl(out, cls.AsRegister<CpuRegister>());
5720 } else {
5721 DCHECK(cls.IsStackSlot()) << cls;
5722 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5723 }
5724 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005725 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5726 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 codegen_->AddSlowPath(slow_path);
5728 __ j(kNotEqual, slow_path->GetEntryLabel());
5729 __ movl(out, Immediate(1));
5730 if (zero.IsLinked()) {
5731 __ jmp(&done);
5732 }
5733 break;
5734 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735
Calin Juravle98893e12015-10-02 21:05:03 +01005736 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 case TypeCheckKind::kInterfaceCheck: {
5738 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005739 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005740 // cases.
5741 //
5742 // We cannot directly call the InstanceofNonTrivial runtime
5743 // entry point without resorting to a type checking slow path
5744 // here (i.e. by calling InvokeRuntime directly), as it would
5745 // require to assign fixed registers for the inputs of this
5746 // HInstanceOf instruction (following the runtime calling
5747 // convention), which might be cluttered by the potential first
5748 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005749 //
5750 // TODO: Introduce a new runtime entry point taking the object
5751 // to test (instead of its class) as argument, and let it deal
5752 // with the read barrier issues. This will let us refactor this
5753 // case of the `switch` code as it was previously (with a direct
5754 // call to the runtime not using a type checking slow path).
5755 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005756 DCHECK(locations->OnlyCallsOnSlowPath());
5757 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5758 /* is_fatal */ false);
5759 codegen_->AddSlowPath(slow_path);
5760 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 if (zero.IsLinked()) {
5762 __ jmp(&done);
5763 }
5764 break;
5765 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005766 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005769 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005770 __ xorl(out, out);
5771 }
5772
5773 if (done.IsLinked()) {
5774 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005775 }
5776
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005777 if (slow_path != nullptr) {
5778 __ Bind(slow_path->GetExitLabel());
5779 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005780}
5781
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005782void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5784 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5786 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 case TypeCheckKind::kExactCheck:
5788 case TypeCheckKind::kAbstractClassCheck:
5789 case TypeCheckKind::kClassHierarchyCheck:
5790 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005791 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5792 LocationSummary::kCallOnSlowPath :
5793 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 break;
5795 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796 case TypeCheckKind::kUnresolvedCheck:
5797 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005798 call_kind = LocationSummary::kCallOnSlowPath;
5799 break;
5800 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5802 locations->SetInAt(0, Location::RequiresRegister());
5803 locations->SetInAt(1, Location::Any());
5804 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5805 locations->AddTemp(Location::RequiresRegister());
5806 // When read barriers are enabled, we need an additional temporary
5807 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005808 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005810 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005811}
5812
5813void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005814 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005815 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 Location obj_loc = locations->InAt(0);
5817 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005818 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 Location temp_loc = locations->GetTemp(0);
5820 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005821 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005822 locations->GetTemp(1) :
5823 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005824 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5825 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5826 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5827 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828
Roland Levillain0d5a2812015-11-13 10:07:31 +00005829 bool is_type_check_slow_path_fatal =
5830 (type_check_kind == TypeCheckKind::kExactCheck ||
5831 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5832 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5833 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5834 !instruction->CanThrowIntoCatchBlock();
5835 SlowPathCode* type_check_slow_path =
5836 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5837 is_type_check_slow_path_fatal);
5838 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839
Roland Levillain0d5a2812015-11-13 10:07:31 +00005840 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 case TypeCheckKind::kExactCheck:
5842 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005843 NearLabel done;
5844 // Avoid null check if we know obj is not null.
5845 if (instruction->MustDoNullCheck()) {
5846 __ testl(obj, obj);
5847 __ j(kEqual, &done);
5848 }
5849
5850 // /* HeapReference<Class> */ temp = obj->klass_
5851 GenerateReferenceLoadTwoRegisters(
5852 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5853
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005854 if (cls.IsRegister()) {
5855 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5856 } else {
5857 DCHECK(cls.IsStackSlot()) << cls;
5858 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5859 }
5860 // Jump to slow path for throwing the exception or doing a
5861 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005863 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864 break;
5865 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005868 NearLabel done;
5869 // Avoid null check if we know obj is not null.
5870 if (instruction->MustDoNullCheck()) {
5871 __ testl(obj, obj);
5872 __ j(kEqual, &done);
5873 }
5874
5875 // /* HeapReference<Class> */ temp = obj->klass_
5876 GenerateReferenceLoadTwoRegisters(
5877 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5878
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 // If the class is abstract, we eagerly fetch the super class of the
5880 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005884 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005885
5886 // If the class reference currently in `temp` is not null, jump
5887 // to the `compare_classes` label to compare it with the checked
5888 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890 __ j(kNotEqual, &compare_classes);
5891 // Otherwise, jump to the slow path to throw the exception.
5892 //
5893 // But before, move back the object's class into `temp` before
5894 // going into the slow path, as it has been overwritten in the
5895 // meantime.
5896 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005897 GenerateReferenceLoadTwoRegisters(
5898 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 __ jmp(type_check_slow_path->GetEntryLabel());
5900
5901 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 if (cls.IsRegister()) {
5903 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5904 } else {
5905 DCHECK(cls.IsStackSlot()) << cls;
5906 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5907 }
5908 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005909 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 break;
5911 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005914 NearLabel done;
5915 // Avoid null check if we know obj is not null.
5916 if (instruction->MustDoNullCheck()) {
5917 __ testl(obj, obj);
5918 __ j(kEqual, &done);
5919 }
5920
5921 // /* HeapReference<Class> */ temp = obj->klass_
5922 GenerateReferenceLoadTwoRegisters(
5923 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5924
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005926 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 __ Bind(&loop);
5928 if (cls.IsRegister()) {
5929 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5930 } else {
5931 DCHECK(cls.IsStackSlot()) << cls;
5932 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5933 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005934 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005937 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938
5939 // If the class reference currently in `temp` is not null, jump
5940 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 __ testl(temp, temp);
5942 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005943 // Otherwise, jump to the slow path to throw the exception.
5944 //
5945 // But before, move back the object's class into `temp` before
5946 // going into the slow path, as it has been overwritten in the
5947 // meantime.
5948 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005949 GenerateReferenceLoadTwoRegisters(
5950 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005951 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005952 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 break;
5954 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005955
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005957 // We cannot use a NearLabel here, as its range might be too
5958 // short in some cases when read barriers are enabled. This has
5959 // been observed for instance when the code emitted for this
5960 // case uses high x86-64 registers (R8-R15).
5961 Label done;
5962 // Avoid null check if we know obj is not null.
5963 if (instruction->MustDoNullCheck()) {
5964 __ testl(obj, obj);
5965 __ j(kEqual, &done);
5966 }
5967
5968 // /* HeapReference<Class> */ temp = obj->klass_
5969 GenerateReferenceLoadTwoRegisters(
5970 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5971
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005972 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005973 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005974 if (cls.IsRegister()) {
5975 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5976 } else {
5977 DCHECK(cls.IsStackSlot()) << cls;
5978 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5979 }
5980 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981
5982 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005983 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005984 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005985
5986 // If the component type is not null (i.e. the object is indeed
5987 // an array), jump to label `check_non_primitive_component_type`
5988 // to further check that this component type is not a primitive
5989 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005990 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005991 __ j(kNotEqual, &check_non_primitive_component_type);
5992 // Otherwise, jump to the slow path to throw the exception.
5993 //
5994 // But before, move back the object's class into `temp` before
5995 // going into the slow path, as it has been overwritten in the
5996 // meantime.
5997 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005998 GenerateReferenceLoadTwoRegisters(
5999 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000 __ jmp(type_check_slow_path->GetEntryLabel());
6001
6002 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006004 __ j(kEqual, &done);
6005 // Same comment as above regarding `temp` and the slow path.
6006 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006007 GenerateReferenceLoadTwoRegisters(
6008 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006010 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 break;
6012 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013
Calin Juravle98893e12015-10-02 21:05:03 +01006014 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006015 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006016 NearLabel done;
6017 // Avoid null check if we know obj is not null.
6018 if (instruction->MustDoNullCheck()) {
6019 __ testl(obj, obj);
6020 __ j(kEqual, &done);
6021 }
6022
6023 // /* HeapReference<Class> */ temp = obj->klass_
6024 GenerateReferenceLoadTwoRegisters(
6025 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6026
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006027 // We always go into the type check slow path for the unresolved
6028 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006029 //
6030 // We cannot directly call the CheckCast runtime entry point
6031 // without resorting to a type checking slow path here (i.e. by
6032 // calling InvokeRuntime directly), as it would require to
6033 // assign fixed registers for the inputs of this HInstanceOf
6034 // instruction (following the runtime calling convention), which
6035 // might be cluttered by the potential first read barrier
6036 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006037 //
6038 // TODO: Introduce a new runtime entry point taking the object
6039 // to test (instead of its class) as argument, and let it deal
6040 // with the read barrier issues. This will let us refactor this
6041 // case of the `switch` code as it was previously (with a direct
6042 // call to the runtime not using a type checking slow path).
6043 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006045 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006046 break;
6047 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006050}
6051
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006052void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6053 LocationSummary* locations =
6054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6055 InvokeRuntimeCallingConvention calling_convention;
6056 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6057}
6058
6059void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006060 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6061 : QUICK_ENTRY_POINT(pUnlockObject),
6062 instruction,
6063 instruction->GetDexPc(),
6064 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006065 if (instruction->IsEnter()) {
6066 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6067 } else {
6068 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6069 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006070}
6071
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006072void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6073void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6074void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6075
6076void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6077 LocationSummary* locations =
6078 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6079 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6080 || instruction->GetResultType() == Primitive::kPrimLong);
6081 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006082 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006083 locations->SetOut(Location::SameAsFirstInput());
6084}
6085
6086void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6087 HandleBitwiseOperation(instruction);
6088}
6089
6090void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6091 HandleBitwiseOperation(instruction);
6092}
6093
6094void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6095 HandleBitwiseOperation(instruction);
6096}
6097
6098void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6099 LocationSummary* locations = instruction->GetLocations();
6100 Location first = locations->InAt(0);
6101 Location second = locations->InAt(1);
6102 DCHECK(first.Equals(locations->Out()));
6103
6104 if (instruction->GetResultType() == Primitive::kPrimInt) {
6105 if (second.IsRegister()) {
6106 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006107 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006108 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006109 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
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>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006113 }
6114 } else if (second.IsConstant()) {
6115 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6116 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006117 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006118 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006119 __ orl(first.AsRegister<CpuRegister>(), imm);
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>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006123 }
6124 } else {
6125 Address address(CpuRegister(RSP), second.GetStackIndex());
6126 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006127 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006128 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006129 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006130 } else {
6131 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006132 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006133 }
6134 }
6135 } else {
6136 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006137 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6138 bool second_is_constant = false;
6139 int64_t value = 0;
6140 if (second.IsConstant()) {
6141 second_is_constant = true;
6142 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006143 }
Mark Mendell40741f32015-04-20 22:10:34 -04006144 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006145
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006146 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006147 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006148 if (is_int32_value) {
6149 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6150 } else {
6151 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6152 }
6153 } else if (second.IsDoubleStackSlot()) {
6154 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006155 } else {
6156 __ andq(first_reg, second.AsRegister<CpuRegister>());
6157 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006158 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006159 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006160 if (is_int32_value) {
6161 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6162 } else {
6163 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6164 }
6165 } else if (second.IsDoubleStackSlot()) {
6166 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006167 } else {
6168 __ orq(first_reg, second.AsRegister<CpuRegister>());
6169 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006170 } else {
6171 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006172 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006173 if (is_int32_value) {
6174 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6175 } else {
6176 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6177 }
6178 } else if (second.IsDoubleStackSlot()) {
6179 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006180 } else {
6181 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6182 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006183 }
6184 }
6185}
6186
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006187void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6188 Location out,
6189 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006190 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006191 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6192 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006193 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006194 if (kUseBakerReadBarrier) {
6195 // Load with fast path based Baker's read barrier.
6196 // /* HeapReference<Object> */ out = *(out + offset)
6197 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006198 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006199 } else {
6200 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006201 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006202 // in the following move operation, as we will need it for the
6203 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006204 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006205 // /* HeapReference<Object> */ out = *(out + offset)
6206 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006207 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006208 }
6209 } else {
6210 // Plain load with no read barrier.
6211 // /* HeapReference<Object> */ out = *(out + offset)
6212 __ movl(out_reg, Address(out_reg, offset));
6213 __ MaybeUnpoisonHeapReference(out_reg);
6214 }
6215}
6216
6217void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6218 Location out,
6219 Location obj,
6220 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006221 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006222 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6223 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6224 if (kEmitCompilerReadBarrier) {
6225 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006226 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006227 // Load with fast path based Baker's read barrier.
6228 // /* HeapReference<Object> */ out = *(obj + offset)
6229 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006230 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006231 } else {
6232 // Load with slow path based read barrier.
6233 // /* HeapReference<Object> */ out = *(obj + offset)
6234 __ movl(out_reg, Address(obj_reg, offset));
6235 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6236 }
6237 } else {
6238 // Plain load with no read barrier.
6239 // /* HeapReference<Object> */ out = *(obj + offset)
6240 __ movl(out_reg, Address(obj_reg, offset));
6241 __ MaybeUnpoisonHeapReference(out_reg);
6242 }
6243}
6244
6245void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6246 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 const Address& address,
6248 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006249 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6250 if (kEmitCompilerReadBarrier) {
6251 if (kUseBakerReadBarrier) {
6252 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6253 // Baker's read barrier are used:
6254 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006255 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006256 // if (Thread::Current()->GetIsGcMarking()) {
6257 // root = ReadBarrier::Mark(root)
6258 // }
6259
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006260 // /* GcRoot<mirror::Object> */ root = *address
6261 __ movl(root_reg, address);
6262 if (fixup_label != nullptr) {
6263 __ Bind(fixup_label);
6264 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006265 static_assert(
6266 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6267 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6268 "have different sizes.");
6269 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6270 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6271 "have different sizes.");
6272
6273 // Slow path used to mark the GC root `root`.
6274 SlowPathCode* slow_path =
6275 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6276 codegen_->AddSlowPath(slow_path);
6277
6278 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6279 /* no_rip */ true),
6280 Immediate(0));
6281 __ j(kNotEqual, slow_path->GetEntryLabel());
6282 __ Bind(slow_path->GetExitLabel());
6283 } else {
6284 // GC root loaded through a slow path for read barriers other
6285 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 // /* GcRoot<mirror::Object>* */ root = address
6287 __ leaq(root_reg, address);
6288 if (fixup_label != nullptr) {
6289 __ Bind(fixup_label);
6290 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006291 // /* mirror::Object* */ root = root->Read()
6292 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6293 }
6294 } else {
6295 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006296 // /* GcRoot<mirror::Object> */ root = *address
6297 __ movl(root_reg, address);
6298 if (fixup_label != nullptr) {
6299 __ Bind(fixup_label);
6300 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006301 // Note that GC roots are not affected by heap poisoning, thus we
6302 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006303 }
6304}
6305
6306void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6307 Location ref,
6308 CpuRegister obj,
6309 uint32_t offset,
6310 Location temp,
6311 bool needs_null_check) {
6312 DCHECK(kEmitCompilerReadBarrier);
6313 DCHECK(kUseBakerReadBarrier);
6314
6315 // /* HeapReference<Object> */ ref = *(obj + offset)
6316 Address src(obj, offset);
6317 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6318}
6319
6320void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6321 Location ref,
6322 CpuRegister obj,
6323 uint32_t data_offset,
6324 Location index,
6325 Location temp,
6326 bool needs_null_check) {
6327 DCHECK(kEmitCompilerReadBarrier);
6328 DCHECK(kUseBakerReadBarrier);
6329
Roland Levillain3d312422016-06-23 13:53:42 +01006330 static_assert(
6331 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6332 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006333 // /* HeapReference<Object> */ ref =
6334 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6335 Address src = index.IsConstant() ?
6336 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6337 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6338 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6339}
6340
6341void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6342 Location ref,
6343 CpuRegister obj,
6344 const Address& src,
6345 Location temp,
6346 bool needs_null_check) {
6347 DCHECK(kEmitCompilerReadBarrier);
6348 DCHECK(kUseBakerReadBarrier);
6349
6350 // In slow path based read barriers, the read barrier call is
6351 // inserted after the original load. However, in fast path based
6352 // Baker's read barriers, we need to perform the load of
6353 // mirror::Object::monitor_ *before* the original reference load.
6354 // This load-load ordering is required by the read barrier.
6355 // The fast path/slow path (for Baker's algorithm) should look like:
6356 //
6357 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6358 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6359 // HeapReference<Object> ref = *src; // Original reference load.
6360 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6361 // if (is_gray) {
6362 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6363 // }
6364 //
6365 // Note: the original implementation in ReadBarrier::Barrier is
6366 // slightly more complex as:
6367 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006368 // the high-bits of rb_state, which are expected to be all zeroes
6369 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6370 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006371 // - it performs additional checks that we do not do here for
6372 // performance reasons.
6373
6374 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6375 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6376 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6377
6378 // /* int32_t */ monitor = obj->monitor_
6379 __ movl(temp_reg, Address(obj, monitor_offset));
6380 if (needs_null_check) {
6381 MaybeRecordImplicitNullCheck(instruction);
6382 }
6383 // /* LockWord */ lock_word = LockWord(monitor)
6384 static_assert(sizeof(LockWord) == sizeof(int32_t),
6385 "art::LockWord and int32_t have different sizes.");
6386 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6387 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6388 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6389 static_assert(
6390 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6391 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6392
6393 // Load fence to prevent load-load reordering.
6394 // Note that this is a no-op, thanks to the x86-64 memory model.
6395 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6396
6397 // The actual reference load.
6398 // /* HeapReference<Object> */ ref = *src
6399 __ movl(ref_reg, src);
6400
6401 // Object* ref = ref_addr->AsMirrorPtr()
6402 __ MaybeUnpoisonHeapReference(ref_reg);
6403
6404 // Slow path used to mark the object `ref` when it is gray.
6405 SlowPathCode* slow_path =
6406 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6407 AddSlowPath(slow_path);
6408
6409 // if (rb_state == ReadBarrier::gray_ptr_)
6410 // ref = ReadBarrier::Mark(ref);
6411 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6412 __ j(kEqual, slow_path->GetEntryLabel());
6413 __ Bind(slow_path->GetExitLabel());
6414}
6415
6416void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6417 Location out,
6418 Location ref,
6419 Location obj,
6420 uint32_t offset,
6421 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 DCHECK(kEmitCompilerReadBarrier);
6423
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006424 // Insert a slow path based read barrier *after* the reference load.
6425 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 // If heap poisoning is enabled, the unpoisoning of the loaded
6427 // reference will be carried out by the runtime within the slow
6428 // path.
6429 //
6430 // Note that `ref` currently does not get unpoisoned (when heap
6431 // poisoning is enabled), which is alright as the `ref` argument is
6432 // not used by the artReadBarrierSlow entry point.
6433 //
6434 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6435 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6436 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6437 AddSlowPath(slow_path);
6438
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 __ jmp(slow_path->GetEntryLabel());
6440 __ Bind(slow_path->GetExitLabel());
6441}
6442
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006443void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6444 Location out,
6445 Location ref,
6446 Location obj,
6447 uint32_t offset,
6448 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006450 // Baker's read barriers shall be handled by the fast path
6451 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6452 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006453 // If heap poisoning is enabled, unpoisoning will be taken care of
6454 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006455 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006456 } else if (kPoisonHeapReferences) {
6457 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6458 }
6459}
6460
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6462 Location out,
6463 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006464 DCHECK(kEmitCompilerReadBarrier);
6465
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006466 // Insert a slow path based read barrier *after* the GC root load.
6467 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468 // Note that GC roots are not affected by heap poisoning, so we do
6469 // not need to do anything special for this here.
6470 SlowPathCode* slow_path =
6471 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6472 AddSlowPath(slow_path);
6473
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474 __ jmp(slow_path->GetEntryLabel());
6475 __ Bind(slow_path->GetExitLabel());
6476}
6477
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006478void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006479 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006480 LOG(FATAL) << "Unreachable";
6481}
6482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006483void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006484 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006485 LOG(FATAL) << "Unreachable";
6486}
6487
Mark Mendellfe57faa2015-09-18 09:26:15 -04006488// Simple implementation of packed switch - generate cascaded compare/jumps.
6489void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6490 LocationSummary* locations =
6491 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6492 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006493 locations->AddTemp(Location::RequiresRegister());
6494 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006495}
6496
6497void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6498 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006499 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006500 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006501 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6502 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6503 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006504 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6505
6506 // Should we generate smaller inline compare/jumps?
6507 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6508 // Figure out the correct compare values and jump conditions.
6509 // Handle the first compare/branch as a special case because it might
6510 // jump to the default case.
6511 DCHECK_GT(num_entries, 2u);
6512 Condition first_condition;
6513 uint32_t index;
6514 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6515 if (lower_bound != 0) {
6516 first_condition = kLess;
6517 __ cmpl(value_reg_in, Immediate(lower_bound));
6518 __ j(first_condition, codegen_->GetLabelOf(default_block));
6519 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6520
6521 index = 1;
6522 } else {
6523 // Handle all the compare/jumps below.
6524 first_condition = kBelow;
6525 index = 0;
6526 }
6527
6528 // Handle the rest of the compare/jumps.
6529 for (; index + 1 < num_entries; index += 2) {
6530 int32_t compare_to_value = lower_bound + index + 1;
6531 __ cmpl(value_reg_in, Immediate(compare_to_value));
6532 // Jump to successors[index] if value < case_value[index].
6533 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6534 // Jump to successors[index + 1] if value == case_value[index + 1].
6535 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6536 }
6537
6538 if (index != num_entries) {
6539 // There are an odd number of entries. Handle the last one.
6540 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006541 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006542 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6543 }
6544
6545 // And the default for any other value.
6546 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6547 __ jmp(codegen_->GetLabelOf(default_block));
6548 }
6549 return;
6550 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006551
6552 // Remove the bias, if needed.
6553 Register value_reg_out = value_reg_in.AsRegister();
6554 if (lower_bound != 0) {
6555 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6556 value_reg_out = temp_reg.AsRegister();
6557 }
6558 CpuRegister value_reg(value_reg_out);
6559
6560 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006561 __ cmpl(value_reg, Immediate(num_entries - 1));
6562 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006563
Mark Mendell9c86b482015-09-18 13:36:07 -04006564 // We are in the range of the table.
6565 // Load the address of the jump table in the constant area.
6566 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006567
Mark Mendell9c86b482015-09-18 13:36:07 -04006568 // Load the (signed) offset from the jump table.
6569 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6570
6571 // Add the offset to the address of the table base.
6572 __ addq(temp_reg, base_reg);
6573
6574 // And jump.
6575 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006576}
6577
Aart Bikc5d47542016-01-27 17:00:35 -08006578void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6579 if (value == 0) {
6580 __ xorl(dest, dest);
6581 } else {
6582 __ movl(dest, Immediate(value));
6583 }
6584}
6585
Mark Mendell92e83bf2015-05-07 11:25:03 -04006586void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6587 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006588 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006589 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006590 } else if (IsUint<32>(value)) {
6591 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006592 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6593 } else {
6594 __ movq(dest, Immediate(value));
6595 }
6596}
6597
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006598void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6599 if (value == 0) {
6600 __ xorps(dest, dest);
6601 } else {
6602 __ movss(dest, LiteralInt32Address(value));
6603 }
6604}
6605
6606void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6607 if (value == 0) {
6608 __ xorpd(dest, dest);
6609 } else {
6610 __ movsd(dest, LiteralInt64Address(value));
6611 }
6612}
6613
6614void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6615 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6616}
6617
6618void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6619 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6620}
6621
Aart Bika19616e2016-02-01 18:57:58 -08006622void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6623 if (value == 0) {
6624 __ testl(dest, dest);
6625 } else {
6626 __ cmpl(dest, Immediate(value));
6627 }
6628}
6629
6630void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6631 if (IsInt<32>(value)) {
6632 if (value == 0) {
6633 __ testq(dest, dest);
6634 } else {
6635 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6636 }
6637 } else {
6638 // Value won't fit in an int.
6639 __ cmpq(dest, LiteralInt64Address(value));
6640 }
6641}
6642
Mark Mendellcfa410b2015-05-25 16:02:44 -04006643void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6644 DCHECK(dest.IsDoubleStackSlot());
6645 if (IsInt<32>(value)) {
6646 // Can move directly as an int32 constant.
6647 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6648 Immediate(static_cast<int32_t>(value)));
6649 } else {
6650 Load64BitValue(CpuRegister(TMP), value);
6651 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6652 }
6653}
6654
Mark Mendell9c86b482015-09-18 13:36:07 -04006655/**
6656 * Class to handle late fixup of offsets into constant area.
6657 */
6658class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6659 public:
6660 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6661 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6662
6663 protected:
6664 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6665
6666 CodeGeneratorX86_64* codegen_;
6667
6668 private:
6669 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6670 // Patch the correct offset for the instruction. We use the address of the
6671 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6672 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6673 int32_t relative_position = constant_offset - pos;
6674
6675 // Patch in the right value.
6676 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6677 }
6678
6679 // Location in constant area that the fixup refers to.
6680 size_t offset_into_constant_area_;
6681};
6682
6683/**
6684 t * Class to handle late fixup of offsets to a jump table that will be created in the
6685 * constant area.
6686 */
6687class JumpTableRIPFixup : public RIPFixup {
6688 public:
6689 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6690 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6691
6692 void CreateJumpTable() {
6693 X86_64Assembler* assembler = codegen_->GetAssembler();
6694
6695 // Ensure that the reference to the jump table has the correct offset.
6696 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6697 SetOffset(offset_in_constant_table);
6698
6699 // Compute the offset from the start of the function to this jump table.
6700 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6701
6702 // Populate the jump table with the correct values for the jump table.
6703 int32_t num_entries = switch_instr_->GetNumEntries();
6704 HBasicBlock* block = switch_instr_->GetBlock();
6705 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6706 // The value that we want is the target offset - the position of the table.
6707 for (int32_t i = 0; i < num_entries; i++) {
6708 HBasicBlock* b = successors[i];
6709 Label* l = codegen_->GetLabelOf(b);
6710 DCHECK(l->IsBound());
6711 int32_t offset_to_block = l->Position() - current_table_offset;
6712 assembler->AppendInt32(offset_to_block);
6713 }
6714 }
6715
6716 private:
6717 const HPackedSwitch* switch_instr_;
6718};
6719
Mark Mendellf55c3e02015-03-26 21:07:46 -04006720void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6721 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006722 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006723 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6724 // 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 -04006725 assembler->Align(4, 0);
6726 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006727
6728 // Populate any jump tables.
6729 for (auto jump_table : fixups_to_jump_tables_) {
6730 jump_table->CreateJumpTable();
6731 }
6732
6733 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006734 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006735 }
6736
6737 // And finish up.
6738 CodeGenerator::Finalize(allocator);
6739}
6740
Mark Mendellf55c3e02015-03-26 21:07:46 -04006741Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6742 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6743 return Address::RIP(fixup);
6744}
6745
6746Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6747 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6748 return Address::RIP(fixup);
6749}
6750
6751Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6752 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6753 return Address::RIP(fixup);
6754}
6755
6756Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6757 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6758 return Address::RIP(fixup);
6759}
6760
Andreas Gampe85b62f22015-09-09 13:15:38 -07006761// TODO: trg as memory.
6762void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6763 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006764 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006765 return;
6766 }
6767
6768 DCHECK_NE(type, Primitive::kPrimVoid);
6769
6770 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6771 if (trg.Equals(return_loc)) {
6772 return;
6773 }
6774
6775 // Let the parallel move resolver take care of all of this.
6776 HParallelMove parallel_move(GetGraph()->GetArena());
6777 parallel_move.AddMove(return_loc, trg, type, nullptr);
6778 GetMoveResolver()->EmitNativeCode(&parallel_move);
6779}
6780
Mark Mendell9c86b482015-09-18 13:36:07 -04006781Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6782 // Create a fixup to be used to create and address the jump table.
6783 JumpTableRIPFixup* table_fixup =
6784 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6785
6786 // We have to populate the jump tables.
6787 fixups_to_jump_tables_.push_back(table_fixup);
6788 return Address::RIP(table_fixup);
6789}
6790
Mark Mendellea5af682015-10-22 17:35:49 -04006791void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6792 const Address& addr_high,
6793 int64_t v,
6794 HInstruction* instruction) {
6795 if (IsInt<32>(v)) {
6796 int32_t v_32 = v;
6797 __ movq(addr_low, Immediate(v_32));
6798 MaybeRecordImplicitNullCheck(instruction);
6799 } else {
6800 // Didn't fit in a register. Do it in pieces.
6801 int32_t low_v = Low32Bits(v);
6802 int32_t high_v = High32Bits(v);
6803 __ movl(addr_low, Immediate(low_v));
6804 MaybeRecordImplicitNullCheck(instruction);
6805 __ movl(addr_high, Immediate(high_v));
6806 }
6807}
6808
Roland Levillain4d027112015-07-01 15:41:14 +01006809#undef __
6810
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006811} // namespace x86_64
6812} // namespace art