blob: 504eaa8bba47e8b8e84af3b34987aa8b7f6a68f3 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain62a46b22015-06-01 18:24:13 +010054#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010055#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Andreas Gampe85b62f22015-09-09 13:15:38 -070057class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000059 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Alexandre Rames2ed20af2015-03-06 13:55:35 +000061 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000062 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000064 if (instruction_->CanThrowIntoCatchBlock()) {
65 // Live registers will be restored in the catch block if caught.
66 SaveLiveRegisters(codegen, instruction_->GetLocations());
67 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000068 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
69 instruction_,
70 instruction_->GetDexPc(),
71 this);
Roland Levillain888d0672015-11-23 18:53:50 +000072 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 }
74
Alexandre Rames8158f282015-08-07 10:26:17 +010075 bool IsFatal() const OVERRIDE { return true; }
76
Alexandre Rames9931f312015-06-19 14:47:01 +010077 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
78
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
81};
82
Andreas Gampe85b62f22015-09-09 13:15:38 -070083class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000085 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000086
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000088 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000090 if (instruction_->CanThrowIntoCatchBlock()) {
91 // Live registers will be restored in the catch block if caught.
92 SaveLiveRegisters(codegen, instruction_->GetLocations());
93 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000094 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
95 instruction_,
96 instruction_->GetDexPc(),
97 this);
Roland Levillain888d0672015-11-23 18:53:50 +000098 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000099 }
100
Alexandre Rames8158f282015-08-07 10:26:17 +0100101 bool IsFatal() const OVERRIDE { return true; }
102
Alexandre Rames9931f312015-06-19 14:47:01 +0100103 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
104
Calin Juravled0d48522014-11-04 16:40:20 +0000105 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
107};
108
Andreas Gampe85b62f22015-09-09 13:15:38 -0700109class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000110 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000111 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
112 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000113
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000114 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 if (is_div_) {
118 __ negl(cpu_reg_);
119 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400120 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000121 }
122
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000123 } else {
124 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 if (is_div_) {
126 __ negq(cpu_reg_);
127 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400128 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000129 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000130 }
Calin Juravled0d48522014-11-04 16:40:20 +0000131 __ jmp(GetExitLabel());
132 }
133
Alexandre Rames9931f312015-06-19 14:47:01 +0100134 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
135
Calin Juravled0d48522014-11-04 16:40:20 +0000136 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000138 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000139 const bool is_div_;
140 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000141};
142
Andreas Gampe85b62f22015-09-09 13:15:38 -0700143class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100145 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000146 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000148 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000149 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000151 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000152 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000157 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 if (successor_ == nullptr) {
159 __ jmp(GetReturnLabel());
160 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000161 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 }
164
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 Label* GetReturnLabel() {
166 DCHECK(successor_ == nullptr);
167 return &return_label_;
168 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100170 HBasicBlock* GetSuccessor() const {
171 return successor_;
172 }
173
Alexandre Rames9931f312015-06-19 14:47:01 +0100174 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
175
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178 Label return_label_;
179
180 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
181};
182
Andreas Gampe85b62f22015-09-09 13:15:38 -0700183class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000186 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100189 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000190 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000192 if (instruction_->CanThrowIntoCatchBlock()) {
193 // Live registers will be restored in the catch block if caught.
194 SaveLiveRegisters(codegen, instruction_->GetLocations());
195 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000196 // We're moving two locations to locations that could overlap, so we need a parallel
197 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000199 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100200 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000201 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100202 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100203 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100204 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
205 Primitive::kPrimInt);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000206 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
207 instruction_,
208 instruction_->GetDexPc(),
209 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000210 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100211 }
212
Alexandre Rames8158f282015-08-07 10:26:17 +0100213 bool IsFatal() const OVERRIDE { return true; }
214
Alexandre Rames9931f312015-06-19 14:47:01 +0100215 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
216
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100217 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100218 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
219};
220
Andreas Gampe85b62f22015-09-09 13:15:38 -0700221class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 LoadClassSlowPathX86_64(HLoadClass* cls,
224 HInstruction* at,
225 uint32_t dex_pc,
226 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000227 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000233 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000236 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100238 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000240 x86_64_codegen->InvokeRuntime(do_clinit_ ?
241 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
242 QUICK_ENTRY_POINT(pInitializeType),
243 at_,
244 dex_pc_,
245 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000246 if (do_clinit_) {
247 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
248 } else {
249 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
250 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000252 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 if (out.IsValid()) {
255 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000256 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257 }
258
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260 __ jmp(GetExitLabel());
261 }
262
Alexandre Rames9931f312015-06-19 14:47:01 +0100263 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100265 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 // The class this slow path will load.
267 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100268
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 // The instruction where this slow path is happening.
270 // (Might be the load class or an initialization check).
271 HInstruction* const at_;
272
273 // The dex PC of `at_`.
274 const uint32_t dex_pc_;
275
276 // Whether to initialize the class.
277 const bool do_clinit_;
278
279 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100280};
281
Andreas Gampe85b62f22015-09-09 13:15:38 -0700282class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000285
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000287 LocationSummary* locations = instruction_->GetLocations();
288 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
289
Roland Levillain0d5a2812015-11-13 10:07:31 +0000290 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000291 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000292 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000293
294 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000295 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
296 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000297 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
298 instruction_,
299 instruction_->GetDexPc(),
300 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000302 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000303 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000304 __ jmp(GetExitLabel());
305 }
306
Alexandre Rames9931f312015-06-19 14:47:01 +0100307 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
308
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000309 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000310 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
311};
312
Andreas Gampe85b62f22015-09-09 13:15:38 -0700313class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000316 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
321 : locations->Out();
322 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 DCHECK(instruction_->IsCheckCast()
324 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
Roland Levillain0d5a2812015-11-13 10:07:31 +0000326 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 if (!is_fatal_) {
330 SaveLiveRegisters(codegen, locations);
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 // We're moving two locations to locations that could overlap, so we need a parallel
334 // move resolver.
335 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000336 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100339 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100340 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
342 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000345 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
346 instruction_,
347 dex_pc,
348 this);
349 CheckEntrypointTypes<
350 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 } else {
352 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000353 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
354 instruction_,
355 dex_pc,
356 this);
357 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 if (!is_fatal_) {
361 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000362 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 RestoreLiveRegisters(codegen, locations);
366 __ jmp(GetExitLabel());
367 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 bool IsFatal() const OVERRIDE { return is_fatal_; }
373
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000374 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000376
377 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
378};
379
Andreas Gampe85b62f22015-09-09 13:15:38 -0700380class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 public:
Aart Bik42249c32016-01-07 15:33:50 -0800382 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000386 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000389 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800390 instruction_,
391 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000392 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000393 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
397
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
400};
401
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402class ArraySetSlowPathX86_64 : public SlowPathCode {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405
406 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
407 LocationSummary* locations = instruction_->GetLocations();
408 __ Bind(GetEntryLabel());
409 SaveLiveRegisters(codegen, locations);
410
411 InvokeRuntimeCallingConvention calling_convention;
412 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
413 parallel_move.AddMove(
414 locations->InAt(0),
415 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
416 Primitive::kPrimNot,
417 nullptr);
418 parallel_move.AddMove(
419 locations->InAt(1),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
421 Primitive::kPrimInt,
422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(2),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
426 Primitive::kPrimNot,
427 nullptr);
428 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
429
Roland Levillain0d5a2812015-11-13 10:07:31 +0000430 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
431 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
432 instruction_,
433 instruction_->GetDexPc(),
434 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000435 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 RestoreLiveRegisters(codegen, locations);
437 __ jmp(GetExitLabel());
438 }
439
440 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
441
442 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100443 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
444};
445
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000446// Slow path marking an object during a read barrier.
447class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
448 public:
449 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000450 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000451 DCHECK(kEmitCompilerReadBarrier);
452 }
453
454 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
455
456 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
457 LocationSummary* locations = instruction_->GetLocations();
458 Register reg_out = out_.AsRegister<Register>();
459 DCHECK(locations->CanCall());
460 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
461 DCHECK(instruction_->IsInstanceFieldGet() ||
462 instruction_->IsStaticFieldGet() ||
463 instruction_->IsArrayGet() ||
464 instruction_->IsLoadClass() ||
465 instruction_->IsLoadString() ||
466 instruction_->IsInstanceOf() ||
467 instruction_->IsCheckCast())
468 << "Unexpected instruction in read barrier marking slow path: "
469 << instruction_->DebugName();
470
471 __ Bind(GetEntryLabel());
472 SaveLiveRegisters(codegen, locations);
473
474 InvokeRuntimeCallingConvention calling_convention;
475 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
476 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
477 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
478 instruction_,
479 instruction_->GetDexPc(),
480 this);
481 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
482 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
483
484 RestoreLiveRegisters(codegen, locations);
485 __ jmp(GetExitLabel());
486 }
487
488 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000489 const Location out_;
490 const Location obj_;
491
492 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
493};
494
Roland Levillain0d5a2812015-11-13 10:07:31 +0000495// Slow path generating a read barrier for a heap reference.
496class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
497 public:
498 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
499 Location out,
500 Location ref,
501 Location obj,
502 uint32_t offset,
503 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000504 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000505 out_(out),
506 ref_(ref),
507 obj_(obj),
508 offset_(offset),
509 index_(index) {
510 DCHECK(kEmitCompilerReadBarrier);
511 // If `obj` is equal to `out` or `ref`, it means the initial
512 // object has been overwritten by (or after) the heap object
513 // reference load to be instrumented, e.g.:
514 //
515 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000516 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000517 //
518 // In that case, we have lost the information about the original
519 // object, and the emitted read barrier cannot work properly.
520 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
521 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
522}
523
524 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
525 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
526 LocationSummary* locations = instruction_->GetLocations();
527 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
528 DCHECK(locations->CanCall());
529 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
530 DCHECK(!instruction_->IsInvoke() ||
531 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000532 instruction_->GetLocations()->Intrinsified()))
533 << "Unexpected instruction in read barrier for heap reference slow path: "
534 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000535
536 __ Bind(GetEntryLabel());
537 SaveLiveRegisters(codegen, locations);
538
539 // We may have to change the index's value, but as `index_` is a
540 // constant member (like other "inputs" of this slow path),
541 // introduce a copy of it, `index`.
542 Location index = index_;
543 if (index_.IsValid()) {
544 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
545 if (instruction_->IsArrayGet()) {
546 // Compute real offset and store it in index_.
547 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
548 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
549 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
550 // We are about to change the value of `index_reg` (see the
551 // calls to art::x86_64::X86_64Assembler::shll and
552 // art::x86_64::X86_64Assembler::AddImmediate below), but it
553 // has not been saved by the previous call to
554 // art::SlowPathCode::SaveLiveRegisters, as it is a
555 // callee-save register --
556 // art::SlowPathCode::SaveLiveRegisters does not consider
557 // callee-save registers, as it has been designed with the
558 // assumption that callee-save registers are supposed to be
559 // handled by the called function. So, as a callee-save
560 // register, `index_reg` _would_ eventually be saved onto
561 // the stack, but it would be too late: we would have
562 // changed its value earlier. Therefore, we manually save
563 // it here into another freely available register,
564 // `free_reg`, chosen of course among the caller-save
565 // registers (as a callee-save `free_reg` register would
566 // exhibit the same problem).
567 //
568 // Note we could have requested a temporary register from
569 // the register allocator instead; but we prefer not to, as
570 // this is a slow path, and we know we can find a
571 // caller-save register that is available.
572 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
573 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
574 index_reg = free_reg;
575 index = Location::RegisterLocation(index_reg);
576 } else {
577 // The initial register stored in `index_` has already been
578 // saved in the call to art::SlowPathCode::SaveLiveRegisters
579 // (as it is not a callee-save register), so we can freely
580 // use it.
581 }
582 // Shifting the index value contained in `index_reg` by the
583 // scale factor (2) cannot overflow in practice, as the
584 // runtime is unable to allocate object arrays with a size
585 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
586 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
587 static_assert(
588 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
589 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
590 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
591 } else {
592 DCHECK(instruction_->IsInvoke());
593 DCHECK(instruction_->GetLocations()->Intrinsified());
594 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
595 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
596 << instruction_->AsInvoke()->GetIntrinsic();
597 DCHECK_EQ(offset_, 0U);
598 DCHECK(index_.IsRegister());
599 }
600 }
601
602 // We're moving two or three locations to locations that could
603 // overlap, so we need a parallel move resolver.
604 InvokeRuntimeCallingConvention calling_convention;
605 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
606 parallel_move.AddMove(ref_,
607 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
608 Primitive::kPrimNot,
609 nullptr);
610 parallel_move.AddMove(obj_,
611 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
612 Primitive::kPrimNot,
613 nullptr);
614 if (index.IsValid()) {
615 parallel_move.AddMove(index,
616 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
617 Primitive::kPrimInt,
618 nullptr);
619 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
620 } else {
621 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
622 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
623 }
624 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
625 instruction_,
626 instruction_->GetDexPc(),
627 this);
628 CheckEntrypointTypes<
629 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
630 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
631
632 RestoreLiveRegisters(codegen, locations);
633 __ jmp(GetExitLabel());
634 }
635
636 const char* GetDescription() const OVERRIDE {
637 return "ReadBarrierForHeapReferenceSlowPathX86_64";
638 }
639
640 private:
641 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
642 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
643 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
644 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
645 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
646 return static_cast<CpuRegister>(i);
647 }
648 }
649 // We shall never fail to find a free caller-save register, as
650 // there are more than two core caller-save registers on x86-64
651 // (meaning it is possible to find one which is different from
652 // `ref` and `obj`).
653 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
654 LOG(FATAL) << "Could not find a free caller-save register";
655 UNREACHABLE();
656 }
657
Roland Levillain0d5a2812015-11-13 10:07:31 +0000658 const Location out_;
659 const Location ref_;
660 const Location obj_;
661 const uint32_t offset_;
662 // An additional location containing an index to an array.
663 // Only used for HArrayGet and the UnsafeGetObject &
664 // UnsafeGetObjectVolatile intrinsics.
665 const Location index_;
666
667 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
668};
669
670// Slow path generating a read barrier for a GC root.
671class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
672 public:
673 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000674 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000675 DCHECK(kEmitCompilerReadBarrier);
676 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 LocationSummary* locations = instruction_->GetLocations();
680 DCHECK(locations->CanCall());
681 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000682 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
683 << "Unexpected instruction in read barrier for GC root slow path: "
684 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000685
686 __ Bind(GetEntryLabel());
687 SaveLiveRegisters(codegen, locations);
688
689 InvokeRuntimeCallingConvention calling_convention;
690 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
691 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
692 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
693 instruction_,
694 instruction_->GetDexPc(),
695 this);
696 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
697 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
698
699 RestoreLiveRegisters(codegen, locations);
700 __ jmp(GetExitLabel());
701 }
702
703 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
704
705 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000706 const Location out_;
707 const Location root_;
708
709 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
710};
711
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100712#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100713#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100714
Roland Levillain4fa13f62015-07-06 18:11:54 +0100715inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700716 switch (cond) {
717 case kCondEQ: return kEqual;
718 case kCondNE: return kNotEqual;
719 case kCondLT: return kLess;
720 case kCondLE: return kLessEqual;
721 case kCondGT: return kGreater;
722 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700723 case kCondB: return kBelow;
724 case kCondBE: return kBelowEqual;
725 case kCondA: return kAbove;
726 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700727 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728 LOG(FATAL) << "Unreachable";
729 UNREACHABLE();
730}
731
Aart Bike9f37602015-10-09 11:15:55 -0700732// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733inline Condition X86_64FPCondition(IfCondition cond) {
734 switch (cond) {
735 case kCondEQ: return kEqual;
736 case kCondNE: return kNotEqual;
737 case kCondLT: return kBelow;
738 case kCondLE: return kBelowEqual;
739 case kCondGT: return kAbove;
740 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700741 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100742 };
743 LOG(FATAL) << "Unreachable";
744 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700745}
746
Vladimir Markodc151b22015-10-15 18:02:30 +0100747HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
748 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
749 MethodReference target_method ATTRIBUTE_UNUSED) {
750 switch (desired_dispatch_info.code_ptr_location) {
751 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
752 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
753 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
754 return HInvokeStaticOrDirect::DispatchInfo {
755 desired_dispatch_info.method_load_kind,
756 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
757 desired_dispatch_info.method_load_data,
758 0u
759 };
760 default:
761 return desired_dispatch_info;
762 }
763}
764
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800765void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100766 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800767 // All registers are assumed to be correctly set up.
768
Vladimir Marko58155012015-08-19 12:49:41 +0000769 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
770 switch (invoke->GetMethodLoadKind()) {
771 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
772 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000773 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000774 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000775 break;
776 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000777 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000778 break;
779 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
780 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
781 break;
782 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
783 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
784 method_patches_.emplace_back(invoke->GetTargetMethod());
785 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
786 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000787 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000788 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000789 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000790 // Bind a new fixup label at the end of the "movl" insn.
791 uint32_t offset = invoke->GetDexCacheArrayOffset();
792 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000793 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000794 }
Vladimir Marko58155012015-08-19 12:49:41 +0000795 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000796 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000797 Register method_reg;
798 CpuRegister reg = temp.AsRegister<CpuRegister>();
799 if (current_method.IsRegister()) {
800 method_reg = current_method.AsRegister<Register>();
801 } else {
802 DCHECK(invoke->GetLocations()->Intrinsified());
803 DCHECK(!current_method.IsValid());
804 method_reg = reg.AsRegister();
805 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
806 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000807 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100808 __ movq(reg,
809 Address(CpuRegister(method_reg),
810 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000811 // temp = temp[index_in_cache]
812 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
813 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
814 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100815 }
Vladimir Marko58155012015-08-19 12:49:41 +0000816 }
817
818 switch (invoke->GetCodePtrLocation()) {
819 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
820 __ call(&frame_entry_label_);
821 break;
822 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
823 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
824 Label* label = &relative_call_patches_.back().label;
825 __ call(label); // Bind to the patch label, override at link time.
826 __ Bind(label); // Bind the label at the end of the "call" insn.
827 break;
828 }
829 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
830 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100831 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
832 LOG(FATAL) << "Unsupported";
833 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000834 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
835 // (callee_method + offset_of_quick_compiled_code)()
836 __ call(Address(callee_method.AsRegister<CpuRegister>(),
837 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
838 kX86_64WordSize).SizeValue()));
839 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000840 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800841
842 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800843}
844
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000845void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
846 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
847 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
848 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000849
850 // Use the calling convention instead of the location of the receiver, as
851 // intrinsics may have put the receiver in a different register. In the intrinsics
852 // slow path, the arguments have been moved to the right place, so here we are
853 // guaranteed that the receiver is the first register of the calling convention.
854 InvokeDexCallingConvention calling_convention;
855 Register receiver = calling_convention.GetRegisterAt(0);
856
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000857 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000858 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000859 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000860 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000861 // Instead of simply (possibly) unpoisoning `temp` here, we should
862 // emit a read barrier for the previous class reference load.
863 // However this is not required in practice, as this is an
864 // intermediate/temporary reference and because the current
865 // concurrent copying collector keeps the from-space memory
866 // intact/accessible until the end of the marking phase (the
867 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000868 __ MaybeUnpoisonHeapReference(temp);
869 // temp = temp->GetMethodAt(method_offset);
870 __ movq(temp, Address(temp, method_offset));
871 // call temp->GetEntryPoint();
872 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
873 kX86_64WordSize).SizeValue()));
874}
875
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000876void CodeGeneratorX86_64::RecordSimplePatch() {
877 if (GetCompilerOptions().GetIncludePatchInformation()) {
878 simple_patches_.emplace_back();
879 __ Bind(&simple_patches_.back());
880 }
881}
882
883void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
884 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
885 __ Bind(&string_patches_.back().label);
886}
887
888Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
889 uint32_t element_offset) {
890 // Add a patch entry and return the label.
891 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
892 return &pc_relative_dex_cache_patches_.back().label;
893}
894
Vladimir Marko58155012015-08-19 12:49:41 +0000895void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
896 DCHECK(linker_patches->empty());
897 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000898 method_patches_.size() +
899 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000900 pc_relative_dex_cache_patches_.size() +
901 simple_patches_.size() +
902 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000903 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000904 // The label points to the end of the "movl" insn but the literal offset for method
905 // patch needs to point to the embedded constant which occupies the last 4 bytes.
906 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000907 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000908 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000909 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
910 info.target_method.dex_file,
911 info.target_method.dex_method_index));
912 }
913 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000914 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000915 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
916 info.target_method.dex_file,
917 info.target_method.dex_method_index));
918 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000919 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
920 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000921 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
922 &info.target_dex_file,
923 info.label.Position(),
924 info.element_offset));
925 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000926 for (const Label& label : simple_patches_) {
927 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
928 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
929 }
930 for (const StringPatchInfo<Label>& info : string_patches_) {
931 // These are always PC-relative, see GetSupportedLoadStringKind().
932 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
933 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
934 &info.dex_file,
935 info.label.Position(),
936 info.string_index));
937 }
Vladimir Marko58155012015-08-19 12:49:41 +0000938}
939
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100940void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100941 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942}
943
944void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100945 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100946}
947
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100948size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
949 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
950 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100951}
952
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100953size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
954 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
955 return kX86_64WordSize;
956}
957
958size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
959 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
960 return kX86_64WordSize;
961}
962
963size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
964 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
965 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100966}
967
Calin Juravle175dc732015-08-25 15:42:32 +0100968void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
969 HInstruction* instruction,
970 uint32_t dex_pc,
971 SlowPathCode* slow_path) {
972 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
973 instruction,
974 dex_pc,
975 slow_path);
976}
977
978void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100979 HInstruction* instruction,
980 uint32_t dex_pc,
981 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100982 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000983 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100984 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100985}
986
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000987static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000988// Use a fake return address register to mimic Quick.
989static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400990CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000991 const X86_64InstructionSetFeatures& isa_features,
992 const CompilerOptions& compiler_options,
993 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000994 : CodeGenerator(graph,
995 kNumberOfCpuRegisters,
996 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000997 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000998 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
999 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001000 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001001 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1002 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001003 compiler_options,
1004 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001005 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001007 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001008 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001009 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001010 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001011 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1012 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001013 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001014 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1015 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001016 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001017 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1018}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001019
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001020InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1021 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001022 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023 assembler_(codegen->GetAssembler()),
1024 codegen_(codegen) {}
1025
David Brazdil58282f42016-01-14 12:45:10 +00001026void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001027 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001028 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001030 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001031 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001032}
1033
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001034static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001035 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001036}
David Srbecky9d8606d2015-04-12 09:35:32 +01001037
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001039 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001040}
1041
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001043 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001044 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001045 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001046 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001047 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001048
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001049 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001050 __ testq(CpuRegister(RAX), Address(
1051 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001052 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001053 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001054
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 if (HasEmptyFrame()) {
1056 return;
1057 }
1058
Nicolas Geoffray98893962015-01-21 12:32:32 +00001059 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001060 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001061 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001062 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1064 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001065 }
1066 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001067
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001068 int adjust = GetFrameSize() - GetCoreSpillSize();
1069 __ subq(CpuRegister(RSP), Immediate(adjust));
1070 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001071 uint32_t xmm_spill_location = GetFpuSpillStart();
1072 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001073
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001074 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1075 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1077 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1078 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001079 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001080 }
1081
Mathieu Chartiere401d142015-04-22 13:56:20 -07001082 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001083 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084}
1085
1086void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001087 __ cfi().RememberState();
1088 if (!HasEmptyFrame()) {
1089 uint32_t xmm_spill_location = GetFpuSpillStart();
1090 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1091 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1092 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1093 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1094 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1095 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1096 }
1097 }
1098
1099 int adjust = GetFrameSize() - GetCoreSpillSize();
1100 __ addq(CpuRegister(RSP), Immediate(adjust));
1101 __ cfi().AdjustCFAOffset(-adjust);
1102
1103 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1104 Register reg = kCoreCalleeSaves[i];
1105 if (allocated_registers_.ContainsCoreRegister(reg)) {
1106 __ popq(CpuRegister(reg));
1107 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1108 __ cfi().Restore(DWARFReg(reg));
1109 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001110 }
1111 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001112 __ ret();
1113 __ cfi().RestoreState();
1114 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001115}
1116
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001117void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1118 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119}
1120
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001121Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1122 switch (load->GetType()) {
1123 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001124 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001126
1127 case Primitive::kPrimInt:
1128 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001130 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131
1132 case Primitive::kPrimBoolean:
1133 case Primitive::kPrimByte:
1134 case Primitive::kPrimChar:
1135 case Primitive::kPrimShort:
1136 case Primitive::kPrimVoid:
1137 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001138 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139 }
1140
1141 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001142 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001143}
1144
1145void CodeGeneratorX86_64::Move(Location destination, Location source) {
1146 if (source.Equals(destination)) {
1147 return;
1148 }
1149 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001150 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001152 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001153 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001154 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001156 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1157 } else if (source.IsConstant()) {
1158 HConstant* constant = source.GetConstant();
1159 if (constant->IsLongConstant()) {
1160 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1161 } else {
1162 Load32BitValue(dest, GetInt32ValueOf(constant));
1163 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001164 } else {
1165 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001166 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001167 }
1168 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001169 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001171 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001173 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1174 } else if (source.IsConstant()) {
1175 HConstant* constant = source.GetConstant();
1176 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1177 if (constant->IsFloatConstant()) {
1178 Load32BitValue(dest, static_cast<int32_t>(value));
1179 } else {
1180 Load64BitValue(dest, value);
1181 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001183 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 } else {
1185 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001186 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187 }
1188 } else if (destination.IsStackSlot()) {
1189 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 } else if (source.IsFpuRegister()) {
1193 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001195 } else if (source.IsConstant()) {
1196 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001197 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001198 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001200 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001201 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1202 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001203 }
1204 } else {
1205 DCHECK(destination.IsDoubleStackSlot());
1206 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001208 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 } else if (source.IsFpuRegister()) {
1210 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001211 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001212 } else if (source.IsConstant()) {
1213 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001214 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001215 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001216 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001217 } else {
1218 DCHECK(constant->IsLongConstant());
1219 value = constant->AsLongConstant()->GetValue();
1220 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001221 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222 } else {
1223 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001224 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1225 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001226 }
1227 }
1228}
1229
Calin Juravle175dc732015-08-25 15:42:32 +01001230void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1231 DCHECK(location.IsRegister());
1232 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1233}
1234
Calin Juravlee460d1d2015-09-29 04:52:17 +01001235void CodeGeneratorX86_64::MoveLocation(
1236 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1237 Move(dst, src);
1238}
1239
1240void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1241 if (location.IsRegister()) {
1242 locations->AddTemp(location);
1243 } else {
1244 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1245 }
1246}
1247
David Brazdilfc6a86a2015-06-26 10:33:45 +00001248void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001249 DCHECK(!successor->IsExitBlock());
1250
1251 HBasicBlock* block = got->GetBlock();
1252 HInstruction* previous = got->GetPrevious();
1253
1254 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001255 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001256 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1257 return;
1258 }
1259
1260 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1261 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1262 }
1263 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264 __ jmp(codegen_->GetLabelOf(successor));
1265 }
1266}
1267
David Brazdilfc6a86a2015-06-26 10:33:45 +00001268void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1269 got->SetLocations(nullptr);
1270}
1271
1272void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1273 HandleGoto(got, got->GetSuccessor());
1274}
1275
1276void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1277 try_boundary->SetLocations(nullptr);
1278}
1279
1280void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1281 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1282 if (!successor->IsExitBlock()) {
1283 HandleGoto(try_boundary, successor);
1284 }
1285}
1286
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1288 exit->SetLocations(nullptr);
1289}
1290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001291void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001292}
1293
Mark Mendell152408f2015-12-31 12:28:50 -05001294template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001295void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001296 LabelType* true_label,
1297 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001298 if (cond->IsFPConditionTrueIfNaN()) {
1299 __ j(kUnordered, true_label);
1300 } else if (cond->IsFPConditionFalseIfNaN()) {
1301 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001302 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001303 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001304}
1305
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001306void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001307 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001308
Mark Mendellc4701932015-04-10 13:18:51 -04001309 Location left = locations->InAt(0);
1310 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001311 Primitive::Type type = condition->InputAt(0)->GetType();
1312 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001313 case Primitive::kPrimBoolean:
1314 case Primitive::kPrimByte:
1315 case Primitive::kPrimChar:
1316 case Primitive::kPrimShort:
1317 case Primitive::kPrimInt:
1318 case Primitive::kPrimNot: {
1319 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1320 if (right.IsConstant()) {
1321 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1322 if (value == 0) {
1323 __ testl(left_reg, left_reg);
1324 } else {
1325 __ cmpl(left_reg, Immediate(value));
1326 }
1327 } else if (right.IsStackSlot()) {
1328 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1329 } else {
1330 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1331 }
1332 break;
1333 }
Mark Mendellc4701932015-04-10 13:18:51 -04001334 case Primitive::kPrimLong: {
1335 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1336 if (right.IsConstant()) {
1337 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001338 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001339 } else if (right.IsDoubleStackSlot()) {
1340 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1341 } else {
1342 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1343 }
Mark Mendellc4701932015-04-10 13:18:51 -04001344 break;
1345 }
1346 case Primitive::kPrimFloat: {
1347 if (right.IsFpuRegister()) {
1348 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1349 } else if (right.IsConstant()) {
1350 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1351 codegen_->LiteralFloatAddress(
1352 right.GetConstant()->AsFloatConstant()->GetValue()));
1353 } else {
1354 DCHECK(right.IsStackSlot());
1355 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1356 Address(CpuRegister(RSP), right.GetStackIndex()));
1357 }
Mark Mendellc4701932015-04-10 13:18:51 -04001358 break;
1359 }
1360 case Primitive::kPrimDouble: {
1361 if (right.IsFpuRegister()) {
1362 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1363 } else if (right.IsConstant()) {
1364 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1365 codegen_->LiteralDoubleAddress(
1366 right.GetConstant()->AsDoubleConstant()->GetValue()));
1367 } else {
1368 DCHECK(right.IsDoubleStackSlot());
1369 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1370 Address(CpuRegister(RSP), right.GetStackIndex()));
1371 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001372 break;
1373 }
1374 default:
1375 LOG(FATAL) << "Unexpected condition type " << type;
1376 }
1377}
1378
1379template<class LabelType>
1380void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1381 LabelType* true_target_in,
1382 LabelType* false_target_in) {
1383 // Generated branching requires both targets to be explicit. If either of the
1384 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1385 LabelType fallthrough_target;
1386 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1387 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1388
1389 // Generate the comparison to set the CC.
1390 GenerateCompareTest(condition);
1391
1392 // Now generate the correct jump(s).
1393 Primitive::Type type = condition->InputAt(0)->GetType();
1394 switch (type) {
1395 case Primitive::kPrimLong: {
1396 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1397 break;
1398 }
1399 case Primitive::kPrimFloat: {
1400 GenerateFPJumps(condition, true_target, false_target);
1401 break;
1402 }
1403 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001404 GenerateFPJumps(condition, true_target, false_target);
1405 break;
1406 }
1407 default:
1408 LOG(FATAL) << "Unexpected condition type " << type;
1409 }
1410
David Brazdil0debae72015-11-12 18:37:00 +00001411 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001412 __ jmp(false_target);
1413 }
David Brazdil0debae72015-11-12 18:37:00 +00001414
1415 if (fallthrough_target.IsLinked()) {
1416 __ Bind(&fallthrough_target);
1417 }
Mark Mendellc4701932015-04-10 13:18:51 -04001418}
1419
David Brazdil0debae72015-11-12 18:37:00 +00001420static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1421 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1422 // are set only strictly before `branch`. We can't use the eflags on long
1423 // conditions if they are materialized due to the complex branching.
1424 return cond->IsCondition() &&
1425 cond->GetNext() == branch &&
1426 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1427}
1428
Mark Mendell152408f2015-12-31 12:28:50 -05001429template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001430void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001431 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001432 LabelType* true_target,
1433 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001434 HInstruction* cond = instruction->InputAt(condition_input_index);
1435
1436 if (true_target == nullptr && false_target == nullptr) {
1437 // Nothing to do. The code always falls through.
1438 return;
1439 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001440 // Constant condition, statically compared against "true" (integer value 1).
1441 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001442 if (true_target != nullptr) {
1443 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001444 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001445 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001446 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001447 if (false_target != nullptr) {
1448 __ jmp(false_target);
1449 }
1450 }
1451 return;
1452 }
1453
1454 // The following code generates these patterns:
1455 // (1) true_target == nullptr && false_target != nullptr
1456 // - opposite condition true => branch to false_target
1457 // (2) true_target != nullptr && false_target == nullptr
1458 // - condition true => branch to true_target
1459 // (3) true_target != nullptr && false_target != nullptr
1460 // - condition true => branch to true_target
1461 // - branch to false_target
1462 if (IsBooleanValueOrMaterializedCondition(cond)) {
1463 if (AreEflagsSetFrom(cond, instruction)) {
1464 if (true_target == nullptr) {
1465 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1466 } else {
1467 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1468 }
1469 } else {
1470 // Materialized condition, compare against 0.
1471 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1472 if (lhs.IsRegister()) {
1473 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1474 } else {
1475 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1476 }
1477 if (true_target == nullptr) {
1478 __ j(kEqual, false_target);
1479 } else {
1480 __ j(kNotEqual, true_target);
1481 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001482 }
1483 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001484 // Condition has not been materialized, use its inputs as the
1485 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001486 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001487
David Brazdil0debae72015-11-12 18:37:00 +00001488 // If this is a long or FP comparison that has been folded into
1489 // the HCondition, generate the comparison directly.
1490 Primitive::Type type = condition->InputAt(0)->GetType();
1491 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1492 GenerateCompareTestAndBranch(condition, true_target, false_target);
1493 return;
1494 }
1495
1496 Location lhs = condition->GetLocations()->InAt(0);
1497 Location rhs = condition->GetLocations()->InAt(1);
1498 if (rhs.IsRegister()) {
1499 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1500 } else if (rhs.IsConstant()) {
1501 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001502 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001503 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001504 __ cmpl(lhs.AsRegister<CpuRegister>(),
1505 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1506 }
1507 if (true_target == nullptr) {
1508 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1509 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001510 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001511 }
Dave Allison20dfc792014-06-16 20:44:29 -07001512 }
David Brazdil0debae72015-11-12 18:37:00 +00001513
1514 // If neither branch falls through (case 3), the conditional branch to `true_target`
1515 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1516 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001518 }
1519}
1520
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001521void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1523 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524 locations->SetInAt(0, Location::Any());
1525 }
1526}
1527
1528void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001529 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1530 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1531 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1532 nullptr : codegen_->GetLabelOf(true_successor);
1533 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1534 nullptr : codegen_->GetLabelOf(false_successor);
1535 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001536}
1537
1538void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1539 LocationSummary* locations = new (GetGraph()->GetArena())
1540 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001541 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001542 locations->SetInAt(0, Location::Any());
1543 }
1544}
1545
1546void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001547 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001548 GenerateTestAndBranch<Label>(deoptimize,
1549 /* condition_input_index */ 0,
1550 slow_path->GetEntryLabel(),
1551 /* false_target */ nullptr);
1552}
1553
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001554static bool SelectCanUseCMOV(HSelect* select) {
1555 // There are no conditional move instructions for XMMs.
1556 if (Primitive::IsFloatingPointType(select->GetType())) {
1557 return false;
1558 }
1559
1560 // A FP condition doesn't generate the single CC that we need.
1561 HInstruction* condition = select->GetCondition();
1562 if (condition->IsCondition() &&
1563 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1564 return false;
1565 }
1566
1567 // We can generate a CMOV for this Select.
1568 return true;
1569}
1570
David Brazdil74eb1b22015-12-14 11:44:01 +00001571void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1573 if (Primitive::IsFloatingPointType(select->GetType())) {
1574 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001575 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001576 } else {
1577 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001578 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001579 if (select->InputAt(1)->IsConstant()) {
1580 locations->SetInAt(1, Location::RequiresRegister());
1581 } else {
1582 locations->SetInAt(1, Location::Any());
1583 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001584 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001585 locations->SetInAt(1, Location::Any());
1586 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001587 }
1588 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1589 locations->SetInAt(2, Location::RequiresRegister());
1590 }
1591 locations->SetOut(Location::SameAsFirstInput());
1592}
1593
1594void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1595 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001596 if (SelectCanUseCMOV(select)) {
1597 // If both the condition and the source types are integer, we can generate
1598 // a CMOV to implement Select.
1599 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001600 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001601 DCHECK(locations->InAt(0).Equals(locations->Out()));
1602
1603 HInstruction* select_condition = select->GetCondition();
1604 Condition cond = kNotEqual;
1605
1606 // Figure out how to test the 'condition'.
1607 if (select_condition->IsCondition()) {
1608 HCondition* condition = select_condition->AsCondition();
1609 if (!condition->IsEmittedAtUseSite()) {
1610 // This was a previously materialized condition.
1611 // Can we use the existing condition code?
1612 if (AreEflagsSetFrom(condition, select)) {
1613 // Materialization was the previous instruction. Condition codes are right.
1614 cond = X86_64IntegerCondition(condition->GetCondition());
1615 } else {
1616 // No, we have to recreate the condition code.
1617 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1618 __ testl(cond_reg, cond_reg);
1619 }
1620 } else {
1621 GenerateCompareTest(condition);
1622 cond = X86_64IntegerCondition(condition->GetCondition());
1623 }
1624 } else {
1625 // Must be a boolean condition, which needs to be compared to 0.
1626 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1627 __ testl(cond_reg, cond_reg);
1628 }
1629
1630 // If the condition is true, overwrite the output, which already contains false.
1631 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001632 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1633 if (value_true_loc.IsRegister()) {
1634 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1635 } else {
1636 __ cmov(cond,
1637 value_false,
1638 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1639 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001640 } else {
1641 NearLabel false_target;
1642 GenerateTestAndBranch<NearLabel>(select,
1643 /* condition_input_index */ 2,
1644 /* true_target */ nullptr,
1645 &false_target);
1646 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1647 __ Bind(&false_target);
1648 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001649}
1650
David Srbecky0cf44932015-12-09 14:09:59 +00001651void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1652 new (GetGraph()->GetArena()) LocationSummary(info);
1653}
1654
David Srbeckyd28f4a02016-03-14 17:14:24 +00001655void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1656 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001657}
1658
1659void CodeGeneratorX86_64::GenerateNop() {
1660 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001661}
1662
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001663void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1664 local->SetLocations(nullptr);
1665}
1666
1667void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1668 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1669}
1670
1671void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1672 local->SetLocations(nullptr);
1673}
1674
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001675void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001676 // Nothing to do, this is driven by the code generator.
1677}
1678
1679void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001680 LocationSummary* locations =
1681 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001682 switch (store->InputAt(1)->GetType()) {
1683 case Primitive::kPrimBoolean:
1684 case Primitive::kPrimByte:
1685 case Primitive::kPrimChar:
1686 case Primitive::kPrimShort:
1687 case Primitive::kPrimInt:
1688 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1691 break;
1692
1693 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001694 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001695 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1696 break;
1697
1698 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001699 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001700 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001701}
1702
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001703void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001704}
1705
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001707 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001708 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001709 // Handle the long/FP comparisons made in instruction simplification.
1710 switch (cond->InputAt(0)->GetType()) {
1711 case Primitive::kPrimLong:
1712 locations->SetInAt(0, Location::RequiresRegister());
1713 locations->SetInAt(1, Location::Any());
1714 break;
1715 case Primitive::kPrimFloat:
1716 case Primitive::kPrimDouble:
1717 locations->SetInAt(0, Location::RequiresFpuRegister());
1718 locations->SetInAt(1, Location::Any());
1719 break;
1720 default:
1721 locations->SetInAt(0, Location::RequiresRegister());
1722 locations->SetInAt(1, Location::Any());
1723 break;
1724 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001725 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001726 locations->SetOut(Location::RequiresRegister());
1727 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001728}
1729
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001731 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001732 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001733 }
Mark Mendellc4701932015-04-10 13:18:51 -04001734
1735 LocationSummary* locations = cond->GetLocations();
1736 Location lhs = locations->InAt(0);
1737 Location rhs = locations->InAt(1);
1738 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001739 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001740
1741 switch (cond->InputAt(0)->GetType()) {
1742 default:
1743 // Integer case.
1744
1745 // Clear output register: setcc only sets the low byte.
1746 __ xorl(reg, reg);
1747
1748 if (rhs.IsRegister()) {
1749 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1750 } else if (rhs.IsConstant()) {
1751 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001752 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001753 } else {
1754 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1755 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001757 return;
1758 case Primitive::kPrimLong:
1759 // Clear output register: setcc only sets the low byte.
1760 __ xorl(reg, reg);
1761
1762 if (rhs.IsRegister()) {
1763 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1764 } else if (rhs.IsConstant()) {
1765 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001766 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001767 } else {
1768 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1769 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001770 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001771 return;
1772 case Primitive::kPrimFloat: {
1773 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1774 if (rhs.IsConstant()) {
1775 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1776 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1777 } else if (rhs.IsStackSlot()) {
1778 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1779 } else {
1780 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1781 }
1782 GenerateFPJumps(cond, &true_label, &false_label);
1783 break;
1784 }
1785 case Primitive::kPrimDouble: {
1786 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1787 if (rhs.IsConstant()) {
1788 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1789 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1790 } else if (rhs.IsDoubleStackSlot()) {
1791 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1792 } else {
1793 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1794 }
1795 GenerateFPJumps(cond, &true_label, &false_label);
1796 break;
1797 }
1798 }
1799
1800 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001801 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001802
Roland Levillain4fa13f62015-07-06 18:11:54 +01001803 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001804 __ Bind(&false_label);
1805 __ xorl(reg, reg);
1806 __ jmp(&done_label);
1807
Roland Levillain4fa13f62015-07-06 18:11:54 +01001808 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001809 __ Bind(&true_label);
1810 __ movl(reg, Immediate(1));
1811 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001812}
1813
1814void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001816}
1817
1818void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001820}
1821
1822void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001824}
1825
1826void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001828}
1829
1830void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001832}
1833
1834void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001836}
1837
1838void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001840}
1841
1842void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001844}
1845
1846void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001848}
1849
1850void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001852}
1853
1854void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001856}
1857
1858void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001859 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001860}
1861
Aart Bike9f37602015-10-09 11:15:55 -07001862void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001863 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001864}
1865
1866void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001867 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001868}
1869
1870void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001871 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001872}
1873
1874void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001875 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001876}
1877
1878void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001879 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001880}
1881
1882void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001883 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001884}
1885
1886void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001887 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001888}
1889
1890void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001891 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001892}
1893
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001894void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001895 LocationSummary* locations =
1896 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001897 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001898 case Primitive::kPrimBoolean:
1899 case Primitive::kPrimByte:
1900 case Primitive::kPrimShort:
1901 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001902 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001903 case Primitive::kPrimLong: {
1904 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001905 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1907 break;
1908 }
1909 case Primitive::kPrimFloat:
1910 case Primitive::kPrimDouble: {
1911 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001912 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 locations->SetOut(Location::RequiresRegister());
1914 break;
1915 }
1916 default:
1917 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1918 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001919}
1920
1921void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001922 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 Location left = locations->InAt(0);
1925 Location right = locations->InAt(1);
1926
Mark Mendell0c9497d2015-08-21 09:30:05 -04001927 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001928 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001929 Condition less_cond = kLess;
1930
Calin Juravleddb7df22014-11-25 20:56:51 +00001931 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001932 case Primitive::kPrimBoolean:
1933 case Primitive::kPrimByte:
1934 case Primitive::kPrimShort:
1935 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001936 case Primitive::kPrimInt: {
1937 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1938 if (right.IsConstant()) {
1939 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1940 codegen_->Compare32BitValue(left_reg, value);
1941 } else if (right.IsStackSlot()) {
1942 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1943 } else {
1944 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1945 }
1946 break;
1947 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001949 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1950 if (right.IsConstant()) {
1951 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001952 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001953 } else if (right.IsDoubleStackSlot()) {
1954 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001955 } else {
1956 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1957 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001958 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001959 }
1960 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001961 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1962 if (right.IsConstant()) {
1963 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1964 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1965 } else if (right.IsStackSlot()) {
1966 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1967 } else {
1968 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1969 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001970 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001971 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001972 break;
1973 }
1974 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001975 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1976 if (right.IsConstant()) {
1977 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1978 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1979 } else if (right.IsDoubleStackSlot()) {
1980 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1981 } else {
1982 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1983 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001984 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001985 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001986 break;
1987 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001988 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001989 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001990 }
Aart Bika19616e2016-02-01 18:57:58 -08001991
Calin Juravleddb7df22014-11-25 20:56:51 +00001992 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001993 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001994 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001995
Calin Juravle91debbc2014-11-26 19:01:09 +00001996 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001997 __ movl(out, Immediate(1));
1998 __ jmp(&done);
1999
2000 __ Bind(&less);
2001 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002002
2003 __ Bind(&done);
2004}
2005
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002006void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002007 LocationSummary* locations =
2008 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002009 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002010}
2011
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002012void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002013 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014}
2015
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002016void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2017 LocationSummary* locations =
2018 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2019 locations->SetOut(Location::ConstantLocation(constant));
2020}
2021
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002022void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002023 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002024}
2025
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002026void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002027 LocationSummary* locations =
2028 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002029 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030}
2031
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002032void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002033 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002034}
2035
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002036void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2037 LocationSummary* locations =
2038 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2039 locations->SetOut(Location::ConstantLocation(constant));
2040}
2041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002042void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002043 // Will be generated at use site.
2044}
2045
2046void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2049 locations->SetOut(Location::ConstantLocation(constant));
2050}
2051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002052void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2053 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002054 // Will be generated at use site.
2055}
2056
Calin Juravle27df7582015-04-17 19:12:31 +01002057void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2058 memory_barrier->SetLocations(nullptr);
2059}
2060
2061void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002062 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002063}
2064
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002065void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2066 ret->SetLocations(nullptr);
2067}
2068
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002069void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002070 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002071}
2072
2073void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002074 LocationSummary* locations =
2075 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002076 switch (ret->InputAt(0)->GetType()) {
2077 case Primitive::kPrimBoolean:
2078 case Primitive::kPrimByte:
2079 case Primitive::kPrimChar:
2080 case Primitive::kPrimShort:
2081 case Primitive::kPrimInt:
2082 case Primitive::kPrimNot:
2083 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002084 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085 break;
2086
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002087 case Primitive::kPrimFloat:
2088 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002089 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002090 break;
2091
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002092 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002093 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002094 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002095}
2096
2097void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2098 if (kIsDebugBuild) {
2099 switch (ret->InputAt(0)->GetType()) {
2100 case Primitive::kPrimBoolean:
2101 case Primitive::kPrimByte:
2102 case Primitive::kPrimChar:
2103 case Primitive::kPrimShort:
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimNot:
2106 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002108 break;
2109
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002110 case Primitive::kPrimFloat:
2111 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002113 XMM0);
2114 break;
2115
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002116 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002117 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002118 }
2119 }
2120 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002121}
2122
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002123Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2124 switch (type) {
2125 case Primitive::kPrimBoolean:
2126 case Primitive::kPrimByte:
2127 case Primitive::kPrimChar:
2128 case Primitive::kPrimShort:
2129 case Primitive::kPrimInt:
2130 case Primitive::kPrimNot:
2131 case Primitive::kPrimLong:
2132 return Location::RegisterLocation(RAX);
2133
2134 case Primitive::kPrimVoid:
2135 return Location::NoLocation();
2136
2137 case Primitive::kPrimDouble:
2138 case Primitive::kPrimFloat:
2139 return Location::FpuRegisterLocation(XMM0);
2140 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002141
2142 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002143}
2144
2145Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2146 return Location::RegisterLocation(kMethodRegisterArgument);
2147}
2148
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002149Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150 switch (type) {
2151 case Primitive::kPrimBoolean:
2152 case Primitive::kPrimByte:
2153 case Primitive::kPrimChar:
2154 case Primitive::kPrimShort:
2155 case Primitive::kPrimInt:
2156 case Primitive::kPrimNot: {
2157 uint32_t index = gp_index_++;
2158 stack_index_++;
2159 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002160 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002161 } else {
2162 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2163 }
2164 }
2165
2166 case Primitive::kPrimLong: {
2167 uint32_t index = gp_index_;
2168 stack_index_ += 2;
2169 if (index < calling_convention.GetNumberOfRegisters()) {
2170 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002171 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002172 } else {
2173 gp_index_ += 2;
2174 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2175 }
2176 }
2177
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002178 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002179 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002180 stack_index_++;
2181 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002182 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002183 } else {
2184 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2185 }
2186 }
2187
2188 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002189 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002190 stack_index_ += 2;
2191 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002192 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002193 } else {
2194 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2195 }
2196 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002197
2198 case Primitive::kPrimVoid:
2199 LOG(FATAL) << "Unexpected parameter type " << type;
2200 break;
2201 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002202 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002203}
2204
Calin Juravle175dc732015-08-25 15:42:32 +01002205void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2206 // The trampoline uses the same calling convention as dex calling conventions,
2207 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2208 // the method_idx.
2209 HandleInvoke(invoke);
2210}
2211
2212void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2213 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2214}
2215
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002216void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002217 // Explicit clinit checks triggered by static invokes must have been pruned by
2218 // art::PrepareForRegisterAllocation.
2219 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002220
Mark Mendellfb8d2792015-03-31 22:16:59 -04002221 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002222 if (intrinsic.TryDispatch(invoke)) {
2223 return;
2224 }
2225
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002226 HandleInvoke(invoke);
2227}
2228
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002229static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2230 if (invoke->GetLocations()->Intrinsified()) {
2231 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2232 intrinsic.Dispatch(invoke);
2233 return true;
2234 }
2235 return false;
2236}
2237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002238void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002239 // Explicit clinit checks triggered by static invokes must have been pruned by
2240 // art::PrepareForRegisterAllocation.
2241 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002242
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002243 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2244 return;
2245 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002246
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002247 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002248 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002249 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002250 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002251}
2252
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002253void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002254 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002255 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002256}
2257
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002259 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002260 if (intrinsic.TryDispatch(invoke)) {
2261 return;
2262 }
2263
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 HandleInvoke(invoke);
2265}
2266
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002267void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002268 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2269 return;
2270 }
2271
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002272 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002273 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002274 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002275}
2276
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2278 HandleInvoke(invoke);
2279 // Add the hidden argument.
2280 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2281}
2282
2283void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2284 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002285 LocationSummary* locations = invoke->GetLocations();
2286 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2287 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002288 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2289 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002290 Location receiver = locations->InAt(0);
2291 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2292
Roland Levillain0d5a2812015-11-13 10:07:31 +00002293 // Set the hidden argument. This is safe to do this here, as RAX
2294 // won't be modified thereafter, before the `call` instruction.
2295 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002296 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002298 if (receiver.IsStackSlot()) {
2299 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002300 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002301 __ movl(temp, Address(temp, class_offset));
2302 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002303 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002305 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002306 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002307 // Instead of simply (possibly) unpoisoning `temp` here, we should
2308 // emit a read barrier for the previous class reference load.
2309 // However this is not required in practice, as this is an
2310 // intermediate/temporary reference and because the current
2311 // concurrent copying collector keeps the from-space memory
2312 // intact/accessible until the end of the marking phase (the
2313 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002314 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002315 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002316 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002317 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002318 __ call(Address(temp,
2319 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002320
2321 DCHECK(!codegen_->IsLeafMethod());
2322 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2323}
2324
Roland Levillain88cb1752014-10-20 16:36:47 +01002325void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2326 LocationSummary* locations =
2327 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2328 switch (neg->GetResultType()) {
2329 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002330 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002331 locations->SetInAt(0, Location::RequiresRegister());
2332 locations->SetOut(Location::SameAsFirstInput());
2333 break;
2334
Roland Levillain88cb1752014-10-20 16:36:47 +01002335 case Primitive::kPrimFloat:
2336 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002337 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002338 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002339 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002340 break;
2341
2342 default:
2343 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2344 }
2345}
2346
2347void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2348 LocationSummary* locations = neg->GetLocations();
2349 Location out = locations->Out();
2350 Location in = locations->InAt(0);
2351 switch (neg->GetResultType()) {
2352 case Primitive::kPrimInt:
2353 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002354 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002356 break;
2357
2358 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002359 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002362 break;
2363
Roland Levillain5368c212014-11-27 15:03:41 +00002364 case Primitive::kPrimFloat: {
2365 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002366 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002367 // Implement float negation with an exclusive or with value
2368 // 0x80000000 (mask for bit 31, representing the sign of a
2369 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002370 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002372 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002373 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002374
Roland Levillain5368c212014-11-27 15:03:41 +00002375 case Primitive::kPrimDouble: {
2376 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002377 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002378 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002379 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002380 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002381 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002383 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002384 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002385
2386 default:
2387 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2388 }
2389}
2390
Roland Levillaindff1f282014-11-05 14:15:05 +00002391void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2392 LocationSummary* locations =
2393 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2394 Primitive::Type result_type = conversion->GetResultType();
2395 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002396 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002397
David Brazdilb2bd1c52015-03-25 11:17:37 +00002398 // The Java language does not allow treating boolean as an integral type but
2399 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002400
Roland Levillaindff1f282014-11-05 14:15:05 +00002401 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002402 case Primitive::kPrimByte:
2403 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002404 case Primitive::kPrimLong:
2405 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002406 case Primitive::kPrimBoolean:
2407 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002408 case Primitive::kPrimShort:
2409 case Primitive::kPrimInt:
2410 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002411 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002412 locations->SetInAt(0, Location::Any());
2413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2414 break;
2415
2416 default:
2417 LOG(FATAL) << "Unexpected type conversion from " << input_type
2418 << " to " << result_type;
2419 }
2420 break;
2421
Roland Levillain01a8d712014-11-14 16:27:39 +00002422 case Primitive::kPrimShort:
2423 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002424 case Primitive::kPrimLong:
2425 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002426 case Primitive::kPrimBoolean:
2427 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 case Primitive::kPrimByte:
2429 case Primitive::kPrimInt:
2430 case Primitive::kPrimChar:
2431 // Processing a Dex `int-to-short' instruction.
2432 locations->SetInAt(0, Location::Any());
2433 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2434 break;
2435
2436 default:
2437 LOG(FATAL) << "Unexpected type conversion from " << input_type
2438 << " to " << result_type;
2439 }
2440 break;
2441
Roland Levillain946e1432014-11-11 17:35:19 +00002442 case Primitive::kPrimInt:
2443 switch (input_type) {
2444 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002445 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002446 locations->SetInAt(0, Location::Any());
2447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2448 break;
2449
2450 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002451 // Processing a Dex `float-to-int' instruction.
2452 locations->SetInAt(0, Location::RequiresFpuRegister());
2453 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002454 break;
2455
Roland Levillain946e1432014-11-11 17:35:19 +00002456 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002457 // Processing a Dex `double-to-int' instruction.
2458 locations->SetInAt(0, Location::RequiresFpuRegister());
2459 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002460 break;
2461
2462 default:
2463 LOG(FATAL) << "Unexpected type conversion from " << input_type
2464 << " to " << result_type;
2465 }
2466 break;
2467
Roland Levillaindff1f282014-11-05 14:15:05 +00002468 case Primitive::kPrimLong:
2469 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002470 case Primitive::kPrimBoolean:
2471 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002472 case Primitive::kPrimByte:
2473 case Primitive::kPrimShort:
2474 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002475 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002476 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002477 // TODO: We would benefit from a (to-be-implemented)
2478 // Location::RegisterOrStackSlot requirement for this input.
2479 locations->SetInAt(0, Location::RequiresRegister());
2480 locations->SetOut(Location::RequiresRegister());
2481 break;
2482
2483 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002484 // Processing a Dex `float-to-long' instruction.
2485 locations->SetInAt(0, Location::RequiresFpuRegister());
2486 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002487 break;
2488
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002490 // Processing a Dex `double-to-long' instruction.
2491 locations->SetInAt(0, Location::RequiresFpuRegister());
2492 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 break;
2494
2495 default:
2496 LOG(FATAL) << "Unexpected type conversion from " << input_type
2497 << " to " << result_type;
2498 }
2499 break;
2500
Roland Levillain981e4542014-11-14 11:47:14 +00002501 case Primitive::kPrimChar:
2502 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002503 case Primitive::kPrimLong:
2504 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002505 case Primitive::kPrimBoolean:
2506 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002507 case Primitive::kPrimByte:
2508 case Primitive::kPrimShort:
2509 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002510 // Processing a Dex `int-to-char' instruction.
2511 locations->SetInAt(0, Location::Any());
2512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519 break;
2520
Roland Levillaindff1f282014-11-05 14:15:05 +00002521 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002522 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002523 case Primitive::kPrimBoolean:
2524 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002525 case Primitive::kPrimByte:
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimInt:
2528 case Primitive::kPrimChar:
2529 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002530 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002531 locations->SetOut(Location::RequiresFpuRegister());
2532 break;
2533
2534 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002535 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002536 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002537 locations->SetOut(Location::RequiresFpuRegister());
2538 break;
2539
Roland Levillaincff13742014-11-17 14:32:17 +00002540 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002541 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002542 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002543 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002544 break;
2545
2546 default:
2547 LOG(FATAL) << "Unexpected type conversion from " << input_type
2548 << " to " << result_type;
2549 };
2550 break;
2551
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002553 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002554 case Primitive::kPrimBoolean:
2555 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002556 case Primitive::kPrimByte:
2557 case Primitive::kPrimShort:
2558 case Primitive::kPrimInt:
2559 case Primitive::kPrimChar:
2560 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002561 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002562 locations->SetOut(Location::RequiresFpuRegister());
2563 break;
2564
2565 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002566 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002567 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002568 locations->SetOut(Location::RequiresFpuRegister());
2569 break;
2570
Roland Levillaincff13742014-11-17 14:32:17 +00002571 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002572 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002573 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002574 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002575 break;
2576
2577 default:
2578 LOG(FATAL) << "Unexpected type conversion from " << input_type
2579 << " to " << result_type;
2580 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 }
2587}
2588
2589void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2590 LocationSummary* locations = conversion->GetLocations();
2591 Location out = locations->Out();
2592 Location in = locations->InAt(0);
2593 Primitive::Type result_type = conversion->GetResultType();
2594 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002595 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002596 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002597 case Primitive::kPrimByte:
2598 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002599 case Primitive::kPrimLong:
2600 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002601 case Primitive::kPrimBoolean:
2602 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002603 case Primitive::kPrimShort:
2604 case Primitive::kPrimInt:
2605 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002606 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002607 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002608 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002609 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002611 Address(CpuRegister(RSP), in.GetStackIndex()));
2612 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002614 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002615 }
2616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected type conversion from " << input_type
2620 << " to " << result_type;
2621 }
2622 break;
2623
Roland Levillain01a8d712014-11-14 16:27:39 +00002624 case Primitive::kPrimShort:
2625 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002626 case Primitive::kPrimLong:
2627 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002628 case Primitive::kPrimBoolean:
2629 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002630 case Primitive::kPrimByte:
2631 case Primitive::kPrimInt:
2632 case Primitive::kPrimChar:
2633 // Processing a Dex `int-to-short' instruction.
2634 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002636 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002638 Address(CpuRegister(RSP), in.GetStackIndex()));
2639 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002641 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002642 }
2643 break;
2644
2645 default:
2646 LOG(FATAL) << "Unexpected type conversion from " << input_type
2647 << " to " << result_type;
2648 }
2649 break;
2650
Roland Levillain946e1432014-11-11 17:35:19 +00002651 case Primitive::kPrimInt:
2652 switch (input_type) {
2653 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002654 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002655 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002656 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002657 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002659 Address(CpuRegister(RSP), in.GetStackIndex()));
2660 } else {
2661 DCHECK(in.IsConstant());
2662 DCHECK(in.GetConstant()->IsLongConstant());
2663 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002665 }
2666 break;
2667
Roland Levillain3f8f9362014-12-02 17:45:01 +00002668 case Primitive::kPrimFloat: {
2669 // Processing a Dex `float-to-int' instruction.
2670 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2671 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002672 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002673
2674 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002675 // if input >= (float)INT_MAX goto done
2676 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002677 __ j(kAboveEqual, &done);
2678 // if input == NaN goto nan
2679 __ j(kUnordered, &nan);
2680 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002681 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002682 __ jmp(&done);
2683 __ Bind(&nan);
2684 // output = 0
2685 __ xorl(output, output);
2686 __ Bind(&done);
2687 break;
2688 }
2689
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002690 case Primitive::kPrimDouble: {
2691 // Processing a Dex `double-to-int' instruction.
2692 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2693 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002694 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002695
2696 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002697 // if input >= (double)INT_MAX goto done
2698 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002699 __ j(kAboveEqual, &done);
2700 // if input == NaN goto nan
2701 __ j(kUnordered, &nan);
2702 // output = double-to-int-truncate(input)
2703 __ cvttsd2si(output, input);
2704 __ jmp(&done);
2705 __ Bind(&nan);
2706 // output = 0
2707 __ xorl(output, output);
2708 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002709 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002710 }
Roland Levillain946e1432014-11-11 17:35:19 +00002711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716 break;
2717
Roland Levillaindff1f282014-11-05 14:15:05 +00002718 case Primitive::kPrimLong:
2719 switch (input_type) {
2720 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002721 case Primitive::kPrimBoolean:
2722 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002723 case Primitive::kPrimByte:
2724 case Primitive::kPrimShort:
2725 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002726 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002727 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002728 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002730 break;
2731
Roland Levillain624279f2014-12-04 11:54:28 +00002732 case Primitive::kPrimFloat: {
2733 // Processing a Dex `float-to-long' instruction.
2734 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2735 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002736 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002737
Mark Mendell92e83bf2015-05-07 11:25:03 -04002738 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002739 // if input >= (float)LONG_MAX goto done
2740 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002741 __ j(kAboveEqual, &done);
2742 // if input == NaN goto nan
2743 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002744 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002745 __ cvttss2si(output, input, true);
2746 __ jmp(&done);
2747 __ Bind(&nan);
2748 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002749 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002750 __ Bind(&done);
2751 break;
2752 }
2753
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002754 case Primitive::kPrimDouble: {
2755 // Processing a Dex `double-to-long' instruction.
2756 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2757 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002758 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002759
Mark Mendell92e83bf2015-05-07 11:25:03 -04002760 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002761 // if input >= (double)LONG_MAX goto done
2762 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002763 __ j(kAboveEqual, &done);
2764 // if input == NaN goto nan
2765 __ j(kUnordered, &nan);
2766 // output = double-to-long-truncate(input)
2767 __ cvttsd2si(output, input, true);
2768 __ jmp(&done);
2769 __ Bind(&nan);
2770 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002771 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002772 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002773 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002774 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002775
2776 default:
2777 LOG(FATAL) << "Unexpected type conversion from " << input_type
2778 << " to " << result_type;
2779 }
2780 break;
2781
Roland Levillain981e4542014-11-14 11:47:14 +00002782 case Primitive::kPrimChar:
2783 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002784 case Primitive::kPrimLong:
2785 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002786 case Primitive::kPrimBoolean:
2787 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002788 case Primitive::kPrimByte:
2789 case Primitive::kPrimShort:
2790 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002791 // Processing a Dex `int-to-char' instruction.
2792 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002794 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002795 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002796 Address(CpuRegister(RSP), in.GetStackIndex()));
2797 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002798 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002799 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002800 }
2801 break;
2802
2803 default:
2804 LOG(FATAL) << "Unexpected type conversion from " << input_type
2805 << " to " << result_type;
2806 }
2807 break;
2808
Roland Levillaindff1f282014-11-05 14:15:05 +00002809 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002810 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002811 case Primitive::kPrimBoolean:
2812 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002813 case Primitive::kPrimByte:
2814 case Primitive::kPrimShort:
2815 case Primitive::kPrimInt:
2816 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002817 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002818 if (in.IsRegister()) {
2819 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2820 } else if (in.IsConstant()) {
2821 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2822 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002823 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002824 } else {
2825 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2826 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2827 }
Roland Levillaincff13742014-11-17 14:32:17 +00002828 break;
2829
2830 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002831 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002832 if (in.IsRegister()) {
2833 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2834 } else if (in.IsConstant()) {
2835 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2836 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002837 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002838 } else {
2839 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2840 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2841 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002842 break;
2843
Roland Levillaincff13742014-11-17 14:32:17 +00002844 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002845 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002846 if (in.IsFpuRegister()) {
2847 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2848 } else if (in.IsConstant()) {
2849 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2850 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002851 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002852 } else {
2853 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2854 Address(CpuRegister(RSP), in.GetStackIndex()));
2855 }
Roland Levillaincff13742014-11-17 14:32:17 +00002856 break;
2857
2858 default:
2859 LOG(FATAL) << "Unexpected type conversion from " << input_type
2860 << " to " << result_type;
2861 };
2862 break;
2863
Roland Levillaindff1f282014-11-05 14:15:05 +00002864 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002865 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002866 case Primitive::kPrimBoolean:
2867 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002868 case Primitive::kPrimByte:
2869 case Primitive::kPrimShort:
2870 case Primitive::kPrimInt:
2871 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002872 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002873 if (in.IsRegister()) {
2874 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2875 } else if (in.IsConstant()) {
2876 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2877 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002878 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002879 } else {
2880 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2881 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2882 }
Roland Levillaincff13742014-11-17 14:32:17 +00002883 break;
2884
2885 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002886 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002887 if (in.IsRegister()) {
2888 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2889 } else if (in.IsConstant()) {
2890 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2891 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002892 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002893 } else {
2894 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2895 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2896 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002897 break;
2898
Roland Levillaincff13742014-11-17 14:32:17 +00002899 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002900 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002901 if (in.IsFpuRegister()) {
2902 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2903 } else if (in.IsConstant()) {
2904 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2905 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002906 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002907 } else {
2908 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2909 Address(CpuRegister(RSP), in.GetStackIndex()));
2910 }
Roland Levillaincff13742014-11-17 14:32:17 +00002911 break;
2912
2913 default:
2914 LOG(FATAL) << "Unexpected type conversion from " << input_type
2915 << " to " << result_type;
2916 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002917 break;
2918
2919 default:
2920 LOG(FATAL) << "Unexpected type conversion from " << input_type
2921 << " to " << result_type;
2922 }
2923}
2924
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002925void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002926 LocationSummary* locations =
2927 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002928 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002929 case Primitive::kPrimInt: {
2930 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002931 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002933 break;
2934 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002936 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002937 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002938 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002939 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002941 break;
2942 }
2943
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002944 case Primitive::kPrimDouble:
2945 case Primitive::kPrimFloat: {
2946 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002947 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002949 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002951
2952 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002953 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002954 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002955}
2956
2957void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2958 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002959 Location first = locations->InAt(0);
2960 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002961 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002962
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002963 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002964 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002966 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2967 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002968 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2969 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002970 } else {
2971 __ leal(out.AsRegister<CpuRegister>(), Address(
2972 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2973 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002975 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2976 __ addl(out.AsRegister<CpuRegister>(),
2977 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2978 } else {
2979 __ leal(out.AsRegister<CpuRegister>(), Address(
2980 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2981 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002982 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002983 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002984 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002985 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002986 break;
2987 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002989 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002990 if (second.IsRegister()) {
2991 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2992 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002993 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2994 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002995 } else {
2996 __ leaq(out.AsRegister<CpuRegister>(), Address(
2997 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2998 }
2999 } else {
3000 DCHECK(second.IsConstant());
3001 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3002 int32_t int32_value = Low32Bits(value);
3003 DCHECK_EQ(int32_value, value);
3004 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3005 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3006 } else {
3007 __ leaq(out.AsRegister<CpuRegister>(), Address(
3008 first.AsRegister<CpuRegister>(), int32_value));
3009 }
3010 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011 break;
3012 }
3013
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003014 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003015 if (second.IsFpuRegister()) {
3016 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3017 } else if (second.IsConstant()) {
3018 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003019 codegen_->LiteralFloatAddress(
3020 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003021 } else {
3022 DCHECK(second.IsStackSlot());
3023 __ addss(first.AsFpuRegister<XmmRegister>(),
3024 Address(CpuRegister(RSP), second.GetStackIndex()));
3025 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003026 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 }
3028
3029 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003030 if (second.IsFpuRegister()) {
3031 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3032 } else if (second.IsConstant()) {
3033 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003034 codegen_->LiteralDoubleAddress(
3035 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003036 } else {
3037 DCHECK(second.IsDoubleStackSlot());
3038 __ addsd(first.AsFpuRegister<XmmRegister>(),
3039 Address(CpuRegister(RSP), second.GetStackIndex()));
3040 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003041 break;
3042 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003043
3044 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003046 }
3047}
3048
3049void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003050 LocationSummary* locations =
3051 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003052 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003053 case Primitive::kPrimInt: {
3054 locations->SetInAt(0, Location::RequiresRegister());
3055 locations->SetInAt(1, Location::Any());
3056 locations->SetOut(Location::SameAsFirstInput());
3057 break;
3058 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003060 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003061 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003062 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003063 break;
3064 }
Calin Juravle11351682014-10-23 15:38:15 +01003065 case Primitive::kPrimFloat:
3066 case Primitive::kPrimDouble: {
3067 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003068 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003069 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070 break;
Calin Juravle11351682014-10-23 15:38:15 +01003071 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003072 default:
Calin Juravle11351682014-10-23 15:38:15 +01003073 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003075}
3076
3077void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3078 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003079 Location first = locations->InAt(0);
3080 Location second = locations->InAt(1);
3081 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003082 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003083 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003084 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003086 } else if (second.IsConstant()) {
3087 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003091 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003092 break;
3093 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003094 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003095 if (second.IsConstant()) {
3096 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3097 DCHECK(IsInt<32>(value));
3098 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3099 } else {
3100 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3101 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003102 break;
3103 }
3104
Calin Juravle11351682014-10-23 15:38:15 +01003105 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003106 if (second.IsFpuRegister()) {
3107 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3108 } else if (second.IsConstant()) {
3109 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003110 codegen_->LiteralFloatAddress(
3111 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003112 } else {
3113 DCHECK(second.IsStackSlot());
3114 __ subss(first.AsFpuRegister<XmmRegister>(),
3115 Address(CpuRegister(RSP), second.GetStackIndex()));
3116 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003117 break;
Calin Juravle11351682014-10-23 15:38:15 +01003118 }
3119
3120 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003121 if (second.IsFpuRegister()) {
3122 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3123 } else if (second.IsConstant()) {
3124 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003125 codegen_->LiteralDoubleAddress(
3126 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003127 } else {
3128 DCHECK(second.IsDoubleStackSlot());
3129 __ subsd(first.AsFpuRegister<XmmRegister>(),
3130 Address(CpuRegister(RSP), second.GetStackIndex()));
3131 }
Calin Juravle11351682014-10-23 15:38:15 +01003132 break;
3133 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003134
3135 default:
Calin Juravle11351682014-10-23 15:38:15 +01003136 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003137 }
3138}
3139
Calin Juravle34bacdf2014-10-07 20:23:36 +01003140void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3141 LocationSummary* locations =
3142 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3143 switch (mul->GetResultType()) {
3144 case Primitive::kPrimInt: {
3145 locations->SetInAt(0, Location::RequiresRegister());
3146 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003147 if (mul->InputAt(1)->IsIntConstant()) {
3148 // Can use 3 operand multiply.
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3150 } else {
3151 locations->SetOut(Location::SameAsFirstInput());
3152 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153 break;
3154 }
3155 case Primitive::kPrimLong: {
3156 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003157 locations->SetInAt(1, Location::Any());
3158 if (mul->InputAt(1)->IsLongConstant() &&
3159 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003160 // Can use 3 operand multiply.
3161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3162 } else {
3163 locations->SetOut(Location::SameAsFirstInput());
3164 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 break;
3166 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003167 case Primitive::kPrimFloat:
3168 case Primitive::kPrimDouble: {
3169 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003170 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003171 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174
3175 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003176 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003177 }
3178}
3179
3180void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3181 LocationSummary* locations = mul->GetLocations();
3182 Location first = locations->InAt(0);
3183 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003184 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003185 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003186 case Primitive::kPrimInt:
3187 // The constant may have ended up in a register, so test explicitly to avoid
3188 // problems where the output may not be the same as the first operand.
3189 if (mul->InputAt(1)->IsIntConstant()) {
3190 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3191 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3192 } else if (second.IsRegister()) {
3193 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003196 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003198 __ imull(first.AsRegister<CpuRegister>(),
3199 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 }
3201 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003203 // The constant may have ended up in a register, so test explicitly to avoid
3204 // problems where the output may not be the same as the first operand.
3205 if (mul->InputAt(1)->IsLongConstant()) {
3206 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3207 if (IsInt<32>(value)) {
3208 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3209 Immediate(static_cast<int32_t>(value)));
3210 } else {
3211 // Have to use the constant area.
3212 DCHECK(first.Equals(out));
3213 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3214 }
3215 } else if (second.IsRegister()) {
3216 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003217 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003218 } else {
3219 DCHECK(second.IsDoubleStackSlot());
3220 DCHECK(first.Equals(out));
3221 __ imulq(first.AsRegister<CpuRegister>(),
3222 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003223 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224 break;
3225 }
3226
Calin Juravleb5bfa962014-10-21 18:02:24 +01003227 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003228 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003229 if (second.IsFpuRegister()) {
3230 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3231 } else if (second.IsConstant()) {
3232 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003233 codegen_->LiteralFloatAddress(
3234 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003235 } else {
3236 DCHECK(second.IsStackSlot());
3237 __ mulss(first.AsFpuRegister<XmmRegister>(),
3238 Address(CpuRegister(RSP), second.GetStackIndex()));
3239 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003240 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003241 }
3242
3243 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003244 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003245 if (second.IsFpuRegister()) {
3246 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3247 } else if (second.IsConstant()) {
3248 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003249 codegen_->LiteralDoubleAddress(
3250 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003251 } else {
3252 DCHECK(second.IsDoubleStackSlot());
3253 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3254 Address(CpuRegister(RSP), second.GetStackIndex()));
3255 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003256 break;
3257 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003258
3259 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003260 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003261 }
3262}
3263
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003264void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3265 uint32_t stack_adjustment, bool is_float) {
3266 if (source.IsStackSlot()) {
3267 DCHECK(is_float);
3268 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3269 } else if (source.IsDoubleStackSlot()) {
3270 DCHECK(!is_float);
3271 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3272 } else {
3273 // Write the value to the temporary location on the stack and load to FP stack.
3274 if (is_float) {
3275 Location stack_temp = Location::StackSlot(temp_offset);
3276 codegen_->Move(stack_temp, source);
3277 __ flds(Address(CpuRegister(RSP), temp_offset));
3278 } else {
3279 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3280 codegen_->Move(stack_temp, source);
3281 __ fldl(Address(CpuRegister(RSP), temp_offset));
3282 }
3283 }
3284}
3285
3286void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3287 Primitive::Type type = rem->GetResultType();
3288 bool is_float = type == Primitive::kPrimFloat;
3289 size_t elem_size = Primitive::ComponentSize(type);
3290 LocationSummary* locations = rem->GetLocations();
3291 Location first = locations->InAt(0);
3292 Location second = locations->InAt(1);
3293 Location out = locations->Out();
3294
3295 // Create stack space for 2 elements.
3296 // TODO: enhance register allocator to ask for stack temporaries.
3297 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3298
3299 // Load the values to the FP stack in reverse order, using temporaries if needed.
3300 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3301 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3302
3303 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003304 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003305 __ Bind(&retry);
3306 __ fprem();
3307
3308 // Move FP status to AX.
3309 __ fstsw();
3310
3311 // And see if the argument reduction is complete. This is signaled by the
3312 // C2 FPU flag bit set to 0.
3313 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3314 __ j(kNotEqual, &retry);
3315
3316 // We have settled on the final value. Retrieve it into an XMM register.
3317 // Store FP top of stack to real stack.
3318 if (is_float) {
3319 __ fsts(Address(CpuRegister(RSP), 0));
3320 } else {
3321 __ fstl(Address(CpuRegister(RSP), 0));
3322 }
3323
3324 // Pop the 2 items from the FP stack.
3325 __ fucompp();
3326
3327 // Load the value from the stack into an XMM register.
3328 DCHECK(out.IsFpuRegister()) << out;
3329 if (is_float) {
3330 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3331 } else {
3332 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3333 }
3334
3335 // And remove the temporary stack space we allocated.
3336 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3337}
3338
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003339void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3340 DCHECK(instruction->IsDiv() || instruction->IsRem());
3341
3342 LocationSummary* locations = instruction->GetLocations();
3343 Location second = locations->InAt(1);
3344 DCHECK(second.IsConstant());
3345
3346 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3347 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003348 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349
3350 DCHECK(imm == 1 || imm == -1);
3351
3352 switch (instruction->GetResultType()) {
3353 case Primitive::kPrimInt: {
3354 if (instruction->IsRem()) {
3355 __ xorl(output_register, output_register);
3356 } else {
3357 __ movl(output_register, input_register);
3358 if (imm == -1) {
3359 __ negl(output_register);
3360 }
3361 }
3362 break;
3363 }
3364
3365 case Primitive::kPrimLong: {
3366 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003367 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003368 } else {
3369 __ movq(output_register, input_register);
3370 if (imm == -1) {
3371 __ negq(output_register);
3372 }
3373 }
3374 break;
3375 }
3376
3377 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003378 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003379 }
3380}
3381
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003382void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003383 LocationSummary* locations = instruction->GetLocations();
3384 Location second = locations->InAt(1);
3385
3386 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3387 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3388
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003389 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003390 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3391 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003392
3393 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3394
3395 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003396 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003397 __ testl(numerator, numerator);
3398 __ cmov(kGreaterEqual, tmp, numerator);
3399 int shift = CTZ(imm);
3400 __ sarl(tmp, Immediate(shift));
3401
3402 if (imm < 0) {
3403 __ negl(tmp);
3404 }
3405
3406 __ movl(output_register, tmp);
3407 } else {
3408 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3409 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3410
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003411 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003412 __ addq(rdx, numerator);
3413 __ testq(numerator, numerator);
3414 __ cmov(kGreaterEqual, rdx, numerator);
3415 int shift = CTZ(imm);
3416 __ sarq(rdx, Immediate(shift));
3417
3418 if (imm < 0) {
3419 __ negq(rdx);
3420 }
3421
3422 __ movq(output_register, rdx);
3423 }
3424}
3425
3426void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3427 DCHECK(instruction->IsDiv() || instruction->IsRem());
3428
3429 LocationSummary* locations = instruction->GetLocations();
3430 Location second = locations->InAt(1);
3431
3432 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3433 : locations->GetTemp(0).AsRegister<CpuRegister>();
3434 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3435 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3436 : locations->Out().AsRegister<CpuRegister>();
3437 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3438
3439 DCHECK_EQ(RAX, eax.AsRegister());
3440 DCHECK_EQ(RDX, edx.AsRegister());
3441 if (instruction->IsDiv()) {
3442 DCHECK_EQ(RAX, out.AsRegister());
3443 } else {
3444 DCHECK_EQ(RDX, out.AsRegister());
3445 }
3446
3447 int64_t magic;
3448 int shift;
3449
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003450 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 if (instruction->GetResultType() == Primitive::kPrimInt) {
3452 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3453
3454 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3455
3456 __ movl(numerator, eax);
3457
Mark Mendell0c9497d2015-08-21 09:30:05 -04003458 NearLabel no_div;
3459 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003460 __ testl(eax, eax);
3461 __ j(kNotEqual, &no_div);
3462
3463 __ xorl(out, out);
3464 __ jmp(&end);
3465
3466 __ Bind(&no_div);
3467
3468 __ movl(eax, Immediate(magic));
3469 __ imull(numerator);
3470
3471 if (imm > 0 && magic < 0) {
3472 __ addl(edx, numerator);
3473 } else if (imm < 0 && magic > 0) {
3474 __ subl(edx, numerator);
3475 }
3476
3477 if (shift != 0) {
3478 __ sarl(edx, Immediate(shift));
3479 }
3480
3481 __ movl(eax, edx);
3482 __ shrl(edx, Immediate(31));
3483 __ addl(edx, eax);
3484
3485 if (instruction->IsRem()) {
3486 __ movl(eax, numerator);
3487 __ imull(edx, Immediate(imm));
3488 __ subl(eax, edx);
3489 __ movl(edx, eax);
3490 } else {
3491 __ movl(eax, edx);
3492 }
3493 __ Bind(&end);
3494 } else {
3495 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3496
3497 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3498
3499 CpuRegister rax = eax;
3500 CpuRegister rdx = edx;
3501
3502 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3503
3504 // Save the numerator.
3505 __ movq(numerator, rax);
3506
3507 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003508 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509
3510 // RDX:RAX = magic * numerator
3511 __ imulq(numerator);
3512
3513 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003514 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003515 __ addq(rdx, numerator);
3516 } else if (imm < 0 && magic > 0) {
3517 // RDX -= numerator
3518 __ subq(rdx, numerator);
3519 }
3520
3521 // Shift if needed.
3522 if (shift != 0) {
3523 __ sarq(rdx, Immediate(shift));
3524 }
3525
3526 // RDX += 1 if RDX < 0
3527 __ movq(rax, rdx);
3528 __ shrq(rdx, Immediate(63));
3529 __ addq(rdx, rax);
3530
3531 if (instruction->IsRem()) {
3532 __ movq(rax, numerator);
3533
3534 if (IsInt<32>(imm)) {
3535 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3536 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003537 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 }
3539
3540 __ subq(rax, rdx);
3541 __ movq(rdx, rax);
3542 } else {
3543 __ movq(rax, rdx);
3544 }
3545 }
3546}
3547
Calin Juravlebacfec32014-11-14 15:54:36 +00003548void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3549 DCHECK(instruction->IsDiv() || instruction->IsRem());
3550 Primitive::Type type = instruction->GetResultType();
3551 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3552
3553 bool is_div = instruction->IsDiv();
3554 LocationSummary* locations = instruction->GetLocations();
3555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3557 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003558
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003560 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003561
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003562 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003563 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003564
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003565 if (imm == 0) {
3566 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3567 } else if (imm == 1 || imm == -1) {
3568 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003569 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003570 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003571 } else {
3572 DCHECK(imm <= -2 || imm >= 2);
3573 GenerateDivRemWithAnyConstant(instruction);
3574 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003575 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003576 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003577 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003578 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003579 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003580
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3582 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3583 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3584 // so it's safe to just use negl instead of more complex comparisons.
3585 if (type == Primitive::kPrimInt) {
3586 __ cmpl(second_reg, Immediate(-1));
3587 __ j(kEqual, slow_path->GetEntryLabel());
3588 // edx:eax <- sign-extended of eax
3589 __ cdq();
3590 // eax = quotient, edx = remainder
3591 __ idivl(second_reg);
3592 } else {
3593 __ cmpq(second_reg, Immediate(-1));
3594 __ j(kEqual, slow_path->GetEntryLabel());
3595 // rdx:rax <- sign-extended of rax
3596 __ cqo();
3597 // rax = quotient, rdx = remainder
3598 __ idivq(second_reg);
3599 }
3600 __ Bind(slow_path->GetExitLabel());
3601 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003602}
3603
Calin Juravle7c4954d2014-10-28 16:57:40 +00003604void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3605 LocationSummary* locations =
3606 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3607 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003608 case Primitive::kPrimInt:
3609 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003610 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003611 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003612 locations->SetOut(Location::SameAsFirstInput());
3613 // Intel uses edx:eax as the dividend.
3614 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3616 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3617 // output and request another temp.
3618 if (div->InputAt(1)->IsConstant()) {
3619 locations->AddTemp(Location::RequiresRegister());
3620 }
Calin Juravled0d48522014-11-04 16:40:20 +00003621 break;
3622 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003623
Calin Juravle7c4954d2014-10-28 16:57:40 +00003624 case Primitive::kPrimFloat:
3625 case Primitive::kPrimDouble: {
3626 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003627 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003628 locations->SetOut(Location::SameAsFirstInput());
3629 break;
3630 }
3631
3632 default:
3633 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3634 }
3635}
3636
3637void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3638 LocationSummary* locations = div->GetLocations();
3639 Location first = locations->InAt(0);
3640 Location second = locations->InAt(1);
3641 DCHECK(first.Equals(locations->Out()));
3642
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003643 Primitive::Type type = div->GetResultType();
3644 switch (type) {
3645 case Primitive::kPrimInt:
3646 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003647 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003648 break;
3649 }
3650
Calin Juravle7c4954d2014-10-28 16:57:40 +00003651 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003652 if (second.IsFpuRegister()) {
3653 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3654 } else if (second.IsConstant()) {
3655 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003656 codegen_->LiteralFloatAddress(
3657 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003658 } else {
3659 DCHECK(second.IsStackSlot());
3660 __ divss(first.AsFpuRegister<XmmRegister>(),
3661 Address(CpuRegister(RSP), second.GetStackIndex()));
3662 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003663 break;
3664 }
3665
3666 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003667 if (second.IsFpuRegister()) {
3668 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3669 } else if (second.IsConstant()) {
3670 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003671 codegen_->LiteralDoubleAddress(
3672 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003673 } else {
3674 DCHECK(second.IsDoubleStackSlot());
3675 __ divsd(first.AsFpuRegister<XmmRegister>(),
3676 Address(CpuRegister(RSP), second.GetStackIndex()));
3677 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003678 break;
3679 }
3680
3681 default:
3682 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3683 }
3684}
3685
Calin Juravlebacfec32014-11-14 15:54:36 +00003686void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003687 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003688 LocationSummary* locations =
3689 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003690
3691 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003692 case Primitive::kPrimInt:
3693 case Primitive::kPrimLong: {
3694 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003695 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003696 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3697 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003698 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3699 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3700 // output and request another temp.
3701 if (rem->InputAt(1)->IsConstant()) {
3702 locations->AddTemp(Location::RequiresRegister());
3703 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003704 break;
3705 }
3706
3707 case Primitive::kPrimFloat:
3708 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003709 locations->SetInAt(0, Location::Any());
3710 locations->SetInAt(1, Location::Any());
3711 locations->SetOut(Location::RequiresFpuRegister());
3712 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003713 break;
3714 }
3715
3716 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003717 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003718 }
3719}
3720
3721void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3722 Primitive::Type type = rem->GetResultType();
3723 switch (type) {
3724 case Primitive::kPrimInt:
3725 case Primitive::kPrimLong: {
3726 GenerateDivRemIntegral(rem);
3727 break;
3728 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003729 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003730 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003731 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003732 break;
3733 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003734 default:
3735 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3736 }
3737}
3738
Calin Juravled0d48522014-11-04 16:40:20 +00003739void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003740 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3741 ? LocationSummary::kCallOnSlowPath
3742 : LocationSummary::kNoCall;
3743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003744 locations->SetInAt(0, Location::Any());
3745 if (instruction->HasUses()) {
3746 locations->SetOut(Location::SameAsFirstInput());
3747 }
3748}
3749
3750void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003751 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003752 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3753 codegen_->AddSlowPath(slow_path);
3754
3755 LocationSummary* locations = instruction->GetLocations();
3756 Location value = locations->InAt(0);
3757
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003758 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003759 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003760 case Primitive::kPrimByte:
3761 case Primitive::kPrimChar:
3762 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003763 case Primitive::kPrimInt: {
3764 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003765 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003766 __ j(kEqual, slow_path->GetEntryLabel());
3767 } else if (value.IsStackSlot()) {
3768 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3769 __ j(kEqual, slow_path->GetEntryLabel());
3770 } else {
3771 DCHECK(value.IsConstant()) << value;
3772 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3773 __ jmp(slow_path->GetEntryLabel());
3774 }
3775 }
3776 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003777 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003778 case Primitive::kPrimLong: {
3779 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003781 __ j(kEqual, slow_path->GetEntryLabel());
3782 } else if (value.IsDoubleStackSlot()) {
3783 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3784 __ j(kEqual, slow_path->GetEntryLabel());
3785 } else {
3786 DCHECK(value.IsConstant()) << value;
3787 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3788 __ jmp(slow_path->GetEntryLabel());
3789 }
3790 }
3791 break;
3792 }
3793 default:
3794 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003795 }
Calin Juravled0d48522014-11-04 16:40:20 +00003796}
3797
Calin Juravle9aec02f2014-11-18 23:06:35 +00003798void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3799 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3800
3801 LocationSummary* locations =
3802 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3803
3804 switch (op->GetResultType()) {
3805 case Primitive::kPrimInt:
3806 case Primitive::kPrimLong: {
3807 locations->SetInAt(0, Location::RequiresRegister());
3808 // The shift count needs to be in CL.
3809 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3810 locations->SetOut(Location::SameAsFirstInput());
3811 break;
3812 }
3813 default:
3814 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3815 }
3816}
3817
3818void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3819 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3820
3821 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003823 Location second = locations->InAt(1);
3824
3825 switch (op->GetResultType()) {
3826 case Primitive::kPrimInt: {
3827 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003829 if (op->IsShl()) {
3830 __ shll(first_reg, second_reg);
3831 } else if (op->IsShr()) {
3832 __ sarl(first_reg, second_reg);
3833 } else {
3834 __ shrl(first_reg, second_reg);
3835 }
3836 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003837 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 if (op->IsShl()) {
3839 __ shll(first_reg, imm);
3840 } else if (op->IsShr()) {
3841 __ sarl(first_reg, imm);
3842 } else {
3843 __ shrl(first_reg, imm);
3844 }
3845 }
3846 break;
3847 }
3848 case Primitive::kPrimLong: {
3849 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003851 if (op->IsShl()) {
3852 __ shlq(first_reg, second_reg);
3853 } else if (op->IsShr()) {
3854 __ sarq(first_reg, second_reg);
3855 } else {
3856 __ shrq(first_reg, second_reg);
3857 }
3858 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003859 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003860 if (op->IsShl()) {
3861 __ shlq(first_reg, imm);
3862 } else if (op->IsShr()) {
3863 __ sarq(first_reg, imm);
3864 } else {
3865 __ shrq(first_reg, imm);
3866 }
3867 }
3868 break;
3869 }
3870 default:
3871 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003872 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873 }
3874}
3875
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003876void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3877 LocationSummary* locations =
3878 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3879
3880 switch (ror->GetResultType()) {
3881 case Primitive::kPrimInt:
3882 case Primitive::kPrimLong: {
3883 locations->SetInAt(0, Location::RequiresRegister());
3884 // The shift count needs to be in CL (unless it is a constant).
3885 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3886 locations->SetOut(Location::SameAsFirstInput());
3887 break;
3888 }
3889 default:
3890 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3891 UNREACHABLE();
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3896 LocationSummary* locations = ror->GetLocations();
3897 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3898 Location second = locations->InAt(1);
3899
3900 switch (ror->GetResultType()) {
3901 case Primitive::kPrimInt:
3902 if (second.IsRegister()) {
3903 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3904 __ rorl(first_reg, second_reg);
3905 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003906 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003907 __ rorl(first_reg, imm);
3908 }
3909 break;
3910 case Primitive::kPrimLong:
3911 if (second.IsRegister()) {
3912 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3913 __ rorq(first_reg, second_reg);
3914 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003915 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003916 __ rorq(first_reg, imm);
3917 }
3918 break;
3919 default:
3920 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3921 UNREACHABLE();
3922 }
3923}
3924
Calin Juravle9aec02f2014-11-18 23:06:35 +00003925void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3926 HandleShift(shl);
3927}
3928
3929void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3930 HandleShift(shl);
3931}
3932
3933void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3934 HandleShift(shr);
3935}
3936
3937void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3938 HandleShift(shr);
3939}
3940
3941void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3942 HandleShift(ushr);
3943}
3944
3945void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3946 HandleShift(ushr);
3947}
3948
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003949void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003950 LocationSummary* locations =
3951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003952 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003953 if (instruction->IsStringAlloc()) {
3954 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3955 } else {
3956 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3957 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3958 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003959 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003960}
3961
3962void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003963 // Note: if heap poisoning is enabled, the entry point takes cares
3964 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003965 if (instruction->IsStringAlloc()) {
3966 // String is allocated through StringFactory. Call NewEmptyString entry point.
3967 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3968 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3969 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3970 __ call(Address(temp, code_offset.SizeValue()));
3971 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3972 } else {
3973 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3974 instruction,
3975 instruction->GetDexPc(),
3976 nullptr);
3977 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3978 DCHECK(!codegen_->IsLeafMethod());
3979 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003980}
3981
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003982void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3985 InvokeRuntimeCallingConvention calling_convention;
3986 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003988 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003989 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003990}
3991
3992void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3993 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003994 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3995 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003996 // Note: if heap poisoning is enabled, the entry point takes cares
3997 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003998 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3999 instruction,
4000 instruction->GetDexPc(),
4001 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004002 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004003
4004 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004005}
4006
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004007void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004008 LocationSummary* locations =
4009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004010 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4011 if (location.IsStackSlot()) {
4012 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4013 } else if (location.IsDoubleStackSlot()) {
4014 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4015 }
4016 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004017}
4018
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004019void InstructionCodeGeneratorX86_64::VisitParameterValue(
4020 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004021 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004022}
4023
4024void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4027 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4028}
4029
4030void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4031 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4032 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004033}
4034
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004035void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4036 LocationSummary* locations =
4037 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetOut(Location::RequiresRegister());
4040}
4041
4042void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4043 LocationSummary* locations = instruction->GetLocations();
4044 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004046 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4047 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4048 } else {
4049 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4050 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
4051 }
4052 __ movq(locations->Out().AsRegister<CpuRegister>(),
4053 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4054}
4055
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004056void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004057 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004058 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004059 locations->SetInAt(0, Location::RequiresRegister());
4060 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004061}
4062
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004063void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4064 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004065 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4066 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004067 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004068 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004069 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004071 break;
4072
4073 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004074 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004075 break;
4076
4077 default:
4078 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4079 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004080}
4081
David Brazdil66d126e2015-04-03 16:02:44 +01004082void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4083 LocationSummary* locations =
4084 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4085 locations->SetInAt(0, Location::RequiresRegister());
4086 locations->SetOut(Location::SameAsFirstInput());
4087}
4088
4089void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004090 LocationSummary* locations = bool_not->GetLocations();
4091 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4092 locations->Out().AsRegister<CpuRegister>().AsRegister());
4093 Location out = locations->Out();
4094 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4095}
4096
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004097void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004098 LocationSummary* locations =
4099 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004100 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4101 locations->SetInAt(i, Location::Any());
4102 }
4103 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004104}
4105
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004106void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004107 LOG(FATAL) << "Unimplemented";
4108}
4109
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004110void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004111 /*
4112 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004113 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004114 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4115 */
4116 switch (kind) {
4117 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004118 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004119 break;
4120 }
4121 case MemBarrierKind::kAnyStore:
4122 case MemBarrierKind::kLoadAny:
4123 case MemBarrierKind::kStoreStore: {
4124 // nop
4125 break;
4126 }
4127 default:
4128 LOG(FATAL) << "Unexpected memory barier " << kind;
4129 }
4130}
4131
4132void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4133 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4134
Roland Levillain0d5a2812015-11-13 10:07:31 +00004135 bool object_field_get_with_read_barrier =
4136 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004137 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004138 new (GetGraph()->GetArena()) LocationSummary(instruction,
4139 object_field_get_with_read_barrier ?
4140 LocationSummary::kCallOnSlowPath :
4141 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004142 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004143 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4144 locations->SetOut(Location::RequiresFpuRegister());
4145 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004146 // The output overlaps for an object field get when read barriers
4147 // are enabled: we do not want the move to overwrite the object's
4148 // location, as we need it to emit the read barrier.
4149 locations->SetOut(
4150 Location::RequiresRegister(),
4151 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004152 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004153 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4154 // We need a temporary register for the read barrier marking slow
4155 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4156 locations->AddTemp(Location::RequiresRegister());
4157 }
Calin Juravle52c48962014-12-16 17:02:57 +00004158}
4159
4160void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4161 const FieldInfo& field_info) {
4162 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4163
4164 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004165 Location base_loc = locations->InAt(0);
4166 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004167 Location out = locations->Out();
4168 bool is_volatile = field_info.IsVolatile();
4169 Primitive::Type field_type = field_info.GetFieldType();
4170 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4171
4172 switch (field_type) {
4173 case Primitive::kPrimBoolean: {
4174 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4175 break;
4176 }
4177
4178 case Primitive::kPrimByte: {
4179 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4180 break;
4181 }
4182
4183 case Primitive::kPrimShort: {
4184 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4185 break;
4186 }
4187
4188 case Primitive::kPrimChar: {
4189 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4190 break;
4191 }
4192
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004193 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004194 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4195 break;
4196 }
4197
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004198 case Primitive::kPrimNot: {
4199 // /* HeapReference<Object> */ out = *(base + offset)
4200 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4201 Location temp_loc = locations->GetTemp(0);
4202 // Note that a potential implicit null check is handled in this
4203 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4204 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4205 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4206 if (is_volatile) {
4207 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4208 }
4209 } else {
4210 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4211 codegen_->MaybeRecordImplicitNullCheck(instruction);
4212 if (is_volatile) {
4213 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4214 }
4215 // If read barriers are enabled, emit read barriers other than
4216 // Baker's using a slow path (and also unpoison the loaded
4217 // reference, if heap poisoning is enabled).
4218 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4219 }
4220 break;
4221 }
4222
Calin Juravle52c48962014-12-16 17:02:57 +00004223 case Primitive::kPrimLong: {
4224 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4225 break;
4226 }
4227
4228 case Primitive::kPrimFloat: {
4229 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4230 break;
4231 }
4232
4233 case Primitive::kPrimDouble: {
4234 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4235 break;
4236 }
4237
4238 case Primitive::kPrimVoid:
4239 LOG(FATAL) << "Unreachable type " << field_type;
4240 UNREACHABLE();
4241 }
4242
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004243 if (field_type == Primitive::kPrimNot) {
4244 // Potential implicit null checks, in the case of reference
4245 // fields, are handled in the previous switch statement.
4246 } else {
4247 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004248 }
Roland Levillain4d027112015-07-01 15:41:14 +01004249
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004250 if (is_volatile) {
4251 if (field_type == Primitive::kPrimNot) {
4252 // Memory barriers, in the case of references, are also handled
4253 // in the previous switch statement.
4254 } else {
4255 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4256 }
Roland Levillain4d027112015-07-01 15:41:14 +01004257 }
Calin Juravle52c48962014-12-16 17:02:57 +00004258}
4259
4260void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4261 const FieldInfo& field_info) {
4262 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4263
4264 LocationSummary* locations =
4265 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004266 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004267 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004268 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004269 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004270
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004271 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004272 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004273 if (is_volatile) {
4274 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4275 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4276 } else {
4277 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4278 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004279 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004280 if (is_volatile) {
4281 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4282 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4283 } else {
4284 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4285 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004286 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004287 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004288 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004289 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004290 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004291 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4292 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004293 locations->AddTemp(Location::RequiresRegister());
4294 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295}
4296
Calin Juravle52c48962014-12-16 17:02:57 +00004297void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004298 const FieldInfo& field_info,
4299 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004300 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4301
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004302 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004303 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4304 Location value = locations->InAt(1);
4305 bool is_volatile = field_info.IsVolatile();
4306 Primitive::Type field_type = field_info.GetFieldType();
4307 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4308
4309 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004310 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004311 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312
Mark Mendellea5af682015-10-22 17:35:49 -04004313 bool maybe_record_implicit_null_check_done = false;
4314
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004315 switch (field_type) {
4316 case Primitive::kPrimBoolean:
4317 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004318 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004319 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004320 __ movb(Address(base, offset), Immediate(v));
4321 } else {
4322 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4323 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324 break;
4325 }
4326
4327 case Primitive::kPrimShort:
4328 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004329 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004330 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004331 __ movw(Address(base, offset), Immediate(v));
4332 } else {
4333 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4334 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004335 break;
4336 }
4337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004340 if (value.IsConstant()) {
4341 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004342 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4343 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4344 // Note: if heap poisoning is enabled, no need to poison
4345 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004346 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004347 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004348 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4349 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4350 __ movl(temp, value.AsRegister<CpuRegister>());
4351 __ PoisonHeapReference(temp);
4352 __ movl(Address(base, offset), temp);
4353 } else {
4354 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4355 }
Mark Mendell40741f32015-04-20 22:10:34 -04004356 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004357 break;
4358 }
4359
4360 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004361 if (value.IsConstant()) {
4362 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004363 codegen_->MoveInt64ToAddress(Address(base, offset),
4364 Address(base, offset + sizeof(int32_t)),
4365 v,
4366 instruction);
4367 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004368 } else {
4369 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4370 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371 break;
4372 }
4373
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004374 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004375 if (value.IsConstant()) {
4376 int32_t v =
4377 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4378 __ movl(Address(base, offset), Immediate(v));
4379 } else {
4380 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4381 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004382 break;
4383 }
4384
4385 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004386 if (value.IsConstant()) {
4387 int64_t v =
4388 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4389 codegen_->MoveInt64ToAddress(Address(base, offset),
4390 Address(base, offset + sizeof(int32_t)),
4391 v,
4392 instruction);
4393 maybe_record_implicit_null_check_done = true;
4394 } else {
4395 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4396 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004397 break;
4398 }
4399
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 case Primitive::kPrimVoid:
4401 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004402 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004403 }
Calin Juravle52c48962014-12-16 17:02:57 +00004404
Mark Mendellea5af682015-10-22 17:35:49 -04004405 if (!maybe_record_implicit_null_check_done) {
4406 codegen_->MaybeRecordImplicitNullCheck(instruction);
4407 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004408
4409 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4410 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4411 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004412 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 }
4414
Calin Juravle52c48962014-12-16 17:02:57 +00004415 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004416 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004417 }
4418}
4419
4420void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4421 HandleFieldSet(instruction, instruction->GetFieldInfo());
4422}
4423
4424void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004425 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426}
4427
4428void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004429 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430}
4431
4432void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004433 HandleFieldGet(instruction, instruction->GetFieldInfo());
4434}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435
Calin Juravle52c48962014-12-16 17:02:57 +00004436void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4437 HandleFieldGet(instruction);
4438}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439
Calin Juravle52c48962014-12-16 17:02:57 +00004440void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4441 HandleFieldGet(instruction, instruction->GetFieldInfo());
4442}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443
Calin Juravle52c48962014-12-16 17:02:57 +00004444void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4445 HandleFieldSet(instruction, instruction->GetFieldInfo());
4446}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447
Calin Juravle52c48962014-12-16 17:02:57 +00004448void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004449 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004450}
4451
Calin Juravlee460d1d2015-09-29 04:52:17 +01004452void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4453 HUnresolvedInstanceFieldGet* instruction) {
4454 FieldAccessCallingConventionX86_64 calling_convention;
4455 codegen_->CreateUnresolvedFieldLocationSummary(
4456 instruction, instruction->GetFieldType(), calling_convention);
4457}
4458
4459void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4460 HUnresolvedInstanceFieldGet* instruction) {
4461 FieldAccessCallingConventionX86_64 calling_convention;
4462 codegen_->GenerateUnresolvedFieldAccess(instruction,
4463 instruction->GetFieldType(),
4464 instruction->GetFieldIndex(),
4465 instruction->GetDexPc(),
4466 calling_convention);
4467}
4468
4469void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4470 HUnresolvedInstanceFieldSet* instruction) {
4471 FieldAccessCallingConventionX86_64 calling_convention;
4472 codegen_->CreateUnresolvedFieldLocationSummary(
4473 instruction, instruction->GetFieldType(), calling_convention);
4474}
4475
4476void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4477 HUnresolvedInstanceFieldSet* instruction) {
4478 FieldAccessCallingConventionX86_64 calling_convention;
4479 codegen_->GenerateUnresolvedFieldAccess(instruction,
4480 instruction->GetFieldType(),
4481 instruction->GetFieldIndex(),
4482 instruction->GetDexPc(),
4483 calling_convention);
4484}
4485
4486void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4487 HUnresolvedStaticFieldGet* instruction) {
4488 FieldAccessCallingConventionX86_64 calling_convention;
4489 codegen_->CreateUnresolvedFieldLocationSummary(
4490 instruction, instruction->GetFieldType(), calling_convention);
4491}
4492
4493void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4494 HUnresolvedStaticFieldGet* instruction) {
4495 FieldAccessCallingConventionX86_64 calling_convention;
4496 codegen_->GenerateUnresolvedFieldAccess(instruction,
4497 instruction->GetFieldType(),
4498 instruction->GetFieldIndex(),
4499 instruction->GetDexPc(),
4500 calling_convention);
4501}
4502
4503void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4504 HUnresolvedStaticFieldSet* instruction) {
4505 FieldAccessCallingConventionX86_64 calling_convention;
4506 codegen_->CreateUnresolvedFieldLocationSummary(
4507 instruction, instruction->GetFieldType(), calling_convention);
4508}
4509
4510void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4511 HUnresolvedStaticFieldSet* instruction) {
4512 FieldAccessCallingConventionX86_64 calling_convention;
4513 codegen_->GenerateUnresolvedFieldAccess(instruction,
4514 instruction->GetFieldType(),
4515 instruction->GetFieldIndex(),
4516 instruction->GetDexPc(),
4517 calling_convention);
4518}
4519
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004521 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4522 ? LocationSummary::kCallOnSlowPath
4523 : LocationSummary::kNoCall;
4524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4525 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004526 ? Location::RequiresRegister()
4527 : Location::Any();
4528 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004529 if (instruction->HasUses()) {
4530 locations->SetOut(Location::SameAsFirstInput());
4531 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532}
4533
Calin Juravle2ae48182016-03-16 14:05:09 +00004534void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4535 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004536 return;
4537 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004538 LocationSummary* locations = instruction->GetLocations();
4539 Location obj = locations->InAt(0);
4540
4541 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004542 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004543}
4544
Calin Juravle2ae48182016-03-16 14:05:09 +00004545void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004546 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004547 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004548
4549 LocationSummary* locations = instruction->GetLocations();
4550 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551
4552 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004553 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004554 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004556 } else {
4557 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004558 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004559 __ jmp(slow_path->GetEntryLabel());
4560 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004561 }
4562 __ j(kEqual, slow_path->GetEntryLabel());
4563}
4564
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004565void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004566 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004567}
4568
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004570 bool object_array_get_with_read_barrier =
4571 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004572 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004573 new (GetGraph()->GetArena()) LocationSummary(instruction,
4574 object_array_get_with_read_barrier ?
4575 LocationSummary::kCallOnSlowPath :
4576 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004577 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004578 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004579 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4581 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004582 // The output overlaps for an object array get when read barriers
4583 // are enabled: we do not want the move to overwrite the array's
4584 // location, as we need it to emit the read barrier.
4585 locations->SetOut(
4586 Location::RequiresRegister(),
4587 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004588 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004589 // We need a temporary register for the read barrier marking slow
4590 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4591 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4592 locations->AddTemp(Location::RequiresRegister());
4593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594}
4595
4596void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4597 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004598 Location obj_loc = locations->InAt(0);
4599 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004601 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004603 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004604 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 case Primitive::kPrimBoolean: {
4606 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004607 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608 if (index.IsConstant()) {
4609 __ movzxb(out, Address(obj,
4610 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4611 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004612 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004613 }
4614 break;
4615 }
4616
4617 case Primitive::kPrimByte: {
4618 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004619 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004620 if (index.IsConstant()) {
4621 __ movsxb(out, Address(obj,
4622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4623 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004624 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 }
4626 break;
4627 }
4628
4629 case Primitive::kPrimShort: {
4630 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004631 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 if (index.IsConstant()) {
4633 __ movsxw(out, Address(obj,
4634 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4635 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004636 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637 }
4638 break;
4639 }
4640
4641 case Primitive::kPrimChar: {
4642 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004643 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004644 if (index.IsConstant()) {
4645 __ movzxw(out, Address(obj,
4646 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4647 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004648 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 }
4650 break;
4651 }
4652
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004653 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004655 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 if (index.IsConstant()) {
4657 __ movl(out, Address(obj,
4658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4659 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004660 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 }
4662 break;
4663 }
4664
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004665 case Primitive::kPrimNot: {
4666 static_assert(
4667 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4668 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4669 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4670 // /* HeapReference<Object> */ out =
4671 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4672 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4673 Location temp = locations->GetTemp(0);
4674 // Note that a potential implicit null check is handled in this
4675 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4676 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4677 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4678 } else {
4679 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4680 if (index.IsConstant()) {
4681 uint32_t offset =
4682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4683 __ movl(out, Address(obj, offset));
4684 codegen_->MaybeRecordImplicitNullCheck(instruction);
4685 // If read barriers are enabled, emit read barriers other than
4686 // Baker's using a slow path (and also unpoison the loaded
4687 // reference, if heap poisoning is enabled).
4688 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4689 } else {
4690 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4691 codegen_->MaybeRecordImplicitNullCheck(instruction);
4692 // If read barriers are enabled, emit read barriers other than
4693 // Baker's using a slow path (and also unpoison the loaded
4694 // reference, if heap poisoning is enabled).
4695 codegen_->MaybeGenerateReadBarrierSlow(
4696 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4697 }
4698 }
4699 break;
4700 }
4701
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 case Primitive::kPrimLong: {
4703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004704 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004705 if (index.IsConstant()) {
4706 __ movq(out, Address(obj,
4707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4708 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004709 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 }
4711 break;
4712 }
4713
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004714 case Primitive::kPrimFloat: {
4715 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004716 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004717 if (index.IsConstant()) {
4718 __ movss(out, Address(obj,
4719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4720 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004721 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004722 }
4723 break;
4724 }
4725
4726 case Primitive::kPrimDouble: {
4727 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004728 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004729 if (index.IsConstant()) {
4730 __ movsd(out, Address(obj,
4731 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4732 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004733 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004734 }
4735 break;
4736 }
4737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004739 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004740 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004741 }
Roland Levillain4d027112015-07-01 15:41:14 +01004742
4743 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 // Potential implicit null checks, in the case of reference
4745 // arrays, are handled in the previous switch statement.
4746 } else {
4747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004748 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749}
4750
4751void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004752 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004753
4754 bool needs_write_barrier =
4755 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004756 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004757 bool object_array_set_with_read_barrier =
4758 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004759
Nicolas Geoffray39468442014-09-02 15:17:15 +01004760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004761 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004762 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004763 LocationSummary::kCallOnSlowPath :
4764 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004765
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004767 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4768 if (Primitive::IsFloatingPointType(value_type)) {
4769 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004770 } else {
4771 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4772 }
4773
4774 if (needs_write_barrier) {
4775 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004776
4777 // This first temporary register is possibly used for heap
4778 // reference poisoning and/or read barrier emission too.
4779 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004780 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004781 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782}
4783
4784void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4785 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004786 Location array_loc = locations->InAt(0);
4787 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004789 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004790 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004791 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004792 bool needs_write_barrier =
4793 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4795 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4796 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797
4798 switch (value_type) {
4799 case Primitive::kPrimBoolean:
4800 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4802 Address address = index.IsConstant()
4803 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4804 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4805 if (value.IsRegister()) {
4806 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004807 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004809 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004810 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004811 break;
4812 }
4813
4814 case Primitive::kPrimShort:
4815 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004816 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4817 Address address = index.IsConstant()
4818 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4819 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4820 if (value.IsRegister()) {
4821 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004822 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 DCHECK(value.IsConstant()) << value;
4824 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004825 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004826 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 break;
4828 }
4829
4830 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004831 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4832 Address address = index.IsConstant()
4833 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4834 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004835
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004836 if (!value.IsRegister()) {
4837 // Just setting null.
4838 DCHECK(instruction->InputAt(2)->IsNullConstant());
4839 DCHECK(value.IsConstant()) << value;
4840 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004842 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004843 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004845 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846
4847 DCHECK(needs_write_barrier);
4848 CpuRegister register_value = value.AsRegister<CpuRegister>();
4849 NearLabel done, not_null, do_put;
4850 SlowPathCode* slow_path = nullptr;
4851 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004852 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4854 codegen_->AddSlowPath(slow_path);
4855 if (instruction->GetValueCanBeNull()) {
4856 __ testl(register_value, register_value);
4857 __ j(kNotEqual, &not_null);
4858 __ movl(address, Immediate(0));
4859 codegen_->MaybeRecordImplicitNullCheck(instruction);
4860 __ jmp(&done);
4861 __ Bind(&not_null);
4862 }
4863
Roland Levillain0d5a2812015-11-13 10:07:31 +00004864 if (kEmitCompilerReadBarrier) {
4865 // When read barriers are enabled, the type checking
4866 // instrumentation requires two read barriers:
4867 //
4868 // __ movl(temp2, temp);
4869 // // /* HeapReference<Class> */ temp = temp->component_type_
4870 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004871 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004872 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4873 //
4874 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4875 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004876 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004877 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4878 //
4879 // __ cmpl(temp, temp2);
4880 //
4881 // However, the second read barrier may trash `temp`, as it
4882 // is a temporary register, and as such would not be saved
4883 // along with live registers before calling the runtime (nor
4884 // restored afterwards). So in this case, we bail out and
4885 // delegate the work to the array set slow path.
4886 //
4887 // TODO: Extend the register allocator to support a new
4888 // "(locally) live temp" location so as to avoid always
4889 // going into the slow path when read barriers are enabled.
4890 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004891 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004892 // /* HeapReference<Class> */ temp = array->klass_
4893 __ movl(temp, Address(array, class_offset));
4894 codegen_->MaybeRecordImplicitNullCheck(instruction);
4895 __ MaybeUnpoisonHeapReference(temp);
4896
4897 // /* HeapReference<Class> */ temp = temp->component_type_
4898 __ movl(temp, Address(temp, component_offset));
4899 // If heap poisoning is enabled, no need to unpoison `temp`
4900 // nor the object reference in `register_value->klass`, as
4901 // we are comparing two poisoned references.
4902 __ cmpl(temp, Address(register_value, class_offset));
4903
4904 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4905 __ j(kEqual, &do_put);
4906 // If heap poisoning is enabled, the `temp` reference has
4907 // not been unpoisoned yet; unpoison it now.
4908 __ MaybeUnpoisonHeapReference(temp);
4909
4910 // /* HeapReference<Class> */ temp = temp->super_class_
4911 __ movl(temp, Address(temp, super_offset));
4912 // If heap poisoning is enabled, no need to unpoison
4913 // `temp`, as we are comparing against null below.
4914 __ testl(temp, temp);
4915 __ j(kNotEqual, slow_path->GetEntryLabel());
4916 __ Bind(&do_put);
4917 } else {
4918 __ j(kNotEqual, slow_path->GetEntryLabel());
4919 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004920 }
4921 }
4922
4923 if (kPoisonHeapReferences) {
4924 __ movl(temp, register_value);
4925 __ PoisonHeapReference(temp);
4926 __ movl(address, temp);
4927 } else {
4928 __ movl(address, register_value);
4929 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004930 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004931 codegen_->MaybeRecordImplicitNullCheck(instruction);
4932 }
4933
4934 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4935 codegen_->MarkGCCard(
4936 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4937 __ Bind(&done);
4938
4939 if (slow_path != nullptr) {
4940 __ Bind(slow_path->GetExitLabel());
4941 }
4942
4943 break;
4944 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004945
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004946 case Primitive::kPrimInt: {
4947 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4948 Address address = index.IsConstant()
4949 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4950 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4951 if (value.IsRegister()) {
4952 __ movl(address, value.AsRegister<CpuRegister>());
4953 } else {
4954 DCHECK(value.IsConstant()) << value;
4955 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4956 __ movl(address, Immediate(v));
4957 }
4958 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004959 break;
4960 }
4961
4962 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004963 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4964 Address address = index.IsConstant()
4965 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4966 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4967 if (value.IsRegister()) {
4968 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004969 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004971 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004972 Address address_high = index.IsConstant()
4973 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4974 offset + sizeof(int32_t))
4975 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4976 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004977 }
4978 break;
4979 }
4980
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004981 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004982 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4983 Address address = index.IsConstant()
4984 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4985 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004986 if (value.IsFpuRegister()) {
4987 __ movss(address, value.AsFpuRegister<XmmRegister>());
4988 } else {
4989 DCHECK(value.IsConstant());
4990 int32_t v =
4991 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4992 __ movl(address, Immediate(v));
4993 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004994 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004995 break;
4996 }
4997
4998 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004999 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5000 Address address = index.IsConstant()
5001 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5002 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005003 if (value.IsFpuRegister()) {
5004 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5005 codegen_->MaybeRecordImplicitNullCheck(instruction);
5006 } else {
5007 int64_t v =
5008 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5009 Address address_high = index.IsConstant()
5010 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5011 offset + sizeof(int32_t))
5012 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5013 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5014 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005015 break;
5016 }
5017
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018 case Primitive::kPrimVoid:
5019 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005020 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021 }
5022}
5023
5024void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005025 LocationSummary* locations =
5026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005027 locations->SetInAt(0, Location::RequiresRegister());
5028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029}
5030
5031void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
5032 LocationSummary* locations = instruction->GetLocations();
5033 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005034 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5035 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005037 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038}
5039
5040void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005041 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5042 ? LocationSummary::kCallOnSlowPath
5043 : LocationSummary::kNoCall;
5044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005045 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005046 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005047 if (instruction->HasUses()) {
5048 locations->SetOut(Location::SameAsFirstInput());
5049 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005050}
5051
5052void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5053 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005054 Location index_loc = locations->InAt(0);
5055 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005056 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005057 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058
Mark Mendell99dbd682015-04-22 16:18:52 -04005059 if (length_loc.IsConstant()) {
5060 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5061 if (index_loc.IsConstant()) {
5062 // BCE will remove the bounds check if we are guarenteed to pass.
5063 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5064 if (index < 0 || index >= length) {
5065 codegen_->AddSlowPath(slow_path);
5066 __ jmp(slow_path->GetEntryLabel());
5067 } else {
5068 // Some optimization after BCE may have generated this, and we should not
5069 // generate a bounds check if it is a valid range.
5070 }
5071 return;
5072 }
5073
5074 // We have to reverse the jump condition because the length is the constant.
5075 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5076 __ cmpl(index_reg, Immediate(length));
5077 codegen_->AddSlowPath(slow_path);
5078 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005079 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005080 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5081 if (index_loc.IsConstant()) {
5082 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5083 __ cmpl(length, Immediate(value));
5084 } else {
5085 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5086 }
5087 codegen_->AddSlowPath(slow_path);
5088 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005089 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090}
5091
5092void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5093 CpuRegister card,
5094 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005095 CpuRegister value,
5096 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005097 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005098 if (value_can_be_null) {
5099 __ testl(value, value);
5100 __ j(kEqual, &is_null);
5101 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005102 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5103 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 __ movq(temp, object);
5105 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005106 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005107 if (value_can_be_null) {
5108 __ Bind(&is_null);
5109 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110}
5111
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005112void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005113 LOG(FATAL) << "Unimplemented";
5114}
5115
5116void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005117 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5118}
5119
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005120void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5122}
5123
5124void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005125 HBasicBlock* block = instruction->GetBlock();
5126 if (block->GetLoopInformation() != nullptr) {
5127 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5128 // The back edge will generate the suspend check.
5129 return;
5130 }
5131 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5132 // The goto will generate the suspend check.
5133 return;
5134 }
5135 GenerateSuspendCheck(instruction, nullptr);
5136}
5137
5138void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5139 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005140 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005141 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5142 if (slow_path == nullptr) {
5143 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5144 instruction->SetSlowPath(slow_path);
5145 codegen_->AddSlowPath(slow_path);
5146 if (successor != nullptr) {
5147 DCHECK(successor->IsLoopHeader());
5148 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5149 }
5150 } else {
5151 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5152 }
5153
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005154 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5155 /* no_rip */ true),
5156 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005157 if (successor == nullptr) {
5158 __ j(kNotEqual, slow_path->GetEntryLabel());
5159 __ Bind(slow_path->GetReturnLabel());
5160 } else {
5161 __ j(kEqual, codegen_->GetLabelOf(successor));
5162 __ jmp(slow_path->GetEntryLabel());
5163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005164}
5165
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005166X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5167 return codegen_->GetAssembler();
5168}
5169
5170void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005171 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005172 Location source = move->GetSource();
5173 Location destination = move->GetDestination();
5174
5175 if (source.IsRegister()) {
5176 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005177 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005178 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005179 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005180 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005181 } else {
5182 DCHECK(destination.IsDoubleStackSlot());
5183 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005184 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005185 }
5186 } else if (source.IsStackSlot()) {
5187 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005188 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005189 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005190 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005191 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005192 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005193 } else {
5194 DCHECK(destination.IsStackSlot());
5195 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5196 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5197 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005198 } else if (source.IsDoubleStackSlot()) {
5199 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005200 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005201 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005202 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005203 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5204 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005205 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005206 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005207 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5208 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5209 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005210 } else if (source.IsConstant()) {
5211 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005212 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5213 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005214 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005215 if (value == 0) {
5216 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5217 } else {
5218 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5219 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005220 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005221 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005222 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005223 }
5224 } else if (constant->IsLongConstant()) {
5225 int64_t value = constant->AsLongConstant()->GetValue();
5226 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005227 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005228 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005229 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005230 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005231 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005232 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005233 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005234 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005235 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005236 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005237 } else {
5238 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005239 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005240 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5241 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005242 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005243 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005244 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005245 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005246 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005247 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005248 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 } else {
5250 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005251 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005252 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005253 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 } else if (source.IsFpuRegister()) {
5255 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005256 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005257 } else if (destination.IsStackSlot()) {
5258 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005259 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005261 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005263 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005264 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005265 }
5266}
5267
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005268void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005269 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005270 __ movl(Address(CpuRegister(RSP), mem), reg);
5271 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005272}
5273
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005274void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005275 ScratchRegisterScope ensure_scratch(
5276 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5277
5278 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5279 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5280 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5281 Address(CpuRegister(RSP), mem2 + stack_offset));
5282 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5283 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5284 CpuRegister(ensure_scratch.GetRegister()));
5285}
5286
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005287void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5288 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5289 __ movq(Address(CpuRegister(RSP), mem), reg);
5290 __ movq(reg, CpuRegister(TMP));
5291}
5292
5293void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5294 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005295 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005296
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005297 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5298 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5299 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5300 Address(CpuRegister(RSP), mem2 + stack_offset));
5301 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5302 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5303 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005304}
5305
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5307 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5308 __ movss(Address(CpuRegister(RSP), mem), reg);
5309 __ movd(reg, CpuRegister(TMP));
5310}
5311
5312void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5313 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5314 __ movsd(Address(CpuRegister(RSP), mem), reg);
5315 __ movd(reg, CpuRegister(TMP));
5316}
5317
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005318void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005319 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005320 Location source = move->GetSource();
5321 Location destination = move->GetDestination();
5322
5323 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005324 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005325 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005326 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005327 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005328 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005329 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005330 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5331 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005332 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005333 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005334 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005335 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5336 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005337 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005338 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5339 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5340 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005341 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005342 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005343 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005344 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005345 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005346 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005347 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005348 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005349 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005350 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005351 }
5352}
5353
5354
5355void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5356 __ pushq(CpuRegister(reg));
5357}
5358
5359
5360void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5361 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005362}
5363
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005364void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005365 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005366 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5367 Immediate(mirror::Class::kStatusInitialized));
5368 __ j(kLess, slow_path->GetEntryLabel());
5369 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005370 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005371}
5372
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005373void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005374 InvokeRuntimeCallingConvention calling_convention;
5375 CodeGenerator::CreateLoadClassLocationSummary(
5376 cls,
5377 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005378 Location::RegisterLocation(RAX),
5379 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005380}
5381
5382void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005383 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005384 if (cls->NeedsAccessCheck()) {
5385 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5386 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5387 cls,
5388 cls->GetDexPc(),
5389 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005390 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005391 return;
5392 }
5393
Roland Levillain0d5a2812015-11-13 10:07:31 +00005394 Location out_loc = locations->Out();
5395 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005396 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005397
Calin Juravle580b6092015-10-06 17:35:58 +01005398 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399 DCHECK(!cls->CanCallRuntime());
5400 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005401 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5402 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005403 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005404 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005405 // /* GcRoot<mirror::Class>[] */ out =
5406 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5407 __ movq(out, Address(current_method,
5408 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005409 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005410 GenerateGcRootFieldLoad(
5411 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01005412
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005413 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5414 DCHECK(cls->CanCallRuntime());
5415 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5416 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5417 codegen_->AddSlowPath(slow_path);
5418 if (!cls->IsInDexCache()) {
5419 __ testl(out, out);
5420 __ j(kEqual, slow_path->GetEntryLabel());
5421 }
5422 if (cls->MustGenerateClinitCheck()) {
5423 GenerateClassInitializationCheck(slow_path, out);
5424 } else {
5425 __ Bind(slow_path->GetExitLabel());
5426 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005427 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005428 }
5429}
5430
5431void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5432 LocationSummary* locations =
5433 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5434 locations->SetInAt(0, Location::RequiresRegister());
5435 if (check->HasUses()) {
5436 locations->SetOut(Location::SameAsFirstInput());
5437 }
5438}
5439
5440void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005441 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005442 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005443 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005444 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005445 GenerateClassInitializationCheck(slow_path,
5446 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005447}
5448
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005449HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5450 HLoadString::LoadKind desired_string_load_kind) {
5451 if (kEmitCompilerReadBarrier) {
5452 switch (desired_string_load_kind) {
5453 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5454 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5455 case HLoadString::LoadKind::kBootImageAddress:
5456 // TODO: Implement for read barrier.
5457 return HLoadString::LoadKind::kDexCacheViaMethod;
5458 default:
5459 break;
5460 }
5461 }
5462 switch (desired_string_load_kind) {
5463 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5464 DCHECK(!GetCompilerOptions().GetCompilePic());
5465 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5466 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5467 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5468 DCHECK(GetCompilerOptions().GetCompilePic());
5469 break;
5470 case HLoadString::LoadKind::kBootImageAddress:
5471 break;
5472 case HLoadString::LoadKind::kDexCacheAddress:
5473 DCHECK(Runtime::Current()->UseJit());
5474 break;
5475 case HLoadString::LoadKind::kDexCachePcRelative:
5476 DCHECK(!Runtime::Current()->UseJit());
5477 break;
5478 case HLoadString::LoadKind::kDexCacheViaMethod:
5479 break;
5480 }
5481 return desired_string_load_kind;
5482}
5483
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005484void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005485 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005486 ? LocationSummary::kCallOnSlowPath
5487 : LocationSummary::kNoCall;
5488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005489 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5490 locations->SetInAt(0, Location::RequiresRegister());
5491 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005492 locations->SetOut(Location::RequiresRegister());
5493}
5494
5495void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005496 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005497 Location out_loc = locations->Out();
5498 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005500 switch (load->GetLoadKind()) {
5501 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5502 DCHECK(!kEmitCompilerReadBarrier);
5503 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5504 codegen_->RecordStringPatch(load);
5505 return; // No dex cache slow path.
5506 }
5507 case HLoadString::LoadKind::kBootImageAddress: {
5508 DCHECK(!kEmitCompilerReadBarrier);
5509 DCHECK_NE(load->GetAddress(), 0u);
5510 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5511 __ movl(out, Immediate(address)); // Zero-extended.
5512 codegen_->RecordSimplePatch();
5513 return; // No dex cache slow path.
5514 }
5515 case HLoadString::LoadKind::kDexCacheAddress: {
5516 DCHECK_NE(load->GetAddress(), 0u);
5517 if (IsUint<32>(load->GetAddress())) {
5518 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5519 GenerateGcRootFieldLoad(load, out_loc, address);
5520 } else {
5521 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5522 __ movq(out, Immediate(load->GetAddress()));
5523 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5524 }
5525 break;
5526 }
5527 case HLoadString::LoadKind::kDexCachePcRelative: {
5528 uint32_t offset = load->GetDexCacheElementOffset();
5529 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5530 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5531 /* no_rip */ false);
5532 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5533 break;
5534 }
5535 case HLoadString::LoadKind::kDexCacheViaMethod: {
5536 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5537
5538 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5539 GenerateGcRootFieldLoad(
5540 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5541 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5542 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5543 // /* GcRoot<mirror::String> */ out = out[string_index]
5544 GenerateGcRootFieldLoad(
5545 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5546 break;
5547 }
5548 default:
5549 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5550 UNREACHABLE();
5551 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005552
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005553 if (!load->IsInDexCache()) {
5554 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5555 codegen_->AddSlowPath(slow_path);
5556 __ testl(out, out);
5557 __ j(kEqual, slow_path->GetEntryLabel());
5558 __ Bind(slow_path->GetExitLabel());
5559 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005560}
5561
David Brazdilcb1c0552015-08-04 16:22:25 +01005562static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005563 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5564 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005565}
5566
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005567void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5568 LocationSummary* locations =
5569 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5570 locations->SetOut(Location::RequiresRegister());
5571}
5572
5573void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005574 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5575}
5576
5577void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5578 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5579}
5580
5581void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5582 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005583}
5584
5585void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5586 LocationSummary* locations =
5587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5588 InvokeRuntimeCallingConvention calling_convention;
5589 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5590}
5591
5592void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005593 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5594 instruction,
5595 instruction->GetDexPc(),
5596 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005597 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005598}
5599
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005600static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5601 return kEmitCompilerReadBarrier &&
5602 (kUseBakerReadBarrier ||
5603 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5604 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5605 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5606}
5607
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005608void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005609 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005610 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5611 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612 case TypeCheckKind::kExactCheck:
5613 case TypeCheckKind::kAbstractClassCheck:
5614 case TypeCheckKind::kClassHierarchyCheck:
5615 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005616 call_kind =
5617 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005618 break;
5619 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620 case TypeCheckKind::kUnresolvedCheck:
5621 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622 call_kind = LocationSummary::kCallOnSlowPath;
5623 break;
5624 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005625
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005627 locations->SetInAt(0, Location::RequiresRegister());
5628 locations->SetInAt(1, Location::Any());
5629 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5630 locations->SetOut(Location::RequiresRegister());
5631 // When read barriers are enabled, we need a temporary register for
5632 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005633 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005634 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005635 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005636}
5637
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005638void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005639 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005640 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005641 Location obj_loc = locations->InAt(0);
5642 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005643 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005644 Location out_loc = locations->Out();
5645 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005646 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005647 locations->GetTemp(0) :
5648 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005649 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5651 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5652 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005653 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005655
5656 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005657 // Avoid null check if we know obj is not null.
5658 if (instruction->MustDoNullCheck()) {
5659 __ testl(obj, obj);
5660 __ j(kEqual, &zero);
5661 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005662
Roland Levillain0d5a2812015-11-13 10:07:31 +00005663 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005664 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005666 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005667 case TypeCheckKind::kExactCheck: {
5668 if (cls.IsRegister()) {
5669 __ cmpl(out, cls.AsRegister<CpuRegister>());
5670 } else {
5671 DCHECK(cls.IsStackSlot()) << cls;
5672 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5673 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005674 if (zero.IsLinked()) {
5675 // Classes must be equal for the instanceof to succeed.
5676 __ j(kNotEqual, &zero);
5677 __ movl(out, Immediate(1));
5678 __ jmp(&done);
5679 } else {
5680 __ setcc(kEqual, out);
5681 // setcc only sets the low byte.
5682 __ andl(out, Immediate(1));
5683 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 break;
5685 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005686
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005687 case TypeCheckKind::kAbstractClassCheck: {
5688 // If the class is abstract, we eagerly fetch the super class of the
5689 // object to avoid doing a comparison we know will fail.
5690 NearLabel loop, success;
5691 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005692 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005693 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005694 __ testl(out, out);
5695 // If `out` is null, we use it for the result, and jump to `done`.
5696 __ j(kEqual, &done);
5697 if (cls.IsRegister()) {
5698 __ cmpl(out, cls.AsRegister<CpuRegister>());
5699 } else {
5700 DCHECK(cls.IsStackSlot()) << cls;
5701 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5702 }
5703 __ j(kNotEqual, &loop);
5704 __ movl(out, Immediate(1));
5705 if (zero.IsLinked()) {
5706 __ jmp(&done);
5707 }
5708 break;
5709 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 case TypeCheckKind::kClassHierarchyCheck: {
5712 // Walk over the class hierarchy to find a match.
5713 NearLabel loop, success;
5714 __ Bind(&loop);
5715 if (cls.IsRegister()) {
5716 __ cmpl(out, cls.AsRegister<CpuRegister>());
5717 } else {
5718 DCHECK(cls.IsStackSlot()) << cls;
5719 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5720 }
5721 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005723 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 __ testl(out, out);
5725 __ j(kNotEqual, &loop);
5726 // If `out` is null, we use it for the result, and jump to `done`.
5727 __ jmp(&done);
5728 __ Bind(&success);
5729 __ movl(out, Immediate(1));
5730 if (zero.IsLinked()) {
5731 __ jmp(&done);
5732 }
5733 break;
5734 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005737 // Do an exact check.
5738 NearLabel exact_check;
5739 if (cls.IsRegister()) {
5740 __ cmpl(out, cls.AsRegister<CpuRegister>());
5741 } else {
5742 DCHECK(cls.IsStackSlot()) << cls;
5743 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5744 }
5745 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005747 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005748 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005749 __ testl(out, out);
5750 // If `out` is null, we use it for the result, and jump to `done`.
5751 __ j(kEqual, &done);
5752 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5753 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005754 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005755 __ movl(out, Immediate(1));
5756 __ jmp(&done);
5757 break;
5758 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005760 case TypeCheckKind::kArrayCheck: {
5761 if (cls.IsRegister()) {
5762 __ cmpl(out, cls.AsRegister<CpuRegister>());
5763 } else {
5764 DCHECK(cls.IsStackSlot()) << cls;
5765 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5766 }
5767 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005768 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5769 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005770 codegen_->AddSlowPath(slow_path);
5771 __ j(kNotEqual, slow_path->GetEntryLabel());
5772 __ movl(out, Immediate(1));
5773 if (zero.IsLinked()) {
5774 __ jmp(&done);
5775 }
5776 break;
5777 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778
Calin Juravle98893e12015-10-02 21:05:03 +01005779 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780 case TypeCheckKind::kInterfaceCheck: {
5781 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005782 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005783 // cases.
5784 //
5785 // We cannot directly call the InstanceofNonTrivial runtime
5786 // entry point without resorting to a type checking slow path
5787 // here (i.e. by calling InvokeRuntime directly), as it would
5788 // require to assign fixed registers for the inputs of this
5789 // HInstanceOf instruction (following the runtime calling
5790 // convention), which might be cluttered by the potential first
5791 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005792 //
5793 // TODO: Introduce a new runtime entry point taking the object
5794 // to test (instead of its class) as argument, and let it deal
5795 // with the read barrier issues. This will let us refactor this
5796 // case of the `switch` code as it was previously (with a direct
5797 // call to the runtime not using a type checking slow path).
5798 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 DCHECK(locations->OnlyCallsOnSlowPath());
5800 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5801 /* is_fatal */ false);
5802 codegen_->AddSlowPath(slow_path);
5803 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005804 if (zero.IsLinked()) {
5805 __ jmp(&done);
5806 }
5807 break;
5808 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005809 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005810
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005811 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005812 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005813 __ xorl(out, out);
5814 }
5815
5816 if (done.IsLinked()) {
5817 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005818 }
5819
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005820 if (slow_path != nullptr) {
5821 __ Bind(slow_path->GetExitLabel());
5822 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005823}
5824
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005825void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5827 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5829 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830 case TypeCheckKind::kExactCheck:
5831 case TypeCheckKind::kAbstractClassCheck:
5832 case TypeCheckKind::kClassHierarchyCheck:
5833 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005834 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5835 LocationSummary::kCallOnSlowPath :
5836 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 break;
5838 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839 case TypeCheckKind::kUnresolvedCheck:
5840 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 call_kind = LocationSummary::kCallOnSlowPath;
5842 break;
5843 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5845 locations->SetInAt(0, Location::RequiresRegister());
5846 locations->SetInAt(1, Location::Any());
5847 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5848 locations->AddTemp(Location::RequiresRegister());
5849 // When read barriers are enabled, we need an additional temporary
5850 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005851 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005854}
5855
5856void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005857 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005858 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859 Location obj_loc = locations->InAt(0);
5860 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005861 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 Location temp_loc = locations->GetTemp(0);
5863 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005864 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005865 locations->GetTemp(1) :
5866 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5868 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5869 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5870 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005871
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 bool is_type_check_slow_path_fatal =
5873 (type_check_kind == TypeCheckKind::kExactCheck ||
5874 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5875 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5876 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5877 !instruction->CanThrowIntoCatchBlock();
5878 SlowPathCode* type_check_slow_path =
5879 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5880 is_type_check_slow_path_fatal);
5881 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 case TypeCheckKind::kExactCheck:
5885 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005886 NearLabel done;
5887 // Avoid null check if we know obj is not null.
5888 if (instruction->MustDoNullCheck()) {
5889 __ testl(obj, obj);
5890 __ j(kEqual, &done);
5891 }
5892
5893 // /* HeapReference<Class> */ temp = obj->klass_
5894 GenerateReferenceLoadTwoRegisters(
5895 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5896
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 if (cls.IsRegister()) {
5898 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5899 } else {
5900 DCHECK(cls.IsStackSlot()) << cls;
5901 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5902 }
5903 // Jump to slow path for throwing the exception or doing a
5904 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005906 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907 break;
5908 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005911 NearLabel done;
5912 // Avoid null check if we know obj is not null.
5913 if (instruction->MustDoNullCheck()) {
5914 __ testl(obj, obj);
5915 __ j(kEqual, &done);
5916 }
5917
5918 // /* HeapReference<Class> */ temp = obj->klass_
5919 GenerateReferenceLoadTwoRegisters(
5920 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5921
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 // If the class is abstract, we eagerly fetch the super class of the
5923 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005924 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005926 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005927 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928
5929 // If the class reference currently in `temp` is not null, jump
5930 // to the `compare_classes` label to compare it with the checked
5931 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933 __ j(kNotEqual, &compare_classes);
5934 // Otherwise, jump to the slow path to throw the exception.
5935 //
5936 // But before, move back the object's class into `temp` before
5937 // going into the slow path, as it has been overwritten in the
5938 // meantime.
5939 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005940 GenerateReferenceLoadTwoRegisters(
5941 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 __ jmp(type_check_slow_path->GetEntryLabel());
5943
5944 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 if (cls.IsRegister()) {
5946 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5947 } else {
5948 DCHECK(cls.IsStackSlot()) << cls;
5949 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5950 }
5951 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005952 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 break;
5954 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005955
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005957 NearLabel done;
5958 // Avoid null check if we know obj is not null.
5959 if (instruction->MustDoNullCheck()) {
5960 __ testl(obj, obj);
5961 __ j(kEqual, &done);
5962 }
5963
5964 // /* HeapReference<Class> */ temp = obj->klass_
5965 GenerateReferenceLoadTwoRegisters(
5966 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5967
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005969 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 __ Bind(&loop);
5971 if (cls.IsRegister()) {
5972 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5973 } else {
5974 DCHECK(cls.IsStackSlot()) << cls;
5975 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5976 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005977 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978
Roland Levillain0d5a2812015-11-13 10:07:31 +00005979 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005980 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981
5982 // If the class reference currently in `temp` is not null, jump
5983 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005984 __ testl(temp, temp);
5985 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 // Otherwise, jump to the slow path to throw the exception.
5987 //
5988 // But before, move back the object's class into `temp` before
5989 // going into the slow path, as it has been overwritten in the
5990 // meantime.
5991 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005992 GenerateReferenceLoadTwoRegisters(
5993 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005995 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 break;
5997 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006000 // We cannot use a NearLabel here, as its range might be too
6001 // short in some cases when read barriers are enabled. This has
6002 // been observed for instance when the code emitted for this
6003 // case uses high x86-64 registers (R8-R15).
6004 Label done;
6005 // Avoid null check if we know obj is not null.
6006 if (instruction->MustDoNullCheck()) {
6007 __ testl(obj, obj);
6008 __ j(kEqual, &done);
6009 }
6010
6011 // /* HeapReference<Class> */ temp = obj->klass_
6012 GenerateReferenceLoadTwoRegisters(
6013 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6014
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006015 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006017 if (cls.IsRegister()) {
6018 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6019 } else {
6020 DCHECK(cls.IsStackSlot()) << cls;
6021 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6022 }
6023 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006024
6025 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006026 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006027 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006028
6029 // If the component type is not null (i.e. the object is indeed
6030 // an array), jump to label `check_non_primitive_component_type`
6031 // to further check that this component type is not a primitive
6032 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006034 __ j(kNotEqual, &check_non_primitive_component_type);
6035 // Otherwise, jump to the slow path to throw the exception.
6036 //
6037 // But before, move back the object's class into `temp` before
6038 // going into the slow path, as it has been overwritten in the
6039 // meantime.
6040 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006041 GenerateReferenceLoadTwoRegisters(
6042 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 __ jmp(type_check_slow_path->GetEntryLabel());
6044
6045 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006046 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006047 __ j(kEqual, &done);
6048 // Same comment as above regarding `temp` and the slow path.
6049 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006050 GenerateReferenceLoadTwoRegisters(
6051 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006052 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006053 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006054 break;
6055 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056
Calin Juravle98893e12015-10-02 21:05:03 +01006057 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006058 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006059 NearLabel done;
6060 // Avoid null check if we know obj is not null.
6061 if (instruction->MustDoNullCheck()) {
6062 __ testl(obj, obj);
6063 __ j(kEqual, &done);
6064 }
6065
6066 // /* HeapReference<Class> */ temp = obj->klass_
6067 GenerateReferenceLoadTwoRegisters(
6068 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6069
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006070 // We always go into the type check slow path for the unresolved
6071 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006072 //
6073 // We cannot directly call the CheckCast runtime entry point
6074 // without resorting to a type checking slow path here (i.e. by
6075 // calling InvokeRuntime directly), as it would require to
6076 // assign fixed registers for the inputs of this HInstanceOf
6077 // instruction (following the runtime calling convention), which
6078 // might be cluttered by the potential first read barrier
6079 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006080 //
6081 // TODO: Introduce a new runtime entry point taking the object
6082 // to test (instead of its class) as argument, and let it deal
6083 // with the read barrier issues. This will let us refactor this
6084 // case of the `switch` code as it was previously (with a direct
6085 // call to the runtime not using a type checking slow path).
6086 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006088 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 break;
6090 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006091
Roland Levillain0d5a2812015-11-13 10:07:31 +00006092 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006093}
6094
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006095void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6096 LocationSummary* locations =
6097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6098 InvokeRuntimeCallingConvention calling_convention;
6099 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6100}
6101
6102void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006103 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6104 : QUICK_ENTRY_POINT(pUnlockObject),
6105 instruction,
6106 instruction->GetDexPc(),
6107 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006108 if (instruction->IsEnter()) {
6109 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6110 } else {
6111 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6112 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006113}
6114
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006115void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6116void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6117void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6118
6119void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6120 LocationSummary* locations =
6121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6122 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6123 || instruction->GetResultType() == Primitive::kPrimLong);
6124 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006125 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006126 locations->SetOut(Location::SameAsFirstInput());
6127}
6128
6129void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6130 HandleBitwiseOperation(instruction);
6131}
6132
6133void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6134 HandleBitwiseOperation(instruction);
6135}
6136
6137void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6138 HandleBitwiseOperation(instruction);
6139}
6140
6141void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6142 LocationSummary* locations = instruction->GetLocations();
6143 Location first = locations->InAt(0);
6144 Location second = locations->InAt(1);
6145 DCHECK(first.Equals(locations->Out()));
6146
6147 if (instruction->GetResultType() == Primitive::kPrimInt) {
6148 if (second.IsRegister()) {
6149 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006150 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006151 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006152 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006153 } else {
6154 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006155 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006156 }
6157 } else if (second.IsConstant()) {
6158 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6159 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006160 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006161 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006162 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006163 } else {
6164 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006165 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006166 }
6167 } else {
6168 Address address(CpuRegister(RSP), second.GetStackIndex());
6169 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006170 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006171 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006172 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006173 } else {
6174 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006175 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006176 }
6177 }
6178 } else {
6179 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006180 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6181 bool second_is_constant = false;
6182 int64_t value = 0;
6183 if (second.IsConstant()) {
6184 second_is_constant = true;
6185 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006186 }
Mark Mendell40741f32015-04-20 22:10:34 -04006187 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006188
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006189 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006190 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006191 if (is_int32_value) {
6192 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6193 } else {
6194 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6195 }
6196 } else if (second.IsDoubleStackSlot()) {
6197 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006198 } else {
6199 __ andq(first_reg, second.AsRegister<CpuRegister>());
6200 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006201 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006202 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006203 if (is_int32_value) {
6204 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6205 } else {
6206 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6207 }
6208 } else if (second.IsDoubleStackSlot()) {
6209 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006210 } else {
6211 __ orq(first_reg, second.AsRegister<CpuRegister>());
6212 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006213 } else {
6214 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006215 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006216 if (is_int32_value) {
6217 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6218 } else {
6219 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6220 }
6221 } else if (second.IsDoubleStackSlot()) {
6222 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006223 } else {
6224 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6225 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006226 }
6227 }
6228}
6229
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006230void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6231 Location out,
6232 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006233 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006234 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6235 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006236 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006237 if (kUseBakerReadBarrier) {
6238 // Load with fast path based Baker's read barrier.
6239 // /* HeapReference<Object> */ out = *(out + offset)
6240 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006241 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006242 } else {
6243 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006244 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006245 // in the following move operation, as we will need it for the
6246 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006247 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006248 // /* HeapReference<Object> */ out = *(out + offset)
6249 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006250 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006251 }
6252 } else {
6253 // Plain load with no read barrier.
6254 // /* HeapReference<Object> */ out = *(out + offset)
6255 __ movl(out_reg, Address(out_reg, offset));
6256 __ MaybeUnpoisonHeapReference(out_reg);
6257 }
6258}
6259
6260void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6261 Location out,
6262 Location obj,
6263 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006264 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006265 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6266 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6267 if (kEmitCompilerReadBarrier) {
6268 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006269 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006270 // Load with fast path based Baker's read barrier.
6271 // /* HeapReference<Object> */ out = *(obj + offset)
6272 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006273 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006274 } else {
6275 // Load with slow path based read barrier.
6276 // /* HeapReference<Object> */ out = *(obj + offset)
6277 __ movl(out_reg, Address(obj_reg, offset));
6278 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6279 }
6280 } else {
6281 // Plain load with no read barrier.
6282 // /* HeapReference<Object> */ out = *(obj + offset)
6283 __ movl(out_reg, Address(obj_reg, offset));
6284 __ MaybeUnpoisonHeapReference(out_reg);
6285 }
6286}
6287
6288void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6289 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006290 const Address& address,
6291 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006292 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6293 if (kEmitCompilerReadBarrier) {
6294 if (kUseBakerReadBarrier) {
6295 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6296 // Baker's read barrier are used:
6297 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006298 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006299 // if (Thread::Current()->GetIsGcMarking()) {
6300 // root = ReadBarrier::Mark(root)
6301 // }
6302
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006303 // /* GcRoot<mirror::Object> */ root = *address
6304 __ movl(root_reg, address);
6305 if (fixup_label != nullptr) {
6306 __ Bind(fixup_label);
6307 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006308 static_assert(
6309 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6310 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6311 "have different sizes.");
6312 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6313 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6314 "have different sizes.");
6315
6316 // Slow path used to mark the GC root `root`.
6317 SlowPathCode* slow_path =
6318 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6319 codegen_->AddSlowPath(slow_path);
6320
6321 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6322 /* no_rip */ true),
6323 Immediate(0));
6324 __ j(kNotEqual, slow_path->GetEntryLabel());
6325 __ Bind(slow_path->GetExitLabel());
6326 } else {
6327 // GC root loaded through a slow path for read barriers other
6328 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006329 // /* GcRoot<mirror::Object>* */ root = address
6330 __ leaq(root_reg, address);
6331 if (fixup_label != nullptr) {
6332 __ Bind(fixup_label);
6333 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006334 // /* mirror::Object* */ root = root->Read()
6335 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6336 }
6337 } else {
6338 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006339 // /* GcRoot<mirror::Object> */ root = *address
6340 __ movl(root_reg, address);
6341 if (fixup_label != nullptr) {
6342 __ Bind(fixup_label);
6343 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006344 // Note that GC roots are not affected by heap poisoning, thus we
6345 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006346 }
6347}
6348
6349void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6350 Location ref,
6351 CpuRegister obj,
6352 uint32_t offset,
6353 Location temp,
6354 bool needs_null_check) {
6355 DCHECK(kEmitCompilerReadBarrier);
6356 DCHECK(kUseBakerReadBarrier);
6357
6358 // /* HeapReference<Object> */ ref = *(obj + offset)
6359 Address src(obj, offset);
6360 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6361}
6362
6363void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6364 Location ref,
6365 CpuRegister obj,
6366 uint32_t data_offset,
6367 Location index,
6368 Location temp,
6369 bool needs_null_check) {
6370 DCHECK(kEmitCompilerReadBarrier);
6371 DCHECK(kUseBakerReadBarrier);
6372
6373 // /* HeapReference<Object> */ ref =
6374 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6375 Address src = index.IsConstant() ?
6376 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6377 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6378 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6379}
6380
6381void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6382 Location ref,
6383 CpuRegister obj,
6384 const Address& src,
6385 Location temp,
6386 bool needs_null_check) {
6387 DCHECK(kEmitCompilerReadBarrier);
6388 DCHECK(kUseBakerReadBarrier);
6389
6390 // In slow path based read barriers, the read barrier call is
6391 // inserted after the original load. However, in fast path based
6392 // Baker's read barriers, we need to perform the load of
6393 // mirror::Object::monitor_ *before* the original reference load.
6394 // This load-load ordering is required by the read barrier.
6395 // The fast path/slow path (for Baker's algorithm) should look like:
6396 //
6397 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6398 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6399 // HeapReference<Object> ref = *src; // Original reference load.
6400 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6401 // if (is_gray) {
6402 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6403 // }
6404 //
6405 // Note: the original implementation in ReadBarrier::Barrier is
6406 // slightly more complex as:
6407 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006408 // the high-bits of rb_state, which are expected to be all zeroes
6409 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6410 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006411 // - it performs additional checks that we do not do here for
6412 // performance reasons.
6413
6414 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6415 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6416 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6417
6418 // /* int32_t */ monitor = obj->monitor_
6419 __ movl(temp_reg, Address(obj, monitor_offset));
6420 if (needs_null_check) {
6421 MaybeRecordImplicitNullCheck(instruction);
6422 }
6423 // /* LockWord */ lock_word = LockWord(monitor)
6424 static_assert(sizeof(LockWord) == sizeof(int32_t),
6425 "art::LockWord and int32_t have different sizes.");
6426 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6427 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6428 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6429 static_assert(
6430 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6431 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6432
6433 // Load fence to prevent load-load reordering.
6434 // Note that this is a no-op, thanks to the x86-64 memory model.
6435 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6436
6437 // The actual reference load.
6438 // /* HeapReference<Object> */ ref = *src
6439 __ movl(ref_reg, src);
6440
6441 // Object* ref = ref_addr->AsMirrorPtr()
6442 __ MaybeUnpoisonHeapReference(ref_reg);
6443
6444 // Slow path used to mark the object `ref` when it is gray.
6445 SlowPathCode* slow_path =
6446 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6447 AddSlowPath(slow_path);
6448
6449 // if (rb_state == ReadBarrier::gray_ptr_)
6450 // ref = ReadBarrier::Mark(ref);
6451 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6452 __ j(kEqual, slow_path->GetEntryLabel());
6453 __ Bind(slow_path->GetExitLabel());
6454}
6455
6456void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6457 Location out,
6458 Location ref,
6459 Location obj,
6460 uint32_t offset,
6461 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006462 DCHECK(kEmitCompilerReadBarrier);
6463
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006464 // Insert a slow path based read barrier *after* the reference load.
6465 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466 // If heap poisoning is enabled, the unpoisoning of the loaded
6467 // reference will be carried out by the runtime within the slow
6468 // path.
6469 //
6470 // Note that `ref` currently does not get unpoisoned (when heap
6471 // poisoning is enabled), which is alright as the `ref` argument is
6472 // not used by the artReadBarrierSlow entry point.
6473 //
6474 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6475 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6476 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6477 AddSlowPath(slow_path);
6478
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479 __ jmp(slow_path->GetEntryLabel());
6480 __ Bind(slow_path->GetExitLabel());
6481}
6482
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006483void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6484 Location out,
6485 Location ref,
6486 Location obj,
6487 uint32_t offset,
6488 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006490 // Baker's read barriers shall be handled by the fast path
6491 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6492 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006493 // If heap poisoning is enabled, unpoisoning will be taken care of
6494 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006495 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496 } else if (kPoisonHeapReferences) {
6497 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6498 }
6499}
6500
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006501void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6502 Location out,
6503 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504 DCHECK(kEmitCompilerReadBarrier);
6505
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006506 // Insert a slow path based read barrier *after* the GC root load.
6507 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 // Note that GC roots are not affected by heap poisoning, so we do
6509 // not need to do anything special for this here.
6510 SlowPathCode* slow_path =
6511 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6512 AddSlowPath(slow_path);
6513
Roland Levillain0d5a2812015-11-13 10:07:31 +00006514 __ jmp(slow_path->GetEntryLabel());
6515 __ Bind(slow_path->GetExitLabel());
6516}
6517
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006518void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006519 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006520 LOG(FATAL) << "Unreachable";
6521}
6522
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006523void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006524 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006525 LOG(FATAL) << "Unreachable";
6526}
6527
Mark Mendellfe57faa2015-09-18 09:26:15 -04006528// Simple implementation of packed switch - generate cascaded compare/jumps.
6529void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6530 LocationSummary* locations =
6531 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6532 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006533 locations->AddTemp(Location::RequiresRegister());
6534 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006535}
6536
6537void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6538 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006539 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006540 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006541 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6542 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6543 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006544 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6545
6546 // Should we generate smaller inline compare/jumps?
6547 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6548 // Figure out the correct compare values and jump conditions.
6549 // Handle the first compare/branch as a special case because it might
6550 // jump to the default case.
6551 DCHECK_GT(num_entries, 2u);
6552 Condition first_condition;
6553 uint32_t index;
6554 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6555 if (lower_bound != 0) {
6556 first_condition = kLess;
6557 __ cmpl(value_reg_in, Immediate(lower_bound));
6558 __ j(first_condition, codegen_->GetLabelOf(default_block));
6559 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6560
6561 index = 1;
6562 } else {
6563 // Handle all the compare/jumps below.
6564 first_condition = kBelow;
6565 index = 0;
6566 }
6567
6568 // Handle the rest of the compare/jumps.
6569 for (; index + 1 < num_entries; index += 2) {
6570 int32_t compare_to_value = lower_bound + index + 1;
6571 __ cmpl(value_reg_in, Immediate(compare_to_value));
6572 // Jump to successors[index] if value < case_value[index].
6573 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6574 // Jump to successors[index + 1] if value == case_value[index + 1].
6575 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6576 }
6577
6578 if (index != num_entries) {
6579 // There are an odd number of entries. Handle the last one.
6580 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006581 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006582 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6583 }
6584
6585 // And the default for any other value.
6586 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6587 __ jmp(codegen_->GetLabelOf(default_block));
6588 }
6589 return;
6590 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006591
6592 // Remove the bias, if needed.
6593 Register value_reg_out = value_reg_in.AsRegister();
6594 if (lower_bound != 0) {
6595 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6596 value_reg_out = temp_reg.AsRegister();
6597 }
6598 CpuRegister value_reg(value_reg_out);
6599
6600 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006601 __ cmpl(value_reg, Immediate(num_entries - 1));
6602 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006603
Mark Mendell9c86b482015-09-18 13:36:07 -04006604 // We are in the range of the table.
6605 // Load the address of the jump table in the constant area.
6606 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006607
Mark Mendell9c86b482015-09-18 13:36:07 -04006608 // Load the (signed) offset from the jump table.
6609 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6610
6611 // Add the offset to the address of the table base.
6612 __ addq(temp_reg, base_reg);
6613
6614 // And jump.
6615 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006616}
6617
Aart Bikc5d47542016-01-27 17:00:35 -08006618void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6619 if (value == 0) {
6620 __ xorl(dest, dest);
6621 } else {
6622 __ movl(dest, Immediate(value));
6623 }
6624}
6625
Mark Mendell92e83bf2015-05-07 11:25:03 -04006626void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6627 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006628 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006629 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006630 } else if (IsUint<32>(value)) {
6631 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006632 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6633 } else {
6634 __ movq(dest, Immediate(value));
6635 }
6636}
6637
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006638void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6639 if (value == 0) {
6640 __ xorps(dest, dest);
6641 } else {
6642 __ movss(dest, LiteralInt32Address(value));
6643 }
6644}
6645
6646void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6647 if (value == 0) {
6648 __ xorpd(dest, dest);
6649 } else {
6650 __ movsd(dest, LiteralInt64Address(value));
6651 }
6652}
6653
6654void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6655 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6656}
6657
6658void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6659 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6660}
6661
Aart Bika19616e2016-02-01 18:57:58 -08006662void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6663 if (value == 0) {
6664 __ testl(dest, dest);
6665 } else {
6666 __ cmpl(dest, Immediate(value));
6667 }
6668}
6669
6670void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6671 if (IsInt<32>(value)) {
6672 if (value == 0) {
6673 __ testq(dest, dest);
6674 } else {
6675 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6676 }
6677 } else {
6678 // Value won't fit in an int.
6679 __ cmpq(dest, LiteralInt64Address(value));
6680 }
6681}
6682
Mark Mendellcfa410b2015-05-25 16:02:44 -04006683void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6684 DCHECK(dest.IsDoubleStackSlot());
6685 if (IsInt<32>(value)) {
6686 // Can move directly as an int32 constant.
6687 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6688 Immediate(static_cast<int32_t>(value)));
6689 } else {
6690 Load64BitValue(CpuRegister(TMP), value);
6691 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6692 }
6693}
6694
Mark Mendell9c86b482015-09-18 13:36:07 -04006695/**
6696 * Class to handle late fixup of offsets into constant area.
6697 */
6698class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6699 public:
6700 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6701 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6702
6703 protected:
6704 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6705
6706 CodeGeneratorX86_64* codegen_;
6707
6708 private:
6709 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6710 // Patch the correct offset for the instruction. We use the address of the
6711 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6712 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6713 int32_t relative_position = constant_offset - pos;
6714
6715 // Patch in the right value.
6716 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6717 }
6718
6719 // Location in constant area that the fixup refers to.
6720 size_t offset_into_constant_area_;
6721};
6722
6723/**
6724 t * Class to handle late fixup of offsets to a jump table that will be created in the
6725 * constant area.
6726 */
6727class JumpTableRIPFixup : public RIPFixup {
6728 public:
6729 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6730 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6731
6732 void CreateJumpTable() {
6733 X86_64Assembler* assembler = codegen_->GetAssembler();
6734
6735 // Ensure that the reference to the jump table has the correct offset.
6736 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6737 SetOffset(offset_in_constant_table);
6738
6739 // Compute the offset from the start of the function to this jump table.
6740 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6741
6742 // Populate the jump table with the correct values for the jump table.
6743 int32_t num_entries = switch_instr_->GetNumEntries();
6744 HBasicBlock* block = switch_instr_->GetBlock();
6745 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6746 // The value that we want is the target offset - the position of the table.
6747 for (int32_t i = 0; i < num_entries; i++) {
6748 HBasicBlock* b = successors[i];
6749 Label* l = codegen_->GetLabelOf(b);
6750 DCHECK(l->IsBound());
6751 int32_t offset_to_block = l->Position() - current_table_offset;
6752 assembler->AppendInt32(offset_to_block);
6753 }
6754 }
6755
6756 private:
6757 const HPackedSwitch* switch_instr_;
6758};
6759
Mark Mendellf55c3e02015-03-26 21:07:46 -04006760void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6761 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006762 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006763 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6764 // 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 -04006765 assembler->Align(4, 0);
6766 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006767
6768 // Populate any jump tables.
6769 for (auto jump_table : fixups_to_jump_tables_) {
6770 jump_table->CreateJumpTable();
6771 }
6772
6773 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006774 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006775 }
6776
6777 // And finish up.
6778 CodeGenerator::Finalize(allocator);
6779}
6780
Mark Mendellf55c3e02015-03-26 21:07:46 -04006781Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6782 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6783 return Address::RIP(fixup);
6784}
6785
6786Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6787 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6788 return Address::RIP(fixup);
6789}
6790
6791Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6792 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6793 return Address::RIP(fixup);
6794}
6795
6796Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6797 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6798 return Address::RIP(fixup);
6799}
6800
Andreas Gampe85b62f22015-09-09 13:15:38 -07006801// TODO: trg as memory.
6802void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6803 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006804 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006805 return;
6806 }
6807
6808 DCHECK_NE(type, Primitive::kPrimVoid);
6809
6810 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6811 if (trg.Equals(return_loc)) {
6812 return;
6813 }
6814
6815 // Let the parallel move resolver take care of all of this.
6816 HParallelMove parallel_move(GetGraph()->GetArena());
6817 parallel_move.AddMove(return_loc, trg, type, nullptr);
6818 GetMoveResolver()->EmitNativeCode(&parallel_move);
6819}
6820
Mark Mendell9c86b482015-09-18 13:36:07 -04006821Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6822 // Create a fixup to be used to create and address the jump table.
6823 JumpTableRIPFixup* table_fixup =
6824 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6825
6826 // We have to populate the jump tables.
6827 fixups_to_jump_tables_.push_back(table_fixup);
6828 return Address::RIP(table_fixup);
6829}
6830
Mark Mendellea5af682015-10-22 17:35:49 -04006831void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6832 const Address& addr_high,
6833 int64_t v,
6834 HInstruction* instruction) {
6835 if (IsInt<32>(v)) {
6836 int32_t v_32 = v;
6837 __ movq(addr_low, Immediate(v_32));
6838 MaybeRecordImplicitNullCheck(instruction);
6839 } else {
6840 // Didn't fit in a register. Do it in pieces.
6841 int32_t low_v = Low32Bits(v);
6842 int32_t high_v = High32Bits(v);
6843 __ movl(addr_low, Immediate(low_v));
6844 MaybeRecordImplicitNullCheck(instruction);
6845 __ movl(addr_high, Immediate(high_v));
6846 }
6847}
6848
Roland Levillain4d027112015-07-01 15:41:14 +01006849#undef __
6850
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006851} // namespace x86_64
6852} // namespace art