blob: 51bc8c204a1bc646be4ed3f76c36695d692b0805 [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;
787 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000788 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
789 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000790 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000791 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko58155012015-08-19 12:49:41 +0000792 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000793 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000794 break;
795 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 Marko58155012015-08-19 12:49:41 +0000876void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
877 DCHECK(linker_patches->empty());
878 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000879 method_patches_.size() +
880 relative_call_patches_.size() +
881 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000882 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000883 // The label points to the end of the "movl" insn but the literal offset for method
884 // patch needs to point to the embedded constant which occupies the last 4 bytes.
885 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000886 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000887 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000888 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
889 info.target_method.dex_file,
890 info.target_method.dex_method_index));
891 }
892 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000893 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000894 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
895 info.target_method.dex_file,
896 info.target_method.dex_method_index));
897 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000898 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
899 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000900 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
901 &info.target_dex_file,
902 info.label.Position(),
903 info.element_offset));
904 }
905}
906
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100907void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100908 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100909}
910
911void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100912 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100913}
914
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100915size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
916 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
917 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100918}
919
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100920size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
921 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
922 return kX86_64WordSize;
923}
924
925size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
926 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
927 return kX86_64WordSize;
928}
929
930size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
931 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
932 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100933}
934
Calin Juravle175dc732015-08-25 15:42:32 +0100935void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
936 HInstruction* instruction,
937 uint32_t dex_pc,
938 SlowPathCode* slow_path) {
939 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
940 instruction,
941 dex_pc,
942 slow_path);
943}
944
945void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100946 HInstruction* instruction,
947 uint32_t dex_pc,
948 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100949 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000950 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100951 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100952}
953
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000954static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000955// Use a fake return address register to mimic Quick.
956static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400957CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000958 const X86_64InstructionSetFeatures& isa_features,
959 const CompilerOptions& compiler_options,
960 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000961 : CodeGenerator(graph,
962 kNumberOfCpuRegisters,
963 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000964 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000965 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
966 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000967 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000968 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
969 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100970 compiler_options,
971 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100972 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100973 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000974 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400975 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400976 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000977 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100978 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
979 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000980 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400981 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000982 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
983}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100985InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
986 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800987 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100988 assembler_(codegen->GetAssembler()),
989 codegen_(codegen) {}
990
David Brazdil58282f42016-01-14 12:45:10 +0000991void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100993 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100994
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000995 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100996 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100997}
998
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100999static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001000 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001001}
David Srbecky9d8606d2015-04-12 09:35:32 +01001002
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001003static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001004 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001005}
1006
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001008 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001009 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001010 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001011 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001012 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001013
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001014 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001015 __ testq(CpuRegister(RAX), Address(
1016 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001018 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001019
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001020 if (HasEmptyFrame()) {
1021 return;
1022 }
1023
Nicolas Geoffray98893962015-01-21 12:32:32 +00001024 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001025 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001026 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001027 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001028 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1029 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001030 }
1031 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001032
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001033 int adjust = GetFrameSize() - GetCoreSpillSize();
1034 __ subq(CpuRegister(RSP), Immediate(adjust));
1035 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001036 uint32_t xmm_spill_location = GetFpuSpillStart();
1037 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001038
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001039 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1040 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001041 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1042 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1043 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001045 }
1046
Mathieu Chartiere401d142015-04-22 13:56:20 -07001047 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001048 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001049}
1050
1051void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001052 __ cfi().RememberState();
1053 if (!HasEmptyFrame()) {
1054 uint32_t xmm_spill_location = GetFpuSpillStart();
1055 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1056 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1057 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1058 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1059 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1060 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1061 }
1062 }
1063
1064 int adjust = GetFrameSize() - GetCoreSpillSize();
1065 __ addq(CpuRegister(RSP), Immediate(adjust));
1066 __ cfi().AdjustCFAOffset(-adjust);
1067
1068 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1069 Register reg = kCoreCalleeSaves[i];
1070 if (allocated_registers_.ContainsCoreRegister(reg)) {
1071 __ popq(CpuRegister(reg));
1072 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1073 __ cfi().Restore(DWARFReg(reg));
1074 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001075 }
1076 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001077 __ ret();
1078 __ cfi().RestoreState();
1079 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080}
1081
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001082void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1083 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084}
1085
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1087 switch (load->GetType()) {
1088 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091
1092 case Primitive::kPrimInt:
1093 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001096
1097 case Primitive::kPrimBoolean:
1098 case Primitive::kPrimByte:
1099 case Primitive::kPrimChar:
1100 case Primitive::kPrimShort:
1101 case Primitive::kPrimVoid:
1102 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001103 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104 }
1105
1106 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001107 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108}
1109
1110void CodeGeneratorX86_64::Move(Location destination, Location source) {
1111 if (source.Equals(destination)) {
1112 return;
1113 }
1114 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001115 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001117 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001118 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001119 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001120 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001121 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1122 } else if (source.IsConstant()) {
1123 HConstant* constant = source.GetConstant();
1124 if (constant->IsLongConstant()) {
1125 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1126 } else {
1127 Load32BitValue(dest, GetInt32ValueOf(constant));
1128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 } else {
1130 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001131 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 }
1133 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001134 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001135 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001136 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001138 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1139 } else if (source.IsConstant()) {
1140 HConstant* constant = source.GetConstant();
1141 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1142 if (constant->IsFloatConstant()) {
1143 Load32BitValue(dest, static_cast<int32_t>(value));
1144 } else {
1145 Load64BitValue(dest, value);
1146 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001148 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 } else {
1150 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001151 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152 }
1153 } else if (destination.IsStackSlot()) {
1154 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001156 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 } else if (source.IsFpuRegister()) {
1158 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001159 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001160 } else if (source.IsConstant()) {
1161 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001162 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001163 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001164 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001165 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001166 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1167 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168 }
1169 } else {
1170 DCHECK(destination.IsDoubleStackSlot());
1171 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001173 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 } else if (source.IsFpuRegister()) {
1175 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001176 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001177 } else if (source.IsConstant()) {
1178 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001179 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001180 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001181 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001182 } else {
1183 DCHECK(constant->IsLongConstant());
1184 value = constant->AsLongConstant()->GetValue();
1185 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001186 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187 } else {
1188 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001189 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1190 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191 }
1192 }
1193}
1194
Calin Juravle175dc732015-08-25 15:42:32 +01001195void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1196 DCHECK(location.IsRegister());
1197 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1198}
1199
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200void CodeGeneratorX86_64::MoveLocation(
1201 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1202 Move(dst, src);
1203}
1204
1205void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1206 if (location.IsRegister()) {
1207 locations->AddTemp(location);
1208 } else {
1209 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1210 }
1211}
1212
David Brazdilfc6a86a2015-06-26 10:33:45 +00001213void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001214 DCHECK(!successor->IsExitBlock());
1215
1216 HBasicBlock* block = got->GetBlock();
1217 HInstruction* previous = got->GetPrevious();
1218
1219 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001220 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001221 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1222 return;
1223 }
1224
1225 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1226 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1227 }
1228 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001229 __ jmp(codegen_->GetLabelOf(successor));
1230 }
1231}
1232
David Brazdilfc6a86a2015-06-26 10:33:45 +00001233void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1234 got->SetLocations(nullptr);
1235}
1236
1237void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1238 HandleGoto(got, got->GetSuccessor());
1239}
1240
1241void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1242 try_boundary->SetLocations(nullptr);
1243}
1244
1245void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1246 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1247 if (!successor->IsExitBlock()) {
1248 HandleGoto(try_boundary, successor);
1249 }
1250}
1251
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001252void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1253 exit->SetLocations(nullptr);
1254}
1255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001256void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001257}
1258
Mark Mendell152408f2015-12-31 12:28:50 -05001259template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001260void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001261 LabelType* true_label,
1262 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001263 if (cond->IsFPConditionTrueIfNaN()) {
1264 __ j(kUnordered, true_label);
1265 } else if (cond->IsFPConditionFalseIfNaN()) {
1266 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001267 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001268 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001269}
1270
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001271void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001272 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001273
Mark Mendellc4701932015-04-10 13:18:51 -04001274 Location left = locations->InAt(0);
1275 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 Primitive::Type type = condition->InputAt(0)->GetType();
1277 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001278 case Primitive::kPrimBoolean:
1279 case Primitive::kPrimByte:
1280 case Primitive::kPrimChar:
1281 case Primitive::kPrimShort:
1282 case Primitive::kPrimInt:
1283 case Primitive::kPrimNot: {
1284 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1285 if (right.IsConstant()) {
1286 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1287 if (value == 0) {
1288 __ testl(left_reg, left_reg);
1289 } else {
1290 __ cmpl(left_reg, Immediate(value));
1291 }
1292 } else if (right.IsStackSlot()) {
1293 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1294 } else {
1295 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1296 }
1297 break;
1298 }
Mark Mendellc4701932015-04-10 13:18:51 -04001299 case Primitive::kPrimLong: {
1300 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1301 if (right.IsConstant()) {
1302 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001303 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001304 } else if (right.IsDoubleStackSlot()) {
1305 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1306 } else {
1307 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1308 }
Mark Mendellc4701932015-04-10 13:18:51 -04001309 break;
1310 }
1311 case Primitive::kPrimFloat: {
1312 if (right.IsFpuRegister()) {
1313 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1314 } else if (right.IsConstant()) {
1315 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1316 codegen_->LiteralFloatAddress(
1317 right.GetConstant()->AsFloatConstant()->GetValue()));
1318 } else {
1319 DCHECK(right.IsStackSlot());
1320 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1321 Address(CpuRegister(RSP), right.GetStackIndex()));
1322 }
Mark Mendellc4701932015-04-10 13:18:51 -04001323 break;
1324 }
1325 case Primitive::kPrimDouble: {
1326 if (right.IsFpuRegister()) {
1327 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1328 } else if (right.IsConstant()) {
1329 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1330 codegen_->LiteralDoubleAddress(
1331 right.GetConstant()->AsDoubleConstant()->GetValue()));
1332 } else {
1333 DCHECK(right.IsDoubleStackSlot());
1334 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1335 Address(CpuRegister(RSP), right.GetStackIndex()));
1336 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001337 break;
1338 }
1339 default:
1340 LOG(FATAL) << "Unexpected condition type " << type;
1341 }
1342}
1343
1344template<class LabelType>
1345void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1346 LabelType* true_target_in,
1347 LabelType* false_target_in) {
1348 // Generated branching requires both targets to be explicit. If either of the
1349 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1350 LabelType fallthrough_target;
1351 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1352 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1353
1354 // Generate the comparison to set the CC.
1355 GenerateCompareTest(condition);
1356
1357 // Now generate the correct jump(s).
1358 Primitive::Type type = condition->InputAt(0)->GetType();
1359 switch (type) {
1360 case Primitive::kPrimLong: {
1361 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1362 break;
1363 }
1364 case Primitive::kPrimFloat: {
1365 GenerateFPJumps(condition, true_target, false_target);
1366 break;
1367 }
1368 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001369 GenerateFPJumps(condition, true_target, false_target);
1370 break;
1371 }
1372 default:
1373 LOG(FATAL) << "Unexpected condition type " << type;
1374 }
1375
David Brazdil0debae72015-11-12 18:37:00 +00001376 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001377 __ jmp(false_target);
1378 }
David Brazdil0debae72015-11-12 18:37:00 +00001379
1380 if (fallthrough_target.IsLinked()) {
1381 __ Bind(&fallthrough_target);
1382 }
Mark Mendellc4701932015-04-10 13:18:51 -04001383}
1384
David Brazdil0debae72015-11-12 18:37:00 +00001385static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1386 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1387 // are set only strictly before `branch`. We can't use the eflags on long
1388 // conditions if they are materialized due to the complex branching.
1389 return cond->IsCondition() &&
1390 cond->GetNext() == branch &&
1391 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1392}
1393
Mark Mendell152408f2015-12-31 12:28:50 -05001394template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001395void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001396 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001397 LabelType* true_target,
1398 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001399 HInstruction* cond = instruction->InputAt(condition_input_index);
1400
1401 if (true_target == nullptr && false_target == nullptr) {
1402 // Nothing to do. The code always falls through.
1403 return;
1404 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001405 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001406 if (cond->AsIntConstant()->IsOne()) {
1407 if (true_target != nullptr) {
1408 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001409 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001410 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001411 DCHECK(cond->AsIntConstant()->IsZero());
1412 if (false_target != nullptr) {
1413 __ jmp(false_target);
1414 }
1415 }
1416 return;
1417 }
1418
1419 // The following code generates these patterns:
1420 // (1) true_target == nullptr && false_target != nullptr
1421 // - opposite condition true => branch to false_target
1422 // (2) true_target != nullptr && false_target == nullptr
1423 // - condition true => branch to true_target
1424 // (3) true_target != nullptr && false_target != nullptr
1425 // - condition true => branch to true_target
1426 // - branch to false_target
1427 if (IsBooleanValueOrMaterializedCondition(cond)) {
1428 if (AreEflagsSetFrom(cond, instruction)) {
1429 if (true_target == nullptr) {
1430 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1431 } else {
1432 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1433 }
1434 } else {
1435 // Materialized condition, compare against 0.
1436 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1437 if (lhs.IsRegister()) {
1438 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1439 } else {
1440 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1441 }
1442 if (true_target == nullptr) {
1443 __ j(kEqual, false_target);
1444 } else {
1445 __ j(kNotEqual, true_target);
1446 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001447 }
1448 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001449 // Condition has not been materialized, use its inputs as the
1450 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001451 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001452
David Brazdil0debae72015-11-12 18:37:00 +00001453 // If this is a long or FP comparison that has been folded into
1454 // the HCondition, generate the comparison directly.
1455 Primitive::Type type = condition->InputAt(0)->GetType();
1456 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1457 GenerateCompareTestAndBranch(condition, true_target, false_target);
1458 return;
1459 }
1460
1461 Location lhs = condition->GetLocations()->InAt(0);
1462 Location rhs = condition->GetLocations()->InAt(1);
1463 if (rhs.IsRegister()) {
1464 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1465 } else if (rhs.IsConstant()) {
1466 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001467 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001468 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001469 __ cmpl(lhs.AsRegister<CpuRegister>(),
1470 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1471 }
1472 if (true_target == nullptr) {
1473 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1474 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001475 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001476 }
Dave Allison20dfc792014-06-16 20:44:29 -07001477 }
David Brazdil0debae72015-11-12 18:37:00 +00001478
1479 // If neither branch falls through (case 3), the conditional branch to `true_target`
1480 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1481 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001482 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001483 }
1484}
1485
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001486void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001487 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1488 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001489 locations->SetInAt(0, Location::Any());
1490 }
1491}
1492
1493void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001494 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1495 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1496 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1497 nullptr : codegen_->GetLabelOf(true_successor);
1498 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1499 nullptr : codegen_->GetLabelOf(false_successor);
1500 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001501}
1502
1503void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1504 LocationSummary* locations = new (GetGraph()->GetArena())
1505 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001506 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001507 locations->SetInAt(0, Location::Any());
1508 }
1509}
1510
1511void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001512 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001513 GenerateTestAndBranch<Label>(deoptimize,
1514 /* condition_input_index */ 0,
1515 slow_path->GetEntryLabel(),
1516 /* false_target */ nullptr);
1517}
1518
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001519static bool SelectCanUseCMOV(HSelect* select) {
1520 // There are no conditional move instructions for XMMs.
1521 if (Primitive::IsFloatingPointType(select->GetType())) {
1522 return false;
1523 }
1524
1525 // A FP condition doesn't generate the single CC that we need.
1526 HInstruction* condition = select->GetCondition();
1527 if (condition->IsCondition() &&
1528 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1529 return false;
1530 }
1531
1532 // We can generate a CMOV for this Select.
1533 return true;
1534}
1535
David Brazdil74eb1b22015-12-14 11:44:01 +00001536void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1538 if (Primitive::IsFloatingPointType(select->GetType())) {
1539 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001540 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001541 } else {
1542 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001543 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001544 if (select->InputAt(1)->IsConstant()) {
1545 locations->SetInAt(1, Location::RequiresRegister());
1546 } else {
1547 locations->SetInAt(1, Location::Any());
1548 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001549 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001550 locations->SetInAt(1, Location::Any());
1551 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001552 }
1553 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1554 locations->SetInAt(2, Location::RequiresRegister());
1555 }
1556 locations->SetOut(Location::SameAsFirstInput());
1557}
1558
1559void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1560 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001561 if (SelectCanUseCMOV(select)) {
1562 // If both the condition and the source types are integer, we can generate
1563 // a CMOV to implement Select.
1564 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001565 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001566 DCHECK(locations->InAt(0).Equals(locations->Out()));
1567
1568 HInstruction* select_condition = select->GetCondition();
1569 Condition cond = kNotEqual;
1570
1571 // Figure out how to test the 'condition'.
1572 if (select_condition->IsCondition()) {
1573 HCondition* condition = select_condition->AsCondition();
1574 if (!condition->IsEmittedAtUseSite()) {
1575 // This was a previously materialized condition.
1576 // Can we use the existing condition code?
1577 if (AreEflagsSetFrom(condition, select)) {
1578 // Materialization was the previous instruction. Condition codes are right.
1579 cond = X86_64IntegerCondition(condition->GetCondition());
1580 } else {
1581 // No, we have to recreate the condition code.
1582 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1583 __ testl(cond_reg, cond_reg);
1584 }
1585 } else {
1586 GenerateCompareTest(condition);
1587 cond = X86_64IntegerCondition(condition->GetCondition());
1588 }
1589 } else {
1590 // Must be a boolean condition, which needs to be compared to 0.
1591 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1592 __ testl(cond_reg, cond_reg);
1593 }
1594
1595 // If the condition is true, overwrite the output, which already contains false.
1596 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001597 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1598 if (value_true_loc.IsRegister()) {
1599 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1600 } else {
1601 __ cmov(cond,
1602 value_false,
1603 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1604 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001605 } else {
1606 NearLabel false_target;
1607 GenerateTestAndBranch<NearLabel>(select,
1608 /* condition_input_index */ 2,
1609 /* true_target */ nullptr,
1610 &false_target);
1611 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1612 __ Bind(&false_target);
1613 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001614}
1615
David Srbecky0cf44932015-12-09 14:09:59 +00001616void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1617 new (GetGraph()->GetArena()) LocationSummary(info);
1618}
1619
1620void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00001621 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
1622}
1623
1624void CodeGeneratorX86_64::GenerateNop() {
1625 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001626}
1627
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001628void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1629 local->SetLocations(nullptr);
1630}
1631
1632void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1633 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1634}
1635
1636void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1637 local->SetLocations(nullptr);
1638}
1639
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001640void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001641 // Nothing to do, this is driven by the code generator.
1642}
1643
1644void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001645 LocationSummary* locations =
1646 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001647 switch (store->InputAt(1)->GetType()) {
1648 case Primitive::kPrimBoolean:
1649 case Primitive::kPrimByte:
1650 case Primitive::kPrimChar:
1651 case Primitive::kPrimShort:
1652 case Primitive::kPrimInt:
1653 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001654 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001655 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1656 break;
1657
1658 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001659 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001660 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1661 break;
1662
1663 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001664 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001665 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001666}
1667
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001668void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001669}
1670
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001671void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001672 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001673 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001674 // Handle the long/FP comparisons made in instruction simplification.
1675 switch (cond->InputAt(0)->GetType()) {
1676 case Primitive::kPrimLong:
1677 locations->SetInAt(0, Location::RequiresRegister());
1678 locations->SetInAt(1, Location::Any());
1679 break;
1680 case Primitive::kPrimFloat:
1681 case Primitive::kPrimDouble:
1682 locations->SetInAt(0, Location::RequiresFpuRegister());
1683 locations->SetInAt(1, Location::Any());
1684 break;
1685 default:
1686 locations->SetInAt(0, Location::RequiresRegister());
1687 locations->SetInAt(1, Location::Any());
1688 break;
1689 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001690 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001691 locations->SetOut(Location::RequiresRegister());
1692 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001693}
1694
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001695void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001696 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001697 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001698 }
Mark Mendellc4701932015-04-10 13:18:51 -04001699
1700 LocationSummary* locations = cond->GetLocations();
1701 Location lhs = locations->InAt(0);
1702 Location rhs = locations->InAt(1);
1703 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001704 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001705
1706 switch (cond->InputAt(0)->GetType()) {
1707 default:
1708 // Integer case.
1709
1710 // Clear output register: setcc only sets the low byte.
1711 __ xorl(reg, reg);
1712
1713 if (rhs.IsRegister()) {
1714 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1715 } else if (rhs.IsConstant()) {
1716 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001717 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001718 } else {
1719 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1720 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001722 return;
1723 case Primitive::kPrimLong:
1724 // Clear output register: setcc only sets the low byte.
1725 __ xorl(reg, reg);
1726
1727 if (rhs.IsRegister()) {
1728 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1729 } else if (rhs.IsConstant()) {
1730 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001731 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001732 } else {
1733 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1734 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001735 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001736 return;
1737 case Primitive::kPrimFloat: {
1738 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1739 if (rhs.IsConstant()) {
1740 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1741 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1742 } else if (rhs.IsStackSlot()) {
1743 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1744 } else {
1745 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1746 }
1747 GenerateFPJumps(cond, &true_label, &false_label);
1748 break;
1749 }
1750 case Primitive::kPrimDouble: {
1751 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1752 if (rhs.IsConstant()) {
1753 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1754 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1755 } else if (rhs.IsDoubleStackSlot()) {
1756 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1757 } else {
1758 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1759 }
1760 GenerateFPJumps(cond, &true_label, &false_label);
1761 break;
1762 }
1763 }
1764
1765 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001766 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001767
Roland Levillain4fa13f62015-07-06 18:11:54 +01001768 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001769 __ Bind(&false_label);
1770 __ xorl(reg, reg);
1771 __ jmp(&done_label);
1772
Roland Levillain4fa13f62015-07-06 18:11:54 +01001773 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001774 __ Bind(&true_label);
1775 __ movl(reg, Immediate(1));
1776 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001813}
1814
1815void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001817}
1818
1819void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001821}
1822
1823void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001825}
1826
Aart Bike9f37602015-10-09 11:15:55 -07001827void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
1847void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001849}
1850
1851void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001853}
1854
1855void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001856 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001857}
1858
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001859void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001862 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001863 case Primitive::kPrimBoolean:
1864 case Primitive::kPrimByte:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001867 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001868 case Primitive::kPrimLong: {
1869 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001870 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001871 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1872 break;
1873 }
1874 case Primitive::kPrimFloat:
1875 case Primitive::kPrimDouble: {
1876 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001877 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001878 locations->SetOut(Location::RequiresRegister());
1879 break;
1880 }
1881 default:
1882 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1883 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001884}
1885
1886void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001887 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001888 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001889 Location left = locations->InAt(0);
1890 Location right = locations->InAt(1);
1891
Mark Mendell0c9497d2015-08-21 09:30:05 -04001892 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001893 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001894 Condition less_cond = kLess;
1895
Calin Juravleddb7df22014-11-25 20:56:51 +00001896 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001897 case Primitive::kPrimBoolean:
1898 case Primitive::kPrimByte:
1899 case Primitive::kPrimShort:
1900 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001901 case Primitive::kPrimInt: {
1902 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1903 if (right.IsConstant()) {
1904 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1905 codegen_->Compare32BitValue(left_reg, value);
1906 } else if (right.IsStackSlot()) {
1907 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1908 } else {
1909 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1910 }
1911 break;
1912 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001914 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1915 if (right.IsConstant()) {
1916 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001917 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001918 } else if (right.IsDoubleStackSlot()) {
1919 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001920 } else {
1921 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1922 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001923 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 }
1925 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001926 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1927 if (right.IsConstant()) {
1928 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1929 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1930 } else if (right.IsStackSlot()) {
1931 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1932 } else {
1933 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1934 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001935 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001936 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001937 break;
1938 }
1939 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001940 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1941 if (right.IsConstant()) {
1942 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1943 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1944 } else if (right.IsDoubleStackSlot()) {
1945 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1946 } else {
1947 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1948 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001949 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001950 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001951 break;
1952 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001953 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001954 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001955 }
Aart Bika19616e2016-02-01 18:57:58 -08001956
Calin Juravleddb7df22014-11-25 20:56:51 +00001957 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001958 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001959 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001960
Calin Juravle91debbc2014-11-26 19:01:09 +00001961 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001962 __ movl(out, Immediate(1));
1963 __ jmp(&done);
1964
1965 __ Bind(&less);
1966 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001967
1968 __ Bind(&done);
1969}
1970
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001972 LocationSummary* locations =
1973 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001974 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001975}
1976
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001977void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001978 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001979}
1980
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001981void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1982 LocationSummary* locations =
1983 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1984 locations->SetOut(Location::ConstantLocation(constant));
1985}
1986
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001987void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001988 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001989}
1990
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001991void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 LocationSummary* locations =
1993 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001994 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001995}
1996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001997void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001998 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001999}
2000
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002001void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2002 LocationSummary* locations =
2003 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2004 locations->SetOut(Location::ConstantLocation(constant));
2005}
2006
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002007void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002008 // Will be generated at use site.
2009}
2010
2011void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2012 LocationSummary* locations =
2013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2014 locations->SetOut(Location::ConstantLocation(constant));
2015}
2016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002017void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2018 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002019 // Will be generated at use site.
2020}
2021
Calin Juravle27df7582015-04-17 19:12:31 +01002022void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2023 memory_barrier->SetLocations(nullptr);
2024}
2025
2026void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002027 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002028}
2029
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2031 ret->SetLocations(nullptr);
2032}
2033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002035 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036}
2037
2038void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002039 LocationSummary* locations =
2040 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002041 switch (ret->InputAt(0)->GetType()) {
2042 case Primitive::kPrimBoolean:
2043 case Primitive::kPrimByte:
2044 case Primitive::kPrimChar:
2045 case Primitive::kPrimShort:
2046 case Primitive::kPrimInt:
2047 case Primitive::kPrimNot:
2048 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002050 break;
2051
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002052 case Primitive::kPrimFloat:
2053 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002054 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002055 break;
2056
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002057 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002058 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002060}
2061
2062void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2063 if (kIsDebugBuild) {
2064 switch (ret->InputAt(0)->GetType()) {
2065 case Primitive::kPrimBoolean:
2066 case Primitive::kPrimByte:
2067 case Primitive::kPrimChar:
2068 case Primitive::kPrimShort:
2069 case Primitive::kPrimInt:
2070 case Primitive::kPrimNot:
2071 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002072 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002073 break;
2074
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002075 case Primitive::kPrimFloat:
2076 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002078 XMM0);
2079 break;
2080
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002081 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002082 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002083 }
2084 }
2085 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002086}
2087
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002088Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2089 switch (type) {
2090 case Primitive::kPrimBoolean:
2091 case Primitive::kPrimByte:
2092 case Primitive::kPrimChar:
2093 case Primitive::kPrimShort:
2094 case Primitive::kPrimInt:
2095 case Primitive::kPrimNot:
2096 case Primitive::kPrimLong:
2097 return Location::RegisterLocation(RAX);
2098
2099 case Primitive::kPrimVoid:
2100 return Location::NoLocation();
2101
2102 case Primitive::kPrimDouble:
2103 case Primitive::kPrimFloat:
2104 return Location::FpuRegisterLocation(XMM0);
2105 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002106
2107 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002108}
2109
2110Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2111 return Location::RegisterLocation(kMethodRegisterArgument);
2112}
2113
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002114Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002115 switch (type) {
2116 case Primitive::kPrimBoolean:
2117 case Primitive::kPrimByte:
2118 case Primitive::kPrimChar:
2119 case Primitive::kPrimShort:
2120 case Primitive::kPrimInt:
2121 case Primitive::kPrimNot: {
2122 uint32_t index = gp_index_++;
2123 stack_index_++;
2124 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002125 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002126 } else {
2127 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2128 }
2129 }
2130
2131 case Primitive::kPrimLong: {
2132 uint32_t index = gp_index_;
2133 stack_index_ += 2;
2134 if (index < calling_convention.GetNumberOfRegisters()) {
2135 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002136 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002137 } else {
2138 gp_index_ += 2;
2139 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2140 }
2141 }
2142
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002144 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002145 stack_index_++;
2146 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002147 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002148 } else {
2149 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2150 }
2151 }
2152
2153 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002154 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002155 stack_index_ += 2;
2156 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002157 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002158 } else {
2159 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2160 }
2161 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002162
2163 case Primitive::kPrimVoid:
2164 LOG(FATAL) << "Unexpected parameter type " << type;
2165 break;
2166 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002167 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002168}
2169
Calin Juravle175dc732015-08-25 15:42:32 +01002170void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2171 // The trampoline uses the same calling convention as dex calling conventions,
2172 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2173 // the method_idx.
2174 HandleInvoke(invoke);
2175}
2176
2177void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2178 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2179}
2180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002181void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002182 // Explicit clinit checks triggered by static invokes must have been pruned by
2183 // art::PrepareForRegisterAllocation.
2184 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002185
Mark Mendellfb8d2792015-03-31 22:16:59 -04002186 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002187 if (intrinsic.TryDispatch(invoke)) {
2188 return;
2189 }
2190
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002191 HandleInvoke(invoke);
2192}
2193
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2195 if (invoke->GetLocations()->Intrinsified()) {
2196 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2197 intrinsic.Dispatch(invoke);
2198 return true;
2199 }
2200 return false;
2201}
2202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002203void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002204 // Explicit clinit checks triggered by static invokes must have been pruned by
2205 // art::PrepareForRegisterAllocation.
2206 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002207
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002208 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2209 return;
2210 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002211
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002212 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002213 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002214 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002215 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002216}
2217
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002218void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002219 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002220 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002221}
2222
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002223void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002224 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002225 if (intrinsic.TryDispatch(invoke)) {
2226 return;
2227 }
2228
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002229 HandleInvoke(invoke);
2230}
2231
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002232void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002233 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2234 return;
2235 }
2236
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002237 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002238 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002239 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002240}
2241
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2243 HandleInvoke(invoke);
2244 // Add the hidden argument.
2245 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2246}
2247
2248void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2249 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002250 LocationSummary* locations = invoke->GetLocations();
2251 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2252 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002253 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2254 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255 Location receiver = locations->InAt(0);
2256 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2257
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // Set the hidden argument. This is safe to do this here, as RAX
2259 // won't be modified thereafter, before the `call` instruction.
2260 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002261 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002263 if (receiver.IsStackSlot()) {
2264 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266 __ movl(temp, Address(temp, class_offset));
2267 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002268 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002269 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002271 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002272 // Instead of simply (possibly) unpoisoning `temp` here, we should
2273 // emit a read barrier for the previous class reference load.
2274 // However this is not required in practice, as this is an
2275 // intermediate/temporary reference and because the current
2276 // concurrent copying collector keeps the from-space memory
2277 // intact/accessible until the end of the marking phase (the
2278 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002279 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002281 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002282 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002283 __ call(Address(temp,
2284 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002285
2286 DCHECK(!codegen_->IsLeafMethod());
2287 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2288}
2289
Roland Levillain88cb1752014-10-20 16:36:47 +01002290void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2291 LocationSummary* locations =
2292 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2293 switch (neg->GetResultType()) {
2294 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002295 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002296 locations->SetInAt(0, Location::RequiresRegister());
2297 locations->SetOut(Location::SameAsFirstInput());
2298 break;
2299
Roland Levillain88cb1752014-10-20 16:36:47 +01002300 case Primitive::kPrimFloat:
2301 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002302 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002303 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002304 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002305 break;
2306
2307 default:
2308 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2309 }
2310}
2311
2312void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2313 LocationSummary* locations = neg->GetLocations();
2314 Location out = locations->Out();
2315 Location in = locations->InAt(0);
2316 switch (neg->GetResultType()) {
2317 case Primitive::kPrimInt:
2318 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002319 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002320 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002321 break;
2322
2323 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002324 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002325 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002327 break;
2328
Roland Levillain5368c212014-11-27 15:03:41 +00002329 case Primitive::kPrimFloat: {
2330 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002331 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002332 // Implement float negation with an exclusive or with value
2333 // 0x80000000 (mask for bit 31, representing the sign of a
2334 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002335 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002336 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002337 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002338 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002339
Roland Levillain5368c212014-11-27 15:03:41 +00002340 case Primitive::kPrimDouble: {
2341 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002342 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002343 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002344 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002345 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002346 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002349 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002350
2351 default:
2352 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2353 }
2354}
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2357 LocationSummary* locations =
2358 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2359 Primitive::Type result_type = conversion->GetResultType();
2360 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002361 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002362
David Brazdilb2bd1c52015-03-25 11:17:37 +00002363 // The Java language does not allow treating boolean as an integral type but
2364 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002365
Roland Levillaindff1f282014-11-05 14:15:05 +00002366 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002367 case Primitive::kPrimByte:
2368 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002369 case Primitive::kPrimLong:
2370 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002371 case Primitive::kPrimBoolean:
2372 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002373 case Primitive::kPrimShort:
2374 case Primitive::kPrimInt:
2375 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002376 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002377 locations->SetInAt(0, Location::Any());
2378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2379 break;
2380
2381 default:
2382 LOG(FATAL) << "Unexpected type conversion from " << input_type
2383 << " to " << result_type;
2384 }
2385 break;
2386
Roland Levillain01a8d712014-11-14 16:27:39 +00002387 case Primitive::kPrimShort:
2388 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002389 case Primitive::kPrimLong:
2390 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002391 case Primitive::kPrimBoolean:
2392 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002393 case Primitive::kPrimByte:
2394 case Primitive::kPrimInt:
2395 case Primitive::kPrimChar:
2396 // Processing a Dex `int-to-short' instruction.
2397 locations->SetInAt(0, Location::Any());
2398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2399 break;
2400
2401 default:
2402 LOG(FATAL) << "Unexpected type conversion from " << input_type
2403 << " to " << result_type;
2404 }
2405 break;
2406
Roland Levillain946e1432014-11-11 17:35:19 +00002407 case Primitive::kPrimInt:
2408 switch (input_type) {
2409 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002410 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002411 locations->SetInAt(0, Location::Any());
2412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2413 break;
2414
2415 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002416 // Processing a Dex `float-to-int' instruction.
2417 locations->SetInAt(0, Location::RequiresFpuRegister());
2418 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002419 break;
2420
Roland Levillain946e1432014-11-11 17:35:19 +00002421 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002422 // Processing a Dex `double-to-int' instruction.
2423 locations->SetInAt(0, Location::RequiresFpuRegister());
2424 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002425 break;
2426
2427 default:
2428 LOG(FATAL) << "Unexpected type conversion from " << input_type
2429 << " to " << result_type;
2430 }
2431 break;
2432
Roland Levillaindff1f282014-11-05 14:15:05 +00002433 case Primitive::kPrimLong:
2434 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002435 case Primitive::kPrimBoolean:
2436 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002437 case Primitive::kPrimByte:
2438 case Primitive::kPrimShort:
2439 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002440 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002441 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002442 // TODO: We would benefit from a (to-be-implemented)
2443 // Location::RegisterOrStackSlot requirement for this input.
2444 locations->SetInAt(0, Location::RequiresRegister());
2445 locations->SetOut(Location::RequiresRegister());
2446 break;
2447
2448 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002449 // Processing a Dex `float-to-long' instruction.
2450 locations->SetInAt(0, Location::RequiresFpuRegister());
2451 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002452 break;
2453
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002455 // Processing a Dex `double-to-long' instruction.
2456 locations->SetInAt(0, Location::RequiresFpuRegister());
2457 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected type conversion from " << input_type
2462 << " to " << result_type;
2463 }
2464 break;
2465
Roland Levillain981e4542014-11-14 11:47:14 +00002466 case Primitive::kPrimChar:
2467 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002468 case Primitive::kPrimLong:
2469 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002470 case Primitive::kPrimBoolean:
2471 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002472 case Primitive::kPrimByte:
2473 case Primitive::kPrimShort:
2474 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002475 // Processing a Dex `int-to-char' instruction.
2476 locations->SetInAt(0, Location::Any());
2477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2478 break;
2479
2480 default:
2481 LOG(FATAL) << "Unexpected type conversion from " << input_type
2482 << " to " << result_type;
2483 }
2484 break;
2485
Roland Levillaindff1f282014-11-05 14:15:05 +00002486 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002487 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002488 case Primitive::kPrimBoolean:
2489 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002490 case Primitive::kPrimByte:
2491 case Primitive::kPrimShort:
2492 case Primitive::kPrimInt:
2493 case Primitive::kPrimChar:
2494 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002495 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002496 locations->SetOut(Location::RequiresFpuRegister());
2497 break;
2498
2499 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002500 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002501 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002502 locations->SetOut(Location::RequiresFpuRegister());
2503 break;
2504
Roland Levillaincff13742014-11-17 14:32:17 +00002505 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002506 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002507 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002508 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002509 break;
2510
2511 default:
2512 LOG(FATAL) << "Unexpected type conversion from " << input_type
2513 << " to " << result_type;
2514 };
2515 break;
2516
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002518 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002519 case Primitive::kPrimBoolean:
2520 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002521 case Primitive::kPrimByte:
2522 case Primitive::kPrimShort:
2523 case Primitive::kPrimInt:
2524 case Primitive::kPrimChar:
2525 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002526 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002527 locations->SetOut(Location::RequiresFpuRegister());
2528 break;
2529
2530 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002531 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002532 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002533 locations->SetOut(Location::RequiresFpuRegister());
2534 break;
2535
Roland Levillaincff13742014-11-17 14:32:17 +00002536 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002537 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002538 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002539 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002540 break;
2541
2542 default:
2543 LOG(FATAL) << "Unexpected type conversion from " << input_type
2544 << " to " << result_type;
2545 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002546 break;
2547
2548 default:
2549 LOG(FATAL) << "Unexpected type conversion from " << input_type
2550 << " to " << result_type;
2551 }
2552}
2553
2554void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2555 LocationSummary* locations = conversion->GetLocations();
2556 Location out = locations->Out();
2557 Location in = locations->InAt(0);
2558 Primitive::Type result_type = conversion->GetResultType();
2559 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002560 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002561 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002562 case Primitive::kPrimByte:
2563 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002564 case Primitive::kPrimLong:
2565 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002566 case Primitive::kPrimBoolean:
2567 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002568 case Primitive::kPrimShort:
2569 case Primitive::kPrimInt:
2570 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002571 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002572 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002573 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002574 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002576 Address(CpuRegister(RSP), in.GetStackIndex()));
2577 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002578 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002579 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002580 }
2581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 }
2587 break;
2588
Roland Levillain01a8d712014-11-14 16:27:39 +00002589 case Primitive::kPrimShort:
2590 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002591 case Primitive::kPrimLong:
2592 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002593 case Primitive::kPrimBoolean:
2594 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002595 case Primitive::kPrimByte:
2596 case Primitive::kPrimInt:
2597 case Primitive::kPrimChar:
2598 // Processing a Dex `int-to-short' instruction.
2599 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002601 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002602 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002603 Address(CpuRegister(RSP), in.GetStackIndex()));
2604 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002606 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002607 }
2608 break;
2609
2610 default:
2611 LOG(FATAL) << "Unexpected type conversion from " << input_type
2612 << " to " << result_type;
2613 }
2614 break;
2615
Roland Levillain946e1432014-11-11 17:35:19 +00002616 case Primitive::kPrimInt:
2617 switch (input_type) {
2618 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002619 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002620 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002622 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002624 Address(CpuRegister(RSP), in.GetStackIndex()));
2625 } else {
2626 DCHECK(in.IsConstant());
2627 DCHECK(in.GetConstant()->IsLongConstant());
2628 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002629 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002630 }
2631 break;
2632
Roland Levillain3f8f9362014-12-02 17:45:01 +00002633 case Primitive::kPrimFloat: {
2634 // Processing a Dex `float-to-int' instruction.
2635 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2636 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002637 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002638
2639 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002640 // if input >= (float)INT_MAX goto done
2641 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002642 __ j(kAboveEqual, &done);
2643 // if input == NaN goto nan
2644 __ j(kUnordered, &nan);
2645 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002646 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002647 __ jmp(&done);
2648 __ Bind(&nan);
2649 // output = 0
2650 __ xorl(output, output);
2651 __ Bind(&done);
2652 break;
2653 }
2654
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002655 case Primitive::kPrimDouble: {
2656 // Processing a Dex `double-to-int' instruction.
2657 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2658 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002659 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002660
2661 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002662 // if input >= (double)INT_MAX goto done
2663 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002664 __ j(kAboveEqual, &done);
2665 // if input == NaN goto nan
2666 __ j(kUnordered, &nan);
2667 // output = double-to-int-truncate(input)
2668 __ cvttsd2si(output, input);
2669 __ jmp(&done);
2670 __ Bind(&nan);
2671 // output = 0
2672 __ xorl(output, output);
2673 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002674 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002675 }
Roland Levillain946e1432014-11-11 17:35:19 +00002676
2677 default:
2678 LOG(FATAL) << "Unexpected type conversion from " << input_type
2679 << " to " << result_type;
2680 }
2681 break;
2682
Roland Levillaindff1f282014-11-05 14:15:05 +00002683 case Primitive::kPrimLong:
2684 switch (input_type) {
2685 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002686 case Primitive::kPrimBoolean:
2687 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002688 case Primitive::kPrimByte:
2689 case Primitive::kPrimShort:
2690 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002691 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002692 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002695 break;
2696
Roland Levillain624279f2014-12-04 11:54:28 +00002697 case Primitive::kPrimFloat: {
2698 // Processing a Dex `float-to-long' instruction.
2699 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2700 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002701 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002702
Mark Mendell92e83bf2015-05-07 11:25:03 -04002703 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002704 // if input >= (float)LONG_MAX goto done
2705 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002706 __ j(kAboveEqual, &done);
2707 // if input == NaN goto nan
2708 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002709 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002710 __ cvttss2si(output, input, true);
2711 __ jmp(&done);
2712 __ Bind(&nan);
2713 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002714 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002715 __ Bind(&done);
2716 break;
2717 }
2718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002719 case Primitive::kPrimDouble: {
2720 // Processing a Dex `double-to-long' instruction.
2721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2722 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002723 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002724
Mark Mendell92e83bf2015-05-07 11:25:03 -04002725 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002726 // if input >= (double)LONG_MAX goto done
2727 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002728 __ j(kAboveEqual, &done);
2729 // if input == NaN goto nan
2730 __ j(kUnordered, &nan);
2731 // output = double-to-long-truncate(input)
2732 __ cvttsd2si(output, input, true);
2733 __ jmp(&done);
2734 __ Bind(&nan);
2735 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002736 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002737 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002738 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002739 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002740
2741 default:
2742 LOG(FATAL) << "Unexpected type conversion from " << input_type
2743 << " to " << result_type;
2744 }
2745 break;
2746
Roland Levillain981e4542014-11-14 11:47:14 +00002747 case Primitive::kPrimChar:
2748 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002749 case Primitive::kPrimLong:
2750 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002751 case Primitive::kPrimBoolean:
2752 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002753 case Primitive::kPrimByte:
2754 case Primitive::kPrimShort:
2755 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002756 // Processing a Dex `int-to-char' instruction.
2757 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002758 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002759 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002761 Address(CpuRegister(RSP), in.GetStackIndex()));
2762 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002763 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002764 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002765 }
2766 break;
2767
2768 default:
2769 LOG(FATAL) << "Unexpected type conversion from " << input_type
2770 << " to " << result_type;
2771 }
2772 break;
2773
Roland Levillaindff1f282014-11-05 14:15:05 +00002774 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002775 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002776 case Primitive::kPrimBoolean:
2777 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002778 case Primitive::kPrimByte:
2779 case Primitive::kPrimShort:
2780 case Primitive::kPrimInt:
2781 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002782 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002783 if (in.IsRegister()) {
2784 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2785 } else if (in.IsConstant()) {
2786 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2787 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002788 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002789 } else {
2790 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2791 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2792 }
Roland Levillaincff13742014-11-17 14:32:17 +00002793 break;
2794
2795 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002796 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002797 if (in.IsRegister()) {
2798 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2799 } else if (in.IsConstant()) {
2800 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2801 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002802 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002803 } else {
2804 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2805 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2806 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807 break;
2808
Roland Levillaincff13742014-11-17 14:32:17 +00002809 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002810 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002811 if (in.IsFpuRegister()) {
2812 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2813 } else if (in.IsConstant()) {
2814 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2815 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002816 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002817 } else {
2818 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2819 Address(CpuRegister(RSP), in.GetStackIndex()));
2820 }
Roland Levillaincff13742014-11-17 14:32:17 +00002821 break;
2822
2823 default:
2824 LOG(FATAL) << "Unexpected type conversion from " << input_type
2825 << " to " << result_type;
2826 };
2827 break;
2828
Roland Levillaindff1f282014-11-05 14:15:05 +00002829 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002830 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002831 case Primitive::kPrimBoolean:
2832 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002833 case Primitive::kPrimByte:
2834 case Primitive::kPrimShort:
2835 case Primitive::kPrimInt:
2836 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002837 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002838 if (in.IsRegister()) {
2839 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2840 } else if (in.IsConstant()) {
2841 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2842 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002843 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002844 } else {
2845 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2846 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2847 }
Roland Levillaincff13742014-11-17 14:32:17 +00002848 break;
2849
2850 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002851 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002852 if (in.IsRegister()) {
2853 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2854 } else if (in.IsConstant()) {
2855 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2856 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002857 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002858 } else {
2859 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2860 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2861 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002862 break;
2863
Roland Levillaincff13742014-11-17 14:32:17 +00002864 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002865 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002866 if (in.IsFpuRegister()) {
2867 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2868 } else if (in.IsConstant()) {
2869 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2870 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002871 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002872 } else {
2873 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2874 Address(CpuRegister(RSP), in.GetStackIndex()));
2875 }
Roland Levillaincff13742014-11-17 14:32:17 +00002876 break;
2877
2878 default:
2879 LOG(FATAL) << "Unexpected type conversion from " << input_type
2880 << " to " << result_type;
2881 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002882 break;
2883
2884 default:
2885 LOG(FATAL) << "Unexpected type conversion from " << input_type
2886 << " to " << result_type;
2887 }
2888}
2889
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002890void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002891 LocationSummary* locations =
2892 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002893 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002894 case Primitive::kPrimInt: {
2895 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002896 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2897 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002898 break;
2899 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002900
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002901 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002902 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002903 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002904 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002906 break;
2907 }
2908
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002909 case Primitive::kPrimDouble:
2910 case Primitive::kPrimFloat: {
2911 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002912 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002913 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002914 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002915 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002916
2917 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002918 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002919 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002920}
2921
2922void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2923 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 Location first = locations->InAt(0);
2925 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002926 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002927
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002928 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002929 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002930 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002931 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2932 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002933 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2934 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002935 } else {
2936 __ leal(out.AsRegister<CpuRegister>(), Address(
2937 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2938 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002939 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002940 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2941 __ addl(out.AsRegister<CpuRegister>(),
2942 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2943 } else {
2944 __ leal(out.AsRegister<CpuRegister>(), Address(
2945 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2946 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002947 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002948 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002949 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002950 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002951 break;
2952 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002953
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002954 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002955 if (second.IsRegister()) {
2956 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2957 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002958 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2959 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002960 } else {
2961 __ leaq(out.AsRegister<CpuRegister>(), Address(
2962 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2963 }
2964 } else {
2965 DCHECK(second.IsConstant());
2966 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2967 int32_t int32_value = Low32Bits(value);
2968 DCHECK_EQ(int32_value, value);
2969 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2970 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2971 } else {
2972 __ leaq(out.AsRegister<CpuRegister>(), Address(
2973 first.AsRegister<CpuRegister>(), int32_value));
2974 }
2975 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002976 break;
2977 }
2978
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002979 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002980 if (second.IsFpuRegister()) {
2981 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2982 } else if (second.IsConstant()) {
2983 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002984 codegen_->LiteralFloatAddress(
2985 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002986 } else {
2987 DCHECK(second.IsStackSlot());
2988 __ addss(first.AsFpuRegister<XmmRegister>(),
2989 Address(CpuRegister(RSP), second.GetStackIndex()));
2990 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002991 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002992 }
2993
2994 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002995 if (second.IsFpuRegister()) {
2996 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2997 } else if (second.IsConstant()) {
2998 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002999 codegen_->LiteralDoubleAddress(
3000 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003001 } else {
3002 DCHECK(second.IsDoubleStackSlot());
3003 __ addsd(first.AsFpuRegister<XmmRegister>(),
3004 Address(CpuRegister(RSP), second.GetStackIndex()));
3005 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003006 break;
3007 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003008
3009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003010 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011 }
3012}
3013
3014void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003015 LocationSummary* locations =
3016 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003017 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003018 case Primitive::kPrimInt: {
3019 locations->SetInAt(0, Location::RequiresRegister());
3020 locations->SetInAt(1, Location::Any());
3021 locations->SetOut(Location::SameAsFirstInput());
3022 break;
3023 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003024 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003025 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003026 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003027 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003028 break;
3029 }
Calin Juravle11351682014-10-23 15:38:15 +01003030 case Primitive::kPrimFloat:
3031 case Primitive::kPrimDouble: {
3032 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003033 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003034 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 break;
Calin Juravle11351682014-10-23 15:38:15 +01003036 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037 default:
Calin Juravle11351682014-10-23 15:38:15 +01003038 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003040}
3041
3042void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3043 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003044 Location first = locations->InAt(0);
3045 Location second = locations->InAt(1);
3046 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003047 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003048 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003049 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003050 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003051 } else if (second.IsConstant()) {
3052 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003054 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003056 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003057 break;
3058 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003060 if (second.IsConstant()) {
3061 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3062 DCHECK(IsInt<32>(value));
3063 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3064 } else {
3065 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3066 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003067 break;
3068 }
3069
Calin Juravle11351682014-10-23 15:38:15 +01003070 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003071 if (second.IsFpuRegister()) {
3072 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3073 } else if (second.IsConstant()) {
3074 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003075 codegen_->LiteralFloatAddress(
3076 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003077 } else {
3078 DCHECK(second.IsStackSlot());
3079 __ subss(first.AsFpuRegister<XmmRegister>(),
3080 Address(CpuRegister(RSP), second.GetStackIndex()));
3081 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003082 break;
Calin Juravle11351682014-10-23 15:38:15 +01003083 }
3084
3085 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003086 if (second.IsFpuRegister()) {
3087 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3088 } else if (second.IsConstant()) {
3089 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003090 codegen_->LiteralDoubleAddress(
3091 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003092 } else {
3093 DCHECK(second.IsDoubleStackSlot());
3094 __ subsd(first.AsFpuRegister<XmmRegister>(),
3095 Address(CpuRegister(RSP), second.GetStackIndex()));
3096 }
Calin Juravle11351682014-10-23 15:38:15 +01003097 break;
3098 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003099
3100 default:
Calin Juravle11351682014-10-23 15:38:15 +01003101 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003102 }
3103}
3104
Calin Juravle34bacdf2014-10-07 20:23:36 +01003105void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3106 LocationSummary* locations =
3107 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3108 switch (mul->GetResultType()) {
3109 case Primitive::kPrimInt: {
3110 locations->SetInAt(0, Location::RequiresRegister());
3111 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003112 if (mul->InputAt(1)->IsIntConstant()) {
3113 // Can use 3 operand multiply.
3114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3115 } else {
3116 locations->SetOut(Location::SameAsFirstInput());
3117 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003118 break;
3119 }
3120 case Primitive::kPrimLong: {
3121 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003122 locations->SetInAt(1, Location::Any());
3123 if (mul->InputAt(1)->IsLongConstant() &&
3124 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003125 // Can use 3 operand multiply.
3126 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3127 } else {
3128 locations->SetOut(Location::SameAsFirstInput());
3129 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003130 break;
3131 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003132 case Primitive::kPrimFloat:
3133 case Primitive::kPrimDouble: {
3134 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003135 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003136 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003138 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139
3140 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003141 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142 }
3143}
3144
3145void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3146 LocationSummary* locations = mul->GetLocations();
3147 Location first = locations->InAt(0);
3148 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003149 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003151 case Primitive::kPrimInt:
3152 // The constant may have ended up in a register, so test explicitly to avoid
3153 // problems where the output may not be the same as the first operand.
3154 if (mul->InputAt(1)->IsIntConstant()) {
3155 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3156 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3157 } else if (second.IsRegister()) {
3158 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003159 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003161 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003163 __ imull(first.AsRegister<CpuRegister>(),
3164 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 }
3166 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003167 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003168 // The constant may have ended up in a register, so test explicitly to avoid
3169 // problems where the output may not be the same as the first operand.
3170 if (mul->InputAt(1)->IsLongConstant()) {
3171 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3172 if (IsInt<32>(value)) {
3173 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3174 Immediate(static_cast<int32_t>(value)));
3175 } else {
3176 // Have to use the constant area.
3177 DCHECK(first.Equals(out));
3178 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3179 }
3180 } else if (second.IsRegister()) {
3181 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003182 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003183 } else {
3184 DCHECK(second.IsDoubleStackSlot());
3185 DCHECK(first.Equals(out));
3186 __ imulq(first.AsRegister<CpuRegister>(),
3187 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003188 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189 break;
3190 }
3191
Calin Juravleb5bfa962014-10-21 18:02:24 +01003192 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003193 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003194 if (second.IsFpuRegister()) {
3195 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3196 } else if (second.IsConstant()) {
3197 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003198 codegen_->LiteralFloatAddress(
3199 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003200 } else {
3201 DCHECK(second.IsStackSlot());
3202 __ mulss(first.AsFpuRegister<XmmRegister>(),
3203 Address(CpuRegister(RSP), second.GetStackIndex()));
3204 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003205 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003206 }
3207
3208 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003209 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003210 if (second.IsFpuRegister()) {
3211 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3212 } else if (second.IsConstant()) {
3213 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003214 codegen_->LiteralDoubleAddress(
3215 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003216 } else {
3217 DCHECK(second.IsDoubleStackSlot());
3218 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3219 Address(CpuRegister(RSP), second.GetStackIndex()));
3220 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003221 break;
3222 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223
3224 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003225 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226 }
3227}
3228
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003229void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3230 uint32_t stack_adjustment, bool is_float) {
3231 if (source.IsStackSlot()) {
3232 DCHECK(is_float);
3233 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3234 } else if (source.IsDoubleStackSlot()) {
3235 DCHECK(!is_float);
3236 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3237 } else {
3238 // Write the value to the temporary location on the stack and load to FP stack.
3239 if (is_float) {
3240 Location stack_temp = Location::StackSlot(temp_offset);
3241 codegen_->Move(stack_temp, source);
3242 __ flds(Address(CpuRegister(RSP), temp_offset));
3243 } else {
3244 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3245 codegen_->Move(stack_temp, source);
3246 __ fldl(Address(CpuRegister(RSP), temp_offset));
3247 }
3248 }
3249}
3250
3251void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3252 Primitive::Type type = rem->GetResultType();
3253 bool is_float = type == Primitive::kPrimFloat;
3254 size_t elem_size = Primitive::ComponentSize(type);
3255 LocationSummary* locations = rem->GetLocations();
3256 Location first = locations->InAt(0);
3257 Location second = locations->InAt(1);
3258 Location out = locations->Out();
3259
3260 // Create stack space for 2 elements.
3261 // TODO: enhance register allocator to ask for stack temporaries.
3262 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3263
3264 // Load the values to the FP stack in reverse order, using temporaries if needed.
3265 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3266 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3267
3268 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003269 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003270 __ Bind(&retry);
3271 __ fprem();
3272
3273 // Move FP status to AX.
3274 __ fstsw();
3275
3276 // And see if the argument reduction is complete. This is signaled by the
3277 // C2 FPU flag bit set to 0.
3278 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3279 __ j(kNotEqual, &retry);
3280
3281 // We have settled on the final value. Retrieve it into an XMM register.
3282 // Store FP top of stack to real stack.
3283 if (is_float) {
3284 __ fsts(Address(CpuRegister(RSP), 0));
3285 } else {
3286 __ fstl(Address(CpuRegister(RSP), 0));
3287 }
3288
3289 // Pop the 2 items from the FP stack.
3290 __ fucompp();
3291
3292 // Load the value from the stack into an XMM register.
3293 DCHECK(out.IsFpuRegister()) << out;
3294 if (is_float) {
3295 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3296 } else {
3297 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3298 }
3299
3300 // And remove the temporary stack space we allocated.
3301 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3302}
3303
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003304void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3305 DCHECK(instruction->IsDiv() || instruction->IsRem());
3306
3307 LocationSummary* locations = instruction->GetLocations();
3308 Location second = locations->InAt(1);
3309 DCHECK(second.IsConstant());
3310
3311 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3312 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003313 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003314
3315 DCHECK(imm == 1 || imm == -1);
3316
3317 switch (instruction->GetResultType()) {
3318 case Primitive::kPrimInt: {
3319 if (instruction->IsRem()) {
3320 __ xorl(output_register, output_register);
3321 } else {
3322 __ movl(output_register, input_register);
3323 if (imm == -1) {
3324 __ negl(output_register);
3325 }
3326 }
3327 break;
3328 }
3329
3330 case Primitive::kPrimLong: {
3331 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003332 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333 } else {
3334 __ movq(output_register, input_register);
3335 if (imm == -1) {
3336 __ negq(output_register);
3337 }
3338 }
3339 break;
3340 }
3341
3342 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003343 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 }
3345}
3346
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003347void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003348 LocationSummary* locations = instruction->GetLocations();
3349 Location second = locations->InAt(1);
3350
3351 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3352 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3353
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003354 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003355 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3356 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003357
3358 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3359
3360 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003361 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003362 __ testl(numerator, numerator);
3363 __ cmov(kGreaterEqual, tmp, numerator);
3364 int shift = CTZ(imm);
3365 __ sarl(tmp, Immediate(shift));
3366
3367 if (imm < 0) {
3368 __ negl(tmp);
3369 }
3370
3371 __ movl(output_register, tmp);
3372 } else {
3373 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3374 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3375
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003376 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377 __ addq(rdx, numerator);
3378 __ testq(numerator, numerator);
3379 __ cmov(kGreaterEqual, rdx, numerator);
3380 int shift = CTZ(imm);
3381 __ sarq(rdx, Immediate(shift));
3382
3383 if (imm < 0) {
3384 __ negq(rdx);
3385 }
3386
3387 __ movq(output_register, rdx);
3388 }
3389}
3390
3391void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3392 DCHECK(instruction->IsDiv() || instruction->IsRem());
3393
3394 LocationSummary* locations = instruction->GetLocations();
3395 Location second = locations->InAt(1);
3396
3397 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3398 : locations->GetTemp(0).AsRegister<CpuRegister>();
3399 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3400 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3401 : locations->Out().AsRegister<CpuRegister>();
3402 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3403
3404 DCHECK_EQ(RAX, eax.AsRegister());
3405 DCHECK_EQ(RDX, edx.AsRegister());
3406 if (instruction->IsDiv()) {
3407 DCHECK_EQ(RAX, out.AsRegister());
3408 } else {
3409 DCHECK_EQ(RDX, out.AsRegister());
3410 }
3411
3412 int64_t magic;
3413 int shift;
3414
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003415 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003416 if (instruction->GetResultType() == Primitive::kPrimInt) {
3417 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3418
3419 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3420
3421 __ movl(numerator, eax);
3422
Mark Mendell0c9497d2015-08-21 09:30:05 -04003423 NearLabel no_div;
3424 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425 __ testl(eax, eax);
3426 __ j(kNotEqual, &no_div);
3427
3428 __ xorl(out, out);
3429 __ jmp(&end);
3430
3431 __ Bind(&no_div);
3432
3433 __ movl(eax, Immediate(magic));
3434 __ imull(numerator);
3435
3436 if (imm > 0 && magic < 0) {
3437 __ addl(edx, numerator);
3438 } else if (imm < 0 && magic > 0) {
3439 __ subl(edx, numerator);
3440 }
3441
3442 if (shift != 0) {
3443 __ sarl(edx, Immediate(shift));
3444 }
3445
3446 __ movl(eax, edx);
3447 __ shrl(edx, Immediate(31));
3448 __ addl(edx, eax);
3449
3450 if (instruction->IsRem()) {
3451 __ movl(eax, numerator);
3452 __ imull(edx, Immediate(imm));
3453 __ subl(eax, edx);
3454 __ movl(edx, eax);
3455 } else {
3456 __ movl(eax, edx);
3457 }
3458 __ Bind(&end);
3459 } else {
3460 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3461
3462 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3463
3464 CpuRegister rax = eax;
3465 CpuRegister rdx = edx;
3466
3467 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3468
3469 // Save the numerator.
3470 __ movq(numerator, rax);
3471
3472 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003473 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475 // RDX:RAX = magic * numerator
3476 __ imulq(numerator);
3477
3478 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003479 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 __ addq(rdx, numerator);
3481 } else if (imm < 0 && magic > 0) {
3482 // RDX -= numerator
3483 __ subq(rdx, numerator);
3484 }
3485
3486 // Shift if needed.
3487 if (shift != 0) {
3488 __ sarq(rdx, Immediate(shift));
3489 }
3490
3491 // RDX += 1 if RDX < 0
3492 __ movq(rax, rdx);
3493 __ shrq(rdx, Immediate(63));
3494 __ addq(rdx, rax);
3495
3496 if (instruction->IsRem()) {
3497 __ movq(rax, numerator);
3498
3499 if (IsInt<32>(imm)) {
3500 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3501 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003502 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 }
3504
3505 __ subq(rax, rdx);
3506 __ movq(rdx, rax);
3507 } else {
3508 __ movq(rax, rdx);
3509 }
3510 }
3511}
3512
Calin Juravlebacfec32014-11-14 15:54:36 +00003513void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3514 DCHECK(instruction->IsDiv() || instruction->IsRem());
3515 Primitive::Type type = instruction->GetResultType();
3516 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3517
3518 bool is_div = instruction->IsDiv();
3519 LocationSummary* locations = instruction->GetLocations();
3520
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3522 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003523
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003526
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003528 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 if (imm == 0) {
3531 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3532 } else if (imm == 1 || imm == -1) {
3533 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003534 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003535 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 } else {
3537 DCHECK(imm <= -2 || imm >= 2);
3538 GenerateDivRemWithAnyConstant(instruction);
3539 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003541 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003543 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003545
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3547 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3548 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3549 // so it's safe to just use negl instead of more complex comparisons.
3550 if (type == Primitive::kPrimInt) {
3551 __ cmpl(second_reg, Immediate(-1));
3552 __ j(kEqual, slow_path->GetEntryLabel());
3553 // edx:eax <- sign-extended of eax
3554 __ cdq();
3555 // eax = quotient, edx = remainder
3556 __ idivl(second_reg);
3557 } else {
3558 __ cmpq(second_reg, Immediate(-1));
3559 __ j(kEqual, slow_path->GetEntryLabel());
3560 // rdx:rax <- sign-extended of rax
3561 __ cqo();
3562 // rax = quotient, rdx = remainder
3563 __ idivq(second_reg);
3564 }
3565 __ Bind(slow_path->GetExitLabel());
3566 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003567}
3568
Calin Juravle7c4954d2014-10-28 16:57:40 +00003569void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3570 LocationSummary* locations =
3571 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3572 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 case Primitive::kPrimInt:
3574 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003575 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003577 locations->SetOut(Location::SameAsFirstInput());
3578 // Intel uses edx:eax as the dividend.
3579 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3581 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3582 // output and request another temp.
3583 if (div->InputAt(1)->IsConstant()) {
3584 locations->AddTemp(Location::RequiresRegister());
3585 }
Calin Juravled0d48522014-11-04 16:40:20 +00003586 break;
3587 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003588
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589 case Primitive::kPrimFloat:
3590 case Primitive::kPrimDouble: {
3591 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003592 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003593 locations->SetOut(Location::SameAsFirstInput());
3594 break;
3595 }
3596
3597 default:
3598 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3599 }
3600}
3601
3602void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3603 LocationSummary* locations = div->GetLocations();
3604 Location first = locations->InAt(0);
3605 Location second = locations->InAt(1);
3606 DCHECK(first.Equals(locations->Out()));
3607
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003608 Primitive::Type type = div->GetResultType();
3609 switch (type) {
3610 case Primitive::kPrimInt:
3611 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003613 break;
3614 }
3615
Calin Juravle7c4954d2014-10-28 16:57:40 +00003616 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003617 if (second.IsFpuRegister()) {
3618 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3619 } else if (second.IsConstant()) {
3620 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003621 codegen_->LiteralFloatAddress(
3622 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003623 } else {
3624 DCHECK(second.IsStackSlot());
3625 __ divss(first.AsFpuRegister<XmmRegister>(),
3626 Address(CpuRegister(RSP), second.GetStackIndex()));
3627 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003628 break;
3629 }
3630
3631 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003632 if (second.IsFpuRegister()) {
3633 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3634 } else if (second.IsConstant()) {
3635 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003636 codegen_->LiteralDoubleAddress(
3637 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003638 } else {
3639 DCHECK(second.IsDoubleStackSlot());
3640 __ divsd(first.AsFpuRegister<XmmRegister>(),
3641 Address(CpuRegister(RSP), second.GetStackIndex()));
3642 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003643 break;
3644 }
3645
3646 default:
3647 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3648 }
3649}
3650
Calin Juravlebacfec32014-11-14 15:54:36 +00003651void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003652 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003653 LocationSummary* locations =
3654 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003655
3656 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 case Primitive::kPrimInt:
3658 case Primitive::kPrimLong: {
3659 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003660 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003661 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3662 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003663 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3664 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3665 // output and request another temp.
3666 if (rem->InputAt(1)->IsConstant()) {
3667 locations->AddTemp(Location::RequiresRegister());
3668 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003669 break;
3670 }
3671
3672 case Primitive::kPrimFloat:
3673 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003674 locations->SetInAt(0, Location::Any());
3675 locations->SetInAt(1, Location::Any());
3676 locations->SetOut(Location::RequiresFpuRegister());
3677 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 break;
3679 }
3680
3681 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003682 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003683 }
3684}
3685
3686void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3687 Primitive::Type type = rem->GetResultType();
3688 switch (type) {
3689 case Primitive::kPrimInt:
3690 case Primitive::kPrimLong: {
3691 GenerateDivRemIntegral(rem);
3692 break;
3693 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003694 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003695 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003696 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003697 break;
3698 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003699 default:
3700 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3701 }
3702}
3703
Calin Juravled0d48522014-11-04 16:40:20 +00003704void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003705 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3706 ? LocationSummary::kCallOnSlowPath
3707 : LocationSummary::kNoCall;
3708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003709 locations->SetInAt(0, Location::Any());
3710 if (instruction->HasUses()) {
3711 locations->SetOut(Location::SameAsFirstInput());
3712 }
3713}
3714
3715void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003716 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003717 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3718 codegen_->AddSlowPath(slow_path);
3719
3720 LocationSummary* locations = instruction->GetLocations();
3721 Location value = locations->InAt(0);
3722
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003723 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003724 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003725 case Primitive::kPrimByte:
3726 case Primitive::kPrimChar:
3727 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003728 case Primitive::kPrimInt: {
3729 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003731 __ j(kEqual, slow_path->GetEntryLabel());
3732 } else if (value.IsStackSlot()) {
3733 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3734 __ j(kEqual, slow_path->GetEntryLabel());
3735 } else {
3736 DCHECK(value.IsConstant()) << value;
3737 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3738 __ jmp(slow_path->GetEntryLabel());
3739 }
3740 }
3741 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003742 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003743 case Primitive::kPrimLong: {
3744 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003746 __ j(kEqual, slow_path->GetEntryLabel());
3747 } else if (value.IsDoubleStackSlot()) {
3748 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3749 __ j(kEqual, slow_path->GetEntryLabel());
3750 } else {
3751 DCHECK(value.IsConstant()) << value;
3752 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3753 __ jmp(slow_path->GetEntryLabel());
3754 }
3755 }
3756 break;
3757 }
3758 default:
3759 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003760 }
Calin Juravled0d48522014-11-04 16:40:20 +00003761}
3762
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3764 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3765
3766 LocationSummary* locations =
3767 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3768
3769 switch (op->GetResultType()) {
3770 case Primitive::kPrimInt:
3771 case Primitive::kPrimLong: {
3772 locations->SetInAt(0, Location::RequiresRegister());
3773 // The shift count needs to be in CL.
3774 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3775 locations->SetOut(Location::SameAsFirstInput());
3776 break;
3777 }
3778 default:
3779 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3780 }
3781}
3782
3783void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3784 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3785
3786 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788 Location second = locations->InAt(1);
3789
3790 switch (op->GetResultType()) {
3791 case Primitive::kPrimInt: {
3792 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794 if (op->IsShl()) {
3795 __ shll(first_reg, second_reg);
3796 } else if (op->IsShr()) {
3797 __ sarl(first_reg, second_reg);
3798 } else {
3799 __ shrl(first_reg, second_reg);
3800 }
3801 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003802 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003803 if (op->IsShl()) {
3804 __ shll(first_reg, imm);
3805 } else if (op->IsShr()) {
3806 __ sarl(first_reg, imm);
3807 } else {
3808 __ shrl(first_reg, imm);
3809 }
3810 }
3811 break;
3812 }
3813 case Primitive::kPrimLong: {
3814 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816 if (op->IsShl()) {
3817 __ shlq(first_reg, second_reg);
3818 } else if (op->IsShr()) {
3819 __ sarq(first_reg, second_reg);
3820 } else {
3821 __ shrq(first_reg, second_reg);
3822 }
3823 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003824 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003825 if (op->IsShl()) {
3826 __ shlq(first_reg, imm);
3827 } else if (op->IsShr()) {
3828 __ sarq(first_reg, imm);
3829 } else {
3830 __ shrq(first_reg, imm);
3831 }
3832 }
3833 break;
3834 }
3835 default:
3836 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003837 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 }
3839}
3840
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003841void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3842 LocationSummary* locations =
3843 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3844
3845 switch (ror->GetResultType()) {
3846 case Primitive::kPrimInt:
3847 case Primitive::kPrimLong: {
3848 locations->SetInAt(0, Location::RequiresRegister());
3849 // The shift count needs to be in CL (unless it is a constant).
3850 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3851 locations->SetOut(Location::SameAsFirstInput());
3852 break;
3853 }
3854 default:
3855 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3856 UNREACHABLE();
3857 }
3858}
3859
3860void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3861 LocationSummary* locations = ror->GetLocations();
3862 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3863 Location second = locations->InAt(1);
3864
3865 switch (ror->GetResultType()) {
3866 case Primitive::kPrimInt:
3867 if (second.IsRegister()) {
3868 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3869 __ rorl(first_reg, second_reg);
3870 } else {
3871 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3872 __ rorl(first_reg, imm);
3873 }
3874 break;
3875 case Primitive::kPrimLong:
3876 if (second.IsRegister()) {
3877 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3878 __ rorq(first_reg, second_reg);
3879 } else {
3880 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3881 __ rorq(first_reg, imm);
3882 }
3883 break;
3884 default:
3885 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3886 UNREACHABLE();
3887 }
3888}
3889
Calin Juravle9aec02f2014-11-18 23:06:35 +00003890void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3891 HandleShift(shl);
3892}
3893
3894void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3895 HandleShift(shl);
3896}
3897
3898void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3899 HandleShift(shr);
3900}
3901
3902void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3903 HandleShift(shr);
3904}
3905
3906void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3907 HandleShift(ushr);
3908}
3909
3910void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3911 HandleShift(ushr);
3912}
3913
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003914void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003915 LocationSummary* locations =
3916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003917 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003918 if (instruction->IsStringAlloc()) {
3919 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3920 } else {
3921 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3922 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3923 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003924 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003925}
3926
3927void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003928 // Note: if heap poisoning is enabled, the entry point takes cares
3929 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003930 if (instruction->IsStringAlloc()) {
3931 // String is allocated through StringFactory. Call NewEmptyString entry point.
3932 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3933 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3934 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3935 __ call(Address(temp, code_offset.SizeValue()));
3936 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3937 } else {
3938 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3939 instruction,
3940 instruction->GetDexPc(),
3941 nullptr);
3942 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3943 DCHECK(!codegen_->IsLeafMethod());
3944 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003945}
3946
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003947void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3950 InvokeRuntimeCallingConvention calling_convention;
3951 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003952 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003953 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003954 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003955}
3956
3957void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3958 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003959 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3960 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003961 // Note: if heap poisoning is enabled, the entry point takes cares
3962 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003963 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3964 instruction,
3965 instruction->GetDexPc(),
3966 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003967 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968
3969 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003970}
3971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003972void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003973 LocationSummary* locations =
3974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003975 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3976 if (location.IsStackSlot()) {
3977 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3978 } else if (location.IsDoubleStackSlot()) {
3979 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3980 }
3981 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003982}
3983
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003984void InstructionCodeGeneratorX86_64::VisitParameterValue(
3985 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003986 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003987}
3988
3989void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3990 LocationSummary* locations =
3991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3992 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3993}
3994
3995void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3996 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3997 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003998}
3999
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004000void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4001 LocationSummary* locations =
4002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4003 locations->SetInAt(0, Location::RequiresRegister());
4004 locations->SetOut(Location::RequiresRegister());
4005}
4006
4007void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4008 LocationSummary* locations = instruction->GetLocations();
4009 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004011 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4012 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4013 } else {
4014 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4015 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
4016 }
4017 __ movq(locations->Out().AsRegister<CpuRegister>(),
4018 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4019}
4020
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004021void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004022 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004023 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004024 locations->SetInAt(0, Location::RequiresRegister());
4025 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004026}
4027
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004028void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4029 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004030 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4031 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004033 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004034 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004035 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036 break;
4037
4038 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004039 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004040 break;
4041
4042 default:
4043 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4044 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004045}
4046
David Brazdil66d126e2015-04-03 16:02:44 +01004047void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4048 LocationSummary* locations =
4049 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4050 locations->SetInAt(0, Location::RequiresRegister());
4051 locations->SetOut(Location::SameAsFirstInput());
4052}
4053
4054void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004055 LocationSummary* locations = bool_not->GetLocations();
4056 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4057 locations->Out().AsRegister<CpuRegister>().AsRegister());
4058 Location out = locations->Out();
4059 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4060}
4061
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004062void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004063 LocationSummary* locations =
4064 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004065 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4066 locations->SetInAt(i, Location::Any());
4067 }
4068 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004069}
4070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004071void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004072 LOG(FATAL) << "Unimplemented";
4073}
4074
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004075void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004076 /*
4077 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004078 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004079 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4080 */
4081 switch (kind) {
4082 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004083 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004084 break;
4085 }
4086 case MemBarrierKind::kAnyStore:
4087 case MemBarrierKind::kLoadAny:
4088 case MemBarrierKind::kStoreStore: {
4089 // nop
4090 break;
4091 }
4092 default:
4093 LOG(FATAL) << "Unexpected memory barier " << kind;
4094 }
4095}
4096
4097void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4098 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4099
Roland Levillain0d5a2812015-11-13 10:07:31 +00004100 bool object_field_get_with_read_barrier =
4101 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004102 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004103 new (GetGraph()->GetArena()) LocationSummary(instruction,
4104 object_field_get_with_read_barrier ?
4105 LocationSummary::kCallOnSlowPath :
4106 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004107 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004108 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4109 locations->SetOut(Location::RequiresFpuRegister());
4110 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004111 // The output overlaps for an object field get when read barriers
4112 // are enabled: we do not want the move to overwrite the object's
4113 // location, as we need it to emit the read barrier.
4114 locations->SetOut(
4115 Location::RequiresRegister(),
4116 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004117 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004118 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4119 // We need a temporary register for the read barrier marking slow
4120 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4121 locations->AddTemp(Location::RequiresRegister());
4122 }
Calin Juravle52c48962014-12-16 17:02:57 +00004123}
4124
4125void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4126 const FieldInfo& field_info) {
4127 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4128
4129 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004130 Location base_loc = locations->InAt(0);
4131 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004132 Location out = locations->Out();
4133 bool is_volatile = field_info.IsVolatile();
4134 Primitive::Type field_type = field_info.GetFieldType();
4135 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4136
4137 switch (field_type) {
4138 case Primitive::kPrimBoolean: {
4139 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4140 break;
4141 }
4142
4143 case Primitive::kPrimByte: {
4144 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4145 break;
4146 }
4147
4148 case Primitive::kPrimShort: {
4149 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4150 break;
4151 }
4152
4153 case Primitive::kPrimChar: {
4154 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4155 break;
4156 }
4157
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004158 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004159 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4160 break;
4161 }
4162
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004163 case Primitive::kPrimNot: {
4164 // /* HeapReference<Object> */ out = *(base + offset)
4165 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4166 Location temp_loc = locations->GetTemp(0);
4167 // Note that a potential implicit null check is handled in this
4168 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4169 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4170 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4171 if (is_volatile) {
4172 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4173 }
4174 } else {
4175 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4176 codegen_->MaybeRecordImplicitNullCheck(instruction);
4177 if (is_volatile) {
4178 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4179 }
4180 // If read barriers are enabled, emit read barriers other than
4181 // Baker's using a slow path (and also unpoison the loaded
4182 // reference, if heap poisoning is enabled).
4183 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4184 }
4185 break;
4186 }
4187
Calin Juravle52c48962014-12-16 17:02:57 +00004188 case Primitive::kPrimLong: {
4189 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4190 break;
4191 }
4192
4193 case Primitive::kPrimFloat: {
4194 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4195 break;
4196 }
4197
4198 case Primitive::kPrimDouble: {
4199 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4200 break;
4201 }
4202
4203 case Primitive::kPrimVoid:
4204 LOG(FATAL) << "Unreachable type " << field_type;
4205 UNREACHABLE();
4206 }
4207
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004208 if (field_type == Primitive::kPrimNot) {
4209 // Potential implicit null checks, in the case of reference
4210 // fields, are handled in the previous switch statement.
4211 } else {
4212 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004213 }
Roland Levillain4d027112015-07-01 15:41:14 +01004214
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004215 if (is_volatile) {
4216 if (field_type == Primitive::kPrimNot) {
4217 // Memory barriers, in the case of references, are also handled
4218 // in the previous switch statement.
4219 } else {
4220 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4221 }
Roland Levillain4d027112015-07-01 15:41:14 +01004222 }
Calin Juravle52c48962014-12-16 17:02:57 +00004223}
4224
4225void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4226 const FieldInfo& field_info) {
4227 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4228
4229 LocationSummary* locations =
4230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004231 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004232 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004233 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004234 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004235
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004236 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004237 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004238 if (is_volatile) {
4239 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4240 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4241 } else {
4242 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4243 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004244 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004245 if (is_volatile) {
4246 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4247 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4248 } else {
4249 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4250 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004251 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004252 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004253 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004254 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004255 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004256 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4257 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004258 locations->AddTemp(Location::RequiresRegister());
4259 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260}
4261
Calin Juravle52c48962014-12-16 17:02:57 +00004262void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004263 const FieldInfo& field_info,
4264 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004265 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4266
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004268 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4269 Location value = locations->InAt(1);
4270 bool is_volatile = field_info.IsVolatile();
4271 Primitive::Type field_type = field_info.GetFieldType();
4272 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4273
4274 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004275 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004276 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277
Mark Mendellea5af682015-10-22 17:35:49 -04004278 bool maybe_record_implicit_null_check_done = false;
4279
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280 switch (field_type) {
4281 case Primitive::kPrimBoolean:
4282 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004283 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004284 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004285 __ movb(Address(base, offset), Immediate(v));
4286 } else {
4287 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4288 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289 break;
4290 }
4291
4292 case Primitive::kPrimShort:
4293 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004294 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004295 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004296 __ movw(Address(base, offset), Immediate(v));
4297 } else {
4298 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4299 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
4301 }
4302
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004303 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004305 if (value.IsConstant()) {
4306 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004307 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4308 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4309 // Note: if heap poisoning is enabled, no need to poison
4310 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004311 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004312 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004313 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4314 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4315 __ movl(temp, value.AsRegister<CpuRegister>());
4316 __ PoisonHeapReference(temp);
4317 __ movl(Address(base, offset), temp);
4318 } else {
4319 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4320 }
Mark Mendell40741f32015-04-20 22:10:34 -04004321 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 break;
4323 }
4324
4325 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004326 if (value.IsConstant()) {
4327 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004328 codegen_->MoveInt64ToAddress(Address(base, offset),
4329 Address(base, offset + sizeof(int32_t)),
4330 v,
4331 instruction);
4332 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004333 } else {
4334 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4335 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336 break;
4337 }
4338
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004339 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004340 if (value.IsConstant()) {
4341 int32_t v =
4342 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4343 __ movl(Address(base, offset), Immediate(v));
4344 } else {
4345 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4346 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004347 break;
4348 }
4349
4350 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004351 if (value.IsConstant()) {
4352 int64_t v =
4353 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4354 codegen_->MoveInt64ToAddress(Address(base, offset),
4355 Address(base, offset + sizeof(int32_t)),
4356 v,
4357 instruction);
4358 maybe_record_implicit_null_check_done = true;
4359 } else {
4360 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4361 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004362 break;
4363 }
4364
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365 case Primitive::kPrimVoid:
4366 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004367 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004368 }
Calin Juravle52c48962014-12-16 17:02:57 +00004369
Mark Mendellea5af682015-10-22 17:35:49 -04004370 if (!maybe_record_implicit_null_check_done) {
4371 codegen_->MaybeRecordImplicitNullCheck(instruction);
4372 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004373
4374 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4375 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4376 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004377 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004378 }
4379
Calin Juravle52c48962014-12-16 17:02:57 +00004380 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004381 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004382 }
4383}
4384
4385void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4386 HandleFieldSet(instruction, instruction->GetFieldInfo());
4387}
4388
4389void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004390 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391}
4392
4393void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004394 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395}
4396
4397void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004398 HandleFieldGet(instruction, instruction->GetFieldInfo());
4399}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400
Calin Juravle52c48962014-12-16 17:02:57 +00004401void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4402 HandleFieldGet(instruction);
4403}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404
Calin Juravle52c48962014-12-16 17:02:57 +00004405void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4406 HandleFieldGet(instruction, instruction->GetFieldInfo());
4407}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408
Calin Juravle52c48962014-12-16 17:02:57 +00004409void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4410 HandleFieldSet(instruction, instruction->GetFieldInfo());
4411}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412
Calin Juravle52c48962014-12-16 17:02:57 +00004413void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004414 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004415}
4416
Calin Juravlee460d1d2015-09-29 04:52:17 +01004417void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4418 HUnresolvedInstanceFieldGet* instruction) {
4419 FieldAccessCallingConventionX86_64 calling_convention;
4420 codegen_->CreateUnresolvedFieldLocationSummary(
4421 instruction, instruction->GetFieldType(), calling_convention);
4422}
4423
4424void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4425 HUnresolvedInstanceFieldGet* instruction) {
4426 FieldAccessCallingConventionX86_64 calling_convention;
4427 codegen_->GenerateUnresolvedFieldAccess(instruction,
4428 instruction->GetFieldType(),
4429 instruction->GetFieldIndex(),
4430 instruction->GetDexPc(),
4431 calling_convention);
4432}
4433
4434void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4435 HUnresolvedInstanceFieldSet* instruction) {
4436 FieldAccessCallingConventionX86_64 calling_convention;
4437 codegen_->CreateUnresolvedFieldLocationSummary(
4438 instruction, instruction->GetFieldType(), calling_convention);
4439}
4440
4441void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4442 HUnresolvedInstanceFieldSet* instruction) {
4443 FieldAccessCallingConventionX86_64 calling_convention;
4444 codegen_->GenerateUnresolvedFieldAccess(instruction,
4445 instruction->GetFieldType(),
4446 instruction->GetFieldIndex(),
4447 instruction->GetDexPc(),
4448 calling_convention);
4449}
4450
4451void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4452 HUnresolvedStaticFieldGet* instruction) {
4453 FieldAccessCallingConventionX86_64 calling_convention;
4454 codegen_->CreateUnresolvedFieldLocationSummary(
4455 instruction, instruction->GetFieldType(), calling_convention);
4456}
4457
4458void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4459 HUnresolvedStaticFieldGet* instruction) {
4460 FieldAccessCallingConventionX86_64 calling_convention;
4461 codegen_->GenerateUnresolvedFieldAccess(instruction,
4462 instruction->GetFieldType(),
4463 instruction->GetFieldIndex(),
4464 instruction->GetDexPc(),
4465 calling_convention);
4466}
4467
4468void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4469 HUnresolvedStaticFieldSet* instruction) {
4470 FieldAccessCallingConventionX86_64 calling_convention;
4471 codegen_->CreateUnresolvedFieldLocationSummary(
4472 instruction, instruction->GetFieldType(), calling_convention);
4473}
4474
4475void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4476 HUnresolvedStaticFieldSet* instruction) {
4477 FieldAccessCallingConventionX86_64 calling_convention;
4478 codegen_->GenerateUnresolvedFieldAccess(instruction,
4479 instruction->GetFieldType(),
4480 instruction->GetFieldIndex(),
4481 instruction->GetDexPc(),
4482 calling_convention);
4483}
4484
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004486 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4487 ? LocationSummary::kCallOnSlowPath
4488 : LocationSummary::kNoCall;
4489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4490 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004491 ? Location::RequiresRegister()
4492 : Location::Any();
4493 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004494 if (instruction->HasUses()) {
4495 locations->SetOut(Location::SameAsFirstInput());
4496 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004497}
4498
Calin Juravle2ae48182016-03-16 14:05:09 +00004499void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4500 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004501 return;
4502 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004503 LocationSummary* locations = instruction->GetLocations();
4504 Location obj = locations->InAt(0);
4505
4506 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004507 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004508}
4509
Calin Juravle2ae48182016-03-16 14:05:09 +00004510void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004511 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004512 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513
4514 LocationSummary* locations = instruction->GetLocations();
4515 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004516
4517 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004518 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004519 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004521 } else {
4522 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004523 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004524 __ jmp(slow_path->GetEntryLabel());
4525 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004526 }
4527 __ j(kEqual, slow_path->GetEntryLabel());
4528}
4529
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004530void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004531 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004532}
4533
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004535 bool object_array_get_with_read_barrier =
4536 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004537 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004538 new (GetGraph()->GetArena()) LocationSummary(instruction,
4539 object_array_get_with_read_barrier ?
4540 LocationSummary::kCallOnSlowPath :
4541 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004542 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004543 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004544 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4545 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4546 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004547 // The output overlaps for an object array get when read barriers
4548 // are enabled: we do not want the move to overwrite the array's
4549 // location, as we need it to emit the read barrier.
4550 locations->SetOut(
4551 Location::RequiresRegister(),
4552 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004553 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004554 // We need a temporary register for the read barrier marking slow
4555 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4556 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4557 locations->AddTemp(Location::RequiresRegister());
4558 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559}
4560
4561void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4562 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004563 Location obj_loc = locations->InAt(0);
4564 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004566 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004568 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004569 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 case Primitive::kPrimBoolean: {
4571 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004572 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573 if (index.IsConstant()) {
4574 __ movzxb(out, Address(obj,
4575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4576 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004577 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 }
4579 break;
4580 }
4581
4582 case Primitive::kPrimByte: {
4583 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004584 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004585 if (index.IsConstant()) {
4586 __ movsxb(out, Address(obj,
4587 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4588 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004589 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 }
4591 break;
4592 }
4593
4594 case Primitive::kPrimShort: {
4595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004596 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 if (index.IsConstant()) {
4598 __ movsxw(out, Address(obj,
4599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4600 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004601 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602 }
4603 break;
4604 }
4605
4606 case Primitive::kPrimChar: {
4607 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004608 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 if (index.IsConstant()) {
4610 __ movzxw(out, Address(obj,
4611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4612 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004613 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004614 }
4615 break;
4616 }
4617
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004618 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004620 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 if (index.IsConstant()) {
4622 __ movl(out, Address(obj,
4623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4624 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004625 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 }
4627 break;
4628 }
4629
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004630 case Primitive::kPrimNot: {
4631 static_assert(
4632 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4633 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4635 // /* HeapReference<Object> */ out =
4636 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4637 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4638 Location temp = locations->GetTemp(0);
4639 // Note that a potential implicit null check is handled in this
4640 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4641 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4642 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4643 } else {
4644 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4645 if (index.IsConstant()) {
4646 uint32_t offset =
4647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4648 __ movl(out, Address(obj, offset));
4649 codegen_->MaybeRecordImplicitNullCheck(instruction);
4650 // If read barriers are enabled, emit read barriers other than
4651 // Baker's using a slow path (and also unpoison the loaded
4652 // reference, if heap poisoning is enabled).
4653 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4654 } else {
4655 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4656 codegen_->MaybeRecordImplicitNullCheck(instruction);
4657 // If read barriers are enabled, emit read barriers other than
4658 // Baker's using a slow path (and also unpoison the loaded
4659 // reference, if heap poisoning is enabled).
4660 codegen_->MaybeGenerateReadBarrierSlow(
4661 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4662 }
4663 }
4664 break;
4665 }
4666
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 case Primitive::kPrimLong: {
4668 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004669 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 if (index.IsConstant()) {
4671 __ movq(out, Address(obj,
4672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4673 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004674 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675 }
4676 break;
4677 }
4678
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004679 case Primitive::kPrimFloat: {
4680 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004681 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004682 if (index.IsConstant()) {
4683 __ movss(out, Address(obj,
4684 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4685 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004686 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004687 }
4688 break;
4689 }
4690
4691 case Primitive::kPrimDouble: {
4692 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004693 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004694 if (index.IsConstant()) {
4695 __ movsd(out, Address(obj,
4696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4697 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004698 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004699 }
4700 break;
4701 }
4702
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004704 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004705 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 }
Roland Levillain4d027112015-07-01 15:41:14 +01004707
4708 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004709 // Potential implicit null checks, in the case of reference
4710 // arrays, are handled in the previous switch statement.
4711 } else {
4712 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004713 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714}
4715
4716void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004717 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004718
4719 bool needs_write_barrier =
4720 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004721 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004722 bool object_array_set_with_read_barrier =
4723 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004724
Nicolas Geoffray39468442014-09-02 15:17:15 +01004725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004726 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004727 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004728 LocationSummary::kCallOnSlowPath :
4729 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004730
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004731 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004732 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4733 if (Primitive::IsFloatingPointType(value_type)) {
4734 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004735 } else {
4736 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4737 }
4738
4739 if (needs_write_barrier) {
4740 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741
4742 // This first temporary register is possibly used for heap
4743 // reference poisoning and/or read barrier emission too.
4744 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747}
4748
4749void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4750 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004751 Location array_loc = locations->InAt(0);
4752 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004753 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004754 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004755 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004756 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004757 bool needs_write_barrier =
4758 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4760 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4761 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762
4763 switch (value_type) {
4764 case Primitive::kPrimBoolean:
4765 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4767 Address address = index.IsConstant()
4768 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4769 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4770 if (value.IsRegister()) {
4771 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004772 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004775 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004776 break;
4777 }
4778
4779 case Primitive::kPrimShort:
4780 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004781 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4782 Address address = index.IsConstant()
4783 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4784 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4785 if (value.IsRegister()) {
4786 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004788 DCHECK(value.IsConstant()) << value;
4789 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004791 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004792 break;
4793 }
4794
4795 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004796 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4797 Address address = index.IsConstant()
4798 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4799 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004800
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 if (!value.IsRegister()) {
4802 // Just setting null.
4803 DCHECK(instruction->InputAt(2)->IsNullConstant());
4804 DCHECK(value.IsConstant()) << value;
4805 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004806 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004807 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004808 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004809 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004810 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004811
4812 DCHECK(needs_write_barrier);
4813 CpuRegister register_value = value.AsRegister<CpuRegister>();
4814 NearLabel done, not_null, do_put;
4815 SlowPathCode* slow_path = nullptr;
4816 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004817 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004818 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4819 codegen_->AddSlowPath(slow_path);
4820 if (instruction->GetValueCanBeNull()) {
4821 __ testl(register_value, register_value);
4822 __ j(kNotEqual, &not_null);
4823 __ movl(address, Immediate(0));
4824 codegen_->MaybeRecordImplicitNullCheck(instruction);
4825 __ jmp(&done);
4826 __ Bind(&not_null);
4827 }
4828
Roland Levillain0d5a2812015-11-13 10:07:31 +00004829 if (kEmitCompilerReadBarrier) {
4830 // When read barriers are enabled, the type checking
4831 // instrumentation requires two read barriers:
4832 //
4833 // __ movl(temp2, temp);
4834 // // /* HeapReference<Class> */ temp = temp->component_type_
4835 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004836 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004837 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4838 //
4839 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4840 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004841 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004842 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4843 //
4844 // __ cmpl(temp, temp2);
4845 //
4846 // However, the second read barrier may trash `temp`, as it
4847 // is a temporary register, and as such would not be saved
4848 // along with live registers before calling the runtime (nor
4849 // restored afterwards). So in this case, we bail out and
4850 // delegate the work to the array set slow path.
4851 //
4852 // TODO: Extend the register allocator to support a new
4853 // "(locally) live temp" location so as to avoid always
4854 // going into the slow path when read barriers are enabled.
4855 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004857 // /* HeapReference<Class> */ temp = array->klass_
4858 __ movl(temp, Address(array, class_offset));
4859 codegen_->MaybeRecordImplicitNullCheck(instruction);
4860 __ MaybeUnpoisonHeapReference(temp);
4861
4862 // /* HeapReference<Class> */ temp = temp->component_type_
4863 __ movl(temp, Address(temp, component_offset));
4864 // If heap poisoning is enabled, no need to unpoison `temp`
4865 // nor the object reference in `register_value->klass`, as
4866 // we are comparing two poisoned references.
4867 __ cmpl(temp, Address(register_value, class_offset));
4868
4869 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4870 __ j(kEqual, &do_put);
4871 // If heap poisoning is enabled, the `temp` reference has
4872 // not been unpoisoned yet; unpoison it now.
4873 __ MaybeUnpoisonHeapReference(temp);
4874
4875 // /* HeapReference<Class> */ temp = temp->super_class_
4876 __ movl(temp, Address(temp, super_offset));
4877 // If heap poisoning is enabled, no need to unpoison
4878 // `temp`, as we are comparing against null below.
4879 __ testl(temp, temp);
4880 __ j(kNotEqual, slow_path->GetEntryLabel());
4881 __ Bind(&do_put);
4882 } else {
4883 __ j(kNotEqual, slow_path->GetEntryLabel());
4884 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 }
4886 }
4887
4888 if (kPoisonHeapReferences) {
4889 __ movl(temp, register_value);
4890 __ PoisonHeapReference(temp);
4891 __ movl(address, temp);
4892 } else {
4893 __ movl(address, register_value);
4894 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004895 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 codegen_->MaybeRecordImplicitNullCheck(instruction);
4897 }
4898
4899 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4900 codegen_->MarkGCCard(
4901 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4902 __ Bind(&done);
4903
4904 if (slow_path != nullptr) {
4905 __ Bind(slow_path->GetExitLabel());
4906 }
4907
4908 break;
4909 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004910
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 case Primitive::kPrimInt: {
4912 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4913 Address address = index.IsConstant()
4914 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4915 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4916 if (value.IsRegister()) {
4917 __ movl(address, value.AsRegister<CpuRegister>());
4918 } else {
4919 DCHECK(value.IsConstant()) << value;
4920 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4921 __ movl(address, Immediate(v));
4922 }
4923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004924 break;
4925 }
4926
4927 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004928 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4929 Address address = index.IsConstant()
4930 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4931 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4932 if (value.IsRegister()) {
4933 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004934 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004935 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004936 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004937 Address address_high = index.IsConstant()
4938 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4939 offset + sizeof(int32_t))
4940 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4941 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942 }
4943 break;
4944 }
4945
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004946 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004947 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).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);
Mark Mendellea5af682015-10-22 17:35:49 -04004951 if (value.IsFpuRegister()) {
4952 __ movss(address, value.AsFpuRegister<XmmRegister>());
4953 } else {
4954 DCHECK(value.IsConstant());
4955 int32_t v =
4956 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4957 __ movl(address, Immediate(v));
4958 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004959 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004960 break;
4961 }
4962
4963 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4965 Address address = index.IsConstant()
4966 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4967 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004968 if (value.IsFpuRegister()) {
4969 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4970 codegen_->MaybeRecordImplicitNullCheck(instruction);
4971 } else {
4972 int64_t v =
4973 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4974 Address address_high = index.IsConstant()
4975 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4976 offset + sizeof(int32_t))
4977 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4978 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4979 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004980 break;
4981 }
4982
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983 case Primitive::kPrimVoid:
4984 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004985 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986 }
4987}
4988
4989void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004990 LocationSummary* locations =
4991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004992 locations->SetInAt(0, Location::RequiresRegister());
4993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004994}
4995
4996void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4997 LocationSummary* locations = instruction->GetLocations();
4998 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004999 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5000 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005002 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003}
5004
5005void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005006 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5007 ? LocationSummary::kCallOnSlowPath
5008 : LocationSummary::kNoCall;
5009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005010 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005011 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005012 if (instruction->HasUses()) {
5013 locations->SetOut(Location::SameAsFirstInput());
5014 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015}
5016
5017void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5018 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005019 Location index_loc = locations->InAt(0);
5020 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005021 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005022 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023
Mark Mendell99dbd682015-04-22 16:18:52 -04005024 if (length_loc.IsConstant()) {
5025 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5026 if (index_loc.IsConstant()) {
5027 // BCE will remove the bounds check if we are guarenteed to pass.
5028 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5029 if (index < 0 || index >= length) {
5030 codegen_->AddSlowPath(slow_path);
5031 __ jmp(slow_path->GetEntryLabel());
5032 } else {
5033 // Some optimization after BCE may have generated this, and we should not
5034 // generate a bounds check if it is a valid range.
5035 }
5036 return;
5037 }
5038
5039 // We have to reverse the jump condition because the length is the constant.
5040 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5041 __ cmpl(index_reg, Immediate(length));
5042 codegen_->AddSlowPath(slow_path);
5043 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005044 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005045 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5046 if (index_loc.IsConstant()) {
5047 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5048 __ cmpl(length, Immediate(value));
5049 } else {
5050 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5051 }
5052 codegen_->AddSlowPath(slow_path);
5053 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055}
5056
5057void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5058 CpuRegister card,
5059 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005060 CpuRegister value,
5061 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005062 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005063 if (value_can_be_null) {
5064 __ testl(value, value);
5065 __ j(kEqual, &is_null);
5066 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005067 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5068 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 __ movq(temp, object);
5070 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005071 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005072 if (value_can_be_null) {
5073 __ Bind(&is_null);
5074 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075}
5076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005077void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005078 LOG(FATAL) << "Unimplemented";
5079}
5080
5081void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005082 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5083}
5084
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005085void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5087}
5088
5089void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005090 HBasicBlock* block = instruction->GetBlock();
5091 if (block->GetLoopInformation() != nullptr) {
5092 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5093 // The back edge will generate the suspend check.
5094 return;
5095 }
5096 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5097 // The goto will generate the suspend check.
5098 return;
5099 }
5100 GenerateSuspendCheck(instruction, nullptr);
5101}
5102
5103void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5104 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005105 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005106 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5107 if (slow_path == nullptr) {
5108 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5109 instruction->SetSlowPath(slow_path);
5110 codegen_->AddSlowPath(slow_path);
5111 if (successor != nullptr) {
5112 DCHECK(successor->IsLoopHeader());
5113 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5114 }
5115 } else {
5116 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5117 }
5118
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005119 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5120 /* no_rip */ true),
5121 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005122 if (successor == nullptr) {
5123 __ j(kNotEqual, slow_path->GetEntryLabel());
5124 __ Bind(slow_path->GetReturnLabel());
5125 } else {
5126 __ j(kEqual, codegen_->GetLabelOf(successor));
5127 __ jmp(slow_path->GetEntryLabel());
5128 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005129}
5130
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005131X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5132 return codegen_->GetAssembler();
5133}
5134
5135void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005136 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005137 Location source = move->GetSource();
5138 Location destination = move->GetDestination();
5139
5140 if (source.IsRegister()) {
5141 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005144 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005145 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005146 } else {
5147 DCHECK(destination.IsDoubleStackSlot());
5148 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005150 }
5151 } else if (source.IsStackSlot()) {
5152 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005154 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005155 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005157 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005158 } else {
5159 DCHECK(destination.IsStackSlot());
5160 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5161 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5162 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005163 } else if (source.IsDoubleStackSlot()) {
5164 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005165 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005166 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005167 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005168 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5169 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005170 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005171 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005172 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5173 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5174 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005175 } else if (source.IsConstant()) {
5176 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005177 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5178 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005179 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005180 if (value == 0) {
5181 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5182 } else {
5183 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5184 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005185 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005186 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005187 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005188 }
5189 } else if (constant->IsLongConstant()) {
5190 int64_t value = constant->AsLongConstant()->GetValue();
5191 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005192 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005193 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005194 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005195 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005196 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005198 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005199 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005200 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005201 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005202 } else {
5203 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005204 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5206 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005207 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005208 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005209 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005210 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005212 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005213 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 } else {
5215 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005216 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005218 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 } else if (source.IsFpuRegister()) {
5220 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005221 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005222 } else if (destination.IsStackSlot()) {
5223 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005224 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005225 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005226 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005227 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005228 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005229 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 }
5231}
5232
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005233void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005234 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005235 __ movl(Address(CpuRegister(RSP), mem), reg);
5236 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005237}
5238
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005239void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005240 ScratchRegisterScope ensure_scratch(
5241 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5242
5243 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5244 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5245 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5246 Address(CpuRegister(RSP), mem2 + stack_offset));
5247 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5248 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5249 CpuRegister(ensure_scratch.GetRegister()));
5250}
5251
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005252void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5253 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5254 __ movq(Address(CpuRegister(RSP), mem), reg);
5255 __ movq(reg, CpuRegister(TMP));
5256}
5257
5258void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5259 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005260 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005261
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005262 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5263 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5264 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5265 Address(CpuRegister(RSP), mem2 + stack_offset));
5266 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5267 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5268 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005269}
5270
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005271void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5272 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5273 __ movss(Address(CpuRegister(RSP), mem), reg);
5274 __ movd(reg, CpuRegister(TMP));
5275}
5276
5277void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5278 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5279 __ movsd(Address(CpuRegister(RSP), mem), reg);
5280 __ movd(reg, CpuRegister(TMP));
5281}
5282
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005283void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005284 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005285 Location source = move->GetSource();
5286 Location destination = move->GetDestination();
5287
5288 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005289 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005290 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005291 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005292 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005294 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005295 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5296 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005297 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005298 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005300 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5301 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5304 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5305 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005307 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005309 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005311 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005312 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005313 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005314 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005315 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005316 }
5317}
5318
5319
5320void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5321 __ pushq(CpuRegister(reg));
5322}
5323
5324
5325void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5326 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005327}
5328
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005329void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005330 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005331 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5332 Immediate(mirror::Class::kStatusInitialized));
5333 __ j(kLess, slow_path->GetEntryLabel());
5334 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005335 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005336}
5337
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005338void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005339 InvokeRuntimeCallingConvention calling_convention;
5340 CodeGenerator::CreateLoadClassLocationSummary(
5341 cls,
5342 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 Location::RegisterLocation(RAX),
5344 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005345}
5346
5347void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005348 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005349 if (cls->NeedsAccessCheck()) {
5350 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5351 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5352 cls,
5353 cls->GetDexPc(),
5354 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005355 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005356 return;
5357 }
5358
Roland Levillain0d5a2812015-11-13 10:07:31 +00005359 Location out_loc = locations->Out();
5360 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005361 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005362
Calin Juravle580b6092015-10-06 17:35:58 +01005363 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005364 DCHECK(!cls->CanCallRuntime());
5365 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005366 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5367 GenerateGcRootFieldLoad(
5368 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005369 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005370 // /* GcRoot<mirror::Class>[] */ out =
5371 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5372 __ movq(out, Address(current_method,
5373 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005374 // /* GcRoot<mirror::Class> */ out = out[type_index]
5375 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005376
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005377 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5378 DCHECK(cls->CanCallRuntime());
5379 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5380 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5381 codegen_->AddSlowPath(slow_path);
5382 if (!cls->IsInDexCache()) {
5383 __ testl(out, out);
5384 __ j(kEqual, slow_path->GetEntryLabel());
5385 }
5386 if (cls->MustGenerateClinitCheck()) {
5387 GenerateClassInitializationCheck(slow_path, out);
5388 } else {
5389 __ Bind(slow_path->GetExitLabel());
5390 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005391 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005392 }
5393}
5394
5395void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5396 LocationSummary* locations =
5397 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5398 locations->SetInAt(0, Location::RequiresRegister());
5399 if (check->HasUses()) {
5400 locations->SetOut(Location::SameAsFirstInput());
5401 }
5402}
5403
5404void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005405 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005406 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005407 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005408 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005409 GenerateClassInitializationCheck(slow_path,
5410 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005411}
5412
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005413void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005414 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5415 ? LocationSummary::kCallOnSlowPath
5416 : LocationSummary::kNoCall;
5417 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005418 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005419 locations->SetOut(Location::RequiresRegister());
5420}
5421
5422void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005423 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005424 Location out_loc = locations->Out();
5425 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005426 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005427
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005428 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5429 GenerateGcRootFieldLoad(
5430 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005431 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5432 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005433 // /* GcRoot<mirror::String> */ out = out[string_index]
5434 GenerateGcRootFieldLoad(
5435 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005436
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005437 if (!load->IsInDexCache()) {
5438 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5439 codegen_->AddSlowPath(slow_path);
5440 __ testl(out, out);
5441 __ j(kEqual, slow_path->GetEntryLabel());
5442 __ Bind(slow_path->GetExitLabel());
5443 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005444}
5445
David Brazdilcb1c0552015-08-04 16:22:25 +01005446static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005447 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5448 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005449}
5450
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005451void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5452 LocationSummary* locations =
5453 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5454 locations->SetOut(Location::RequiresRegister());
5455}
5456
5457void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005458 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5459}
5460
5461void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5462 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5463}
5464
5465void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5466 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005467}
5468
5469void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5470 LocationSummary* locations =
5471 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5472 InvokeRuntimeCallingConvention calling_convention;
5473 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5474}
5475
5476void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005477 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5478 instruction,
5479 instruction->GetDexPc(),
5480 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005481 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005482}
5483
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005484static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5485 return kEmitCompilerReadBarrier &&
5486 (kUseBakerReadBarrier ||
5487 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5488 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5489 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5490}
5491
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005492void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005493 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005494 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5495 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005496 case TypeCheckKind::kExactCheck:
5497 case TypeCheckKind::kAbstractClassCheck:
5498 case TypeCheckKind::kClassHierarchyCheck:
5499 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005500 call_kind =
5501 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 break;
5503 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504 case TypeCheckKind::kUnresolvedCheck:
5505 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 call_kind = LocationSummary::kCallOnSlowPath;
5507 break;
5508 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005509
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005511 locations->SetInAt(0, Location::RequiresRegister());
5512 locations->SetInAt(1, Location::Any());
5513 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5514 locations->SetOut(Location::RequiresRegister());
5515 // When read barriers are enabled, we need a temporary register for
5516 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005517 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005518 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005519 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005520}
5521
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005522void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005523 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005524 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005525 Location obj_loc = locations->InAt(0);
5526 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005527 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005528 Location out_loc = locations->Out();
5529 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005530 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005531 locations->GetTemp(0) :
5532 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005533 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005534 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5535 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5536 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005537 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005538 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005539
5540 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005541 // Avoid null check if we know obj is not null.
5542 if (instruction->MustDoNullCheck()) {
5543 __ testl(obj, obj);
5544 __ j(kEqual, &zero);
5545 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005546
Roland Levillain0d5a2812015-11-13 10:07:31 +00005547 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005548 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005549
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005550 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005551 case TypeCheckKind::kExactCheck: {
5552 if (cls.IsRegister()) {
5553 __ cmpl(out, cls.AsRegister<CpuRegister>());
5554 } else {
5555 DCHECK(cls.IsStackSlot()) << cls;
5556 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5557 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005558 if (zero.IsLinked()) {
5559 // Classes must be equal for the instanceof to succeed.
5560 __ j(kNotEqual, &zero);
5561 __ movl(out, Immediate(1));
5562 __ jmp(&done);
5563 } else {
5564 __ setcc(kEqual, out);
5565 // setcc only sets the low byte.
5566 __ andl(out, Immediate(1));
5567 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568 break;
5569 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005570
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005571 case TypeCheckKind::kAbstractClassCheck: {
5572 // If the class is abstract, we eagerly fetch the super class of the
5573 // object to avoid doing a comparison we know will fail.
5574 NearLabel loop, success;
5575 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005576 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005577 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005578 __ testl(out, out);
5579 // If `out` is null, we use it for the result, and jump to `done`.
5580 __ j(kEqual, &done);
5581 if (cls.IsRegister()) {
5582 __ cmpl(out, cls.AsRegister<CpuRegister>());
5583 } else {
5584 DCHECK(cls.IsStackSlot()) << cls;
5585 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5586 }
5587 __ j(kNotEqual, &loop);
5588 __ movl(out, Immediate(1));
5589 if (zero.IsLinked()) {
5590 __ jmp(&done);
5591 }
5592 break;
5593 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005594
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 case TypeCheckKind::kClassHierarchyCheck: {
5596 // Walk over the class hierarchy to find a match.
5597 NearLabel loop, success;
5598 __ Bind(&loop);
5599 if (cls.IsRegister()) {
5600 __ cmpl(out, cls.AsRegister<CpuRegister>());
5601 } else {
5602 DCHECK(cls.IsStackSlot()) << cls;
5603 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5604 }
5605 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005606 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005607 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005608 __ testl(out, out);
5609 __ j(kNotEqual, &loop);
5610 // If `out` is null, we use it for the result, and jump to `done`.
5611 __ jmp(&done);
5612 __ Bind(&success);
5613 __ movl(out, Immediate(1));
5614 if (zero.IsLinked()) {
5615 __ jmp(&done);
5616 }
5617 break;
5618 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005619
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005620 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005621 // Do an exact check.
5622 NearLabel exact_check;
5623 if (cls.IsRegister()) {
5624 __ cmpl(out, cls.AsRegister<CpuRegister>());
5625 } else {
5626 DCHECK(cls.IsStackSlot()) << cls;
5627 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5628 }
5629 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005630 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005631 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005632 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633 __ testl(out, out);
5634 // If `out` is null, we use it for the result, and jump to `done`.
5635 __ j(kEqual, &done);
5636 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5637 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005638 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005639 __ movl(out, Immediate(1));
5640 __ jmp(&done);
5641 break;
5642 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kArrayCheck: {
5645 if (cls.IsRegister()) {
5646 __ cmpl(out, cls.AsRegister<CpuRegister>());
5647 } else {
5648 DCHECK(cls.IsStackSlot()) << cls;
5649 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5650 }
5651 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005652 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5653 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 codegen_->AddSlowPath(slow_path);
5655 __ j(kNotEqual, slow_path->GetEntryLabel());
5656 __ movl(out, Immediate(1));
5657 if (zero.IsLinked()) {
5658 __ jmp(&done);
5659 }
5660 break;
5661 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662
Calin Juravle98893e12015-10-02 21:05:03 +01005663 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005664 case TypeCheckKind::kInterfaceCheck: {
5665 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005666 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005667 // cases.
5668 //
5669 // We cannot directly call the InstanceofNonTrivial runtime
5670 // entry point without resorting to a type checking slow path
5671 // here (i.e. by calling InvokeRuntime directly), as it would
5672 // require to assign fixed registers for the inputs of this
5673 // HInstanceOf instruction (following the runtime calling
5674 // convention), which might be cluttered by the potential first
5675 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005676 //
5677 // TODO: Introduce a new runtime entry point taking the object
5678 // to test (instead of its class) as argument, and let it deal
5679 // with the read barrier issues. This will let us refactor this
5680 // case of the `switch` code as it was previously (with a direct
5681 // call to the runtime not using a type checking slow path).
5682 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005683 DCHECK(locations->OnlyCallsOnSlowPath());
5684 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5685 /* is_fatal */ false);
5686 codegen_->AddSlowPath(slow_path);
5687 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005688 if (zero.IsLinked()) {
5689 __ jmp(&done);
5690 }
5691 break;
5692 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005693 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005694
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005695 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005696 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 __ xorl(out, out);
5698 }
5699
5700 if (done.IsLinked()) {
5701 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005702 }
5703
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005704 if (slow_path != nullptr) {
5705 __ Bind(slow_path->GetExitLabel());
5706 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005707}
5708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005709void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005710 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5711 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5713 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 case TypeCheckKind::kExactCheck:
5715 case TypeCheckKind::kAbstractClassCheck:
5716 case TypeCheckKind::kClassHierarchyCheck:
5717 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005718 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5719 LocationSummary::kCallOnSlowPath :
5720 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005721 break;
5722 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005723 case TypeCheckKind::kUnresolvedCheck:
5724 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005725 call_kind = LocationSummary::kCallOnSlowPath;
5726 break;
5727 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5729 locations->SetInAt(0, Location::RequiresRegister());
5730 locations->SetInAt(1, Location::Any());
5731 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5732 locations->AddTemp(Location::RequiresRegister());
5733 // When read barriers are enabled, we need an additional temporary
5734 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005735 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005738}
5739
5740void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005741 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005742 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743 Location obj_loc = locations->InAt(0);
5744 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005745 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 Location temp_loc = locations->GetTemp(0);
5747 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005748 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005749 locations->GetTemp(1) :
5750 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5752 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5753 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5754 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005755
Roland Levillain0d5a2812015-11-13 10:07:31 +00005756 bool is_type_check_slow_path_fatal =
5757 (type_check_kind == TypeCheckKind::kExactCheck ||
5758 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5759 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5760 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5761 !instruction->CanThrowIntoCatchBlock();
5762 SlowPathCode* type_check_slow_path =
5763 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5764 is_type_check_slow_path_fatal);
5765 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005766
Roland Levillain0d5a2812015-11-13 10:07:31 +00005767 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 case TypeCheckKind::kExactCheck:
5769 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005770 NearLabel done;
5771 // Avoid null check if we know obj is not null.
5772 if (instruction->MustDoNullCheck()) {
5773 __ testl(obj, obj);
5774 __ j(kEqual, &done);
5775 }
5776
5777 // /* HeapReference<Class> */ temp = obj->klass_
5778 GenerateReferenceLoadTwoRegisters(
5779 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5780
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 if (cls.IsRegister()) {
5782 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5783 } else {
5784 DCHECK(cls.IsStackSlot()) << cls;
5785 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5786 }
5787 // Jump to slow path for throwing the exception or doing a
5788 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005789 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005790 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005791 break;
5792 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005793
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005795 NearLabel done;
5796 // Avoid null check if we know obj is not null.
5797 if (instruction->MustDoNullCheck()) {
5798 __ testl(obj, obj);
5799 __ j(kEqual, &done);
5800 }
5801
5802 // /* HeapReference<Class> */ temp = obj->klass_
5803 GenerateReferenceLoadTwoRegisters(
5804 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5805
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 // If the class is abstract, we eagerly fetch the super class of the
5807 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005810 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005811 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005812
5813 // If the class reference currently in `temp` is not null, jump
5814 // to the `compare_classes` label to compare it with the checked
5815 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005816 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 __ j(kNotEqual, &compare_classes);
5818 // Otherwise, jump to the slow path to throw the exception.
5819 //
5820 // But before, move back the object's class into `temp` before
5821 // going into the slow path, as it has been overwritten in the
5822 // meantime.
5823 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005824 GenerateReferenceLoadTwoRegisters(
5825 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005826 __ jmp(type_check_slow_path->GetEntryLabel());
5827
5828 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 if (cls.IsRegister()) {
5830 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5831 } else {
5832 DCHECK(cls.IsStackSlot()) << cls;
5833 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5834 }
5835 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005836 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 break;
5838 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005841 NearLabel done;
5842 // Avoid null check if we know obj is not null.
5843 if (instruction->MustDoNullCheck()) {
5844 __ testl(obj, obj);
5845 __ j(kEqual, &done);
5846 }
5847
5848 // /* HeapReference<Class> */ temp = obj->klass_
5849 GenerateReferenceLoadTwoRegisters(
5850 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5851
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005853 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005854 __ Bind(&loop);
5855 if (cls.IsRegister()) {
5856 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5857 } else {
5858 DCHECK(cls.IsStackSlot()) << cls;
5859 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5860 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005861 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862
Roland Levillain0d5a2812015-11-13 10:07:31 +00005863 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005864 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005865
5866 // If the class reference currently in `temp` is not null, jump
5867 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005868 __ testl(temp, temp);
5869 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005870 // Otherwise, jump to the slow path to throw the exception.
5871 //
5872 // But before, move back the object's class into `temp` before
5873 // going into the slow path, as it has been overwritten in the
5874 // meantime.
5875 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005876 GenerateReferenceLoadTwoRegisters(
5877 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005879 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 break;
5881 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005882
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005884 // We cannot use a NearLabel here, as its range might be too
5885 // short in some cases when read barriers are enabled. This has
5886 // been observed for instance when the code emitted for this
5887 // case uses high x86-64 registers (R8-R15).
5888 Label done;
5889 // Avoid null check if we know obj is not null.
5890 if (instruction->MustDoNullCheck()) {
5891 __ testl(obj, obj);
5892 __ j(kEqual, &done);
5893 }
5894
5895 // /* HeapReference<Class> */ temp = obj->klass_
5896 GenerateReferenceLoadTwoRegisters(
5897 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5898
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005899 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005901 if (cls.IsRegister()) {
5902 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5903 } else {
5904 DCHECK(cls.IsStackSlot()) << cls;
5905 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5906 }
5907 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908
5909 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005910 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005911 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912
5913 // If the component type is not null (i.e. the object is indeed
5914 // an array), jump to label `check_non_primitive_component_type`
5915 // to further check that this component type is not a primitive
5916 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005918 __ j(kNotEqual, &check_non_primitive_component_type);
5919 // Otherwise, jump to the slow path to throw the exception.
5920 //
5921 // But before, move back the object's class into `temp` before
5922 // going into the slow path, as it has been overwritten in the
5923 // meantime.
5924 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005925 GenerateReferenceLoadTwoRegisters(
5926 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005927 __ jmp(type_check_slow_path->GetEntryLabel());
5928
5929 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 __ j(kEqual, &done);
5932 // Same comment as above regarding `temp` and the slow path.
5933 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005934 GenerateReferenceLoadTwoRegisters(
5935 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005937 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 break;
5939 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005940
Calin Juravle98893e12015-10-02 21:05:03 +01005941 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00005943 NearLabel done;
5944 // Avoid null check if we know obj is not null.
5945 if (instruction->MustDoNullCheck()) {
5946 __ testl(obj, obj);
5947 __ j(kEqual, &done);
5948 }
5949
5950 // /* HeapReference<Class> */ temp = obj->klass_
5951 GenerateReferenceLoadTwoRegisters(
5952 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5953
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005954 // We always go into the type check slow path for the unresolved
5955 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005956 //
5957 // We cannot directly call the CheckCast runtime entry point
5958 // without resorting to a type checking slow path here (i.e. by
5959 // calling InvokeRuntime directly), as it would require to
5960 // assign fixed registers for the inputs of this HInstanceOf
5961 // instruction (following the runtime calling convention), which
5962 // might be cluttered by the potential first read barrier
5963 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005964 //
5965 // TODO: Introduce a new runtime entry point taking the object
5966 // to test (instead of its class) as argument, and let it deal
5967 // with the read barrier issues. This will let us refactor this
5968 // case of the `switch` code as it was previously (with a direct
5969 // call to the runtime not using a type checking slow path).
5970 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005971 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005972 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 break;
5974 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005975
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005977}
5978
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005979void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5980 LocationSummary* locations =
5981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5982 InvokeRuntimeCallingConvention calling_convention;
5983 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5984}
5985
5986void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005987 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5988 : QUICK_ENTRY_POINT(pUnlockObject),
5989 instruction,
5990 instruction->GetDexPc(),
5991 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005992 if (instruction->IsEnter()) {
5993 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5994 } else {
5995 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5996 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005997}
5998
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005999void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6000void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6001void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6002
6003void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6004 LocationSummary* locations =
6005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6006 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6007 || instruction->GetResultType() == Primitive::kPrimLong);
6008 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006009 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006010 locations->SetOut(Location::SameAsFirstInput());
6011}
6012
6013void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6014 HandleBitwiseOperation(instruction);
6015}
6016
6017void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6018 HandleBitwiseOperation(instruction);
6019}
6020
6021void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6022 HandleBitwiseOperation(instruction);
6023}
6024
6025void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6026 LocationSummary* locations = instruction->GetLocations();
6027 Location first = locations->InAt(0);
6028 Location second = locations->InAt(1);
6029 DCHECK(first.Equals(locations->Out()));
6030
6031 if (instruction->GetResultType() == Primitive::kPrimInt) {
6032 if (second.IsRegister()) {
6033 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006034 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006035 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006036 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006037 } else {
6038 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006039 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006040 }
6041 } else if (second.IsConstant()) {
6042 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6043 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006044 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006045 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006046 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006047 } else {
6048 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006049 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006050 }
6051 } else {
6052 Address address(CpuRegister(RSP), second.GetStackIndex());
6053 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006054 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006055 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006056 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006057 } else {
6058 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006059 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006060 }
6061 }
6062 } else {
6063 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006064 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6065 bool second_is_constant = false;
6066 int64_t value = 0;
6067 if (second.IsConstant()) {
6068 second_is_constant = true;
6069 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006070 }
Mark Mendell40741f32015-04-20 22:10:34 -04006071 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006072
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006073 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006074 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006075 if (is_int32_value) {
6076 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6077 } else {
6078 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6079 }
6080 } else if (second.IsDoubleStackSlot()) {
6081 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006082 } else {
6083 __ andq(first_reg, second.AsRegister<CpuRegister>());
6084 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006085 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006086 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006087 if (is_int32_value) {
6088 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6089 } else {
6090 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6091 }
6092 } else if (second.IsDoubleStackSlot()) {
6093 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006094 } else {
6095 __ orq(first_reg, second.AsRegister<CpuRegister>());
6096 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006097 } else {
6098 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006099 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006100 if (is_int32_value) {
6101 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6102 } else {
6103 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6104 }
6105 } else if (second.IsDoubleStackSlot()) {
6106 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006107 } else {
6108 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6109 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006110 }
6111 }
6112}
6113
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006114void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6115 Location out,
6116 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006117 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006118 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6119 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006120 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006121 if (kUseBakerReadBarrier) {
6122 // Load with fast path based Baker's read barrier.
6123 // /* HeapReference<Object> */ out = *(out + offset)
6124 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006125 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006126 } else {
6127 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006128 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006129 // in the following move operation, as we will need it for the
6130 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006131 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006132 // /* HeapReference<Object> */ out = *(out + offset)
6133 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006134 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006135 }
6136 } else {
6137 // Plain load with no read barrier.
6138 // /* HeapReference<Object> */ out = *(out + offset)
6139 __ movl(out_reg, Address(out_reg, offset));
6140 __ MaybeUnpoisonHeapReference(out_reg);
6141 }
6142}
6143
6144void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6145 Location out,
6146 Location obj,
6147 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006148 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006149 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6150 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6151 if (kEmitCompilerReadBarrier) {
6152 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006153 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006154 // Load with fast path based Baker's read barrier.
6155 // /* HeapReference<Object> */ out = *(obj + offset)
6156 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006157 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006158 } else {
6159 // Load with slow path based read barrier.
6160 // /* HeapReference<Object> */ out = *(obj + offset)
6161 __ movl(out_reg, Address(obj_reg, offset));
6162 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6163 }
6164 } else {
6165 // Plain load with no read barrier.
6166 // /* HeapReference<Object> */ out = *(obj + offset)
6167 __ movl(out_reg, Address(obj_reg, offset));
6168 __ MaybeUnpoisonHeapReference(out_reg);
6169 }
6170}
6171
6172void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6173 Location root,
6174 CpuRegister obj,
6175 uint32_t offset) {
6176 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6177 if (kEmitCompilerReadBarrier) {
6178 if (kUseBakerReadBarrier) {
6179 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6180 // Baker's read barrier are used:
6181 //
6182 // root = obj.field;
6183 // if (Thread::Current()->GetIsGcMarking()) {
6184 // root = ReadBarrier::Mark(root)
6185 // }
6186
6187 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6188 __ movl(root_reg, Address(obj, offset));
6189 static_assert(
6190 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6191 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6192 "have different sizes.");
6193 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6194 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6195 "have different sizes.");
6196
6197 // Slow path used to mark the GC root `root`.
6198 SlowPathCode* slow_path =
6199 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6200 codegen_->AddSlowPath(slow_path);
6201
6202 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6203 /* no_rip */ true),
6204 Immediate(0));
6205 __ j(kNotEqual, slow_path->GetEntryLabel());
6206 __ Bind(slow_path->GetExitLabel());
6207 } else {
6208 // GC root loaded through a slow path for read barriers other
6209 // than Baker's.
6210 // /* GcRoot<mirror::Object>* */ root = obj + offset
6211 __ leaq(root_reg, Address(obj, offset));
6212 // /* mirror::Object* */ root = root->Read()
6213 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6214 }
6215 } else {
6216 // Plain GC root load with no read barrier.
6217 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6218 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006219 // Note that GC roots are not affected by heap poisoning, thus we
6220 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006221 }
6222}
6223
6224void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6225 Location ref,
6226 CpuRegister obj,
6227 uint32_t offset,
6228 Location temp,
6229 bool needs_null_check) {
6230 DCHECK(kEmitCompilerReadBarrier);
6231 DCHECK(kUseBakerReadBarrier);
6232
6233 // /* HeapReference<Object> */ ref = *(obj + offset)
6234 Address src(obj, offset);
6235 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6236}
6237
6238void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6239 Location ref,
6240 CpuRegister obj,
6241 uint32_t data_offset,
6242 Location index,
6243 Location temp,
6244 bool needs_null_check) {
6245 DCHECK(kEmitCompilerReadBarrier);
6246 DCHECK(kUseBakerReadBarrier);
6247
6248 // /* HeapReference<Object> */ ref =
6249 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6250 Address src = index.IsConstant() ?
6251 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6252 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6253 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6254}
6255
6256void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6257 Location ref,
6258 CpuRegister obj,
6259 const Address& src,
6260 Location temp,
6261 bool needs_null_check) {
6262 DCHECK(kEmitCompilerReadBarrier);
6263 DCHECK(kUseBakerReadBarrier);
6264
6265 // In slow path based read barriers, the read barrier call is
6266 // inserted after the original load. However, in fast path based
6267 // Baker's read barriers, we need to perform the load of
6268 // mirror::Object::monitor_ *before* the original reference load.
6269 // This load-load ordering is required by the read barrier.
6270 // The fast path/slow path (for Baker's algorithm) should look like:
6271 //
6272 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6273 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6274 // HeapReference<Object> ref = *src; // Original reference load.
6275 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6276 // if (is_gray) {
6277 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6278 // }
6279 //
6280 // Note: the original implementation in ReadBarrier::Barrier is
6281 // slightly more complex as:
6282 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006283 // the high-bits of rb_state, which are expected to be all zeroes
6284 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6285 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006286 // - it performs additional checks that we do not do here for
6287 // performance reasons.
6288
6289 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6290 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6291 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6292
6293 // /* int32_t */ monitor = obj->monitor_
6294 __ movl(temp_reg, Address(obj, monitor_offset));
6295 if (needs_null_check) {
6296 MaybeRecordImplicitNullCheck(instruction);
6297 }
6298 // /* LockWord */ lock_word = LockWord(monitor)
6299 static_assert(sizeof(LockWord) == sizeof(int32_t),
6300 "art::LockWord and int32_t have different sizes.");
6301 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6302 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6303 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6304 static_assert(
6305 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6306 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6307
6308 // Load fence to prevent load-load reordering.
6309 // Note that this is a no-op, thanks to the x86-64 memory model.
6310 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6311
6312 // The actual reference load.
6313 // /* HeapReference<Object> */ ref = *src
6314 __ movl(ref_reg, src);
6315
6316 // Object* ref = ref_addr->AsMirrorPtr()
6317 __ MaybeUnpoisonHeapReference(ref_reg);
6318
6319 // Slow path used to mark the object `ref` when it is gray.
6320 SlowPathCode* slow_path =
6321 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6322 AddSlowPath(slow_path);
6323
6324 // if (rb_state == ReadBarrier::gray_ptr_)
6325 // ref = ReadBarrier::Mark(ref);
6326 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6327 __ j(kEqual, slow_path->GetEntryLabel());
6328 __ Bind(slow_path->GetExitLabel());
6329}
6330
6331void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6332 Location out,
6333 Location ref,
6334 Location obj,
6335 uint32_t offset,
6336 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337 DCHECK(kEmitCompilerReadBarrier);
6338
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006339 // Insert a slow path based read barrier *after* the reference load.
6340 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 // If heap poisoning is enabled, the unpoisoning of the loaded
6342 // reference will be carried out by the runtime within the slow
6343 // path.
6344 //
6345 // Note that `ref` currently does not get unpoisoned (when heap
6346 // poisoning is enabled), which is alright as the `ref` argument is
6347 // not used by the artReadBarrierSlow entry point.
6348 //
6349 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6350 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6351 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6352 AddSlowPath(slow_path);
6353
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354 __ jmp(slow_path->GetEntryLabel());
6355 __ Bind(slow_path->GetExitLabel());
6356}
6357
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006358void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6359 Location out,
6360 Location ref,
6361 Location obj,
6362 uint32_t offset,
6363 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006364 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006365 // Baker's read barriers shall be handled by the fast path
6366 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6367 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368 // If heap poisoning is enabled, unpoisoning will be taken care of
6369 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006370 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006371 } else if (kPoisonHeapReferences) {
6372 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6373 }
6374}
6375
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006376void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6377 Location out,
6378 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379 DCHECK(kEmitCompilerReadBarrier);
6380
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006381 // Insert a slow path based read barrier *after* the GC root load.
6382 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006383 // Note that GC roots are not affected by heap poisoning, so we do
6384 // not need to do anything special for this here.
6385 SlowPathCode* slow_path =
6386 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6387 AddSlowPath(slow_path);
6388
Roland Levillain0d5a2812015-11-13 10:07:31 +00006389 __ jmp(slow_path->GetEntryLabel());
6390 __ Bind(slow_path->GetExitLabel());
6391}
6392
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006393void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006394 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006395 LOG(FATAL) << "Unreachable";
6396}
6397
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006398void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006399 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006400 LOG(FATAL) << "Unreachable";
6401}
6402
Mark Mendellfe57faa2015-09-18 09:26:15 -04006403// Simple implementation of packed switch - generate cascaded compare/jumps.
6404void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6405 LocationSummary* locations =
6406 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6407 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006408 locations->AddTemp(Location::RequiresRegister());
6409 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006410}
6411
6412void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6413 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006414 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006415 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006416 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6417 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6418 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006419 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6420
6421 // Should we generate smaller inline compare/jumps?
6422 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6423 // Figure out the correct compare values and jump conditions.
6424 // Handle the first compare/branch as a special case because it might
6425 // jump to the default case.
6426 DCHECK_GT(num_entries, 2u);
6427 Condition first_condition;
6428 uint32_t index;
6429 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6430 if (lower_bound != 0) {
6431 first_condition = kLess;
6432 __ cmpl(value_reg_in, Immediate(lower_bound));
6433 __ j(first_condition, codegen_->GetLabelOf(default_block));
6434 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6435
6436 index = 1;
6437 } else {
6438 // Handle all the compare/jumps below.
6439 first_condition = kBelow;
6440 index = 0;
6441 }
6442
6443 // Handle the rest of the compare/jumps.
6444 for (; index + 1 < num_entries; index += 2) {
6445 int32_t compare_to_value = lower_bound + index + 1;
6446 __ cmpl(value_reg_in, Immediate(compare_to_value));
6447 // Jump to successors[index] if value < case_value[index].
6448 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6449 // Jump to successors[index + 1] if value == case_value[index + 1].
6450 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6451 }
6452
6453 if (index != num_entries) {
6454 // There are an odd number of entries. Handle the last one.
6455 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006456 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006457 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6458 }
6459
6460 // And the default for any other value.
6461 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6462 __ jmp(codegen_->GetLabelOf(default_block));
6463 }
6464 return;
6465 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006466
6467 // Remove the bias, if needed.
6468 Register value_reg_out = value_reg_in.AsRegister();
6469 if (lower_bound != 0) {
6470 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6471 value_reg_out = temp_reg.AsRegister();
6472 }
6473 CpuRegister value_reg(value_reg_out);
6474
6475 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006476 __ cmpl(value_reg, Immediate(num_entries - 1));
6477 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006478
Mark Mendell9c86b482015-09-18 13:36:07 -04006479 // We are in the range of the table.
6480 // Load the address of the jump table in the constant area.
6481 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006482
Mark Mendell9c86b482015-09-18 13:36:07 -04006483 // Load the (signed) offset from the jump table.
6484 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6485
6486 // Add the offset to the address of the table base.
6487 __ addq(temp_reg, base_reg);
6488
6489 // And jump.
6490 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006491}
6492
Aart Bikc5d47542016-01-27 17:00:35 -08006493void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6494 if (value == 0) {
6495 __ xorl(dest, dest);
6496 } else {
6497 __ movl(dest, Immediate(value));
6498 }
6499}
6500
Mark Mendell92e83bf2015-05-07 11:25:03 -04006501void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6502 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006503 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006504 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006505 } else if (IsUint<32>(value)) {
6506 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006507 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6508 } else {
6509 __ movq(dest, Immediate(value));
6510 }
6511}
6512
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006513void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6514 if (value == 0) {
6515 __ xorps(dest, dest);
6516 } else {
6517 __ movss(dest, LiteralInt32Address(value));
6518 }
6519}
6520
6521void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6522 if (value == 0) {
6523 __ xorpd(dest, dest);
6524 } else {
6525 __ movsd(dest, LiteralInt64Address(value));
6526 }
6527}
6528
6529void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6530 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6531}
6532
6533void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6534 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6535}
6536
Aart Bika19616e2016-02-01 18:57:58 -08006537void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6538 if (value == 0) {
6539 __ testl(dest, dest);
6540 } else {
6541 __ cmpl(dest, Immediate(value));
6542 }
6543}
6544
6545void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6546 if (IsInt<32>(value)) {
6547 if (value == 0) {
6548 __ testq(dest, dest);
6549 } else {
6550 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6551 }
6552 } else {
6553 // Value won't fit in an int.
6554 __ cmpq(dest, LiteralInt64Address(value));
6555 }
6556}
6557
Mark Mendellcfa410b2015-05-25 16:02:44 -04006558void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6559 DCHECK(dest.IsDoubleStackSlot());
6560 if (IsInt<32>(value)) {
6561 // Can move directly as an int32 constant.
6562 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6563 Immediate(static_cast<int32_t>(value)));
6564 } else {
6565 Load64BitValue(CpuRegister(TMP), value);
6566 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6567 }
6568}
6569
Mark Mendell9c86b482015-09-18 13:36:07 -04006570/**
6571 * Class to handle late fixup of offsets into constant area.
6572 */
6573class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6574 public:
6575 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6576 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6577
6578 protected:
6579 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6580
6581 CodeGeneratorX86_64* codegen_;
6582
6583 private:
6584 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6585 // Patch the correct offset for the instruction. We use the address of the
6586 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6587 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6588 int32_t relative_position = constant_offset - pos;
6589
6590 // Patch in the right value.
6591 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6592 }
6593
6594 // Location in constant area that the fixup refers to.
6595 size_t offset_into_constant_area_;
6596};
6597
6598/**
6599 t * Class to handle late fixup of offsets to a jump table that will be created in the
6600 * constant area.
6601 */
6602class JumpTableRIPFixup : public RIPFixup {
6603 public:
6604 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6605 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6606
6607 void CreateJumpTable() {
6608 X86_64Assembler* assembler = codegen_->GetAssembler();
6609
6610 // Ensure that the reference to the jump table has the correct offset.
6611 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6612 SetOffset(offset_in_constant_table);
6613
6614 // Compute the offset from the start of the function to this jump table.
6615 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6616
6617 // Populate the jump table with the correct values for the jump table.
6618 int32_t num_entries = switch_instr_->GetNumEntries();
6619 HBasicBlock* block = switch_instr_->GetBlock();
6620 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6621 // The value that we want is the target offset - the position of the table.
6622 for (int32_t i = 0; i < num_entries; i++) {
6623 HBasicBlock* b = successors[i];
6624 Label* l = codegen_->GetLabelOf(b);
6625 DCHECK(l->IsBound());
6626 int32_t offset_to_block = l->Position() - current_table_offset;
6627 assembler->AppendInt32(offset_to_block);
6628 }
6629 }
6630
6631 private:
6632 const HPackedSwitch* switch_instr_;
6633};
6634
Mark Mendellf55c3e02015-03-26 21:07:46 -04006635void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6636 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006637 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006638 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6639 // 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 -04006640 assembler->Align(4, 0);
6641 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006642
6643 // Populate any jump tables.
6644 for (auto jump_table : fixups_to_jump_tables_) {
6645 jump_table->CreateJumpTable();
6646 }
6647
6648 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006649 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006650 }
6651
6652 // And finish up.
6653 CodeGenerator::Finalize(allocator);
6654}
6655
Mark Mendellf55c3e02015-03-26 21:07:46 -04006656Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6657 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6658 return Address::RIP(fixup);
6659}
6660
6661Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6662 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6663 return Address::RIP(fixup);
6664}
6665
6666Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6667 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6668 return Address::RIP(fixup);
6669}
6670
6671Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6672 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6673 return Address::RIP(fixup);
6674}
6675
Andreas Gampe85b62f22015-09-09 13:15:38 -07006676// TODO: trg as memory.
6677void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6678 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006679 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006680 return;
6681 }
6682
6683 DCHECK_NE(type, Primitive::kPrimVoid);
6684
6685 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6686 if (trg.Equals(return_loc)) {
6687 return;
6688 }
6689
6690 // Let the parallel move resolver take care of all of this.
6691 HParallelMove parallel_move(GetGraph()->GetArena());
6692 parallel_move.AddMove(return_loc, trg, type, nullptr);
6693 GetMoveResolver()->EmitNativeCode(&parallel_move);
6694}
6695
Mark Mendell9c86b482015-09-18 13:36:07 -04006696Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6697 // Create a fixup to be used to create and address the jump table.
6698 JumpTableRIPFixup* table_fixup =
6699 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6700
6701 // We have to populate the jump tables.
6702 fixups_to_jump_tables_.push_back(table_fixup);
6703 return Address::RIP(table_fixup);
6704}
6705
Mark Mendellea5af682015-10-22 17:35:49 -04006706void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6707 const Address& addr_high,
6708 int64_t v,
6709 HInstruction* instruction) {
6710 if (IsInt<32>(v)) {
6711 int32_t v_32 = v;
6712 __ movq(addr_low, Immediate(v_32));
6713 MaybeRecordImplicitNullCheck(instruction);
6714 } else {
6715 // Didn't fit in a register. Do it in pieces.
6716 int32_t low_v = Low32Bits(v);
6717 int32_t high_v = High32Bits(v);
6718 __ movl(addr_low, Immediate(low_v));
6719 MaybeRecordImplicitNullCheck(instruction);
6720 __ movl(addr_high, Immediate(high_v));
6721 }
6722}
6723
Roland Levillain4d027112015-07-01 15:41:14 +01006724#undef __
6725
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006726} // namespace x86_64
6727} // namespace art