blob: cd040641a6297965c73fc6d4451dfbee266a64b0 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070054// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Andreas Gampe85b62f22015-09-09 13:15:38 -070058class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000060 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Alexandre Rames2ed20af2015-03-06 13:55:35 +000062 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000065 if (instruction_->CanThrowIntoCatchBlock()) {
66 // Live registers will be restored in the catch block if caught.
67 SaveLiveRegisters(codegen, instruction_->GetLocations());
68 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000069 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
70 instruction_,
71 instruction_->GetDexPc(),
72 this);
Roland Levillain888d0672015-11-23 18:53:50 +000073 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 }
75
Alexandre Rames8158f282015-08-07 10:26:17 +010076 bool IsFatal() const OVERRIDE { return true; }
77
Alexandre Rames9931f312015-06-19 14:47:01 +010078 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
79
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000086 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000089 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000091 if (instruction_->CanThrowIntoCatchBlock()) {
92 // Live registers will be restored in the catch block if caught.
93 SaveLiveRegisters(codegen, instruction_->GetLocations());
94 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000095 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
96 instruction_,
97 instruction_->GetDexPc(),
98 this);
Roland Levillain888d0672015-11-23 18:53:50 +000099 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000100 }
101
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 bool IsFatal() const OVERRIDE { return true; }
103
Alexandre Rames9931f312015-06-19 14:47:01 +0100104 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
105
Calin Juravled0d48522014-11-04 16:40:20 +0000106 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
113 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000114
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000116 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negl(cpu_reg_);
120 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400121 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 }
123
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 } else {
125 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negq(cpu_reg_);
128 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400129 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000130 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 }
Calin Juravled0d48522014-11-04 16:40:20 +0000132 __ jmp(GetExitLabel());
133 }
134
Alexandre Rames9931f312015-06-19 14:47:01 +0100135 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
136
Calin Juravled0d48522014-11-04 16:40:20 +0000137 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000139 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 const bool is_div_;
141 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000142};
143
Andreas Gampe85b62f22015-09-09 13:15:38 -0700144class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100146 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000153 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
154 instruction_,
155 instruction_->GetDexPc(),
156 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000157 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000158 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 if (successor_ == nullptr) {
160 __ jmp(GetReturnLabel());
161 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000162 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 }
165
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 Label* GetReturnLabel() {
167 DCHECK(successor_ == nullptr);
168 return &return_label_;
169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100171 HBasicBlock* GetSuccessor() const {
172 return successor_;
173 }
174
Alexandre Rames9931f312015-06-19 14:47:01 +0100175 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
176
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000179 Label return_label_;
180
181 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
182};
183
Andreas Gampe85b62f22015-09-09 13:15:38 -0700184class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100186 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000187 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100190 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000191 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000193 if (instruction_->CanThrowIntoCatchBlock()) {
194 // Live registers will be restored in the catch block if caught.
195 SaveLiveRegisters(codegen, instruction_->GetLocations());
196 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 // We're moving two locations to locations that could overlap, so we need a parallel
198 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000200 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100201 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000202 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100203 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100205 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
206 Primitive::kPrimInt);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000207 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
208 instruction_,
209 instruction_->GetDexPc(),
210 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000211 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212 }
213
Alexandre Rames8158f282015-08-07 10:26:17 +0100214 bool IsFatal() const OVERRIDE { return true; }
215
Alexandre Rames9931f312015-06-19 14:47:01 +0100216 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100218 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100219 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
220};
221
Andreas Gampe85b62f22015-09-09 13:15:38 -0700222class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 LoadClassSlowPathX86_64(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
227 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000228 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
230 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000234 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000237 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000241 x86_64_codegen->InvokeRuntime(do_clinit_ ?
242 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
243 QUICK_ENTRY_POINT(pInitializeType),
244 at_,
245 dex_pc_,
246 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000247 if (do_clinit_) {
248 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
249 } else {
250 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
251 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255 if (out.IsValid()) {
256 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000257 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 }
259
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100261 __ jmp(GetExitLabel());
262 }
263
Alexandre Rames9931f312015-06-19 14:47:01 +0100264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
265
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100266 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000267 // The class this slow path will load.
268 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100269
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100281};
282
Andreas Gampe85b62f22015-09-09 13:15:38 -0700283class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000286
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290
Roland Levillain0d5a2812015-11-13 10:07:31 +0000291 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000293 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000298 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000302 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000303 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000304 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000305 __ jmp(GetExitLabel());
306 }
307
Alexandre Rames9931f312015-06-19 14:47:01 +0100308 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
309
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000310 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000311 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
312};
313
Andreas Gampe85b62f22015-09-09 13:15:38 -0700314class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
322 : locations->Out();
323 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 DCHECK(instruction_->IsCheckCast()
325 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Roland Levillain0d5a2812015-11-13 10:07:31 +0000327 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 if (!is_fatal_) {
331 SaveLiveRegisters(codegen, locations);
332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
334 // We're moving two locations to locations that could overlap, so we need a parallel
335 // move resolver.
336 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000337 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100338 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000339 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100340 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100342 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
343 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000346 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
347 instruction_,
348 dex_pc,
349 this);
350 CheckEntrypointTypes<
351 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352 } else {
353 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000354 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
355 instruction_,
356 dex_pc,
357 this);
358 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 if (!is_fatal_) {
362 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000363 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 RestoreLiveRegisters(codegen, locations);
367 __ jmp(GetExitLabel());
368 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000369 }
370
Alexandre Rames9931f312015-06-19 14:47:01 +0100371 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
372
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 bool IsFatal() const OVERRIDE { return is_fatal_; }
374
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377
378 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
379};
380
Andreas Gampe85b62f22015-09-09 13:15:38 -0700381class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 public:
Aart Bik42249c32016-01-07 15:33:50 -0800383 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000387 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000390 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800391 instruction_,
392 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000393 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
401};
402
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100403class ArraySetSlowPathX86_64 : public SlowPathCode {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConvention calling_convention;
413 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
414 parallel_move.AddMove(
415 locations->InAt(0),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
417 Primitive::kPrimNot,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(1),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
422 Primitive::kPrimInt,
423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(2),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
427 Primitive::kPrimNot,
428 nullptr);
429 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
430
Roland Levillain0d5a2812015-11-13 10:07:31 +0000431 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
432 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
433 instruction_,
434 instruction_->GetDexPc(),
435 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000436 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 RestoreLiveRegisters(codegen, locations);
438 __ jmp(GetExitLabel());
439 }
440
441 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
442
443 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
445};
446
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000447// Slow path marking an object during a read barrier.
448class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
449 public:
450 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000451 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000452 DCHECK(kEmitCompilerReadBarrier);
453 }
454
455 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
456
457 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
458 LocationSummary* locations = instruction_->GetLocations();
459 Register reg_out = out_.AsRegister<Register>();
460 DCHECK(locations->CanCall());
461 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
462 DCHECK(instruction_->IsInstanceFieldGet() ||
463 instruction_->IsStaticFieldGet() ||
464 instruction_->IsArrayGet() ||
465 instruction_->IsLoadClass() ||
466 instruction_->IsLoadString() ||
467 instruction_->IsInstanceOf() ||
468 instruction_->IsCheckCast())
469 << "Unexpected instruction in read barrier marking slow path: "
470 << instruction_->DebugName();
471
472 __ Bind(GetEntryLabel());
473 SaveLiveRegisters(codegen, locations);
474
475 InvokeRuntimeCallingConvention calling_convention;
476 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
477 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
478 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
479 instruction_,
480 instruction_->GetDexPc(),
481 this);
482 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
483 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
484
485 RestoreLiveRegisters(codegen, locations);
486 __ jmp(GetExitLabel());
487 }
488
489 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000490 const Location out_;
491 const Location obj_;
492
493 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
494};
495
Roland Levillain0d5a2812015-11-13 10:07:31 +0000496// Slow path generating a read barrier for a heap reference.
497class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
498 public:
499 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
500 Location out,
501 Location ref,
502 Location obj,
503 uint32_t offset,
504 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000505 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000506 out_(out),
507 ref_(ref),
508 obj_(obj),
509 offset_(offset),
510 index_(index) {
511 DCHECK(kEmitCompilerReadBarrier);
512 // If `obj` is equal to `out` or `ref`, it means the initial
513 // object has been overwritten by (or after) the heap object
514 // reference load to be instrumented, e.g.:
515 //
516 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000517 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000518 //
519 // In that case, we have lost the information about the original
520 // object, and the emitted read barrier cannot work properly.
521 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
522 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
523}
524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
526 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
527 LocationSummary* locations = instruction_->GetLocations();
528 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
529 DCHECK(locations->CanCall());
530 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
531 DCHECK(!instruction_->IsInvoke() ||
532 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000533 instruction_->GetLocations()->Intrinsified()))
534 << "Unexpected instruction in read barrier for heap reference slow path: "
535 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000536
537 __ Bind(GetEntryLabel());
538 SaveLiveRegisters(codegen, locations);
539
540 // We may have to change the index's value, but as `index_` is a
541 // constant member (like other "inputs" of this slow path),
542 // introduce a copy of it, `index`.
543 Location index = index_;
544 if (index_.IsValid()) {
545 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
546 if (instruction_->IsArrayGet()) {
547 // Compute real offset and store it in index_.
548 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
549 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
550 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
551 // We are about to change the value of `index_reg` (see the
552 // calls to art::x86_64::X86_64Assembler::shll and
553 // art::x86_64::X86_64Assembler::AddImmediate below), but it
554 // has not been saved by the previous call to
555 // art::SlowPathCode::SaveLiveRegisters, as it is a
556 // callee-save register --
557 // art::SlowPathCode::SaveLiveRegisters does not consider
558 // callee-save registers, as it has been designed with the
559 // assumption that callee-save registers are supposed to be
560 // handled by the called function. So, as a callee-save
561 // register, `index_reg` _would_ eventually be saved onto
562 // the stack, but it would be too late: we would have
563 // changed its value earlier. Therefore, we manually save
564 // it here into another freely available register,
565 // `free_reg`, chosen of course among the caller-save
566 // registers (as a callee-save `free_reg` register would
567 // exhibit the same problem).
568 //
569 // Note we could have requested a temporary register from
570 // the register allocator instead; but we prefer not to, as
571 // this is a slow path, and we know we can find a
572 // caller-save register that is available.
573 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
574 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
575 index_reg = free_reg;
576 index = Location::RegisterLocation(index_reg);
577 } else {
578 // The initial register stored in `index_` has already been
579 // saved in the call to art::SlowPathCode::SaveLiveRegisters
580 // (as it is not a callee-save register), so we can freely
581 // use it.
582 }
583 // Shifting the index value contained in `index_reg` by the
584 // scale factor (2) cannot overflow in practice, as the
585 // runtime is unable to allocate object arrays with a size
586 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
587 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
588 static_assert(
589 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
590 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
591 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
592 } else {
593 DCHECK(instruction_->IsInvoke());
594 DCHECK(instruction_->GetLocations()->Intrinsified());
595 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
596 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
597 << instruction_->AsInvoke()->GetIntrinsic();
598 DCHECK_EQ(offset_, 0U);
599 DCHECK(index_.IsRegister());
600 }
601 }
602
603 // We're moving two or three locations to locations that could
604 // overlap, so we need a parallel move resolver.
605 InvokeRuntimeCallingConvention calling_convention;
606 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
607 parallel_move.AddMove(ref_,
608 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
609 Primitive::kPrimNot,
610 nullptr);
611 parallel_move.AddMove(obj_,
612 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
613 Primitive::kPrimNot,
614 nullptr);
615 if (index.IsValid()) {
616 parallel_move.AddMove(index,
617 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
618 Primitive::kPrimInt,
619 nullptr);
620 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
621 } else {
622 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
623 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
624 }
625 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
626 instruction_,
627 instruction_->GetDexPc(),
628 this);
629 CheckEntrypointTypes<
630 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
631 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
632
633 RestoreLiveRegisters(codegen, locations);
634 __ jmp(GetExitLabel());
635 }
636
637 const char* GetDescription() const OVERRIDE {
638 return "ReadBarrierForHeapReferenceSlowPathX86_64";
639 }
640
641 private:
642 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
643 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
644 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
645 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
646 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
647 return static_cast<CpuRegister>(i);
648 }
649 }
650 // We shall never fail to find a free caller-save register, as
651 // there are more than two core caller-save registers on x86-64
652 // (meaning it is possible to find one which is different from
653 // `ref` and `obj`).
654 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
655 LOG(FATAL) << "Could not find a free caller-save register";
656 UNREACHABLE();
657 }
658
Roland Levillain0d5a2812015-11-13 10:07:31 +0000659 const Location out_;
660 const Location ref_;
661 const Location obj_;
662 const uint32_t offset_;
663 // An additional location containing an index to an array.
664 // Only used for HArrayGet and the UnsafeGetObject &
665 // UnsafeGetObjectVolatile intrinsics.
666 const Location index_;
667
668 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
669};
670
671// Slow path generating a read barrier for a GC root.
672class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
673 public:
674 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000675 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000676 DCHECK(kEmitCompilerReadBarrier);
677 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
680 LocationSummary* locations = instruction_->GetLocations();
681 DCHECK(locations->CanCall());
682 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000683 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
684 << "Unexpected instruction in read barrier for GC root slow path: "
685 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000686
687 __ Bind(GetEntryLabel());
688 SaveLiveRegisters(codegen, locations);
689
690 InvokeRuntimeCallingConvention calling_convention;
691 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
692 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
693 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
694 instruction_,
695 instruction_->GetDexPc(),
696 this);
697 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
698 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
699
700 RestoreLiveRegisters(codegen, locations);
701 __ jmp(GetExitLabel());
702 }
703
704 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
705
706 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707 const Location out_;
708 const Location root_;
709
710 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
711};
712
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100713#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700714// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
715#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100716
Roland Levillain4fa13f62015-07-06 18:11:54 +0100717inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700718 switch (cond) {
719 case kCondEQ: return kEqual;
720 case kCondNE: return kNotEqual;
721 case kCondLT: return kLess;
722 case kCondLE: return kLessEqual;
723 case kCondGT: return kGreater;
724 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700725 case kCondB: return kBelow;
726 case kCondBE: return kBelowEqual;
727 case kCondA: return kAbove;
728 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700729 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 LOG(FATAL) << "Unreachable";
731 UNREACHABLE();
732}
733
Aart Bike9f37602015-10-09 11:15:55 -0700734// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100735inline Condition X86_64FPCondition(IfCondition cond) {
736 switch (cond) {
737 case kCondEQ: return kEqual;
738 case kCondNE: return kNotEqual;
739 case kCondLT: return kBelow;
740 case kCondLE: return kBelowEqual;
741 case kCondGT: return kAbove;
742 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700743 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 };
745 LOG(FATAL) << "Unreachable";
746 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700747}
748
Vladimir Markodc151b22015-10-15 18:02:30 +0100749HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
750 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
751 MethodReference target_method ATTRIBUTE_UNUSED) {
752 switch (desired_dispatch_info.code_ptr_location) {
753 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
754 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
755 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
756 return HInvokeStaticOrDirect::DispatchInfo {
757 desired_dispatch_info.method_load_kind,
758 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
759 desired_dispatch_info.method_load_data,
760 0u
761 };
762 default:
763 return desired_dispatch_info;
764 }
765}
766
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800767void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100768 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800769 // All registers are assumed to be correctly set up.
770
Vladimir Marko58155012015-08-19 12:49:41 +0000771 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
772 switch (invoke->GetMethodLoadKind()) {
773 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
774 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000775 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000776 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000777 break;
778 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000779 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000780 break;
781 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
782 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
783 break;
784 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
785 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
786 method_patches_.emplace_back(invoke->GetTargetMethod());
787 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
788 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000789 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000790 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000791 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000792 // Bind a new fixup label at the end of the "movl" insn.
793 uint32_t offset = invoke->GetDexCacheArrayOffset();
794 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000795 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000796 }
Vladimir Marko58155012015-08-19 12:49:41 +0000797 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000798 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000799 Register method_reg;
800 CpuRegister reg = temp.AsRegister<CpuRegister>();
801 if (current_method.IsRegister()) {
802 method_reg = current_method.AsRegister<Register>();
803 } else {
804 DCHECK(invoke->GetLocations()->Intrinsified());
805 DCHECK(!current_method.IsValid());
806 method_reg = reg.AsRegister();
807 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
808 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000809 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100810 __ movq(reg,
811 Address(CpuRegister(method_reg),
812 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100813 // temp = temp[index_in_cache];
814 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
815 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000816 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
817 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100818 }
Vladimir Marko58155012015-08-19 12:49:41 +0000819 }
820
821 switch (invoke->GetCodePtrLocation()) {
822 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
823 __ call(&frame_entry_label_);
824 break;
825 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
826 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
827 Label* label = &relative_call_patches_.back().label;
828 __ call(label); // Bind to the patch label, override at link time.
829 __ Bind(label); // Bind the label at the end of the "call" insn.
830 break;
831 }
832 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
833 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100834 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
835 LOG(FATAL) << "Unsupported";
836 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000837 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
838 // (callee_method + offset_of_quick_compiled_code)()
839 __ call(Address(callee_method.AsRegister<CpuRegister>(),
840 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
841 kX86_64WordSize).SizeValue()));
842 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000843 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800844
845 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800846}
847
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000848void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
849 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
850 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
851 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000852
853 // Use the calling convention instead of the location of the receiver, as
854 // intrinsics may have put the receiver in a different register. In the intrinsics
855 // slow path, the arguments have been moved to the right place, so here we are
856 // guaranteed that the receiver is the first register of the calling convention.
857 InvokeDexCallingConvention calling_convention;
858 Register receiver = calling_convention.GetRegisterAt(0);
859
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000860 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000861 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000862 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000863 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000864 // Instead of simply (possibly) unpoisoning `temp` here, we should
865 // emit a read barrier for the previous class reference load.
866 // However this is not required in practice, as this is an
867 // intermediate/temporary reference and because the current
868 // concurrent copying collector keeps the from-space memory
869 // intact/accessible until the end of the marking phase (the
870 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000871 __ MaybeUnpoisonHeapReference(temp);
872 // temp = temp->GetMethodAt(method_offset);
873 __ movq(temp, Address(temp, method_offset));
874 // call temp->GetEntryPoint();
875 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
876 kX86_64WordSize).SizeValue()));
877}
878
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000879void CodeGeneratorX86_64::RecordSimplePatch() {
880 if (GetCompilerOptions().GetIncludePatchInformation()) {
881 simple_patches_.emplace_back();
882 __ Bind(&simple_patches_.back());
883 }
884}
885
886void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
887 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
888 __ Bind(&string_patches_.back().label);
889}
890
891Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
892 uint32_t element_offset) {
893 // Add a patch entry and return the label.
894 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
895 return &pc_relative_dex_cache_patches_.back().label;
896}
897
Vladimir Marko58155012015-08-19 12:49:41 +0000898void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
899 DCHECK(linker_patches->empty());
900 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000901 method_patches_.size() +
902 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000903 pc_relative_dex_cache_patches_.size() +
904 simple_patches_.size() +
905 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000906 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000907 // The label points to the end of the "movl" insn but the literal offset for method
908 // patch needs to point to the embedded constant which occupies the last 4 bytes.
909 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000910 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000911 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000912 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
913 info.target_method.dex_file,
914 info.target_method.dex_method_index));
915 }
916 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000917 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000918 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
919 info.target_method.dex_file,
920 info.target_method.dex_method_index));
921 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000922 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
923 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000924 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
925 &info.target_dex_file,
926 info.label.Position(),
927 info.element_offset));
928 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000929 for (const Label& label : simple_patches_) {
930 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
931 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
932 }
933 for (const StringPatchInfo<Label>& info : string_patches_) {
934 // These are always PC-relative, see GetSupportedLoadStringKind().
935 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
936 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
937 &info.dex_file,
938 info.label.Position(),
939 info.string_index));
940 }
Vladimir Marko58155012015-08-19 12:49:41 +0000941}
942
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100943void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100944 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100945}
946
947void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100948 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949}
950
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100951size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
952 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
953 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100954}
955
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100956size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
957 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
958 return kX86_64WordSize;
959}
960
961size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
962 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
963 return kX86_64WordSize;
964}
965
966size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
967 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
968 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100969}
970
Calin Juravle175dc732015-08-25 15:42:32 +0100971void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
972 HInstruction* instruction,
973 uint32_t dex_pc,
974 SlowPathCode* slow_path) {
975 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
976 instruction,
977 dex_pc,
978 slow_path);
979}
980
981void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100982 HInstruction* instruction,
983 uint32_t dex_pc,
984 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100985 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000986 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100987 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100988}
989
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000990static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000991// Use a fake return address register to mimic Quick.
992static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400993CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000994 const X86_64InstructionSetFeatures& isa_features,
995 const CompilerOptions& compiler_options,
996 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000997 : CodeGenerator(graph,
998 kNumberOfCpuRegisters,
999 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001000 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001001 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1002 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001003 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001004 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1005 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001006 compiler_options,
1007 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001008 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001010 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001011 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001012 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001013 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001014 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001015 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1016 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001017 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001018 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1019 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001020 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001021 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1022}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001024InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1025 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001026 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001027 assembler_(codegen->GetAssembler()),
1028 codegen_(codegen) {}
1029
David Brazdil58282f42016-01-14 12:45:10 +00001030void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001032 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001034 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001035 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001036}
1037
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001039 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001040}
David Srbecky9d8606d2015-04-12 09:35:32 +01001041
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001043 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001044}
1045
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001046void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001047 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001048 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001049 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001050 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001051 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001052
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001053 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001054 __ testq(CpuRegister(RAX), Address(
1055 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001056 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001057 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001058
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001059 if (HasEmptyFrame()) {
1060 return;
1061 }
1062
Nicolas Geoffray98893962015-01-21 12:32:32 +00001063 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001064 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001065 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001066 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001067 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1068 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001069 }
1070 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001071
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001072 int adjust = GetFrameSize() - GetCoreSpillSize();
1073 __ subq(CpuRegister(RSP), Immediate(adjust));
1074 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001075 uint32_t xmm_spill_location = GetFpuSpillStart();
1076 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001077
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1079 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001080 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1081 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1082 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001083 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001084 }
1085
Mathieu Chartiere401d142015-04-22 13:56:20 -07001086 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001087 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088}
1089
1090void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001091 __ cfi().RememberState();
1092 if (!HasEmptyFrame()) {
1093 uint32_t xmm_spill_location = GetFpuSpillStart();
1094 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1095 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1096 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1097 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1098 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1099 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1100 }
1101 }
1102
1103 int adjust = GetFrameSize() - GetCoreSpillSize();
1104 __ addq(CpuRegister(RSP), Immediate(adjust));
1105 __ cfi().AdjustCFAOffset(-adjust);
1106
1107 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1108 Register reg = kCoreCalleeSaves[i];
1109 if (allocated_registers_.ContainsCoreRegister(reg)) {
1110 __ popq(CpuRegister(reg));
1111 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1112 __ cfi().Restore(DWARFReg(reg));
1113 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001114 }
1115 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001116 __ ret();
1117 __ cfi().RestoreState();
1118 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119}
1120
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001121void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1122 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123}
1124
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125void CodeGeneratorX86_64::Move(Location destination, Location source) {
1126 if (source.Equals(destination)) {
1127 return;
1128 }
1129 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001130 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001132 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001134 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001136 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1137 } else if (source.IsConstant()) {
1138 HConstant* constant = source.GetConstant();
1139 if (constant->IsLongConstant()) {
1140 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1141 } else {
1142 Load32BitValue(dest, GetInt32ValueOf(constant));
1143 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001144 } else {
1145 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001146 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 }
1148 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001149 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001150 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001151 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001152 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001153 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1154 } else if (source.IsConstant()) {
1155 HConstant* constant = source.GetConstant();
1156 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1157 if (constant->IsFloatConstant()) {
1158 Load32BitValue(dest, static_cast<int32_t>(value));
1159 } else {
1160 Load64BitValue(dest, value);
1161 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001162 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001163 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 } else {
1165 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001166 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001167 }
1168 } else if (destination.IsStackSlot()) {
1169 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001171 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 } else if (source.IsFpuRegister()) {
1173 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001174 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001175 } else if (source.IsConstant()) {
1176 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001177 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001178 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001179 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001180 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001181 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1182 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001183 }
1184 } else {
1185 DCHECK(destination.IsDoubleStackSlot());
1186 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 } else if (source.IsFpuRegister()) {
1190 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001192 } else if (source.IsConstant()) {
1193 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001194 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001195 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001196 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001197 } else {
1198 DCHECK(constant->IsLongConstant());
1199 value = constant->AsLongConstant()->GetValue();
1200 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001201 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 } else {
1203 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001204 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1205 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 }
1207 }
1208}
1209
Calin Juravle175dc732015-08-25 15:42:32 +01001210void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1211 DCHECK(location.IsRegister());
1212 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1213}
1214
Calin Juravlee460d1d2015-09-29 04:52:17 +01001215void CodeGeneratorX86_64::MoveLocation(
1216 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1217 Move(dst, src);
1218}
1219
1220void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1221 if (location.IsRegister()) {
1222 locations->AddTemp(location);
1223 } else {
1224 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1225 }
1226}
1227
David Brazdilfc6a86a2015-06-26 10:33:45 +00001228void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001229 DCHECK(!successor->IsExitBlock());
1230
1231 HBasicBlock* block = got->GetBlock();
1232 HInstruction* previous = got->GetPrevious();
1233
1234 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001235 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001236 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1237 return;
1238 }
1239
1240 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1241 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1242 }
1243 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001244 __ jmp(codegen_->GetLabelOf(successor));
1245 }
1246}
1247
David Brazdilfc6a86a2015-06-26 10:33:45 +00001248void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1249 got->SetLocations(nullptr);
1250}
1251
1252void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1253 HandleGoto(got, got->GetSuccessor());
1254}
1255
1256void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1257 try_boundary->SetLocations(nullptr);
1258}
1259
1260void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1261 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1262 if (!successor->IsExitBlock()) {
1263 HandleGoto(try_boundary, successor);
1264 }
1265}
1266
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001267void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1268 exit->SetLocations(nullptr);
1269}
1270
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001271void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001272}
1273
Mark Mendell152408f2015-12-31 12:28:50 -05001274template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001275void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001276 LabelType* true_label,
1277 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001278 if (cond->IsFPConditionTrueIfNaN()) {
1279 __ j(kUnordered, true_label);
1280 } else if (cond->IsFPConditionFalseIfNaN()) {
1281 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001282 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001283 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001284}
1285
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001286void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001287 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001288
Mark Mendellc4701932015-04-10 13:18:51 -04001289 Location left = locations->InAt(0);
1290 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 Primitive::Type type = condition->InputAt(0)->GetType();
1292 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001293 case Primitive::kPrimBoolean:
1294 case Primitive::kPrimByte:
1295 case Primitive::kPrimChar:
1296 case Primitive::kPrimShort:
1297 case Primitive::kPrimInt:
1298 case Primitive::kPrimNot: {
1299 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1300 if (right.IsConstant()) {
1301 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1302 if (value == 0) {
1303 __ testl(left_reg, left_reg);
1304 } else {
1305 __ cmpl(left_reg, Immediate(value));
1306 }
1307 } else if (right.IsStackSlot()) {
1308 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1309 } else {
1310 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1311 }
1312 break;
1313 }
Mark Mendellc4701932015-04-10 13:18:51 -04001314 case Primitive::kPrimLong: {
1315 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1316 if (right.IsConstant()) {
1317 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001318 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001319 } else if (right.IsDoubleStackSlot()) {
1320 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1321 } else {
1322 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1323 }
Mark Mendellc4701932015-04-10 13:18:51 -04001324 break;
1325 }
1326 case Primitive::kPrimFloat: {
1327 if (right.IsFpuRegister()) {
1328 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1329 } else if (right.IsConstant()) {
1330 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1331 codegen_->LiteralFloatAddress(
1332 right.GetConstant()->AsFloatConstant()->GetValue()));
1333 } else {
1334 DCHECK(right.IsStackSlot());
1335 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1336 Address(CpuRegister(RSP), right.GetStackIndex()));
1337 }
Mark Mendellc4701932015-04-10 13:18:51 -04001338 break;
1339 }
1340 case Primitive::kPrimDouble: {
1341 if (right.IsFpuRegister()) {
1342 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1343 } else if (right.IsConstant()) {
1344 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1345 codegen_->LiteralDoubleAddress(
1346 right.GetConstant()->AsDoubleConstant()->GetValue()));
1347 } else {
1348 DCHECK(right.IsDoubleStackSlot());
1349 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1350 Address(CpuRegister(RSP), right.GetStackIndex()));
1351 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001352 break;
1353 }
1354 default:
1355 LOG(FATAL) << "Unexpected condition type " << type;
1356 }
1357}
1358
1359template<class LabelType>
1360void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1361 LabelType* true_target_in,
1362 LabelType* false_target_in) {
1363 // Generated branching requires both targets to be explicit. If either of the
1364 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1365 LabelType fallthrough_target;
1366 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1367 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1368
1369 // Generate the comparison to set the CC.
1370 GenerateCompareTest(condition);
1371
1372 // Now generate the correct jump(s).
1373 Primitive::Type type = condition->InputAt(0)->GetType();
1374 switch (type) {
1375 case Primitive::kPrimLong: {
1376 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1377 break;
1378 }
1379 case Primitive::kPrimFloat: {
1380 GenerateFPJumps(condition, true_target, false_target);
1381 break;
1382 }
1383 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001384 GenerateFPJumps(condition, true_target, false_target);
1385 break;
1386 }
1387 default:
1388 LOG(FATAL) << "Unexpected condition type " << type;
1389 }
1390
David Brazdil0debae72015-11-12 18:37:00 +00001391 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001392 __ jmp(false_target);
1393 }
David Brazdil0debae72015-11-12 18:37:00 +00001394
1395 if (fallthrough_target.IsLinked()) {
1396 __ Bind(&fallthrough_target);
1397 }
Mark Mendellc4701932015-04-10 13:18:51 -04001398}
1399
David Brazdil0debae72015-11-12 18:37:00 +00001400static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1401 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1402 // are set only strictly before `branch`. We can't use the eflags on long
1403 // conditions if they are materialized due to the complex branching.
1404 return cond->IsCondition() &&
1405 cond->GetNext() == branch &&
1406 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1407}
1408
Mark Mendell152408f2015-12-31 12:28:50 -05001409template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001410void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001411 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001412 LabelType* true_target,
1413 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001414 HInstruction* cond = instruction->InputAt(condition_input_index);
1415
1416 if (true_target == nullptr && false_target == nullptr) {
1417 // Nothing to do. The code always falls through.
1418 return;
1419 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001420 // Constant condition, statically compared against "true" (integer value 1).
1421 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001422 if (true_target != nullptr) {
1423 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001424 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001425 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001426 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001427 if (false_target != nullptr) {
1428 __ jmp(false_target);
1429 }
1430 }
1431 return;
1432 }
1433
1434 // The following code generates these patterns:
1435 // (1) true_target == nullptr && false_target != nullptr
1436 // - opposite condition true => branch to false_target
1437 // (2) true_target != nullptr && false_target == nullptr
1438 // - condition true => branch to true_target
1439 // (3) true_target != nullptr && false_target != nullptr
1440 // - condition true => branch to true_target
1441 // - branch to false_target
1442 if (IsBooleanValueOrMaterializedCondition(cond)) {
1443 if (AreEflagsSetFrom(cond, instruction)) {
1444 if (true_target == nullptr) {
1445 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1446 } else {
1447 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1448 }
1449 } else {
1450 // Materialized condition, compare against 0.
1451 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1452 if (lhs.IsRegister()) {
1453 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1454 } else {
1455 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1456 }
1457 if (true_target == nullptr) {
1458 __ j(kEqual, false_target);
1459 } else {
1460 __ j(kNotEqual, true_target);
1461 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001462 }
1463 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001464 // Condition has not been materialized, use its inputs as the
1465 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001466 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001467
David Brazdil0debae72015-11-12 18:37:00 +00001468 // If this is a long or FP comparison that has been folded into
1469 // the HCondition, generate the comparison directly.
1470 Primitive::Type type = condition->InputAt(0)->GetType();
1471 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1472 GenerateCompareTestAndBranch(condition, true_target, false_target);
1473 return;
1474 }
1475
1476 Location lhs = condition->GetLocations()->InAt(0);
1477 Location rhs = condition->GetLocations()->InAt(1);
1478 if (rhs.IsRegister()) {
1479 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1480 } else if (rhs.IsConstant()) {
1481 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001482 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001483 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001484 __ cmpl(lhs.AsRegister<CpuRegister>(),
1485 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1486 }
1487 if (true_target == nullptr) {
1488 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1489 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001490 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001491 }
Dave Allison20dfc792014-06-16 20:44:29 -07001492 }
David Brazdil0debae72015-11-12 18:37:00 +00001493
1494 // If neither branch falls through (case 3), the conditional branch to `true_target`
1495 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1496 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001497 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001498 }
1499}
1500
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001501void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001502 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1503 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504 locations->SetInAt(0, Location::Any());
1505 }
1506}
1507
1508void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001509 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1510 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1511 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1512 nullptr : codegen_->GetLabelOf(true_successor);
1513 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1514 nullptr : codegen_->GetLabelOf(false_successor);
1515 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001516}
1517
1518void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1519 LocationSummary* locations = new (GetGraph()->GetArena())
1520 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001521 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001522 locations->SetInAt(0, Location::Any());
1523 }
1524}
1525
1526void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001527 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001528 GenerateTestAndBranch<Label>(deoptimize,
1529 /* condition_input_index */ 0,
1530 slow_path->GetEntryLabel(),
1531 /* false_target */ nullptr);
1532}
1533
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001534static bool SelectCanUseCMOV(HSelect* select) {
1535 // There are no conditional move instructions for XMMs.
1536 if (Primitive::IsFloatingPointType(select->GetType())) {
1537 return false;
1538 }
1539
1540 // A FP condition doesn't generate the single CC that we need.
1541 HInstruction* condition = select->GetCondition();
1542 if (condition->IsCondition() &&
1543 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1544 return false;
1545 }
1546
1547 // We can generate a CMOV for this Select.
1548 return true;
1549}
1550
David Brazdil74eb1b22015-12-14 11:44:01 +00001551void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1553 if (Primitive::IsFloatingPointType(select->GetType())) {
1554 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001555 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001556 } else {
1557 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001558 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001559 if (select->InputAt(1)->IsConstant()) {
1560 locations->SetInAt(1, Location::RequiresRegister());
1561 } else {
1562 locations->SetInAt(1, Location::Any());
1563 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001564 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001565 locations->SetInAt(1, Location::Any());
1566 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001567 }
1568 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1569 locations->SetInAt(2, Location::RequiresRegister());
1570 }
1571 locations->SetOut(Location::SameAsFirstInput());
1572}
1573
1574void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1575 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001576 if (SelectCanUseCMOV(select)) {
1577 // If both the condition and the source types are integer, we can generate
1578 // a CMOV to implement Select.
1579 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001580 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001581 DCHECK(locations->InAt(0).Equals(locations->Out()));
1582
1583 HInstruction* select_condition = select->GetCondition();
1584 Condition cond = kNotEqual;
1585
1586 // Figure out how to test the 'condition'.
1587 if (select_condition->IsCondition()) {
1588 HCondition* condition = select_condition->AsCondition();
1589 if (!condition->IsEmittedAtUseSite()) {
1590 // This was a previously materialized condition.
1591 // Can we use the existing condition code?
1592 if (AreEflagsSetFrom(condition, select)) {
1593 // Materialization was the previous instruction. Condition codes are right.
1594 cond = X86_64IntegerCondition(condition->GetCondition());
1595 } else {
1596 // No, we have to recreate the condition code.
1597 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1598 __ testl(cond_reg, cond_reg);
1599 }
1600 } else {
1601 GenerateCompareTest(condition);
1602 cond = X86_64IntegerCondition(condition->GetCondition());
1603 }
1604 } else {
1605 // Must be a boolean condition, which needs to be compared to 0.
1606 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1607 __ testl(cond_reg, cond_reg);
1608 }
1609
1610 // If the condition is true, overwrite the output, which already contains false.
1611 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001612 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1613 if (value_true_loc.IsRegister()) {
1614 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1615 } else {
1616 __ cmov(cond,
1617 value_false,
1618 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1619 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001620 } else {
1621 NearLabel false_target;
1622 GenerateTestAndBranch<NearLabel>(select,
1623 /* condition_input_index */ 2,
1624 /* true_target */ nullptr,
1625 &false_target);
1626 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1627 __ Bind(&false_target);
1628 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001629}
1630
David Srbecky0cf44932015-12-09 14:09:59 +00001631void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1632 new (GetGraph()->GetArena()) LocationSummary(info);
1633}
1634
David Srbeckyd28f4a02016-03-14 17:14:24 +00001635void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1636 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001637}
1638
1639void CodeGeneratorX86_64::GenerateNop() {
1640 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001641}
1642
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001643void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001644 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001645 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001646 // Handle the long/FP comparisons made in instruction simplification.
1647 switch (cond->InputAt(0)->GetType()) {
1648 case Primitive::kPrimLong:
1649 locations->SetInAt(0, Location::RequiresRegister());
1650 locations->SetInAt(1, Location::Any());
1651 break;
1652 case Primitive::kPrimFloat:
1653 case Primitive::kPrimDouble:
1654 locations->SetInAt(0, Location::RequiresFpuRegister());
1655 locations->SetInAt(1, Location::Any());
1656 break;
1657 default:
1658 locations->SetInAt(0, Location::RequiresRegister());
1659 locations->SetInAt(1, Location::Any());
1660 break;
1661 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001662 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001663 locations->SetOut(Location::RequiresRegister());
1664 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001665}
1666
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001667void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001668 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001669 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001670 }
Mark Mendellc4701932015-04-10 13:18:51 -04001671
1672 LocationSummary* locations = cond->GetLocations();
1673 Location lhs = locations->InAt(0);
1674 Location rhs = locations->InAt(1);
1675 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001676 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001677
1678 switch (cond->InputAt(0)->GetType()) {
1679 default:
1680 // Integer case.
1681
1682 // Clear output register: setcc only sets the low byte.
1683 __ xorl(reg, reg);
1684
1685 if (rhs.IsRegister()) {
1686 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1687 } else if (rhs.IsConstant()) {
1688 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001689 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001690 } else {
1691 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1692 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001693 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001694 return;
1695 case Primitive::kPrimLong:
1696 // Clear output register: setcc only sets the low byte.
1697 __ xorl(reg, reg);
1698
1699 if (rhs.IsRegister()) {
1700 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1701 } else if (rhs.IsConstant()) {
1702 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001703 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001704 } else {
1705 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1706 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001707 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001708 return;
1709 case Primitive::kPrimFloat: {
1710 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1711 if (rhs.IsConstant()) {
1712 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1713 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1714 } else if (rhs.IsStackSlot()) {
1715 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1716 } else {
1717 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1718 }
1719 GenerateFPJumps(cond, &true_label, &false_label);
1720 break;
1721 }
1722 case Primitive::kPrimDouble: {
1723 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1724 if (rhs.IsConstant()) {
1725 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1726 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1727 } else if (rhs.IsDoubleStackSlot()) {
1728 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1729 } else {
1730 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1731 }
1732 GenerateFPJumps(cond, &true_label, &false_label);
1733 break;
1734 }
1735 }
1736
1737 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001738 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001739
Roland Levillain4fa13f62015-07-06 18:11:54 +01001740 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001741 __ Bind(&false_label);
1742 __ xorl(reg, reg);
1743 __ jmp(&done_label);
1744
Roland Levillain4fa13f62015-07-06 18:11:54 +01001745 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001746 __ Bind(&true_label);
1747 __ movl(reg, Immediate(1));
1748 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001749}
1750
1751void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001752 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001753}
1754
1755void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001756 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001757}
1758
1759void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001760 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001761}
1762
1763void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001797}
1798
Aart Bike9f37602015-10-09 11:15:55 -07001799void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001801}
1802
1803void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001805}
1806
1807void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001809}
1810
1811void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001813}
1814
1815void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001831void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001834 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001835 case Primitive::kPrimBoolean:
1836 case Primitive::kPrimByte:
1837 case Primitive::kPrimShort:
1838 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001839 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001840 case Primitive::kPrimLong: {
1841 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001842 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1844 break;
1845 }
1846 case Primitive::kPrimFloat:
1847 case Primitive::kPrimDouble: {
1848 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001849 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001850 locations->SetOut(Location::RequiresRegister());
1851 break;
1852 }
1853 default:
1854 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1855 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001856}
1857
1858void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001859 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001861 Location left = locations->InAt(0);
1862 Location right = locations->InAt(1);
1863
Mark Mendell0c9497d2015-08-21 09:30:05 -04001864 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001865 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001866 Condition less_cond = kLess;
1867
Calin Juravleddb7df22014-11-25 20:56:51 +00001868 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001869 case Primitive::kPrimBoolean:
1870 case Primitive::kPrimByte:
1871 case Primitive::kPrimShort:
1872 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001873 case Primitive::kPrimInt: {
1874 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1875 if (right.IsConstant()) {
1876 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1877 codegen_->Compare32BitValue(left_reg, value);
1878 } else if (right.IsStackSlot()) {
1879 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1880 } else {
1881 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1882 }
1883 break;
1884 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001885 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001886 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1887 if (right.IsConstant()) {
1888 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001889 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001890 } else if (right.IsDoubleStackSlot()) {
1891 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001892 } else {
1893 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1894 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001895 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001896 }
1897 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001898 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1899 if (right.IsConstant()) {
1900 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1901 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1902 } else if (right.IsStackSlot()) {
1903 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1904 } else {
1905 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1906 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001907 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001908 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001909 break;
1910 }
1911 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001912 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1913 if (right.IsConstant()) {
1914 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1915 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1916 } else if (right.IsDoubleStackSlot()) {
1917 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1918 } else {
1919 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1920 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001922 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001923 break;
1924 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001925 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001926 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001927 }
Aart Bika19616e2016-02-01 18:57:58 -08001928
Calin Juravleddb7df22014-11-25 20:56:51 +00001929 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001930 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001931 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001932
Calin Juravle91debbc2014-11-26 19:01:09 +00001933 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001934 __ movl(out, Immediate(1));
1935 __ jmp(&done);
1936
1937 __ Bind(&less);
1938 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001939
1940 __ Bind(&done);
1941}
1942
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001943void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001946 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947}
1948
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001950 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951}
1952
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001953void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1956 locations->SetOut(Location::ConstantLocation(constant));
1957}
1958
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001959void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001960 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001961}
1962
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001963void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001964 LocationSummary* locations =
1965 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001966 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967}
1968
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001969void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001970 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971}
1972
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001973void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1976 locations->SetOut(Location::ConstantLocation(constant));
1977}
1978
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001979void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001980 // Will be generated at use site.
1981}
1982
1983void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1984 LocationSummary* locations =
1985 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1986 locations->SetOut(Location::ConstantLocation(constant));
1987}
1988
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001989void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1990 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001991 // Will be generated at use site.
1992}
1993
Calin Juravle27df7582015-04-17 19:12:31 +01001994void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1995 memory_barrier->SetLocations(nullptr);
1996}
1997
1998void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001999 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002000}
2001
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2003 ret->SetLocations(nullptr);
2004}
2005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002007 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008}
2009
2010void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002013 switch (ret->InputAt(0)->GetType()) {
2014 case Primitive::kPrimBoolean:
2015 case Primitive::kPrimByte:
2016 case Primitive::kPrimChar:
2017 case Primitive::kPrimShort:
2018 case Primitive::kPrimInt:
2019 case Primitive::kPrimNot:
2020 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002021 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022 break;
2023
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002024 case Primitive::kPrimFloat:
2025 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002026 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002027 break;
2028
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002029 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002030 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002031 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002032}
2033
2034void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2035 if (kIsDebugBuild) {
2036 switch (ret->InputAt(0)->GetType()) {
2037 case Primitive::kPrimBoolean:
2038 case Primitive::kPrimByte:
2039 case Primitive::kPrimChar:
2040 case Primitive::kPrimShort:
2041 case Primitive::kPrimInt:
2042 case Primitive::kPrimNot:
2043 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002045 break;
2046
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002047 case Primitive::kPrimFloat:
2048 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002050 XMM0);
2051 break;
2052
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002053 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002054 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002055 }
2056 }
2057 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002058}
2059
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002060Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2061 switch (type) {
2062 case Primitive::kPrimBoolean:
2063 case Primitive::kPrimByte:
2064 case Primitive::kPrimChar:
2065 case Primitive::kPrimShort:
2066 case Primitive::kPrimInt:
2067 case Primitive::kPrimNot:
2068 case Primitive::kPrimLong:
2069 return Location::RegisterLocation(RAX);
2070
2071 case Primitive::kPrimVoid:
2072 return Location::NoLocation();
2073
2074 case Primitive::kPrimDouble:
2075 case Primitive::kPrimFloat:
2076 return Location::FpuRegisterLocation(XMM0);
2077 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002078
2079 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002080}
2081
2082Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2083 return Location::RegisterLocation(kMethodRegisterArgument);
2084}
2085
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002086Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002087 switch (type) {
2088 case Primitive::kPrimBoolean:
2089 case Primitive::kPrimByte:
2090 case Primitive::kPrimChar:
2091 case Primitive::kPrimShort:
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimNot: {
2094 uint32_t index = gp_index_++;
2095 stack_index_++;
2096 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002097 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098 } else {
2099 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2100 }
2101 }
2102
2103 case Primitive::kPrimLong: {
2104 uint32_t index = gp_index_;
2105 stack_index_ += 2;
2106 if (index < calling_convention.GetNumberOfRegisters()) {
2107 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002108 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002109 } else {
2110 gp_index_ += 2;
2111 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2112 }
2113 }
2114
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002115 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002116 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002117 stack_index_++;
2118 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002119 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002120 } else {
2121 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2122 }
2123 }
2124
2125 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002126 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 stack_index_ += 2;
2128 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002130 } else {
2131 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2132 }
2133 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002134
2135 case Primitive::kPrimVoid:
2136 LOG(FATAL) << "Unexpected parameter type " << type;
2137 break;
2138 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002139 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002140}
2141
Calin Juravle175dc732015-08-25 15:42:32 +01002142void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2143 // The trampoline uses the same calling convention as dex calling conventions,
2144 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2145 // the method_idx.
2146 HandleInvoke(invoke);
2147}
2148
2149void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2150 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2151}
2152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002153void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002154 // Explicit clinit checks triggered by static invokes must have been pruned by
2155 // art::PrepareForRegisterAllocation.
2156 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002157
Mark Mendellfb8d2792015-03-31 22:16:59 -04002158 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002159 if (intrinsic.TryDispatch(invoke)) {
2160 return;
2161 }
2162
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002163 HandleInvoke(invoke);
2164}
2165
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002166static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2167 if (invoke->GetLocations()->Intrinsified()) {
2168 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2169 intrinsic.Dispatch(invoke);
2170 return true;
2171 }
2172 return false;
2173}
2174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002175void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002176 // Explicit clinit checks triggered by static invokes must have been pruned by
2177 // art::PrepareForRegisterAllocation.
2178 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002179
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002180 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2181 return;
2182 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002184 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002185 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002186 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002188}
2189
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002190void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002191 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002192 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002193}
2194
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002195void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002196 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002197 if (intrinsic.TryDispatch(invoke)) {
2198 return;
2199 }
2200
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002201 HandleInvoke(invoke);
2202}
2203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2206 return;
2207 }
2208
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002209 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002210 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212}
2213
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2215 HandleInvoke(invoke);
2216 // Add the hidden argument.
2217 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2218}
2219
2220void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2221 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002222 LocationSummary* locations = invoke->GetLocations();
2223 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2224 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002225 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2226 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227 Location receiver = locations->InAt(0);
2228 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2229
Roland Levillain0d5a2812015-11-13 10:07:31 +00002230 // Set the hidden argument. This is safe to do this here, as RAX
2231 // won't be modified thereafter, before the `call` instruction.
2232 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002233 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002235 if (receiver.IsStackSlot()) {
2236 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002237 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238 __ movl(temp, Address(temp, class_offset));
2239 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002240 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002243 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 // Instead of simply (possibly) unpoisoning `temp` here, we should
2245 // emit a read barrier for the previous class reference load.
2246 // However this is not required in practice, as this is an
2247 // intermediate/temporary reference and because the current
2248 // concurrent copying collector keeps the from-space memory
2249 // intact/accessible until the end of the marking phase (the
2250 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002251 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002253 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 __ call(Address(temp,
2256 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257
2258 DCHECK(!codegen_->IsLeafMethod());
2259 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2260}
2261
Roland Levillain88cb1752014-10-20 16:36:47 +01002262void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2263 LocationSummary* locations =
2264 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2265 switch (neg->GetResultType()) {
2266 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002267 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002268 locations->SetInAt(0, Location::RequiresRegister());
2269 locations->SetOut(Location::SameAsFirstInput());
2270 break;
2271
Roland Levillain88cb1752014-10-20 16:36:47 +01002272 case Primitive::kPrimFloat:
2273 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002274 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002275 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002276 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002277 break;
2278
2279 default:
2280 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2281 }
2282}
2283
2284void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2285 LocationSummary* locations = neg->GetLocations();
2286 Location out = locations->Out();
2287 Location in = locations->InAt(0);
2288 switch (neg->GetResultType()) {
2289 case Primitive::kPrimInt:
2290 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002291 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002293 break;
2294
2295 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002296 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002297 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002298 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002299 break;
2300
Roland Levillain5368c212014-11-27 15:03:41 +00002301 case Primitive::kPrimFloat: {
2302 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002303 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002304 // Implement float negation with an exclusive or with value
2305 // 0x80000000 (mask for bit 31, representing the sign of a
2306 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002307 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002309 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002310 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002311
Roland Levillain5368c212014-11-27 15:03:41 +00002312 case Primitive::kPrimDouble: {
2313 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002314 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002315 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002316 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002317 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002318 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002320 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002321 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002322
2323 default:
2324 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2325 }
2326}
2327
Roland Levillaindff1f282014-11-05 14:15:05 +00002328void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2329 LocationSummary* locations =
2330 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2331 Primitive::Type result_type = conversion->GetResultType();
2332 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002333 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002334
David Brazdilb2bd1c52015-03-25 11:17:37 +00002335 // The Java language does not allow treating boolean as an integral type but
2336 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002337
Roland Levillaindff1f282014-11-05 14:15:05 +00002338 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002339 case Primitive::kPrimByte:
2340 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002341 case Primitive::kPrimLong:
2342 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002343 case Primitive::kPrimBoolean:
2344 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002345 case Primitive::kPrimShort:
2346 case Primitive::kPrimInt:
2347 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002348 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002349 locations->SetInAt(0, Location::Any());
2350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2351 break;
2352
2353 default:
2354 LOG(FATAL) << "Unexpected type conversion from " << input_type
2355 << " to " << result_type;
2356 }
2357 break;
2358
Roland Levillain01a8d712014-11-14 16:27:39 +00002359 case Primitive::kPrimShort:
2360 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002361 case Primitive::kPrimLong:
2362 // Type conversion from long to short 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 Levillain01a8d712014-11-14 16:27:39 +00002365 case Primitive::kPrimByte:
2366 case Primitive::kPrimInt:
2367 case Primitive::kPrimChar:
2368 // Processing a Dex `int-to-short' instruction.
2369 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 Levillain946e1432014-11-11 17:35:19 +00002379 case Primitive::kPrimInt:
2380 switch (input_type) {
2381 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002382 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002383 locations->SetInAt(0, Location::Any());
2384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2385 break;
2386
2387 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002388 // Processing a Dex `float-to-int' instruction.
2389 locations->SetInAt(0, Location::RequiresFpuRegister());
2390 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002391 break;
2392
Roland Levillain946e1432014-11-11 17:35:19 +00002393 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002394 // Processing a Dex `double-to-int' instruction.
2395 locations->SetInAt(0, Location::RequiresFpuRegister());
2396 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002397 break;
2398
2399 default:
2400 LOG(FATAL) << "Unexpected type conversion from " << input_type
2401 << " to " << result_type;
2402 }
2403 break;
2404
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 case Primitive::kPrimLong:
2406 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002407 case Primitive::kPrimBoolean:
2408 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002409 case Primitive::kPrimByte:
2410 case Primitive::kPrimShort:
2411 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002412 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002413 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002414 // TODO: We would benefit from a (to-be-implemented)
2415 // Location::RegisterOrStackSlot requirement for this input.
2416 locations->SetInAt(0, Location::RequiresRegister());
2417 locations->SetOut(Location::RequiresRegister());
2418 break;
2419
2420 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002421 // Processing a Dex `float-to-long' instruction.
2422 locations->SetInAt(0, Location::RequiresFpuRegister());
2423 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002424 break;
2425
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002427 // Processing a Dex `double-to-long' instruction.
2428 locations->SetInAt(0, Location::RequiresFpuRegister());
2429 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 break;
2431
2432 default:
2433 LOG(FATAL) << "Unexpected type conversion from " << input_type
2434 << " to " << result_type;
2435 }
2436 break;
2437
Roland Levillain981e4542014-11-14 11:47:14 +00002438 case Primitive::kPrimChar:
2439 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002440 case Primitive::kPrimLong:
2441 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002442 case Primitive::kPrimBoolean:
2443 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002444 case Primitive::kPrimByte:
2445 case Primitive::kPrimShort:
2446 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002447 // Processing a Dex `int-to-char' instruction.
2448 locations->SetInAt(0, Location::Any());
2449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2450 break;
2451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 }
2456 break;
2457
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimShort:
2464 case Primitive::kPrimInt:
2465 case Primitive::kPrimChar:
2466 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002467 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002468 locations->SetOut(Location::RequiresFpuRegister());
2469 break;
2470
2471 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002472 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002473 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002474 locations->SetOut(Location::RequiresFpuRegister());
2475 break;
2476
Roland Levillaincff13742014-11-17 14:32:17 +00002477 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002478 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002479 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002480 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002481 break;
2482
2483 default:
2484 LOG(FATAL) << "Unexpected type conversion from " << input_type
2485 << " to " << result_type;
2486 };
2487 break;
2488
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002490 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002491 case Primitive::kPrimBoolean:
2492 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002493 case Primitive::kPrimByte:
2494 case Primitive::kPrimShort:
2495 case Primitive::kPrimInt:
2496 case Primitive::kPrimChar:
2497 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002498 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002499 locations->SetOut(Location::RequiresFpuRegister());
2500 break;
2501
2502 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002503 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002504 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002505 locations->SetOut(Location::RequiresFpuRegister());
2506 break;
2507
Roland Levillaincff13742014-11-17 14:32:17 +00002508 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002509 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002510 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002511 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002512 break;
2513
2514 default:
2515 LOG(FATAL) << "Unexpected type conversion from " << input_type
2516 << " to " << result_type;
2517 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002518 break;
2519
2520 default:
2521 LOG(FATAL) << "Unexpected type conversion from " << input_type
2522 << " to " << result_type;
2523 }
2524}
2525
2526void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2527 LocationSummary* locations = conversion->GetLocations();
2528 Location out = locations->Out();
2529 Location in = locations->InAt(0);
2530 Primitive::Type result_type = conversion->GetResultType();
2531 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002532 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002533 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002534 case Primitive::kPrimByte:
2535 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002536 case Primitive::kPrimLong:
2537 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002538 case Primitive::kPrimBoolean:
2539 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
2542 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002543 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002544 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002545 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002546 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002547 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002548 Address(CpuRegister(RSP), in.GetStackIndex()));
2549 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002550 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002551 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002552 }
2553 break;
2554
2555 default:
2556 LOG(FATAL) << "Unexpected type conversion from " << input_type
2557 << " to " << result_type;
2558 }
2559 break;
2560
Roland Levillain01a8d712014-11-14 16:27:39 +00002561 case Primitive::kPrimShort:
2562 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002563 case Primitive::kPrimLong:
2564 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002565 case Primitive::kPrimBoolean:
2566 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002567 case Primitive::kPrimByte:
2568 case Primitive::kPrimInt:
2569 case Primitive::kPrimChar:
2570 // Processing a Dex `int-to-short' instruction.
2571 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002572 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002573 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002575 Address(CpuRegister(RSP), in.GetStackIndex()));
2576 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002577 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002578 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002579 }
2580 break;
2581
2582 default:
2583 LOG(FATAL) << "Unexpected type conversion from " << input_type
2584 << " to " << result_type;
2585 }
2586 break;
2587
Roland Levillain946e1432014-11-11 17:35:19 +00002588 case Primitive::kPrimInt:
2589 switch (input_type) {
2590 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002591 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002592 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002594 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002596 Address(CpuRegister(RSP), in.GetStackIndex()));
2597 } else {
2598 DCHECK(in.IsConstant());
2599 DCHECK(in.GetConstant()->IsLongConstant());
2600 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002602 }
2603 break;
2604
Roland Levillain3f8f9362014-12-02 17:45:01 +00002605 case Primitive::kPrimFloat: {
2606 // Processing a Dex `float-to-int' instruction.
2607 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2608 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002609 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002610
2611 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002612 // if input >= (float)INT_MAX goto done
2613 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002614 __ j(kAboveEqual, &done);
2615 // if input == NaN goto nan
2616 __ j(kUnordered, &nan);
2617 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002618 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002619 __ jmp(&done);
2620 __ Bind(&nan);
2621 // output = 0
2622 __ xorl(output, output);
2623 __ Bind(&done);
2624 break;
2625 }
2626
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002627 case Primitive::kPrimDouble: {
2628 // Processing a Dex `double-to-int' instruction.
2629 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2630 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002631 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002632
2633 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002634 // if input >= (double)INT_MAX goto done
2635 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002636 __ j(kAboveEqual, &done);
2637 // if input == NaN goto nan
2638 __ j(kUnordered, &nan);
2639 // output = double-to-int-truncate(input)
2640 __ cvttsd2si(output, input);
2641 __ jmp(&done);
2642 __ Bind(&nan);
2643 // output = 0
2644 __ xorl(output, output);
2645 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002646 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002647 }
Roland Levillain946e1432014-11-11 17:35:19 +00002648
2649 default:
2650 LOG(FATAL) << "Unexpected type conversion from " << input_type
2651 << " to " << result_type;
2652 }
2653 break;
2654
Roland Levillaindff1f282014-11-05 14:15:05 +00002655 case Primitive::kPrimLong:
2656 switch (input_type) {
2657 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002658 case Primitive::kPrimBoolean:
2659 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002660 case Primitive::kPrimByte:
2661 case Primitive::kPrimShort:
2662 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002663 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002664 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002665 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002667 break;
2668
Roland Levillain624279f2014-12-04 11:54:28 +00002669 case Primitive::kPrimFloat: {
2670 // Processing a Dex `float-to-long' instruction.
2671 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2672 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002673 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002674
Mark Mendell92e83bf2015-05-07 11:25:03 -04002675 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002676 // if input >= (float)LONG_MAX goto done
2677 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002678 __ j(kAboveEqual, &done);
2679 // if input == NaN goto nan
2680 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002681 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002682 __ cvttss2si(output, input, true);
2683 __ jmp(&done);
2684 __ Bind(&nan);
2685 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002686 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002687 __ Bind(&done);
2688 break;
2689 }
2690
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002691 case Primitive::kPrimDouble: {
2692 // Processing a Dex `double-to-long' instruction.
2693 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2694 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002695 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002696
Mark Mendell92e83bf2015-05-07 11:25:03 -04002697 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002698 // if input >= (double)LONG_MAX goto done
2699 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002700 __ j(kAboveEqual, &done);
2701 // if input == NaN goto nan
2702 __ j(kUnordered, &nan);
2703 // output = double-to-long-truncate(input)
2704 __ cvttsd2si(output, input, true);
2705 __ jmp(&done);
2706 __ Bind(&nan);
2707 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002708 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002709 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002710 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002711 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002712
2713 default:
2714 LOG(FATAL) << "Unexpected type conversion from " << input_type
2715 << " to " << result_type;
2716 }
2717 break;
2718
Roland Levillain981e4542014-11-14 11:47:14 +00002719 case Primitive::kPrimChar:
2720 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002721 case Primitive::kPrimLong:
2722 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002723 case Primitive::kPrimBoolean:
2724 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002725 case Primitive::kPrimByte:
2726 case Primitive::kPrimShort:
2727 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002728 // Processing a Dex `int-to-char' instruction.
2729 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002731 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002733 Address(CpuRegister(RSP), in.GetStackIndex()));
2734 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002735 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002736 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002737 }
2738 break;
2739
2740 default:
2741 LOG(FATAL) << "Unexpected type conversion from " << input_type
2742 << " to " << result_type;
2743 }
2744 break;
2745
Roland Levillaindff1f282014-11-05 14:15:05 +00002746 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002747 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002748 case Primitive::kPrimBoolean:
2749 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002750 case Primitive::kPrimByte:
2751 case Primitive::kPrimShort:
2752 case Primitive::kPrimInt:
2753 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002754 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002755 if (in.IsRegister()) {
2756 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2757 } else if (in.IsConstant()) {
2758 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2759 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002760 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002761 } else {
2762 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2763 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2764 }
Roland Levillaincff13742014-11-17 14:32:17 +00002765 break;
2766
2767 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002768 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002769 if (in.IsRegister()) {
2770 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2771 } else if (in.IsConstant()) {
2772 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2773 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002774 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002775 } else {
2776 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2777 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2778 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002779 break;
2780
Roland Levillaincff13742014-11-17 14:32:17 +00002781 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002782 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002783 if (in.IsFpuRegister()) {
2784 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2785 } else if (in.IsConstant()) {
2786 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2787 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002788 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002789 } else {
2790 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2791 Address(CpuRegister(RSP), in.GetStackIndex()));
2792 }
Roland Levillaincff13742014-11-17 14:32:17 +00002793 break;
2794
2795 default:
2796 LOG(FATAL) << "Unexpected type conversion from " << input_type
2797 << " to " << result_type;
2798 };
2799 break;
2800
Roland Levillaindff1f282014-11-05 14:15:05 +00002801 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002802 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002803 case Primitive::kPrimBoolean:
2804 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002805 case Primitive::kPrimByte:
2806 case Primitive::kPrimShort:
2807 case Primitive::kPrimInt:
2808 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002809 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002810 if (in.IsRegister()) {
2811 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2812 } else if (in.IsConstant()) {
2813 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2814 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002815 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002816 } else {
2817 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2818 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2819 }
Roland Levillaincff13742014-11-17 14:32:17 +00002820 break;
2821
2822 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002823 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002824 if (in.IsRegister()) {
2825 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2826 } else if (in.IsConstant()) {
2827 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2828 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002829 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002830 } else {
2831 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2832 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2833 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002834 break;
2835
Roland Levillaincff13742014-11-17 14:32:17 +00002836 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002837 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002838 if (in.IsFpuRegister()) {
2839 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2840 } else if (in.IsConstant()) {
2841 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2842 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002843 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002844 } else {
2845 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2846 Address(CpuRegister(RSP), in.GetStackIndex()));
2847 }
Roland Levillaincff13742014-11-17 14:32:17 +00002848 break;
2849
2850 default:
2851 LOG(FATAL) << "Unexpected type conversion from " << input_type
2852 << " to " << result_type;
2853 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002854 break;
2855
2856 default:
2857 LOG(FATAL) << "Unexpected type conversion from " << input_type
2858 << " to " << result_type;
2859 }
2860}
2861
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002862void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002863 LocationSummary* locations =
2864 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002865 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002866 case Primitive::kPrimInt: {
2867 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002868 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002870 break;
2871 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002872
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002873 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002874 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002875 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002876 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002878 break;
2879 }
2880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002881 case Primitive::kPrimDouble:
2882 case Primitive::kPrimFloat: {
2883 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002884 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002885 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002886 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002887 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002888
2889 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002890 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002891 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002892}
2893
2894void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2895 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002896 Location first = locations->InAt(0);
2897 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002898 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002899
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002900 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002901 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002902 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002903 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2904 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002905 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2906 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002907 } else {
2908 __ leal(out.AsRegister<CpuRegister>(), Address(
2909 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2910 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002911 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002912 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2913 __ addl(out.AsRegister<CpuRegister>(),
2914 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2915 } else {
2916 __ leal(out.AsRegister<CpuRegister>(), Address(
2917 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2918 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002919 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002920 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002922 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002923 break;
2924 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002925
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002926 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002927 if (second.IsRegister()) {
2928 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2929 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002930 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2931 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002932 } else {
2933 __ leaq(out.AsRegister<CpuRegister>(), Address(
2934 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2935 }
2936 } else {
2937 DCHECK(second.IsConstant());
2938 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2939 int32_t int32_value = Low32Bits(value);
2940 DCHECK_EQ(int32_value, value);
2941 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2942 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2943 } else {
2944 __ leaq(out.AsRegister<CpuRegister>(), Address(
2945 first.AsRegister<CpuRegister>(), int32_value));
2946 }
2947 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002948 break;
2949 }
2950
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002952 if (second.IsFpuRegister()) {
2953 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2954 } else if (second.IsConstant()) {
2955 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002956 codegen_->LiteralFloatAddress(
2957 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002958 } else {
2959 DCHECK(second.IsStackSlot());
2960 __ addss(first.AsFpuRegister<XmmRegister>(),
2961 Address(CpuRegister(RSP), second.GetStackIndex()));
2962 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002963 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002964 }
2965
2966 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002967 if (second.IsFpuRegister()) {
2968 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2969 } else if (second.IsConstant()) {
2970 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002971 codegen_->LiteralDoubleAddress(
2972 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002973 } else {
2974 DCHECK(second.IsDoubleStackSlot());
2975 __ addsd(first.AsFpuRegister<XmmRegister>(),
2976 Address(CpuRegister(RSP), second.GetStackIndex()));
2977 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 break;
2979 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002980
2981 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002983 }
2984}
2985
2986void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002987 LocationSummary* locations =
2988 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002989 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002990 case Primitive::kPrimInt: {
2991 locations->SetInAt(0, Location::RequiresRegister());
2992 locations->SetInAt(1, Location::Any());
2993 locations->SetOut(Location::SameAsFirstInput());
2994 break;
2995 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002996 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002997 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002998 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002999 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003000 break;
3001 }
Calin Juravle11351682014-10-23 15:38:15 +01003002 case Primitive::kPrimFloat:
3003 case Primitive::kPrimDouble: {
3004 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003005 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003006 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003007 break;
Calin Juravle11351682014-10-23 15:38:15 +01003008 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003009 default:
Calin Juravle11351682014-10-23 15:38:15 +01003010 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003012}
3013
3014void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3015 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003016 Location first = locations->InAt(0);
3017 Location second = locations->InAt(1);
3018 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003020 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003021 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003023 } else if (second.IsConstant()) {
3024 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003025 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003026 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003027 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003028 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003029 break;
3030 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003031 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003032 if (second.IsConstant()) {
3033 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3034 DCHECK(IsInt<32>(value));
3035 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3036 } else {
3037 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3038 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 break;
3040 }
3041
Calin Juravle11351682014-10-23 15:38:15 +01003042 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003043 if (second.IsFpuRegister()) {
3044 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3045 } else if (second.IsConstant()) {
3046 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003047 codegen_->LiteralFloatAddress(
3048 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003049 } else {
3050 DCHECK(second.IsStackSlot());
3051 __ subss(first.AsFpuRegister<XmmRegister>(),
3052 Address(CpuRegister(RSP), second.GetStackIndex()));
3053 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003054 break;
Calin Juravle11351682014-10-23 15:38:15 +01003055 }
3056
3057 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003058 if (second.IsFpuRegister()) {
3059 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3060 } else if (second.IsConstant()) {
3061 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003062 codegen_->LiteralDoubleAddress(
3063 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003064 } else {
3065 DCHECK(second.IsDoubleStackSlot());
3066 __ subsd(first.AsFpuRegister<XmmRegister>(),
3067 Address(CpuRegister(RSP), second.GetStackIndex()));
3068 }
Calin Juravle11351682014-10-23 15:38:15 +01003069 break;
3070 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003071
3072 default:
Calin Juravle11351682014-10-23 15:38:15 +01003073 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 }
3075}
3076
Calin Juravle34bacdf2014-10-07 20:23:36 +01003077void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3080 switch (mul->GetResultType()) {
3081 case Primitive::kPrimInt: {
3082 locations->SetInAt(0, Location::RequiresRegister());
3083 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003084 if (mul->InputAt(1)->IsIntConstant()) {
3085 // Can use 3 operand multiply.
3086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3087 } else {
3088 locations->SetOut(Location::SameAsFirstInput());
3089 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003090 break;
3091 }
3092 case Primitive::kPrimLong: {
3093 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003094 locations->SetInAt(1, Location::Any());
3095 if (mul->InputAt(1)->IsLongConstant() &&
3096 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003097 // Can use 3 operand multiply.
3098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3099 } else {
3100 locations->SetOut(Location::SameAsFirstInput());
3101 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003102 break;
3103 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003104 case Primitive::kPrimFloat:
3105 case Primitive::kPrimDouble: {
3106 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003107 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003108 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003109 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003110 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003111
3112 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003113 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003114 }
3115}
3116
3117void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3118 LocationSummary* locations = mul->GetLocations();
3119 Location first = locations->InAt(0);
3120 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003121 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003122 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003123 case Primitive::kPrimInt:
3124 // The constant may have ended up in a register, so test explicitly to avoid
3125 // problems where the output may not be the same as the first operand.
3126 if (mul->InputAt(1)->IsIntConstant()) {
3127 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3128 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3129 } else if (second.IsRegister()) {
3130 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003131 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003132 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003133 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003135 __ imull(first.AsRegister<CpuRegister>(),
3136 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137 }
3138 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003140 // The constant may have ended up in a register, so test explicitly to avoid
3141 // problems where the output may not be the same as the first operand.
3142 if (mul->InputAt(1)->IsLongConstant()) {
3143 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3144 if (IsInt<32>(value)) {
3145 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3146 Immediate(static_cast<int32_t>(value)));
3147 } else {
3148 // Have to use the constant area.
3149 DCHECK(first.Equals(out));
3150 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3151 }
3152 } else if (second.IsRegister()) {
3153 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003154 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003155 } else {
3156 DCHECK(second.IsDoubleStackSlot());
3157 DCHECK(first.Equals(out));
3158 __ imulq(first.AsRegister<CpuRegister>(),
3159 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003160 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161 break;
3162 }
3163
Calin Juravleb5bfa962014-10-21 18:02:24 +01003164 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003165 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003166 if (second.IsFpuRegister()) {
3167 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3168 } else if (second.IsConstant()) {
3169 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003170 codegen_->LiteralFloatAddress(
3171 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003172 } else {
3173 DCHECK(second.IsStackSlot());
3174 __ mulss(first.AsFpuRegister<XmmRegister>(),
3175 Address(CpuRegister(RSP), second.GetStackIndex()));
3176 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003177 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003178 }
3179
3180 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003181 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003182 if (second.IsFpuRegister()) {
3183 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3184 } else if (second.IsConstant()) {
3185 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003186 codegen_->LiteralDoubleAddress(
3187 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003188 } else {
3189 DCHECK(second.IsDoubleStackSlot());
3190 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3191 Address(CpuRegister(RSP), second.GetStackIndex()));
3192 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003193 break;
3194 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195
3196 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003197 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198 }
3199}
3200
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003201void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3202 uint32_t stack_adjustment, bool is_float) {
3203 if (source.IsStackSlot()) {
3204 DCHECK(is_float);
3205 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3206 } else if (source.IsDoubleStackSlot()) {
3207 DCHECK(!is_float);
3208 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3209 } else {
3210 // Write the value to the temporary location on the stack and load to FP stack.
3211 if (is_float) {
3212 Location stack_temp = Location::StackSlot(temp_offset);
3213 codegen_->Move(stack_temp, source);
3214 __ flds(Address(CpuRegister(RSP), temp_offset));
3215 } else {
3216 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3217 codegen_->Move(stack_temp, source);
3218 __ fldl(Address(CpuRegister(RSP), temp_offset));
3219 }
3220 }
3221}
3222
3223void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3224 Primitive::Type type = rem->GetResultType();
3225 bool is_float = type == Primitive::kPrimFloat;
3226 size_t elem_size = Primitive::ComponentSize(type);
3227 LocationSummary* locations = rem->GetLocations();
3228 Location first = locations->InAt(0);
3229 Location second = locations->InAt(1);
3230 Location out = locations->Out();
3231
3232 // Create stack space for 2 elements.
3233 // TODO: enhance register allocator to ask for stack temporaries.
3234 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3235
3236 // Load the values to the FP stack in reverse order, using temporaries if needed.
3237 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3238 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3239
3240 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003241 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003242 __ Bind(&retry);
3243 __ fprem();
3244
3245 // Move FP status to AX.
3246 __ fstsw();
3247
3248 // And see if the argument reduction is complete. This is signaled by the
3249 // C2 FPU flag bit set to 0.
3250 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3251 __ j(kNotEqual, &retry);
3252
3253 // We have settled on the final value. Retrieve it into an XMM register.
3254 // Store FP top of stack to real stack.
3255 if (is_float) {
3256 __ fsts(Address(CpuRegister(RSP), 0));
3257 } else {
3258 __ fstl(Address(CpuRegister(RSP), 0));
3259 }
3260
3261 // Pop the 2 items from the FP stack.
3262 __ fucompp();
3263
3264 // Load the value from the stack into an XMM register.
3265 DCHECK(out.IsFpuRegister()) << out;
3266 if (is_float) {
3267 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3268 } else {
3269 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3270 }
3271
3272 // And remove the temporary stack space we allocated.
3273 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3274}
3275
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003276void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3277 DCHECK(instruction->IsDiv() || instruction->IsRem());
3278
3279 LocationSummary* locations = instruction->GetLocations();
3280 Location second = locations->InAt(1);
3281 DCHECK(second.IsConstant());
3282
3283 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3284 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003285 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003286
3287 DCHECK(imm == 1 || imm == -1);
3288
3289 switch (instruction->GetResultType()) {
3290 case Primitive::kPrimInt: {
3291 if (instruction->IsRem()) {
3292 __ xorl(output_register, output_register);
3293 } else {
3294 __ movl(output_register, input_register);
3295 if (imm == -1) {
3296 __ negl(output_register);
3297 }
3298 }
3299 break;
3300 }
3301
3302 case Primitive::kPrimLong: {
3303 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003304 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003305 } else {
3306 __ movq(output_register, input_register);
3307 if (imm == -1) {
3308 __ negq(output_register);
3309 }
3310 }
3311 break;
3312 }
3313
3314 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003315 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316 }
3317}
3318
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003319void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003320 LocationSummary* locations = instruction->GetLocations();
3321 Location second = locations->InAt(1);
3322
3323 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3324 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3325
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003326 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003327 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3328 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003329
3330 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3331
3332 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003333 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334 __ testl(numerator, numerator);
3335 __ cmov(kGreaterEqual, tmp, numerator);
3336 int shift = CTZ(imm);
3337 __ sarl(tmp, Immediate(shift));
3338
3339 if (imm < 0) {
3340 __ negl(tmp);
3341 }
3342
3343 __ movl(output_register, tmp);
3344 } else {
3345 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3346 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3347
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003348 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349 __ addq(rdx, numerator);
3350 __ testq(numerator, numerator);
3351 __ cmov(kGreaterEqual, rdx, numerator);
3352 int shift = CTZ(imm);
3353 __ sarq(rdx, Immediate(shift));
3354
3355 if (imm < 0) {
3356 __ negq(rdx);
3357 }
3358
3359 __ movq(output_register, rdx);
3360 }
3361}
3362
3363void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3364 DCHECK(instruction->IsDiv() || instruction->IsRem());
3365
3366 LocationSummary* locations = instruction->GetLocations();
3367 Location second = locations->InAt(1);
3368
3369 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3370 : locations->GetTemp(0).AsRegister<CpuRegister>();
3371 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3372 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3373 : locations->Out().AsRegister<CpuRegister>();
3374 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3375
3376 DCHECK_EQ(RAX, eax.AsRegister());
3377 DCHECK_EQ(RDX, edx.AsRegister());
3378 if (instruction->IsDiv()) {
3379 DCHECK_EQ(RAX, out.AsRegister());
3380 } else {
3381 DCHECK_EQ(RDX, out.AsRegister());
3382 }
3383
3384 int64_t magic;
3385 int shift;
3386
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003387 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 if (instruction->GetResultType() == Primitive::kPrimInt) {
3389 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3390
3391 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3392
3393 __ movl(numerator, eax);
3394
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003395 __ movl(eax, Immediate(magic));
3396 __ imull(numerator);
3397
3398 if (imm > 0 && magic < 0) {
3399 __ addl(edx, numerator);
3400 } else if (imm < 0 && magic > 0) {
3401 __ subl(edx, numerator);
3402 }
3403
3404 if (shift != 0) {
3405 __ sarl(edx, Immediate(shift));
3406 }
3407
3408 __ movl(eax, edx);
3409 __ shrl(edx, Immediate(31));
3410 __ addl(edx, eax);
3411
3412 if (instruction->IsRem()) {
3413 __ movl(eax, numerator);
3414 __ imull(edx, Immediate(imm));
3415 __ subl(eax, edx);
3416 __ movl(edx, eax);
3417 } else {
3418 __ movl(eax, edx);
3419 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003420 } else {
3421 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3422
3423 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3424
3425 CpuRegister rax = eax;
3426 CpuRegister rdx = edx;
3427
3428 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3429
3430 // Save the numerator.
3431 __ movq(numerator, rax);
3432
3433 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003434 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435
3436 // RDX:RAX = magic * numerator
3437 __ imulq(numerator);
3438
3439 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003440 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003441 __ addq(rdx, numerator);
3442 } else if (imm < 0 && magic > 0) {
3443 // RDX -= numerator
3444 __ subq(rdx, numerator);
3445 }
3446
3447 // Shift if needed.
3448 if (shift != 0) {
3449 __ sarq(rdx, Immediate(shift));
3450 }
3451
3452 // RDX += 1 if RDX < 0
3453 __ movq(rax, rdx);
3454 __ shrq(rdx, Immediate(63));
3455 __ addq(rdx, rax);
3456
3457 if (instruction->IsRem()) {
3458 __ movq(rax, numerator);
3459
3460 if (IsInt<32>(imm)) {
3461 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3462 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003463 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464 }
3465
3466 __ subq(rax, rdx);
3467 __ movq(rdx, rax);
3468 } else {
3469 __ movq(rax, rdx);
3470 }
3471 }
3472}
3473
Calin Juravlebacfec32014-11-14 15:54:36 +00003474void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3475 DCHECK(instruction->IsDiv() || instruction->IsRem());
3476 Primitive::Type type = instruction->GetResultType();
3477 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3478
3479 bool is_div = instruction->IsDiv();
3480 LocationSummary* locations = instruction->GetLocations();
3481
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3483 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003484
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003487
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003489 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003490
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003491 if (imm == 0) {
3492 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3493 } else if (imm == 1 || imm == -1) {
3494 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003495 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003496 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003497 } else {
3498 DCHECK(imm <= -2 || imm >= 2);
3499 GenerateDivRemWithAnyConstant(instruction);
3500 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003501 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003502 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003504 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003505 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003506
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3508 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3509 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3510 // so it's safe to just use negl instead of more complex comparisons.
3511 if (type == Primitive::kPrimInt) {
3512 __ cmpl(second_reg, Immediate(-1));
3513 __ j(kEqual, slow_path->GetEntryLabel());
3514 // edx:eax <- sign-extended of eax
3515 __ cdq();
3516 // eax = quotient, edx = remainder
3517 __ idivl(second_reg);
3518 } else {
3519 __ cmpq(second_reg, Immediate(-1));
3520 __ j(kEqual, slow_path->GetEntryLabel());
3521 // rdx:rax <- sign-extended of rax
3522 __ cqo();
3523 // rax = quotient, rdx = remainder
3524 __ idivq(second_reg);
3525 }
3526 __ Bind(slow_path->GetExitLabel());
3527 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003528}
3529
Calin Juravle7c4954d2014-10-28 16:57:40 +00003530void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3531 LocationSummary* locations =
3532 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3533 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003534 case Primitive::kPrimInt:
3535 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003536 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003538 locations->SetOut(Location::SameAsFirstInput());
3539 // Intel uses edx:eax as the dividend.
3540 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3542 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3543 // output and request another temp.
3544 if (div->InputAt(1)->IsConstant()) {
3545 locations->AddTemp(Location::RequiresRegister());
3546 }
Calin Juravled0d48522014-11-04 16:40:20 +00003547 break;
3548 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003549
Calin Juravle7c4954d2014-10-28 16:57:40 +00003550 case Primitive::kPrimFloat:
3551 case Primitive::kPrimDouble: {
3552 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003553 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003554 locations->SetOut(Location::SameAsFirstInput());
3555 break;
3556 }
3557
3558 default:
3559 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3560 }
3561}
3562
3563void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3564 LocationSummary* locations = div->GetLocations();
3565 Location first = locations->InAt(0);
3566 Location second = locations->InAt(1);
3567 DCHECK(first.Equals(locations->Out()));
3568
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003569 Primitive::Type type = div->GetResultType();
3570 switch (type) {
3571 case Primitive::kPrimInt:
3572 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003573 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003574 break;
3575 }
3576
Calin Juravle7c4954d2014-10-28 16:57:40 +00003577 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003578 if (second.IsFpuRegister()) {
3579 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3580 } else if (second.IsConstant()) {
3581 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003582 codegen_->LiteralFloatAddress(
3583 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003584 } else {
3585 DCHECK(second.IsStackSlot());
3586 __ divss(first.AsFpuRegister<XmmRegister>(),
3587 Address(CpuRegister(RSP), second.GetStackIndex()));
3588 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589 break;
3590 }
3591
3592 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003593 if (second.IsFpuRegister()) {
3594 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3595 } else if (second.IsConstant()) {
3596 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003597 codegen_->LiteralDoubleAddress(
3598 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003599 } else {
3600 DCHECK(second.IsDoubleStackSlot());
3601 __ divsd(first.AsFpuRegister<XmmRegister>(),
3602 Address(CpuRegister(RSP), second.GetStackIndex()));
3603 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003604 break;
3605 }
3606
3607 default:
3608 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3609 }
3610}
3611
Calin Juravlebacfec32014-11-14 15:54:36 +00003612void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003613 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003614 LocationSummary* locations =
3615 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003616
3617 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003618 case Primitive::kPrimInt:
3619 case Primitive::kPrimLong: {
3620 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003621 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3623 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3625 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3626 // output and request another temp.
3627 if (rem->InputAt(1)->IsConstant()) {
3628 locations->AddTemp(Location::RequiresRegister());
3629 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 break;
3631 }
3632
3633 case Primitive::kPrimFloat:
3634 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003635 locations->SetInAt(0, Location::Any());
3636 locations->SetInAt(1, Location::Any());
3637 locations->SetOut(Location::RequiresFpuRegister());
3638 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003639 break;
3640 }
3641
3642 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003643 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003644 }
3645}
3646
3647void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3648 Primitive::Type type = rem->GetResultType();
3649 switch (type) {
3650 case Primitive::kPrimInt:
3651 case Primitive::kPrimLong: {
3652 GenerateDivRemIntegral(rem);
3653 break;
3654 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003655 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003656 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003657 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003658 break;
3659 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003660 default:
3661 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3662 }
3663}
3664
Calin Juravled0d48522014-11-04 16:40:20 +00003665void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003666 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3667 ? LocationSummary::kCallOnSlowPath
3668 : LocationSummary::kNoCall;
3669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003670 locations->SetInAt(0, Location::Any());
3671 if (instruction->HasUses()) {
3672 locations->SetOut(Location::SameAsFirstInput());
3673 }
3674}
3675
3676void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003677 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003678 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3679 codegen_->AddSlowPath(slow_path);
3680
3681 LocationSummary* locations = instruction->GetLocations();
3682 Location value = locations->InAt(0);
3683
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003684 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003685 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003686 case Primitive::kPrimByte:
3687 case Primitive::kPrimChar:
3688 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003689 case Primitive::kPrimInt: {
3690 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003692 __ j(kEqual, slow_path->GetEntryLabel());
3693 } else if (value.IsStackSlot()) {
3694 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3695 __ j(kEqual, slow_path->GetEntryLabel());
3696 } else {
3697 DCHECK(value.IsConstant()) << value;
3698 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3699 __ jmp(slow_path->GetEntryLabel());
3700 }
3701 }
3702 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003703 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003704 case Primitive::kPrimLong: {
3705 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003707 __ j(kEqual, slow_path->GetEntryLabel());
3708 } else if (value.IsDoubleStackSlot()) {
3709 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3710 __ j(kEqual, slow_path->GetEntryLabel());
3711 } else {
3712 DCHECK(value.IsConstant()) << value;
3713 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3714 __ jmp(slow_path->GetEntryLabel());
3715 }
3716 }
3717 break;
3718 }
3719 default:
3720 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003721 }
Calin Juravled0d48522014-11-04 16:40:20 +00003722}
3723
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3725 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3726
3727 LocationSummary* locations =
3728 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3729
3730 switch (op->GetResultType()) {
3731 case Primitive::kPrimInt:
3732 case Primitive::kPrimLong: {
3733 locations->SetInAt(0, Location::RequiresRegister());
3734 // The shift count needs to be in CL.
3735 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3736 locations->SetOut(Location::SameAsFirstInput());
3737 break;
3738 }
3739 default:
3740 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3741 }
3742}
3743
3744void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3745 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3746
3747 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 Location second = locations->InAt(1);
3750
3751 switch (op->GetResultType()) {
3752 case Primitive::kPrimInt: {
3753 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003754 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 if (op->IsShl()) {
3756 __ shll(first_reg, second_reg);
3757 } else if (op->IsShr()) {
3758 __ sarl(first_reg, second_reg);
3759 } else {
3760 __ shrl(first_reg, second_reg);
3761 }
3762 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003763 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003764 if (op->IsShl()) {
3765 __ shll(first_reg, imm);
3766 } else if (op->IsShr()) {
3767 __ sarl(first_reg, imm);
3768 } else {
3769 __ shrl(first_reg, imm);
3770 }
3771 }
3772 break;
3773 }
3774 case Primitive::kPrimLong: {
3775 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003777 if (op->IsShl()) {
3778 __ shlq(first_reg, second_reg);
3779 } else if (op->IsShr()) {
3780 __ sarq(first_reg, second_reg);
3781 } else {
3782 __ shrq(first_reg, second_reg);
3783 }
3784 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003785 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003786 if (op->IsShl()) {
3787 __ shlq(first_reg, imm);
3788 } else if (op->IsShr()) {
3789 __ sarq(first_reg, imm);
3790 } else {
3791 __ shrq(first_reg, imm);
3792 }
3793 }
3794 break;
3795 }
3796 default:
3797 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003798 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799 }
3800}
3801
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003802void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3803 LocationSummary* locations =
3804 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3805
3806 switch (ror->GetResultType()) {
3807 case Primitive::kPrimInt:
3808 case Primitive::kPrimLong: {
3809 locations->SetInAt(0, Location::RequiresRegister());
3810 // The shift count needs to be in CL (unless it is a constant).
3811 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3812 locations->SetOut(Location::SameAsFirstInput());
3813 break;
3814 }
3815 default:
3816 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3817 UNREACHABLE();
3818 }
3819}
3820
3821void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3822 LocationSummary* locations = ror->GetLocations();
3823 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3824 Location second = locations->InAt(1);
3825
3826 switch (ror->GetResultType()) {
3827 case Primitive::kPrimInt:
3828 if (second.IsRegister()) {
3829 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3830 __ rorl(first_reg, second_reg);
3831 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003832 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003833 __ rorl(first_reg, imm);
3834 }
3835 break;
3836 case Primitive::kPrimLong:
3837 if (second.IsRegister()) {
3838 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3839 __ rorq(first_reg, second_reg);
3840 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003841 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003842 __ rorq(first_reg, imm);
3843 }
3844 break;
3845 default:
3846 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3847 UNREACHABLE();
3848 }
3849}
3850
Calin Juravle9aec02f2014-11-18 23:06:35 +00003851void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3852 HandleShift(shl);
3853}
3854
3855void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3856 HandleShift(shl);
3857}
3858
3859void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3860 HandleShift(shr);
3861}
3862
3863void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3864 HandleShift(shr);
3865}
3866
3867void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3868 HandleShift(ushr);
3869}
3870
3871void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3872 HandleShift(ushr);
3873}
3874
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003875void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003876 LocationSummary* locations =
3877 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003878 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003879 if (instruction->IsStringAlloc()) {
3880 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3881 } else {
3882 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3883 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3884 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003885 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003886}
3887
3888void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003889 // Note: if heap poisoning is enabled, the entry point takes cares
3890 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003891 if (instruction->IsStringAlloc()) {
3892 // String is allocated through StringFactory. Call NewEmptyString entry point.
3893 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3894 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3895 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3896 __ call(Address(temp, code_offset.SizeValue()));
3897 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3898 } else {
3899 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3900 instruction,
3901 instruction->GetDexPc(),
3902 nullptr);
3903 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3904 DCHECK(!codegen_->IsLeafMethod());
3905 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003906}
3907
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003908void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3909 LocationSummary* locations =
3910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3911 InvokeRuntimeCallingConvention calling_convention;
3912 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003913 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003914 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003915 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003916}
3917
3918void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3919 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003920 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3921 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003922 // Note: if heap poisoning is enabled, the entry point takes cares
3923 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003924 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3925 instruction,
3926 instruction->GetDexPc(),
3927 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003928 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003929
3930 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003931}
3932
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003933void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003934 LocationSummary* locations =
3935 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003936 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3937 if (location.IsStackSlot()) {
3938 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3939 } else if (location.IsDoubleStackSlot()) {
3940 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3941 }
3942 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003943}
3944
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003945void InstructionCodeGeneratorX86_64::VisitParameterValue(
3946 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003947 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003948}
3949
3950void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3951 LocationSummary* locations =
3952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3953 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3954}
3955
3956void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3957 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3958 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003959}
3960
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003961void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3962 LocationSummary* locations =
3963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3964 locations->SetInAt(0, Location::RequiresRegister());
3965 locations->SetOut(Location::RequiresRegister());
3966}
3967
3968void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3969 LocationSummary* locations = instruction->GetLocations();
3970 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003971 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003972 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3973 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
3974 } else {
3975 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3976 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
3977 }
3978 __ movq(locations->Out().AsRegister<CpuRegister>(),
3979 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
3980}
3981
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003982void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003983 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003984 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003985 locations->SetInAt(0, Location::RequiresRegister());
3986 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003987}
3988
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003989void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3990 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003991 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3992 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003993 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003994 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003995 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003997 break;
3998
3999 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004000 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004001 break;
4002
4003 default:
4004 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4005 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004006}
4007
David Brazdil66d126e2015-04-03 16:02:44 +01004008void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetOut(Location::SameAsFirstInput());
4013}
4014
4015void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004016 LocationSummary* locations = bool_not->GetLocations();
4017 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4018 locations->Out().AsRegister<CpuRegister>().AsRegister());
4019 Location out = locations->Out();
4020 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4021}
4022
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004023void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004024 LocationSummary* locations =
4025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004026 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4027 locations->SetInAt(i, Location::Any());
4028 }
4029 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030}
4031
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004032void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004033 LOG(FATAL) << "Unimplemented";
4034}
4035
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004036void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004037 /*
4038 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004039 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004040 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4041 */
4042 switch (kind) {
4043 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004044 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004045 break;
4046 }
4047 case MemBarrierKind::kAnyStore:
4048 case MemBarrierKind::kLoadAny:
4049 case MemBarrierKind::kStoreStore: {
4050 // nop
4051 break;
4052 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004053 case MemBarrierKind::kNTStoreStore:
4054 // Non-Temporal Store/Store needs an explicit fence.
4055 MemoryFence(/* non-temporal */ true);
4056 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004057 }
4058}
4059
4060void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4061 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4062
Roland Levillain0d5a2812015-11-13 10:07:31 +00004063 bool object_field_get_with_read_barrier =
4064 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004065 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004066 new (GetGraph()->GetArena()) LocationSummary(instruction,
4067 object_field_get_with_read_barrier ?
4068 LocationSummary::kCallOnSlowPath :
4069 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004070 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004071 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4072 locations->SetOut(Location::RequiresFpuRegister());
4073 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004074 // The output overlaps for an object field get when read barriers
4075 // are enabled: we do not want the move to overwrite the object's
4076 // location, as we need it to emit the read barrier.
4077 locations->SetOut(
4078 Location::RequiresRegister(),
4079 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004080 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004081 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4082 // We need a temporary register for the read barrier marking slow
4083 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4084 locations->AddTemp(Location::RequiresRegister());
4085 }
Calin Juravle52c48962014-12-16 17:02:57 +00004086}
4087
4088void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4089 const FieldInfo& field_info) {
4090 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4091
4092 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004093 Location base_loc = locations->InAt(0);
4094 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004095 Location out = locations->Out();
4096 bool is_volatile = field_info.IsVolatile();
4097 Primitive::Type field_type = field_info.GetFieldType();
4098 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4099
4100 switch (field_type) {
4101 case Primitive::kPrimBoolean: {
4102 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4103 break;
4104 }
4105
4106 case Primitive::kPrimByte: {
4107 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4108 break;
4109 }
4110
4111 case Primitive::kPrimShort: {
4112 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4113 break;
4114 }
4115
4116 case Primitive::kPrimChar: {
4117 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4118 break;
4119 }
4120
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004121 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004122 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4123 break;
4124 }
4125
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004126 case Primitive::kPrimNot: {
4127 // /* HeapReference<Object> */ out = *(base + offset)
4128 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4129 Location temp_loc = locations->GetTemp(0);
4130 // Note that a potential implicit null check is handled in this
4131 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4132 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4133 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4134 if (is_volatile) {
4135 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4136 }
4137 } else {
4138 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4139 codegen_->MaybeRecordImplicitNullCheck(instruction);
4140 if (is_volatile) {
4141 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4142 }
4143 // If read barriers are enabled, emit read barriers other than
4144 // Baker's using a slow path (and also unpoison the loaded
4145 // reference, if heap poisoning is enabled).
4146 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4147 }
4148 break;
4149 }
4150
Calin Juravle52c48962014-12-16 17:02:57 +00004151 case Primitive::kPrimLong: {
4152 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4153 break;
4154 }
4155
4156 case Primitive::kPrimFloat: {
4157 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4158 break;
4159 }
4160
4161 case Primitive::kPrimDouble: {
4162 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4163 break;
4164 }
4165
4166 case Primitive::kPrimVoid:
4167 LOG(FATAL) << "Unreachable type " << field_type;
4168 UNREACHABLE();
4169 }
4170
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004171 if (field_type == Primitive::kPrimNot) {
4172 // Potential implicit null checks, in the case of reference
4173 // fields, are handled in the previous switch statement.
4174 } else {
4175 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004176 }
Roland Levillain4d027112015-07-01 15:41:14 +01004177
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004178 if (is_volatile) {
4179 if (field_type == Primitive::kPrimNot) {
4180 // Memory barriers, in the case of references, are also handled
4181 // in the previous switch statement.
4182 } else {
4183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4184 }
Roland Levillain4d027112015-07-01 15:41:14 +01004185 }
Calin Juravle52c48962014-12-16 17:02:57 +00004186}
4187
4188void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4189 const FieldInfo& field_info) {
4190 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4191
4192 LocationSummary* locations =
4193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004194 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004195 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004196 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004197 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004198
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004199 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004200 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004201 if (is_volatile) {
4202 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4203 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4204 } else {
4205 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4206 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004207 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004208 if (is_volatile) {
4209 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4210 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4211 } else {
4212 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4213 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004214 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004215 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004216 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004217 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004218 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004219 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4220 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004221 locations->AddTemp(Location::RequiresRegister());
4222 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004223}
4224
Calin Juravle52c48962014-12-16 17:02:57 +00004225void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004226 const FieldInfo& field_info,
4227 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004228 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4229
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004230 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004231 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4232 Location value = locations->InAt(1);
4233 bool is_volatile = field_info.IsVolatile();
4234 Primitive::Type field_type = field_info.GetFieldType();
4235 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4236
4237 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004238 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004239 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004240
Mark Mendellea5af682015-10-22 17:35:49 -04004241 bool maybe_record_implicit_null_check_done = false;
4242
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004243 switch (field_type) {
4244 case Primitive::kPrimBoolean:
4245 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004246 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004247 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004248 __ movb(Address(base, offset), Immediate(v));
4249 } else {
4250 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4251 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252 break;
4253 }
4254
4255 case Primitive::kPrimShort:
4256 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004257 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004258 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004259 __ movw(Address(base, offset), Immediate(v));
4260 } else {
4261 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4262 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004263 break;
4264 }
4265
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004266 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004268 if (value.IsConstant()) {
4269 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004270 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4271 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4272 // Note: if heap poisoning is enabled, no need to poison
4273 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004274 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004275 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004276 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4277 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4278 __ movl(temp, value.AsRegister<CpuRegister>());
4279 __ PoisonHeapReference(temp);
4280 __ movl(Address(base, offset), temp);
4281 } else {
4282 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4283 }
Mark Mendell40741f32015-04-20 22:10:34 -04004284 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285 break;
4286 }
4287
4288 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004289 if (value.IsConstant()) {
4290 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004291 codegen_->MoveInt64ToAddress(Address(base, offset),
4292 Address(base, offset + sizeof(int32_t)),
4293 v,
4294 instruction);
4295 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004296 } else {
4297 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4298 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299 break;
4300 }
4301
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004302 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004303 if (value.IsConstant()) {
4304 int32_t v =
4305 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4306 __ movl(Address(base, offset), Immediate(v));
4307 } else {
4308 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4309 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004310 break;
4311 }
4312
4313 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004314 if (value.IsConstant()) {
4315 int64_t v =
4316 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4317 codegen_->MoveInt64ToAddress(Address(base, offset),
4318 Address(base, offset + sizeof(int32_t)),
4319 v,
4320 instruction);
4321 maybe_record_implicit_null_check_done = true;
4322 } else {
4323 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4324 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004325 break;
4326 }
4327
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328 case Primitive::kPrimVoid:
4329 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004330 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004331 }
Calin Juravle52c48962014-12-16 17:02:57 +00004332
Mark Mendellea5af682015-10-22 17:35:49 -04004333 if (!maybe_record_implicit_null_check_done) {
4334 codegen_->MaybeRecordImplicitNullCheck(instruction);
4335 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004336
4337 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4338 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4339 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004340 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004341 }
4342
Calin Juravle52c48962014-12-16 17:02:57 +00004343 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004344 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004345 }
4346}
4347
4348void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4349 HandleFieldSet(instruction, instruction->GetFieldInfo());
4350}
4351
4352void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004353 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354}
4355
4356void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004357 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358}
4359
4360void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004361 HandleFieldGet(instruction, instruction->GetFieldInfo());
4362}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363
Calin Juravle52c48962014-12-16 17:02:57 +00004364void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4365 HandleFieldGet(instruction);
4366}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367
Calin Juravle52c48962014-12-16 17:02:57 +00004368void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4369 HandleFieldGet(instruction, instruction->GetFieldInfo());
4370}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371
Calin Juravle52c48962014-12-16 17:02:57 +00004372void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4373 HandleFieldSet(instruction, instruction->GetFieldInfo());
4374}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375
Calin Juravle52c48962014-12-16 17:02:57 +00004376void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004377 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004378}
4379
Calin Juravlee460d1d2015-09-29 04:52:17 +01004380void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4381 HUnresolvedInstanceFieldGet* instruction) {
4382 FieldAccessCallingConventionX86_64 calling_convention;
4383 codegen_->CreateUnresolvedFieldLocationSummary(
4384 instruction, instruction->GetFieldType(), calling_convention);
4385}
4386
4387void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4388 HUnresolvedInstanceFieldGet* instruction) {
4389 FieldAccessCallingConventionX86_64 calling_convention;
4390 codegen_->GenerateUnresolvedFieldAccess(instruction,
4391 instruction->GetFieldType(),
4392 instruction->GetFieldIndex(),
4393 instruction->GetDexPc(),
4394 calling_convention);
4395}
4396
4397void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4398 HUnresolvedInstanceFieldSet* instruction) {
4399 FieldAccessCallingConventionX86_64 calling_convention;
4400 codegen_->CreateUnresolvedFieldLocationSummary(
4401 instruction, instruction->GetFieldType(), calling_convention);
4402}
4403
4404void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4405 HUnresolvedInstanceFieldSet* instruction) {
4406 FieldAccessCallingConventionX86_64 calling_convention;
4407 codegen_->GenerateUnresolvedFieldAccess(instruction,
4408 instruction->GetFieldType(),
4409 instruction->GetFieldIndex(),
4410 instruction->GetDexPc(),
4411 calling_convention);
4412}
4413
4414void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4415 HUnresolvedStaticFieldGet* instruction) {
4416 FieldAccessCallingConventionX86_64 calling_convention;
4417 codegen_->CreateUnresolvedFieldLocationSummary(
4418 instruction, instruction->GetFieldType(), calling_convention);
4419}
4420
4421void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4422 HUnresolvedStaticFieldGet* instruction) {
4423 FieldAccessCallingConventionX86_64 calling_convention;
4424 codegen_->GenerateUnresolvedFieldAccess(instruction,
4425 instruction->GetFieldType(),
4426 instruction->GetFieldIndex(),
4427 instruction->GetDexPc(),
4428 calling_convention);
4429}
4430
4431void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4432 HUnresolvedStaticFieldSet* instruction) {
4433 FieldAccessCallingConventionX86_64 calling_convention;
4434 codegen_->CreateUnresolvedFieldLocationSummary(
4435 instruction, instruction->GetFieldType(), calling_convention);
4436}
4437
4438void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4439 HUnresolvedStaticFieldSet* instruction) {
4440 FieldAccessCallingConventionX86_64 calling_convention;
4441 codegen_->GenerateUnresolvedFieldAccess(instruction,
4442 instruction->GetFieldType(),
4443 instruction->GetFieldIndex(),
4444 instruction->GetDexPc(),
4445 calling_convention);
4446}
4447
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004449 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4450 ? LocationSummary::kCallOnSlowPath
4451 : LocationSummary::kNoCall;
4452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4453 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004454 ? Location::RequiresRegister()
4455 : Location::Any();
4456 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004457 if (instruction->HasUses()) {
4458 locations->SetOut(Location::SameAsFirstInput());
4459 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460}
4461
Calin Juravle2ae48182016-03-16 14:05:09 +00004462void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4463 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004464 return;
4465 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004466 LocationSummary* locations = instruction->GetLocations();
4467 Location obj = locations->InAt(0);
4468
4469 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004470 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004471}
4472
Calin Juravle2ae48182016-03-16 14:05:09 +00004473void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004474 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004475 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476
4477 LocationSummary* locations = instruction->GetLocations();
4478 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004479
4480 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004481 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004482 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004484 } else {
4485 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004486 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004487 __ jmp(slow_path->GetEntryLabel());
4488 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489 }
4490 __ j(kEqual, slow_path->GetEntryLabel());
4491}
4492
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004493void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004494 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004495}
4496
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004497void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004498 bool object_array_get_with_read_barrier =
4499 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004500 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004501 new (GetGraph()->GetArena()) LocationSummary(instruction,
4502 object_array_get_with_read_barrier ?
4503 LocationSummary::kCallOnSlowPath :
4504 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004505 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004506 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004507 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4508 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4509 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004510 // The output overlaps for an object array get when read barriers
4511 // are enabled: we do not want the move to overwrite the array's
4512 // location, as we need it to emit the read barrier.
4513 locations->SetOut(
4514 Location::RequiresRegister(),
4515 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004516 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004517 // We need a temporary register for the read barrier marking slow
4518 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4519 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4520 locations->AddTemp(Location::RequiresRegister());
4521 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522}
4523
4524void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4525 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004526 Location obj_loc = locations->InAt(0);
4527 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004529 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004531 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004532 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533 case Primitive::kPrimBoolean: {
4534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004535 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 if (index.IsConstant()) {
4537 __ movzxb(out, Address(obj,
4538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4539 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004540 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541 }
4542 break;
4543 }
4544
4545 case Primitive::kPrimByte: {
4546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004547 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004548 if (index.IsConstant()) {
4549 __ movsxb(out, Address(obj,
4550 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4551 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004552 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004553 }
4554 break;
4555 }
4556
4557 case Primitive::kPrimShort: {
4558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004559 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004560 if (index.IsConstant()) {
4561 __ movsxw(out, Address(obj,
4562 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4563 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 }
4566 break;
4567 }
4568
4569 case Primitive::kPrimChar: {
4570 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004571 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 if (index.IsConstant()) {
4573 __ movzxw(out, Address(obj,
4574 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4575 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004576 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 }
4578 break;
4579 }
4580
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004581 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004583 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004584 if (index.IsConstant()) {
4585 __ movl(out, Address(obj,
4586 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4587 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004588 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 }
4590 break;
4591 }
4592
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004593 case Primitive::kPrimNot: {
4594 static_assert(
4595 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4596 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4597 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4598 // /* HeapReference<Object> */ out =
4599 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4600 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4601 Location temp = locations->GetTemp(0);
4602 // Note that a potential implicit null check is handled in this
4603 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4604 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4605 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4606 } else {
4607 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4608 if (index.IsConstant()) {
4609 uint32_t offset =
4610 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4611 __ movl(out, Address(obj, offset));
4612 codegen_->MaybeRecordImplicitNullCheck(instruction);
4613 // If read barriers are enabled, emit read barriers other than
4614 // Baker's using a slow path (and also unpoison the loaded
4615 // reference, if heap poisoning is enabled).
4616 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4617 } else {
4618 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4619 codegen_->MaybeRecordImplicitNullCheck(instruction);
4620 // If read barriers are enabled, emit read barriers other than
4621 // Baker's using a slow path (and also unpoison the loaded
4622 // reference, if heap poisoning is enabled).
4623 codegen_->MaybeGenerateReadBarrierSlow(
4624 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4625 }
4626 }
4627 break;
4628 }
4629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 case Primitive::kPrimLong: {
4631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004632 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004633 if (index.IsConstant()) {
4634 __ movq(out, Address(obj,
4635 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4636 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004637 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 }
4639 break;
4640 }
4641
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004642 case Primitive::kPrimFloat: {
4643 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004644 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004645 if (index.IsConstant()) {
4646 __ movss(out, Address(obj,
4647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4648 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004649 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004650 }
4651 break;
4652 }
4653
4654 case Primitive::kPrimDouble: {
4655 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004656 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004657 if (index.IsConstant()) {
4658 __ movsd(out, Address(obj,
4659 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4660 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004661 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004662 }
4663 break;
4664 }
4665
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004667 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004668 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004669 }
Roland Levillain4d027112015-07-01 15:41:14 +01004670
4671 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004672 // Potential implicit null checks, in the case of reference
4673 // arrays, are handled in the previous switch statement.
4674 } else {
4675 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004676 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677}
4678
4679void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004680 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004681
4682 bool needs_write_barrier =
4683 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004684 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004685 bool object_array_set_with_read_barrier =
4686 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004687
Nicolas Geoffray39468442014-09-02 15:17:15 +01004688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004689 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004690 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004691 LocationSummary::kCallOnSlowPath :
4692 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004693
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004694 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004695 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4696 if (Primitive::IsFloatingPointType(value_type)) {
4697 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004698 } else {
4699 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4700 }
4701
4702 if (needs_write_barrier) {
4703 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004704
4705 // This first temporary register is possibly used for heap
4706 // reference poisoning and/or read barrier emission too.
4707 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004708 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710}
4711
4712void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4713 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004714 Location array_loc = locations->InAt(0);
4715 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004717 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004718 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004719 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004720 bool needs_write_barrier =
4721 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004722 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4723 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4724 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004725
4726 switch (value_type) {
4727 case Primitive::kPrimBoolean:
4728 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004729 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4730 Address address = index.IsConstant()
4731 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4732 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4733 if (value.IsRegister()) {
4734 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004735 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004736 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004738 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 break;
4740 }
4741
4742 case Primitive::kPrimShort:
4743 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004744 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4745 Address address = index.IsConstant()
4746 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4747 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4748 if (value.IsRegister()) {
4749 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004751 DCHECK(value.IsConstant()) << value;
4752 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004753 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004754 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 break;
4756 }
4757
4758 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4760 Address address = index.IsConstant()
4761 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4762 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004763
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 if (!value.IsRegister()) {
4765 // Just setting null.
4766 DCHECK(instruction->InputAt(2)->IsNullConstant());
4767 DCHECK(value.IsConstant()) << value;
4768 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004769 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004770 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004771 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004772 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004773 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774
4775 DCHECK(needs_write_barrier);
4776 CpuRegister register_value = value.AsRegister<CpuRegister>();
4777 NearLabel done, not_null, do_put;
4778 SlowPathCode* slow_path = nullptr;
4779 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004780 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004781 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4782 codegen_->AddSlowPath(slow_path);
4783 if (instruction->GetValueCanBeNull()) {
4784 __ testl(register_value, register_value);
4785 __ j(kNotEqual, &not_null);
4786 __ movl(address, Immediate(0));
4787 codegen_->MaybeRecordImplicitNullCheck(instruction);
4788 __ jmp(&done);
4789 __ Bind(&not_null);
4790 }
4791
Roland Levillain0d5a2812015-11-13 10:07:31 +00004792 if (kEmitCompilerReadBarrier) {
4793 // When read barriers are enabled, the type checking
4794 // instrumentation requires two read barriers:
4795 //
4796 // __ movl(temp2, temp);
4797 // // /* HeapReference<Class> */ temp = temp->component_type_
4798 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004799 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004800 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4801 //
4802 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4803 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004804 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004805 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4806 //
4807 // __ cmpl(temp, temp2);
4808 //
4809 // However, the second read barrier may trash `temp`, as it
4810 // is a temporary register, and as such would not be saved
4811 // along with live registers before calling the runtime (nor
4812 // restored afterwards). So in this case, we bail out and
4813 // delegate the work to the array set slow path.
4814 //
4815 // TODO: Extend the register allocator to support a new
4816 // "(locally) live temp" location so as to avoid always
4817 // going into the slow path when read barriers are enabled.
4818 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004819 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004820 // /* HeapReference<Class> */ temp = array->klass_
4821 __ movl(temp, Address(array, class_offset));
4822 codegen_->MaybeRecordImplicitNullCheck(instruction);
4823 __ MaybeUnpoisonHeapReference(temp);
4824
4825 // /* HeapReference<Class> */ temp = temp->component_type_
4826 __ movl(temp, Address(temp, component_offset));
4827 // If heap poisoning is enabled, no need to unpoison `temp`
4828 // nor the object reference in `register_value->klass`, as
4829 // we are comparing two poisoned references.
4830 __ cmpl(temp, Address(register_value, class_offset));
4831
4832 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4833 __ j(kEqual, &do_put);
4834 // If heap poisoning is enabled, the `temp` reference has
4835 // not been unpoisoned yet; unpoison it now.
4836 __ MaybeUnpoisonHeapReference(temp);
4837
4838 // /* HeapReference<Class> */ temp = temp->super_class_
4839 __ movl(temp, Address(temp, super_offset));
4840 // If heap poisoning is enabled, no need to unpoison
4841 // `temp`, as we are comparing against null below.
4842 __ testl(temp, temp);
4843 __ j(kNotEqual, slow_path->GetEntryLabel());
4844 __ Bind(&do_put);
4845 } else {
4846 __ j(kNotEqual, slow_path->GetEntryLabel());
4847 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 }
4849 }
4850
4851 if (kPoisonHeapReferences) {
4852 __ movl(temp, register_value);
4853 __ PoisonHeapReference(temp);
4854 __ movl(address, temp);
4855 } else {
4856 __ movl(address, register_value);
4857 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004858 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004859 codegen_->MaybeRecordImplicitNullCheck(instruction);
4860 }
4861
4862 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4863 codegen_->MarkGCCard(
4864 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4865 __ Bind(&done);
4866
4867 if (slow_path != nullptr) {
4868 __ Bind(slow_path->GetExitLabel());
4869 }
4870
4871 break;
4872 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004873
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004874 case Primitive::kPrimInt: {
4875 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4876 Address address = index.IsConstant()
4877 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4878 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4879 if (value.IsRegister()) {
4880 __ movl(address, value.AsRegister<CpuRegister>());
4881 } else {
4882 DCHECK(value.IsConstant()) << value;
4883 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4884 __ movl(address, Immediate(v));
4885 }
4886 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887 break;
4888 }
4889
4890 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004891 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4892 Address address = index.IsConstant()
4893 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4894 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4895 if (value.IsRegister()) {
4896 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004897 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004898 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004899 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004900 Address address_high = index.IsConstant()
4901 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4902 offset + sizeof(int32_t))
4903 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4904 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 }
4906 break;
4907 }
4908
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004909 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004910 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4911 Address address = index.IsConstant()
4912 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4913 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004914 if (value.IsFpuRegister()) {
4915 __ movss(address, value.AsFpuRegister<XmmRegister>());
4916 } else {
4917 DCHECK(value.IsConstant());
4918 int32_t v =
4919 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4920 __ movl(address, Immediate(v));
4921 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004922 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004923 break;
4924 }
4925
4926 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004927 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4928 Address address = index.IsConstant()
4929 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4930 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004931 if (value.IsFpuRegister()) {
4932 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4933 codegen_->MaybeRecordImplicitNullCheck(instruction);
4934 } else {
4935 int64_t v =
4936 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4937 Address address_high = index.IsConstant()
4938 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4939 offset + sizeof(int32_t))
4940 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4941 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4942 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004943 break;
4944 }
4945
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 case Primitive::kPrimVoid:
4947 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004948 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004949 }
4950}
4951
4952void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004953 LocationSummary* locations =
4954 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004955 locations->SetInAt(0, Location::RequiresRegister());
4956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004957}
4958
4959void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4960 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004961 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004962 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4963 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004964 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004965 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966}
4967
4968void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004969 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4970 ? LocationSummary::kCallOnSlowPath
4971 : LocationSummary::kNoCall;
4972 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004973 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004974 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004975 if (instruction->HasUses()) {
4976 locations->SetOut(Location::SameAsFirstInput());
4977 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004978}
4979
4980void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4981 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004982 Location index_loc = locations->InAt(0);
4983 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004984 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004985 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986
Mark Mendell99dbd682015-04-22 16:18:52 -04004987 if (length_loc.IsConstant()) {
4988 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4989 if (index_loc.IsConstant()) {
4990 // BCE will remove the bounds check if we are guarenteed to pass.
4991 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4992 if (index < 0 || index >= length) {
4993 codegen_->AddSlowPath(slow_path);
4994 __ jmp(slow_path->GetEntryLabel());
4995 } else {
4996 // Some optimization after BCE may have generated this, and we should not
4997 // generate a bounds check if it is a valid range.
4998 }
4999 return;
5000 }
5001
5002 // We have to reverse the jump condition because the length is the constant.
5003 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5004 __ cmpl(index_reg, Immediate(length));
5005 codegen_->AddSlowPath(slow_path);
5006 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005007 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005008 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5009 if (index_loc.IsConstant()) {
5010 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5011 __ cmpl(length, Immediate(value));
5012 } else {
5013 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5014 }
5015 codegen_->AddSlowPath(slow_path);
5016 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005017 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018}
5019
5020void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5021 CpuRegister card,
5022 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005023 CpuRegister value,
5024 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005025 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005026 if (value_can_be_null) {
5027 __ testl(value, value);
5028 __ j(kEqual, &is_null);
5029 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005030 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5031 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 __ movq(temp, object);
5033 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005034 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005035 if (value_can_be_null) {
5036 __ Bind(&is_null);
5037 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038}
5039
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005040void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005041 LOG(FATAL) << "Unimplemented";
5042}
5043
5044void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005045 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5046}
5047
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005048void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5050}
5051
5052void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005053 HBasicBlock* block = instruction->GetBlock();
5054 if (block->GetLoopInformation() != nullptr) {
5055 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5056 // The back edge will generate the suspend check.
5057 return;
5058 }
5059 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5060 // The goto will generate the suspend check.
5061 return;
5062 }
5063 GenerateSuspendCheck(instruction, nullptr);
5064}
5065
5066void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5067 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005068 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005069 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5070 if (slow_path == nullptr) {
5071 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5072 instruction->SetSlowPath(slow_path);
5073 codegen_->AddSlowPath(slow_path);
5074 if (successor != nullptr) {
5075 DCHECK(successor->IsLoopHeader());
5076 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5077 }
5078 } else {
5079 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5080 }
5081
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005082 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5083 /* no_rip */ true),
5084 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005085 if (successor == nullptr) {
5086 __ j(kNotEqual, slow_path->GetEntryLabel());
5087 __ Bind(slow_path->GetReturnLabel());
5088 } else {
5089 __ j(kEqual, codegen_->GetLabelOf(successor));
5090 __ jmp(slow_path->GetEntryLabel());
5091 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005092}
5093
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005094X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5095 return codegen_->GetAssembler();
5096}
5097
5098void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005099 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005100 Location source = move->GetSource();
5101 Location destination = move->GetDestination();
5102
5103 if (source.IsRegister()) {
5104 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005105 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005106 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005107 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005108 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005109 } else {
5110 DCHECK(destination.IsDoubleStackSlot());
5111 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005112 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005113 }
5114 } else if (source.IsStackSlot()) {
5115 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005116 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005117 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005118 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005119 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005120 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005121 } else {
5122 DCHECK(destination.IsStackSlot());
5123 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5124 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5125 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005126 } else if (source.IsDoubleStackSlot()) {
5127 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005128 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005129 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005130 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005131 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5132 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005133 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005134 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005135 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5136 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5137 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005138 } else if (source.IsConstant()) {
5139 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005140 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5141 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005142 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005143 if (value == 0) {
5144 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5145 } else {
5146 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5147 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005148 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005149 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005150 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005151 }
5152 } else if (constant->IsLongConstant()) {
5153 int64_t value = constant->AsLongConstant()->GetValue();
5154 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005155 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005156 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005157 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005158 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005159 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005160 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005161 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005162 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005163 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005164 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005165 } else {
5166 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005167 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005168 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5169 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005170 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005171 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005172 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005173 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005174 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005175 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005176 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005177 } else {
5178 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005179 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005180 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005181 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005182 } else if (source.IsFpuRegister()) {
5183 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005184 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005185 } else if (destination.IsStackSlot()) {
5186 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005187 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005188 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005189 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005190 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005191 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005192 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005193 }
5194}
5195
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005196void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005197 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005198 __ movl(Address(CpuRegister(RSP), mem), reg);
5199 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005200}
5201
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005202void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005203 ScratchRegisterScope ensure_scratch(
5204 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5205
5206 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5207 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5208 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5209 Address(CpuRegister(RSP), mem2 + stack_offset));
5210 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5211 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5212 CpuRegister(ensure_scratch.GetRegister()));
5213}
5214
Mark Mendell8a1c7282015-06-29 15:41:28 -04005215void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5216 __ movq(CpuRegister(TMP), reg1);
5217 __ movq(reg1, reg2);
5218 __ movq(reg2, CpuRegister(TMP));
5219}
5220
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005221void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5222 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5223 __ movq(Address(CpuRegister(RSP), mem), reg);
5224 __ movq(reg, CpuRegister(TMP));
5225}
5226
5227void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5228 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005229 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005230
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005231 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5232 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5233 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5234 Address(CpuRegister(RSP), mem2 + stack_offset));
5235 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5236 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5237 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005238}
5239
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005240void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5241 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5242 __ movss(Address(CpuRegister(RSP), mem), reg);
5243 __ movd(reg, CpuRegister(TMP));
5244}
5245
5246void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5247 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5248 __ movsd(Address(CpuRegister(RSP), mem), reg);
5249 __ movd(reg, CpuRegister(TMP));
5250}
5251
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005252void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005253 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005254 Location source = move->GetSource();
5255 Location destination = move->GetDestination();
5256
5257 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005258 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005259 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005260 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005261 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005262 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005263 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005264 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5265 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005266 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005267 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005268 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005269 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5270 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005271 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005272 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5273 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5274 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005275 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005276 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005277 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005278 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005283 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005285 }
5286}
5287
5288
5289void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5290 __ pushq(CpuRegister(reg));
5291}
5292
5293
5294void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5295 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005296}
5297
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005298void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005299 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005300 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5301 Immediate(mirror::Class::kStatusInitialized));
5302 __ j(kLess, slow_path->GetEntryLabel());
5303 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005304 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005305}
5306
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005307void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005308 InvokeRuntimeCallingConvention calling_convention;
5309 CodeGenerator::CreateLoadClassLocationSummary(
5310 cls,
5311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005312 Location::RegisterLocation(RAX),
5313 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005314}
5315
5316void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005317 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005318 if (cls->NeedsAccessCheck()) {
5319 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5320 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5321 cls,
5322 cls->GetDexPc(),
5323 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005324 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005325 return;
5326 }
5327
Roland Levillain0d5a2812015-11-13 10:07:31 +00005328 Location out_loc = locations->Out();
5329 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005330 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005331
Calin Juravle580b6092015-10-06 17:35:58 +01005332 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005333 DCHECK(!cls->CanCallRuntime());
5334 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005335 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5336 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005337 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005338 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005339 // /* GcRoot<mirror::Class>[] */ out =
5340 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5341 __ movq(out, Address(current_method,
5342 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005343 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005344 GenerateGcRootFieldLoad(
5345 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01005346
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005347 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5348 DCHECK(cls->CanCallRuntime());
5349 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5350 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5351 codegen_->AddSlowPath(slow_path);
5352 if (!cls->IsInDexCache()) {
5353 __ testl(out, out);
5354 __ j(kEqual, slow_path->GetEntryLabel());
5355 }
5356 if (cls->MustGenerateClinitCheck()) {
5357 GenerateClassInitializationCheck(slow_path, out);
5358 } else {
5359 __ Bind(slow_path->GetExitLabel());
5360 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005361 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005362 }
5363}
5364
5365void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5366 LocationSummary* locations =
5367 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5368 locations->SetInAt(0, Location::RequiresRegister());
5369 if (check->HasUses()) {
5370 locations->SetOut(Location::SameAsFirstInput());
5371 }
5372}
5373
5374void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005375 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005376 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005377 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005378 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005379 GenerateClassInitializationCheck(slow_path,
5380 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005381}
5382
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005383HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5384 HLoadString::LoadKind desired_string_load_kind) {
5385 if (kEmitCompilerReadBarrier) {
5386 switch (desired_string_load_kind) {
5387 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5388 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5389 case HLoadString::LoadKind::kBootImageAddress:
5390 // TODO: Implement for read barrier.
5391 return HLoadString::LoadKind::kDexCacheViaMethod;
5392 default:
5393 break;
5394 }
5395 }
5396 switch (desired_string_load_kind) {
5397 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5398 DCHECK(!GetCompilerOptions().GetCompilePic());
5399 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5400 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5401 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5402 DCHECK(GetCompilerOptions().GetCompilePic());
5403 break;
5404 case HLoadString::LoadKind::kBootImageAddress:
5405 break;
5406 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005407 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005408 break;
5409 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005410 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005411 break;
5412 case HLoadString::LoadKind::kDexCacheViaMethod:
5413 break;
5414 }
5415 return desired_string_load_kind;
5416}
5417
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005418void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005419 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005420 ? LocationSummary::kCallOnSlowPath
5421 : LocationSummary::kNoCall;
5422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005423 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5424 locations->SetInAt(0, Location::RequiresRegister());
5425 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005426 locations->SetOut(Location::RequiresRegister());
5427}
5428
5429void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005430 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005431 Location out_loc = locations->Out();
5432 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005433
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005434 switch (load->GetLoadKind()) {
5435 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5436 DCHECK(!kEmitCompilerReadBarrier);
5437 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5438 codegen_->RecordStringPatch(load);
5439 return; // No dex cache slow path.
5440 }
5441 case HLoadString::LoadKind::kBootImageAddress: {
5442 DCHECK(!kEmitCompilerReadBarrier);
5443 DCHECK_NE(load->GetAddress(), 0u);
5444 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5445 __ movl(out, Immediate(address)); // Zero-extended.
5446 codegen_->RecordSimplePatch();
5447 return; // No dex cache slow path.
5448 }
5449 case HLoadString::LoadKind::kDexCacheAddress: {
5450 DCHECK_NE(load->GetAddress(), 0u);
5451 if (IsUint<32>(load->GetAddress())) {
5452 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5453 GenerateGcRootFieldLoad(load, out_loc, address);
5454 } else {
5455 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5456 __ movq(out, Immediate(load->GetAddress()));
5457 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5458 }
5459 break;
5460 }
5461 case HLoadString::LoadKind::kDexCachePcRelative: {
5462 uint32_t offset = load->GetDexCacheElementOffset();
5463 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5464 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5465 /* no_rip */ false);
5466 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5467 break;
5468 }
5469 case HLoadString::LoadKind::kDexCacheViaMethod: {
5470 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5471
5472 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5473 GenerateGcRootFieldLoad(
5474 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5475 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5476 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5477 // /* GcRoot<mirror::String> */ out = out[string_index]
5478 GenerateGcRootFieldLoad(
5479 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5480 break;
5481 }
5482 default:
5483 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5484 UNREACHABLE();
5485 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005486
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005487 if (!load->IsInDexCache()) {
5488 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5489 codegen_->AddSlowPath(slow_path);
5490 __ testl(out, out);
5491 __ j(kEqual, slow_path->GetEntryLabel());
5492 __ Bind(slow_path->GetExitLabel());
5493 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005494}
5495
David Brazdilcb1c0552015-08-04 16:22:25 +01005496static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005497 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5498 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005499}
5500
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005501void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5502 LocationSummary* locations =
5503 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5504 locations->SetOut(Location::RequiresRegister());
5505}
5506
5507void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005508 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5509}
5510
5511void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5512 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5513}
5514
5515void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5516 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005517}
5518
5519void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5520 LocationSummary* locations =
5521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5522 InvokeRuntimeCallingConvention calling_convention;
5523 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5524}
5525
5526void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005527 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5528 instruction,
5529 instruction->GetDexPc(),
5530 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005531 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005532}
5533
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005534static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5535 return kEmitCompilerReadBarrier &&
5536 (kUseBakerReadBarrier ||
5537 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5538 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5539 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5540}
5541
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005542void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005543 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005544 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5545 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005546 case TypeCheckKind::kExactCheck:
5547 case TypeCheckKind::kAbstractClassCheck:
5548 case TypeCheckKind::kClassHierarchyCheck:
5549 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005550 call_kind =
5551 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005552 break;
5553 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005554 case TypeCheckKind::kUnresolvedCheck:
5555 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005556 call_kind = LocationSummary::kCallOnSlowPath;
5557 break;
5558 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005559
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005561 locations->SetInAt(0, Location::RequiresRegister());
5562 locations->SetInAt(1, Location::Any());
5563 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5564 locations->SetOut(Location::RequiresRegister());
5565 // When read barriers are enabled, we need a temporary register for
5566 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005567 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005568 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005570}
5571
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005572void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005573 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005574 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005575 Location obj_loc = locations->InAt(0);
5576 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005577 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005578 Location out_loc = locations->Out();
5579 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005580 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005581 locations->GetTemp(0) :
5582 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005583 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5585 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5586 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005587 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005588 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005589
5590 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005591 // Avoid null check if we know obj is not null.
5592 if (instruction->MustDoNullCheck()) {
5593 __ testl(obj, obj);
5594 __ j(kEqual, &zero);
5595 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005596
Roland Levillain0d5a2812015-11-13 10:07:31 +00005597 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005598 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005599
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005600 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 case TypeCheckKind::kExactCheck: {
5602 if (cls.IsRegister()) {
5603 __ cmpl(out, cls.AsRegister<CpuRegister>());
5604 } else {
5605 DCHECK(cls.IsStackSlot()) << cls;
5606 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5607 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005608 if (zero.IsLinked()) {
5609 // Classes must be equal for the instanceof to succeed.
5610 __ j(kNotEqual, &zero);
5611 __ movl(out, Immediate(1));
5612 __ jmp(&done);
5613 } else {
5614 __ setcc(kEqual, out);
5615 // setcc only sets the low byte.
5616 __ andl(out, Immediate(1));
5617 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005618 break;
5619 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005621 case TypeCheckKind::kAbstractClassCheck: {
5622 // If the class is abstract, we eagerly fetch the super class of the
5623 // object to avoid doing a comparison we know will fail.
5624 NearLabel loop, success;
5625 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005626 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005627 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005628 __ testl(out, out);
5629 // If `out` is null, we use it for the result, and jump to `done`.
5630 __ j(kEqual, &done);
5631 if (cls.IsRegister()) {
5632 __ cmpl(out, cls.AsRegister<CpuRegister>());
5633 } else {
5634 DCHECK(cls.IsStackSlot()) << cls;
5635 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5636 }
5637 __ j(kNotEqual, &loop);
5638 __ movl(out, Immediate(1));
5639 if (zero.IsLinked()) {
5640 __ jmp(&done);
5641 }
5642 break;
5643 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005644
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005645 case TypeCheckKind::kClassHierarchyCheck: {
5646 // Walk over the class hierarchy to find a match.
5647 NearLabel loop, success;
5648 __ Bind(&loop);
5649 if (cls.IsRegister()) {
5650 __ cmpl(out, cls.AsRegister<CpuRegister>());
5651 } else {
5652 DCHECK(cls.IsStackSlot()) << cls;
5653 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5654 }
5655 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005656 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005657 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005658 __ testl(out, out);
5659 __ j(kNotEqual, &loop);
5660 // If `out` is null, we use it for the result, and jump to `done`.
5661 __ jmp(&done);
5662 __ Bind(&success);
5663 __ movl(out, Immediate(1));
5664 if (zero.IsLinked()) {
5665 __ jmp(&done);
5666 }
5667 break;
5668 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005669
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005671 // Do an exact check.
5672 NearLabel exact_check;
5673 if (cls.IsRegister()) {
5674 __ cmpl(out, cls.AsRegister<CpuRegister>());
5675 } else {
5676 DCHECK(cls.IsStackSlot()) << cls;
5677 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5678 }
5679 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005680 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005681 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005682 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 __ testl(out, out);
5684 // If `out` is null, we use it for the result, and jump to `done`.
5685 __ j(kEqual, &done);
5686 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5687 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005688 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005689 __ movl(out, Immediate(1));
5690 __ jmp(&done);
5691 break;
5692 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005694 case TypeCheckKind::kArrayCheck: {
5695 if (cls.IsRegister()) {
5696 __ cmpl(out, cls.AsRegister<CpuRegister>());
5697 } else {
5698 DCHECK(cls.IsStackSlot()) << cls;
5699 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5700 }
5701 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005702 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5703 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 codegen_->AddSlowPath(slow_path);
5705 __ j(kNotEqual, slow_path->GetEntryLabel());
5706 __ movl(out, Immediate(1));
5707 if (zero.IsLinked()) {
5708 __ jmp(&done);
5709 }
5710 break;
5711 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712
Calin Juravle98893e12015-10-02 21:05:03 +01005713 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005714 case TypeCheckKind::kInterfaceCheck: {
5715 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005716 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005717 // cases.
5718 //
5719 // We cannot directly call the InstanceofNonTrivial runtime
5720 // entry point without resorting to a type checking slow path
5721 // here (i.e. by calling InvokeRuntime directly), as it would
5722 // require to assign fixed registers for the inputs of this
5723 // HInstanceOf instruction (following the runtime calling
5724 // convention), which might be cluttered by the potential first
5725 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005726 //
5727 // TODO: Introduce a new runtime entry point taking the object
5728 // to test (instead of its class) as argument, and let it deal
5729 // with the read barrier issues. This will let us refactor this
5730 // case of the `switch` code as it was previously (with a direct
5731 // call to the runtime not using a type checking slow path).
5732 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 DCHECK(locations->OnlyCallsOnSlowPath());
5734 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5735 /* is_fatal */ false);
5736 codegen_->AddSlowPath(slow_path);
5737 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005738 if (zero.IsLinked()) {
5739 __ jmp(&done);
5740 }
5741 break;
5742 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005743 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005744
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005745 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005746 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 __ xorl(out, out);
5748 }
5749
5750 if (done.IsLinked()) {
5751 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005752 }
5753
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005754 if (slow_path != nullptr) {
5755 __ Bind(slow_path->GetExitLabel());
5756 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005757}
5758
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005759void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005760 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5761 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005762 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5763 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 case TypeCheckKind::kExactCheck:
5765 case TypeCheckKind::kAbstractClassCheck:
5766 case TypeCheckKind::kClassHierarchyCheck:
5767 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005768 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5769 LocationSummary::kCallOnSlowPath :
5770 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005771 break;
5772 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005773 case TypeCheckKind::kUnresolvedCheck:
5774 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 call_kind = LocationSummary::kCallOnSlowPath;
5776 break;
5777 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5779 locations->SetInAt(0, Location::RequiresRegister());
5780 locations->SetInAt(1, Location::Any());
5781 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5782 locations->AddTemp(Location::RequiresRegister());
5783 // When read barriers are enabled, we need an additional temporary
5784 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005785 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005788}
5789
5790void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005791 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005792 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005793 Location obj_loc = locations->InAt(0);
5794 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005795 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796 Location temp_loc = locations->GetTemp(0);
5797 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005798 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005799 locations->GetTemp(1) :
5800 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5802 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5803 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5804 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 bool is_type_check_slow_path_fatal =
5807 (type_check_kind == TypeCheckKind::kExactCheck ||
5808 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5809 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5810 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5811 !instruction->CanThrowIntoCatchBlock();
5812 SlowPathCode* type_check_slow_path =
5813 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5814 is_type_check_slow_path_fatal);
5815 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005816
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818 case TypeCheckKind::kExactCheck:
5819 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005820 NearLabel done;
5821 // Avoid null check if we know obj is not null.
5822 if (instruction->MustDoNullCheck()) {
5823 __ testl(obj, obj);
5824 __ j(kEqual, &done);
5825 }
5826
5827 // /* HeapReference<Class> */ temp = obj->klass_
5828 GenerateReferenceLoadTwoRegisters(
5829 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5830
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 if (cls.IsRegister()) {
5832 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5833 } else {
5834 DCHECK(cls.IsStackSlot()) << cls;
5835 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5836 }
5837 // Jump to slow path for throwing the exception or doing a
5838 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005840 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 break;
5842 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005845 NearLabel done;
5846 // Avoid null check if we know obj is not null.
5847 if (instruction->MustDoNullCheck()) {
5848 __ testl(obj, obj);
5849 __ j(kEqual, &done);
5850 }
5851
5852 // /* HeapReference<Class> */ temp = obj->klass_
5853 GenerateReferenceLoadTwoRegisters(
5854 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5855
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005856 // If the class is abstract, we eagerly fetch the super class of the
5857 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005859 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005860 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005861 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862
5863 // If the class reference currently in `temp` is not null, jump
5864 // to the `compare_classes` label to compare it with the checked
5865 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005866 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005867 __ j(kNotEqual, &compare_classes);
5868 // Otherwise, jump to the slow path to throw the exception.
5869 //
5870 // But before, move back the object's class into `temp` before
5871 // going into the slow path, as it has been overwritten in the
5872 // meantime.
5873 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005874 GenerateReferenceLoadTwoRegisters(
5875 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876 __ jmp(type_check_slow_path->GetEntryLabel());
5877
5878 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 if (cls.IsRegister()) {
5880 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5881 } else {
5882 DCHECK(cls.IsStackSlot()) << cls;
5883 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5884 }
5885 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005886 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005887 break;
5888 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005889
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005891 NearLabel done;
5892 // Avoid null check if we know obj is not null.
5893 if (instruction->MustDoNullCheck()) {
5894 __ testl(obj, obj);
5895 __ j(kEqual, &done);
5896 }
5897
5898 // /* HeapReference<Class> */ temp = obj->klass_
5899 GenerateReferenceLoadTwoRegisters(
5900 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5901
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005903 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005904 __ Bind(&loop);
5905 if (cls.IsRegister()) {
5906 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5907 } else {
5908 DCHECK(cls.IsStackSlot()) << cls;
5909 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5910 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005911 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005914 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915
5916 // If the class reference currently in `temp` is not null, jump
5917 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 __ testl(temp, temp);
5919 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005920 // Otherwise, jump to the slow path to throw the exception.
5921 //
5922 // But before, move back the object's class into `temp` before
5923 // going into the slow path, as it has been overwritten in the
5924 // meantime.
5925 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005926 GenerateReferenceLoadTwoRegisters(
5927 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005929 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 break;
5931 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005933 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005934 // We cannot use a NearLabel here, as its range might be too
5935 // short in some cases when read barriers are enabled. This has
5936 // been observed for instance when the code emitted for this
5937 // case uses high x86-64 registers (R8-R15).
5938 Label done;
5939 // Avoid null check if we know obj is not null.
5940 if (instruction->MustDoNullCheck()) {
5941 __ testl(obj, obj);
5942 __ j(kEqual, &done);
5943 }
5944
5945 // /* HeapReference<Class> */ temp = obj->klass_
5946 GenerateReferenceLoadTwoRegisters(
5947 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5948
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005949 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005951 if (cls.IsRegister()) {
5952 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5953 } else {
5954 DCHECK(cls.IsStackSlot()) << cls;
5955 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5956 }
5957 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005958
5959 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005960 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005961 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005962
5963 // If the component type is not null (i.e. the object is indeed
5964 // an array), jump to label `check_non_primitive_component_type`
5965 // to further check that this component type is not a primitive
5966 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968 __ j(kNotEqual, &check_non_primitive_component_type);
5969 // Otherwise, jump to the slow path to throw the exception.
5970 //
5971 // But before, move back the object's class into `temp` before
5972 // going into the slow path, as it has been overwritten in the
5973 // meantime.
5974 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005975 GenerateReferenceLoadTwoRegisters(
5976 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005977 __ jmp(type_check_slow_path->GetEntryLabel());
5978
5979 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981 __ j(kEqual, &done);
5982 // Same comment as above regarding `temp` and the slow path.
5983 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005984 GenerateReferenceLoadTwoRegisters(
5985 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005987 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 break;
5989 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990
Calin Juravle98893e12015-10-02 21:05:03 +01005991 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00005993 NearLabel done;
5994 // Avoid null check if we know obj is not null.
5995 if (instruction->MustDoNullCheck()) {
5996 __ testl(obj, obj);
5997 __ j(kEqual, &done);
5998 }
5999
6000 // /* HeapReference<Class> */ temp = obj->klass_
6001 GenerateReferenceLoadTwoRegisters(
6002 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6003
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006004 // We always go into the type check slow path for the unresolved
6005 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006006 //
6007 // We cannot directly call the CheckCast runtime entry point
6008 // without resorting to a type checking slow path here (i.e. by
6009 // calling InvokeRuntime directly), as it would require to
6010 // assign fixed registers for the inputs of this HInstanceOf
6011 // instruction (following the runtime calling convention), which
6012 // might be cluttered by the potential first read barrier
6013 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006014 //
6015 // TODO: Introduce a new runtime entry point taking the object
6016 // to test (instead of its class) as argument, and let it deal
6017 // with the read barrier issues. This will let us refactor this
6018 // case of the `switch` code as it was previously (with a direct
6019 // call to the runtime not using a type checking slow path).
6020 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006021 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006022 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006023 break;
6024 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025
Roland Levillain0d5a2812015-11-13 10:07:31 +00006026 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006027}
6028
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006029void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6030 LocationSummary* locations =
6031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6032 InvokeRuntimeCallingConvention calling_convention;
6033 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6034}
6035
6036void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006037 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6038 : QUICK_ENTRY_POINT(pUnlockObject),
6039 instruction,
6040 instruction->GetDexPc(),
6041 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006042 if (instruction->IsEnter()) {
6043 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6044 } else {
6045 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6046 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006047}
6048
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006049void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6050void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6051void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6052
6053void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6054 LocationSummary* locations =
6055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6056 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6057 || instruction->GetResultType() == Primitive::kPrimLong);
6058 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006059 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006060 locations->SetOut(Location::SameAsFirstInput());
6061}
6062
6063void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6064 HandleBitwiseOperation(instruction);
6065}
6066
6067void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6068 HandleBitwiseOperation(instruction);
6069}
6070
6071void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6072 HandleBitwiseOperation(instruction);
6073}
6074
6075void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6076 LocationSummary* locations = instruction->GetLocations();
6077 Location first = locations->InAt(0);
6078 Location second = locations->InAt(1);
6079 DCHECK(first.Equals(locations->Out()));
6080
6081 if (instruction->GetResultType() == Primitive::kPrimInt) {
6082 if (second.IsRegister()) {
6083 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006084 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006085 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006086 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006087 } else {
6088 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006089 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006090 }
6091 } else if (second.IsConstant()) {
6092 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6093 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006094 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006095 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006096 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006097 } else {
6098 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006099 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006100 }
6101 } else {
6102 Address address(CpuRegister(RSP), second.GetStackIndex());
6103 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006104 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006105 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006106 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006107 } else {
6108 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006109 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006110 }
6111 }
6112 } else {
6113 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006114 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6115 bool second_is_constant = false;
6116 int64_t value = 0;
6117 if (second.IsConstant()) {
6118 second_is_constant = true;
6119 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006120 }
Mark Mendell40741f32015-04-20 22:10:34 -04006121 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006122
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006123 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006124 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006125 if (is_int32_value) {
6126 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6127 } else {
6128 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6129 }
6130 } else if (second.IsDoubleStackSlot()) {
6131 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006132 } else {
6133 __ andq(first_reg, second.AsRegister<CpuRegister>());
6134 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006135 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006136 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006137 if (is_int32_value) {
6138 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6139 } else {
6140 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6141 }
6142 } else if (second.IsDoubleStackSlot()) {
6143 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006144 } else {
6145 __ orq(first_reg, second.AsRegister<CpuRegister>());
6146 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006147 } else {
6148 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006149 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006150 if (is_int32_value) {
6151 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6152 } else {
6153 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6154 }
6155 } else if (second.IsDoubleStackSlot()) {
6156 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006157 } else {
6158 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6159 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006160 }
6161 }
6162}
6163
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006164void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6165 Location out,
6166 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006167 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006168 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6169 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006170 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006171 if (kUseBakerReadBarrier) {
6172 // Load with fast path based Baker's read barrier.
6173 // /* HeapReference<Object> */ out = *(out + offset)
6174 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006175 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006176 } else {
6177 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006178 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006179 // in the following move operation, as we will need it for the
6180 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006181 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006182 // /* HeapReference<Object> */ out = *(out + offset)
6183 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006184 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006185 }
6186 } else {
6187 // Plain load with no read barrier.
6188 // /* HeapReference<Object> */ out = *(out + offset)
6189 __ movl(out_reg, Address(out_reg, offset));
6190 __ MaybeUnpoisonHeapReference(out_reg);
6191 }
6192}
6193
6194void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6195 Location out,
6196 Location obj,
6197 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006198 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006199 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6200 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6201 if (kEmitCompilerReadBarrier) {
6202 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006203 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006204 // Load with fast path based Baker's read barrier.
6205 // /* HeapReference<Object> */ out = *(obj + offset)
6206 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006207 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006208 } else {
6209 // Load with slow path based read barrier.
6210 // /* HeapReference<Object> */ out = *(obj + offset)
6211 __ movl(out_reg, Address(obj_reg, offset));
6212 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6213 }
6214 } else {
6215 // Plain load with no read barrier.
6216 // /* HeapReference<Object> */ out = *(obj + offset)
6217 __ movl(out_reg, Address(obj_reg, offset));
6218 __ MaybeUnpoisonHeapReference(out_reg);
6219 }
6220}
6221
6222void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6223 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006224 const Address& address,
6225 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006226 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6227 if (kEmitCompilerReadBarrier) {
6228 if (kUseBakerReadBarrier) {
6229 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6230 // Baker's read barrier are used:
6231 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006232 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006233 // if (Thread::Current()->GetIsGcMarking()) {
6234 // root = ReadBarrier::Mark(root)
6235 // }
6236
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 // /* GcRoot<mirror::Object> */ root = *address
6238 __ movl(root_reg, address);
6239 if (fixup_label != nullptr) {
6240 __ Bind(fixup_label);
6241 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006242 static_assert(
6243 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6244 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6245 "have different sizes.");
6246 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6247 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6248 "have different sizes.");
6249
6250 // Slow path used to mark the GC root `root`.
6251 SlowPathCode* slow_path =
6252 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6253 codegen_->AddSlowPath(slow_path);
6254
6255 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6256 /* no_rip */ true),
6257 Immediate(0));
6258 __ j(kNotEqual, slow_path->GetEntryLabel());
6259 __ Bind(slow_path->GetExitLabel());
6260 } else {
6261 // GC root loaded through a slow path for read barriers other
6262 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006263 // /* GcRoot<mirror::Object>* */ root = address
6264 __ leaq(root_reg, address);
6265 if (fixup_label != nullptr) {
6266 __ Bind(fixup_label);
6267 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006268 // /* mirror::Object* */ root = root->Read()
6269 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6270 }
6271 } else {
6272 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006273 // /* GcRoot<mirror::Object> */ root = *address
6274 __ movl(root_reg, address);
6275 if (fixup_label != nullptr) {
6276 __ Bind(fixup_label);
6277 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006278 // Note that GC roots are not affected by heap poisoning, thus we
6279 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006280 }
6281}
6282
6283void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6284 Location ref,
6285 CpuRegister obj,
6286 uint32_t offset,
6287 Location temp,
6288 bool needs_null_check) {
6289 DCHECK(kEmitCompilerReadBarrier);
6290 DCHECK(kUseBakerReadBarrier);
6291
6292 // /* HeapReference<Object> */ ref = *(obj + offset)
6293 Address src(obj, offset);
6294 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6295}
6296
6297void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6298 Location ref,
6299 CpuRegister obj,
6300 uint32_t data_offset,
6301 Location index,
6302 Location temp,
6303 bool needs_null_check) {
6304 DCHECK(kEmitCompilerReadBarrier);
6305 DCHECK(kUseBakerReadBarrier);
6306
6307 // /* HeapReference<Object> */ ref =
6308 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6309 Address src = index.IsConstant() ?
6310 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6311 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6312 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6313}
6314
6315void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6316 Location ref,
6317 CpuRegister obj,
6318 const Address& src,
6319 Location temp,
6320 bool needs_null_check) {
6321 DCHECK(kEmitCompilerReadBarrier);
6322 DCHECK(kUseBakerReadBarrier);
6323
6324 // In slow path based read barriers, the read barrier call is
6325 // inserted after the original load. However, in fast path based
6326 // Baker's read barriers, we need to perform the load of
6327 // mirror::Object::monitor_ *before* the original reference load.
6328 // This load-load ordering is required by the read barrier.
6329 // The fast path/slow path (for Baker's algorithm) should look like:
6330 //
6331 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6332 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6333 // HeapReference<Object> ref = *src; // Original reference load.
6334 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6335 // if (is_gray) {
6336 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6337 // }
6338 //
6339 // Note: the original implementation in ReadBarrier::Barrier is
6340 // slightly more complex as:
6341 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006342 // the high-bits of rb_state, which are expected to be all zeroes
6343 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6344 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006345 // - it performs additional checks that we do not do here for
6346 // performance reasons.
6347
6348 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6349 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6350 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6351
6352 // /* int32_t */ monitor = obj->monitor_
6353 __ movl(temp_reg, Address(obj, monitor_offset));
6354 if (needs_null_check) {
6355 MaybeRecordImplicitNullCheck(instruction);
6356 }
6357 // /* LockWord */ lock_word = LockWord(monitor)
6358 static_assert(sizeof(LockWord) == sizeof(int32_t),
6359 "art::LockWord and int32_t have different sizes.");
6360 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6361 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6362 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6363 static_assert(
6364 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6365 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6366
6367 // Load fence to prevent load-load reordering.
6368 // Note that this is a no-op, thanks to the x86-64 memory model.
6369 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6370
6371 // The actual reference load.
6372 // /* HeapReference<Object> */ ref = *src
6373 __ movl(ref_reg, src);
6374
6375 // Object* ref = ref_addr->AsMirrorPtr()
6376 __ MaybeUnpoisonHeapReference(ref_reg);
6377
6378 // Slow path used to mark the object `ref` when it is gray.
6379 SlowPathCode* slow_path =
6380 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6381 AddSlowPath(slow_path);
6382
6383 // if (rb_state == ReadBarrier::gray_ptr_)
6384 // ref = ReadBarrier::Mark(ref);
6385 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6386 __ j(kEqual, slow_path->GetEntryLabel());
6387 __ Bind(slow_path->GetExitLabel());
6388}
6389
6390void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6391 Location out,
6392 Location ref,
6393 Location obj,
6394 uint32_t offset,
6395 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396 DCHECK(kEmitCompilerReadBarrier);
6397
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006398 // Insert a slow path based read barrier *after* the reference load.
6399 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 // If heap poisoning is enabled, the unpoisoning of the loaded
6401 // reference will be carried out by the runtime within the slow
6402 // path.
6403 //
6404 // Note that `ref` currently does not get unpoisoned (when heap
6405 // poisoning is enabled), which is alright as the `ref` argument is
6406 // not used by the artReadBarrierSlow entry point.
6407 //
6408 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6409 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6410 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6411 AddSlowPath(slow_path);
6412
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 __ jmp(slow_path->GetEntryLabel());
6414 __ Bind(slow_path->GetExitLabel());
6415}
6416
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006417void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6418 Location out,
6419 Location ref,
6420 Location obj,
6421 uint32_t offset,
6422 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006424 // Baker's read barriers shall be handled by the fast path
6425 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6426 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427 // If heap poisoning is enabled, unpoisoning will be taken care of
6428 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006429 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 } else if (kPoisonHeapReferences) {
6431 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6432 }
6433}
6434
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006435void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6436 Location out,
6437 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438 DCHECK(kEmitCompilerReadBarrier);
6439
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006440 // Insert a slow path based read barrier *after* the GC root load.
6441 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442 // Note that GC roots are not affected by heap poisoning, so we do
6443 // not need to do anything special for this here.
6444 SlowPathCode* slow_path =
6445 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6446 AddSlowPath(slow_path);
6447
Roland Levillain0d5a2812015-11-13 10:07:31 +00006448 __ jmp(slow_path->GetEntryLabel());
6449 __ Bind(slow_path->GetExitLabel());
6450}
6451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006452void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006453 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006454 LOG(FATAL) << "Unreachable";
6455}
6456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006457void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006458 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006459 LOG(FATAL) << "Unreachable";
6460}
6461
Mark Mendellfe57faa2015-09-18 09:26:15 -04006462// Simple implementation of packed switch - generate cascaded compare/jumps.
6463void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6464 LocationSummary* locations =
6465 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6466 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006467 locations->AddTemp(Location::RequiresRegister());
6468 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006469}
6470
6471void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6472 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006473 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006474 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006475 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6476 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6477 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006478 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6479
6480 // Should we generate smaller inline compare/jumps?
6481 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6482 // Figure out the correct compare values and jump conditions.
6483 // Handle the first compare/branch as a special case because it might
6484 // jump to the default case.
6485 DCHECK_GT(num_entries, 2u);
6486 Condition first_condition;
6487 uint32_t index;
6488 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6489 if (lower_bound != 0) {
6490 first_condition = kLess;
6491 __ cmpl(value_reg_in, Immediate(lower_bound));
6492 __ j(first_condition, codegen_->GetLabelOf(default_block));
6493 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6494
6495 index = 1;
6496 } else {
6497 // Handle all the compare/jumps below.
6498 first_condition = kBelow;
6499 index = 0;
6500 }
6501
6502 // Handle the rest of the compare/jumps.
6503 for (; index + 1 < num_entries; index += 2) {
6504 int32_t compare_to_value = lower_bound + index + 1;
6505 __ cmpl(value_reg_in, Immediate(compare_to_value));
6506 // Jump to successors[index] if value < case_value[index].
6507 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6508 // Jump to successors[index + 1] if value == case_value[index + 1].
6509 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6510 }
6511
6512 if (index != num_entries) {
6513 // There are an odd number of entries. Handle the last one.
6514 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006515 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006516 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6517 }
6518
6519 // And the default for any other value.
6520 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6521 __ jmp(codegen_->GetLabelOf(default_block));
6522 }
6523 return;
6524 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006525
6526 // Remove the bias, if needed.
6527 Register value_reg_out = value_reg_in.AsRegister();
6528 if (lower_bound != 0) {
6529 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6530 value_reg_out = temp_reg.AsRegister();
6531 }
6532 CpuRegister value_reg(value_reg_out);
6533
6534 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006535 __ cmpl(value_reg, Immediate(num_entries - 1));
6536 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006537
Mark Mendell9c86b482015-09-18 13:36:07 -04006538 // We are in the range of the table.
6539 // Load the address of the jump table in the constant area.
6540 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006541
Mark Mendell9c86b482015-09-18 13:36:07 -04006542 // Load the (signed) offset from the jump table.
6543 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6544
6545 // Add the offset to the address of the table base.
6546 __ addq(temp_reg, base_reg);
6547
6548 // And jump.
6549 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006550}
6551
Aart Bikc5d47542016-01-27 17:00:35 -08006552void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6553 if (value == 0) {
6554 __ xorl(dest, dest);
6555 } else {
6556 __ movl(dest, Immediate(value));
6557 }
6558}
6559
Mark Mendell92e83bf2015-05-07 11:25:03 -04006560void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6561 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006562 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006563 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006564 } else if (IsUint<32>(value)) {
6565 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006566 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6567 } else {
6568 __ movq(dest, Immediate(value));
6569 }
6570}
6571
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006572void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6573 if (value == 0) {
6574 __ xorps(dest, dest);
6575 } else {
6576 __ movss(dest, LiteralInt32Address(value));
6577 }
6578}
6579
6580void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6581 if (value == 0) {
6582 __ xorpd(dest, dest);
6583 } else {
6584 __ movsd(dest, LiteralInt64Address(value));
6585 }
6586}
6587
6588void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6589 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6590}
6591
6592void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6593 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6594}
6595
Aart Bika19616e2016-02-01 18:57:58 -08006596void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6597 if (value == 0) {
6598 __ testl(dest, dest);
6599 } else {
6600 __ cmpl(dest, Immediate(value));
6601 }
6602}
6603
6604void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6605 if (IsInt<32>(value)) {
6606 if (value == 0) {
6607 __ testq(dest, dest);
6608 } else {
6609 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6610 }
6611 } else {
6612 // Value won't fit in an int.
6613 __ cmpq(dest, LiteralInt64Address(value));
6614 }
6615}
6616
Mark Mendellcfa410b2015-05-25 16:02:44 -04006617void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6618 DCHECK(dest.IsDoubleStackSlot());
6619 if (IsInt<32>(value)) {
6620 // Can move directly as an int32 constant.
6621 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6622 Immediate(static_cast<int32_t>(value)));
6623 } else {
6624 Load64BitValue(CpuRegister(TMP), value);
6625 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6626 }
6627}
6628
Mark Mendell9c86b482015-09-18 13:36:07 -04006629/**
6630 * Class to handle late fixup of offsets into constant area.
6631 */
6632class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6633 public:
6634 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6635 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6636
6637 protected:
6638 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6639
6640 CodeGeneratorX86_64* codegen_;
6641
6642 private:
6643 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6644 // Patch the correct offset for the instruction. We use the address of the
6645 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6646 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6647 int32_t relative_position = constant_offset - pos;
6648
6649 // Patch in the right value.
6650 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6651 }
6652
6653 // Location in constant area that the fixup refers to.
6654 size_t offset_into_constant_area_;
6655};
6656
6657/**
6658 t * Class to handle late fixup of offsets to a jump table that will be created in the
6659 * constant area.
6660 */
6661class JumpTableRIPFixup : public RIPFixup {
6662 public:
6663 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6664 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6665
6666 void CreateJumpTable() {
6667 X86_64Assembler* assembler = codegen_->GetAssembler();
6668
6669 // Ensure that the reference to the jump table has the correct offset.
6670 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6671 SetOffset(offset_in_constant_table);
6672
6673 // Compute the offset from the start of the function to this jump table.
6674 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6675
6676 // Populate the jump table with the correct values for the jump table.
6677 int32_t num_entries = switch_instr_->GetNumEntries();
6678 HBasicBlock* block = switch_instr_->GetBlock();
6679 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6680 // The value that we want is the target offset - the position of the table.
6681 for (int32_t i = 0; i < num_entries; i++) {
6682 HBasicBlock* b = successors[i];
6683 Label* l = codegen_->GetLabelOf(b);
6684 DCHECK(l->IsBound());
6685 int32_t offset_to_block = l->Position() - current_table_offset;
6686 assembler->AppendInt32(offset_to_block);
6687 }
6688 }
6689
6690 private:
6691 const HPackedSwitch* switch_instr_;
6692};
6693
Mark Mendellf55c3e02015-03-26 21:07:46 -04006694void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6695 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006696 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006697 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6698 // 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 -04006699 assembler->Align(4, 0);
6700 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006701
6702 // Populate any jump tables.
6703 for (auto jump_table : fixups_to_jump_tables_) {
6704 jump_table->CreateJumpTable();
6705 }
6706
6707 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006708 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006709 }
6710
6711 // And finish up.
6712 CodeGenerator::Finalize(allocator);
6713}
6714
Mark Mendellf55c3e02015-03-26 21:07:46 -04006715Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6716 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6717 return Address::RIP(fixup);
6718}
6719
6720Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6721 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6722 return Address::RIP(fixup);
6723}
6724
6725Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6726 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6727 return Address::RIP(fixup);
6728}
6729
6730Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6731 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6732 return Address::RIP(fixup);
6733}
6734
Andreas Gampe85b62f22015-09-09 13:15:38 -07006735// TODO: trg as memory.
6736void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6737 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006738 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006739 return;
6740 }
6741
6742 DCHECK_NE(type, Primitive::kPrimVoid);
6743
6744 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6745 if (trg.Equals(return_loc)) {
6746 return;
6747 }
6748
6749 // Let the parallel move resolver take care of all of this.
6750 HParallelMove parallel_move(GetGraph()->GetArena());
6751 parallel_move.AddMove(return_loc, trg, type, nullptr);
6752 GetMoveResolver()->EmitNativeCode(&parallel_move);
6753}
6754
Mark Mendell9c86b482015-09-18 13:36:07 -04006755Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6756 // Create a fixup to be used to create and address the jump table.
6757 JumpTableRIPFixup* table_fixup =
6758 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6759
6760 // We have to populate the jump tables.
6761 fixups_to_jump_tables_.push_back(table_fixup);
6762 return Address::RIP(table_fixup);
6763}
6764
Mark Mendellea5af682015-10-22 17:35:49 -04006765void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6766 const Address& addr_high,
6767 int64_t v,
6768 HInstruction* instruction) {
6769 if (IsInt<32>(v)) {
6770 int32_t v_32 = v;
6771 __ movq(addr_low, Immediate(v_32));
6772 MaybeRecordImplicitNullCheck(instruction);
6773 } else {
6774 // Didn't fit in a register. Do it in pieces.
6775 int32_t low_v = Low32Bits(v);
6776 int32_t high_v = High32Bits(v);
6777 __ movl(addr_low, Immediate(low_v));
6778 MaybeRecordImplicitNullCheck(instruction);
6779 __ movl(addr_high, Immediate(high_v));
6780 }
6781}
6782
Roland Levillain4d027112015-07-01 15:41:14 +01006783#undef __
6784
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006785} // namespace x86_64
6786} // namespace art