blob: 30eca2c1038a7e77ddac224753716105fcb01395 [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
Serguei Katkov288c7a82016-05-16 11:53:15 +0600765Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
766 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800767 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000768 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
769 switch (invoke->GetMethodLoadKind()) {
770 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
771 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000772 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000773 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000774 break;
775 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000776 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000777 break;
778 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
779 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
780 break;
781 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
782 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
783 method_patches_.emplace_back(invoke->GetTargetMethod());
784 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
785 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000786 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000787 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000788 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000789 // Bind a new fixup label at the end of the "movl" insn.
790 uint32_t offset = invoke->GetDexCacheArrayOffset();
791 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000792 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000793 }
Vladimir Marko58155012015-08-19 12:49:41 +0000794 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000795 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000796 Register method_reg;
797 CpuRegister reg = temp.AsRegister<CpuRegister>();
798 if (current_method.IsRegister()) {
799 method_reg = current_method.AsRegister<Register>();
800 } else {
801 DCHECK(invoke->GetLocations()->Intrinsified());
802 DCHECK(!current_method.IsValid());
803 method_reg = reg.AsRegister();
804 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
805 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100807 __ movq(reg,
808 Address(CpuRegister(method_reg),
809 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100810 // temp = temp[index_in_cache];
811 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
812 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000813 __ 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 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600817 return callee_method;
818}
819
820void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
821 Location temp) {
822 // All registers are assumed to be correctly set up.
823 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000824
825 switch (invoke->GetCodePtrLocation()) {
826 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
827 __ call(&frame_entry_label_);
828 break;
829 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
830 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
831 Label* label = &relative_call_patches_.back().label;
832 __ call(label); // Bind to the patch label, override at link time.
833 __ Bind(label); // Bind the label at the end of the "call" insn.
834 break;
835 }
836 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
837 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100838 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
839 LOG(FATAL) << "Unsupported";
840 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000841 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
842 // (callee_method + offset_of_quick_compiled_code)()
843 __ call(Address(callee_method.AsRegister<CpuRegister>(),
844 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
845 kX86_64WordSize).SizeValue()));
846 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000847 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800848
849 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800850}
851
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000852void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
853 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
854 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
855 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000856
857 // Use the calling convention instead of the location of the receiver, as
858 // intrinsics may have put the receiver in a different register. In the intrinsics
859 // slow path, the arguments have been moved to the right place, so here we are
860 // guaranteed that the receiver is the first register of the calling convention.
861 InvokeDexCallingConvention calling_convention;
862 Register receiver = calling_convention.GetRegisterAt(0);
863
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000864 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000865 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000866 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000867 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 // Instead of simply (possibly) unpoisoning `temp` here, we should
869 // emit a read barrier for the previous class reference load.
870 // However this is not required in practice, as this is an
871 // intermediate/temporary reference and because the current
872 // concurrent copying collector keeps the from-space memory
873 // intact/accessible until the end of the marking phase (the
874 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000875 __ MaybeUnpoisonHeapReference(temp);
876 // temp = temp->GetMethodAt(method_offset);
877 __ movq(temp, Address(temp, method_offset));
878 // call temp->GetEntryPoint();
879 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
880 kX86_64WordSize).SizeValue()));
881}
882
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000883void CodeGeneratorX86_64::RecordSimplePatch() {
884 if (GetCompilerOptions().GetIncludePatchInformation()) {
885 simple_patches_.emplace_back();
886 __ Bind(&simple_patches_.back());
887 }
888}
889
890void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
891 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
892 __ Bind(&string_patches_.back().label);
893}
894
895Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
896 uint32_t element_offset) {
897 // Add a patch entry and return the label.
898 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
899 return &pc_relative_dex_cache_patches_.back().label;
900}
901
Vladimir Marko58155012015-08-19 12:49:41 +0000902void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
903 DCHECK(linker_patches->empty());
904 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000905 method_patches_.size() +
906 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000907 pc_relative_dex_cache_patches_.size() +
908 simple_patches_.size() +
909 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000910 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000911 // The label points to the end of the "movl" insn but the literal offset for method
912 // patch needs to point to the embedded constant which occupies the last 4 bytes.
913 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000914 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000915 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000916 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
917 info.target_method.dex_file,
918 info.target_method.dex_method_index));
919 }
920 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000921 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000922 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
923 info.target_method.dex_file,
924 info.target_method.dex_method_index));
925 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000926 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
927 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000928 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
929 &info.target_dex_file,
930 info.label.Position(),
931 info.element_offset));
932 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000933 for (const Label& label : simple_patches_) {
934 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
935 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
936 }
937 for (const StringPatchInfo<Label>& info : string_patches_) {
938 // These are always PC-relative, see GetSupportedLoadStringKind().
939 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
940 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
941 &info.dex_file,
942 info.label.Position(),
943 info.string_index));
944 }
Vladimir Marko58155012015-08-19 12:49:41 +0000945}
946
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100948 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949}
950
951void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100952 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100953}
954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100955size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
956 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
957 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
962 return kX86_64WordSize;
963}
964
965size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
966 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
967 return kX86_64WordSize;
968}
969
970size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
971 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
972 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100973}
974
Calin Juravle175dc732015-08-25 15:42:32 +0100975void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
976 HInstruction* instruction,
977 uint32_t dex_pc,
978 SlowPathCode* slow_path) {
979 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
980 instruction,
981 dex_pc,
982 slow_path);
983}
984
985void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100986 HInstruction* instruction,
987 uint32_t dex_pc,
988 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100989 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000990 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100991 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100992}
993
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000994static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000995// Use a fake return address register to mimic Quick.
996static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400997CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000998 const X86_64InstructionSetFeatures& isa_features,
999 const CompilerOptions& compiler_options,
1000 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001001 : CodeGenerator(graph,
1002 kNumberOfCpuRegisters,
1003 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001004 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001005 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1006 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001007 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001008 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1009 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001010 compiler_options,
1011 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001012 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001014 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001015 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001016 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001017 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001018 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001019 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1020 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001021 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001022 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1023 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001024 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001025 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1026}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001027
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001028InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1029 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001030 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031 assembler_(codegen->GetAssembler()),
1032 codegen_(codegen) {}
1033
David Brazdil58282f42016-01-14 12:45:10 +00001034void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001035 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001036 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001038 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001039 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040}
1041
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001043 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001044}
David Srbecky9d8606d2015-04-12 09:35:32 +01001045
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001046static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001047 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001048}
1049
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001050void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001051 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001052 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001053 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001054 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001055 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001056
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001058 __ testq(CpuRegister(RAX), Address(
1059 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001061 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001062
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001063 if (HasEmptyFrame()) {
1064 return;
1065 }
1066
Nicolas Geoffray98893962015-01-21 12:32:32 +00001067 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001068 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001069 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1072 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001073 }
1074 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001075
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 int adjust = GetFrameSize() - GetCoreSpillSize();
1077 __ subq(CpuRegister(RSP), Immediate(adjust));
1078 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001079 uint32_t xmm_spill_location = GetFpuSpillStart();
1080 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001081
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001082 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1083 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001084 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1085 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1086 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001087 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001088 }
1089
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001091 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092}
1093
1094void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001095 __ cfi().RememberState();
1096 if (!HasEmptyFrame()) {
1097 uint32_t xmm_spill_location = GetFpuSpillStart();
1098 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1099 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1100 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1101 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1102 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1103 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1104 }
1105 }
1106
1107 int adjust = GetFrameSize() - GetCoreSpillSize();
1108 __ addq(CpuRegister(RSP), Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(-adjust);
1110
1111 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1112 Register reg = kCoreCalleeSaves[i];
1113 if (allocated_registers_.ContainsCoreRegister(reg)) {
1114 __ popq(CpuRegister(reg));
1115 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1116 __ cfi().Restore(DWARFReg(reg));
1117 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001118 }
1119 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001120 __ ret();
1121 __ cfi().RestoreState();
1122 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123}
1124
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001125void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1126 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001127}
1128
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129void CodeGeneratorX86_64::Move(Location destination, Location source) {
1130 if (source.Equals(destination)) {
1131 return;
1132 }
1133 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001134 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001136 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001138 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001140 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1141 } else if (source.IsConstant()) {
1142 HConstant* constant = source.GetConstant();
1143 if (constant->IsLongConstant()) {
1144 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1145 } else {
1146 Load32BitValue(dest, GetInt32ValueOf(constant));
1147 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148 } else {
1149 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001150 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001151 }
1152 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001153 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001154 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001155 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001157 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1158 } else if (source.IsConstant()) {
1159 HConstant* constant = source.GetConstant();
1160 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1161 if (constant->IsFloatConstant()) {
1162 Load32BitValue(dest, static_cast<int32_t>(value));
1163 } else {
1164 Load64BitValue(dest, value);
1165 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001167 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else {
1169 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001170 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001171 }
1172 } else if (destination.IsStackSlot()) {
1173 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001175 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 } else if (source.IsFpuRegister()) {
1177 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001178 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001179 } else if (source.IsConstant()) {
1180 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001181 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001182 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001183 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001184 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001185 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1186 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187 }
1188 } else {
1189 DCHECK(destination.IsDoubleStackSlot());
1190 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001192 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001193 } else if (source.IsFpuRegister()) {
1194 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001195 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001196 } else if (source.IsConstant()) {
1197 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001198 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001199 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001200 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001201 } else {
1202 DCHECK(constant->IsLongConstant());
1203 value = constant->AsLongConstant()->GetValue();
1204 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001205 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 } else {
1207 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001208 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1209 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001210 }
1211 }
1212}
1213
Calin Juravle175dc732015-08-25 15:42:32 +01001214void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1215 DCHECK(location.IsRegister());
1216 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1217}
1218
Calin Juravlee460d1d2015-09-29 04:52:17 +01001219void CodeGeneratorX86_64::MoveLocation(
1220 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1221 Move(dst, src);
1222}
1223
1224void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1225 if (location.IsRegister()) {
1226 locations->AddTemp(location);
1227 } else {
1228 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1229 }
1230}
1231
David Brazdilfc6a86a2015-06-26 10:33:45 +00001232void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001233 DCHECK(!successor->IsExitBlock());
1234
1235 HBasicBlock* block = got->GetBlock();
1236 HInstruction* previous = got->GetPrevious();
1237
1238 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001239 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001240 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1241 return;
1242 }
1243
1244 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1245 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1246 }
1247 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248 __ jmp(codegen_->GetLabelOf(successor));
1249 }
1250}
1251
David Brazdilfc6a86a2015-06-26 10:33:45 +00001252void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1253 got->SetLocations(nullptr);
1254}
1255
1256void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1257 HandleGoto(got, got->GetSuccessor());
1258}
1259
1260void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1261 try_boundary->SetLocations(nullptr);
1262}
1263
1264void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1265 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1266 if (!successor->IsExitBlock()) {
1267 HandleGoto(try_boundary, successor);
1268 }
1269}
1270
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001271void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1272 exit->SetLocations(nullptr);
1273}
1274
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001275void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276}
1277
Mark Mendell152408f2015-12-31 12:28:50 -05001278template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001279void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001280 LabelType* true_label,
1281 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001282 if (cond->IsFPConditionTrueIfNaN()) {
1283 __ j(kUnordered, true_label);
1284 } else if (cond->IsFPConditionFalseIfNaN()) {
1285 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288}
1289
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001290void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001291 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001292
Mark Mendellc4701932015-04-10 13:18:51 -04001293 Location left = locations->InAt(0);
1294 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001295 Primitive::Type type = condition->InputAt(0)->GetType();
1296 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001297 case Primitive::kPrimBoolean:
1298 case Primitive::kPrimByte:
1299 case Primitive::kPrimChar:
1300 case Primitive::kPrimShort:
1301 case Primitive::kPrimInt:
1302 case Primitive::kPrimNot: {
1303 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1304 if (right.IsConstant()) {
1305 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1306 if (value == 0) {
1307 __ testl(left_reg, left_reg);
1308 } else {
1309 __ cmpl(left_reg, Immediate(value));
1310 }
1311 } else if (right.IsStackSlot()) {
1312 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1313 } else {
1314 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1315 }
1316 break;
1317 }
Mark Mendellc4701932015-04-10 13:18:51 -04001318 case Primitive::kPrimLong: {
1319 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1320 if (right.IsConstant()) {
1321 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001322 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001323 } else if (right.IsDoubleStackSlot()) {
1324 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1325 } else {
1326 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1327 }
Mark Mendellc4701932015-04-10 13:18:51 -04001328 break;
1329 }
1330 case Primitive::kPrimFloat: {
1331 if (right.IsFpuRegister()) {
1332 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1333 } else if (right.IsConstant()) {
1334 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1335 codegen_->LiteralFloatAddress(
1336 right.GetConstant()->AsFloatConstant()->GetValue()));
1337 } else {
1338 DCHECK(right.IsStackSlot());
1339 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1340 Address(CpuRegister(RSP), right.GetStackIndex()));
1341 }
Mark Mendellc4701932015-04-10 13:18:51 -04001342 break;
1343 }
1344 case Primitive::kPrimDouble: {
1345 if (right.IsFpuRegister()) {
1346 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1347 } else if (right.IsConstant()) {
1348 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1349 codegen_->LiteralDoubleAddress(
1350 right.GetConstant()->AsDoubleConstant()->GetValue()));
1351 } else {
1352 DCHECK(right.IsDoubleStackSlot());
1353 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1354 Address(CpuRegister(RSP), right.GetStackIndex()));
1355 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001356 break;
1357 }
1358 default:
1359 LOG(FATAL) << "Unexpected condition type " << type;
1360 }
1361}
1362
1363template<class LabelType>
1364void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1365 LabelType* true_target_in,
1366 LabelType* false_target_in) {
1367 // Generated branching requires both targets to be explicit. If either of the
1368 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1369 LabelType fallthrough_target;
1370 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1371 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1372
1373 // Generate the comparison to set the CC.
1374 GenerateCompareTest(condition);
1375
1376 // Now generate the correct jump(s).
1377 Primitive::Type type = condition->InputAt(0)->GetType();
1378 switch (type) {
1379 case Primitive::kPrimLong: {
1380 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1381 break;
1382 }
1383 case Primitive::kPrimFloat: {
1384 GenerateFPJumps(condition, true_target, false_target);
1385 break;
1386 }
1387 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001388 GenerateFPJumps(condition, true_target, false_target);
1389 break;
1390 }
1391 default:
1392 LOG(FATAL) << "Unexpected condition type " << type;
1393 }
1394
David Brazdil0debae72015-11-12 18:37:00 +00001395 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001396 __ jmp(false_target);
1397 }
David Brazdil0debae72015-11-12 18:37:00 +00001398
1399 if (fallthrough_target.IsLinked()) {
1400 __ Bind(&fallthrough_target);
1401 }
Mark Mendellc4701932015-04-10 13:18:51 -04001402}
1403
David Brazdil0debae72015-11-12 18:37:00 +00001404static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1405 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1406 // are set only strictly before `branch`. We can't use the eflags on long
1407 // conditions if they are materialized due to the complex branching.
1408 return cond->IsCondition() &&
1409 cond->GetNext() == branch &&
1410 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1411}
1412
Mark Mendell152408f2015-12-31 12:28:50 -05001413template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001414void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001415 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001416 LabelType* true_target,
1417 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001418 HInstruction* cond = instruction->InputAt(condition_input_index);
1419
1420 if (true_target == nullptr && false_target == nullptr) {
1421 // Nothing to do. The code always falls through.
1422 return;
1423 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001424 // Constant condition, statically compared against "true" (integer value 1).
1425 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001426 if (true_target != nullptr) {
1427 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001428 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001429 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001430 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001431 if (false_target != nullptr) {
1432 __ jmp(false_target);
1433 }
1434 }
1435 return;
1436 }
1437
1438 // The following code generates these patterns:
1439 // (1) true_target == nullptr && false_target != nullptr
1440 // - opposite condition true => branch to false_target
1441 // (2) true_target != nullptr && false_target == nullptr
1442 // - condition true => branch to true_target
1443 // (3) true_target != nullptr && false_target != nullptr
1444 // - condition true => branch to true_target
1445 // - branch to false_target
1446 if (IsBooleanValueOrMaterializedCondition(cond)) {
1447 if (AreEflagsSetFrom(cond, instruction)) {
1448 if (true_target == nullptr) {
1449 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1450 } else {
1451 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1452 }
1453 } else {
1454 // Materialized condition, compare against 0.
1455 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1456 if (lhs.IsRegister()) {
1457 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1458 } else {
1459 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1460 }
1461 if (true_target == nullptr) {
1462 __ j(kEqual, false_target);
1463 } else {
1464 __ j(kNotEqual, true_target);
1465 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001466 }
1467 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001468 // Condition has not been materialized, use its inputs as the
1469 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001470 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001471
David Brazdil0debae72015-11-12 18:37:00 +00001472 // If this is a long or FP comparison that has been folded into
1473 // the HCondition, generate the comparison directly.
1474 Primitive::Type type = condition->InputAt(0)->GetType();
1475 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1476 GenerateCompareTestAndBranch(condition, true_target, false_target);
1477 return;
1478 }
1479
1480 Location lhs = condition->GetLocations()->InAt(0);
1481 Location rhs = condition->GetLocations()->InAt(1);
1482 if (rhs.IsRegister()) {
1483 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1484 } else if (rhs.IsConstant()) {
1485 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001486 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001487 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001488 __ cmpl(lhs.AsRegister<CpuRegister>(),
1489 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1490 }
1491 if (true_target == nullptr) {
1492 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1493 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001494 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001495 }
Dave Allison20dfc792014-06-16 20:44:29 -07001496 }
David Brazdil0debae72015-11-12 18:37:00 +00001497
1498 // If neither branch falls through (case 3), the conditional branch to `true_target`
1499 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1500 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001501 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001502 }
1503}
1504
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001505void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1507 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508 locations->SetInAt(0, Location::Any());
1509 }
1510}
1511
1512void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001513 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1514 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1515 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1516 nullptr : codegen_->GetLabelOf(true_successor);
1517 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1518 nullptr : codegen_->GetLabelOf(false_successor);
1519 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520}
1521
1522void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1523 LocationSummary* locations = new (GetGraph()->GetArena())
1524 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001525 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001526 locations->SetInAt(0, Location::Any());
1527 }
1528}
1529
1530void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001531 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001532 GenerateTestAndBranch<Label>(deoptimize,
1533 /* condition_input_index */ 0,
1534 slow_path->GetEntryLabel(),
1535 /* false_target */ nullptr);
1536}
1537
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001538static bool SelectCanUseCMOV(HSelect* select) {
1539 // There are no conditional move instructions for XMMs.
1540 if (Primitive::IsFloatingPointType(select->GetType())) {
1541 return false;
1542 }
1543
1544 // A FP condition doesn't generate the single CC that we need.
1545 HInstruction* condition = select->GetCondition();
1546 if (condition->IsCondition() &&
1547 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1548 return false;
1549 }
1550
1551 // We can generate a CMOV for this Select.
1552 return true;
1553}
1554
David Brazdil74eb1b22015-12-14 11:44:01 +00001555void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1557 if (Primitive::IsFloatingPointType(select->GetType())) {
1558 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001559 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001560 } else {
1561 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001562 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001563 if (select->InputAt(1)->IsConstant()) {
1564 locations->SetInAt(1, Location::RequiresRegister());
1565 } else {
1566 locations->SetInAt(1, Location::Any());
1567 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001568 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001569 locations->SetInAt(1, Location::Any());
1570 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001571 }
1572 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1573 locations->SetInAt(2, Location::RequiresRegister());
1574 }
1575 locations->SetOut(Location::SameAsFirstInput());
1576}
1577
1578void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1579 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001580 if (SelectCanUseCMOV(select)) {
1581 // If both the condition and the source types are integer, we can generate
1582 // a CMOV to implement Select.
1583 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001584 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001585 DCHECK(locations->InAt(0).Equals(locations->Out()));
1586
1587 HInstruction* select_condition = select->GetCondition();
1588 Condition cond = kNotEqual;
1589
1590 // Figure out how to test the 'condition'.
1591 if (select_condition->IsCondition()) {
1592 HCondition* condition = select_condition->AsCondition();
1593 if (!condition->IsEmittedAtUseSite()) {
1594 // This was a previously materialized condition.
1595 // Can we use the existing condition code?
1596 if (AreEflagsSetFrom(condition, select)) {
1597 // Materialization was the previous instruction. Condition codes are right.
1598 cond = X86_64IntegerCondition(condition->GetCondition());
1599 } else {
1600 // No, we have to recreate the condition code.
1601 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1602 __ testl(cond_reg, cond_reg);
1603 }
1604 } else {
1605 GenerateCompareTest(condition);
1606 cond = X86_64IntegerCondition(condition->GetCondition());
1607 }
1608 } else {
1609 // Must be a boolean condition, which needs to be compared to 0.
1610 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1611 __ testl(cond_reg, cond_reg);
1612 }
1613
1614 // If the condition is true, overwrite the output, which already contains false.
1615 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001616 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1617 if (value_true_loc.IsRegister()) {
1618 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1619 } else {
1620 __ cmov(cond,
1621 value_false,
1622 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1623 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001624 } else {
1625 NearLabel false_target;
1626 GenerateTestAndBranch<NearLabel>(select,
1627 /* condition_input_index */ 2,
1628 /* true_target */ nullptr,
1629 &false_target);
1630 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1631 __ Bind(&false_target);
1632 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001633}
1634
David Srbecky0cf44932015-12-09 14:09:59 +00001635void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1636 new (GetGraph()->GetArena()) LocationSummary(info);
1637}
1638
David Srbeckyd28f4a02016-03-14 17:14:24 +00001639void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1640 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001641}
1642
1643void CodeGeneratorX86_64::GenerateNop() {
1644 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001645}
1646
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001647void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001648 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001649 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001650 // Handle the long/FP comparisons made in instruction simplification.
1651 switch (cond->InputAt(0)->GetType()) {
1652 case Primitive::kPrimLong:
1653 locations->SetInAt(0, Location::RequiresRegister());
1654 locations->SetInAt(1, Location::Any());
1655 break;
1656 case Primitive::kPrimFloat:
1657 case Primitive::kPrimDouble:
1658 locations->SetInAt(0, Location::RequiresFpuRegister());
1659 locations->SetInAt(1, Location::Any());
1660 break;
1661 default:
1662 locations->SetInAt(0, Location::RequiresRegister());
1663 locations->SetInAt(1, Location::Any());
1664 break;
1665 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001666 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001667 locations->SetOut(Location::RequiresRegister());
1668 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001669}
1670
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001671void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001672 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001673 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001674 }
Mark Mendellc4701932015-04-10 13:18:51 -04001675
1676 LocationSummary* locations = cond->GetLocations();
1677 Location lhs = locations->InAt(0);
1678 Location rhs = locations->InAt(1);
1679 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001680 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001681
1682 switch (cond->InputAt(0)->GetType()) {
1683 default:
1684 // Integer case.
1685
1686 // Clear output register: setcc only sets the low byte.
1687 __ xorl(reg, reg);
1688
1689 if (rhs.IsRegister()) {
1690 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1691 } else if (rhs.IsConstant()) {
1692 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001693 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001694 } else {
1695 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1696 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001697 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001698 return;
1699 case Primitive::kPrimLong:
1700 // Clear output register: setcc only sets the low byte.
1701 __ xorl(reg, reg);
1702
1703 if (rhs.IsRegister()) {
1704 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1705 } else if (rhs.IsConstant()) {
1706 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001707 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001708 } else {
1709 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1710 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001711 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001712 return;
1713 case Primitive::kPrimFloat: {
1714 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1715 if (rhs.IsConstant()) {
1716 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1717 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1718 } else if (rhs.IsStackSlot()) {
1719 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1720 } else {
1721 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1722 }
1723 GenerateFPJumps(cond, &true_label, &false_label);
1724 break;
1725 }
1726 case Primitive::kPrimDouble: {
1727 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1728 if (rhs.IsConstant()) {
1729 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1730 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1731 } else if (rhs.IsDoubleStackSlot()) {
1732 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1733 } else {
1734 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1735 }
1736 GenerateFPJumps(cond, &true_label, &false_label);
1737 break;
1738 }
1739 }
1740
1741 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001742 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001743
Roland Levillain4fa13f62015-07-06 18:11:54 +01001744 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001745 __ Bind(&false_label);
1746 __ xorl(reg, reg);
1747 __ jmp(&done_label);
1748
Roland Levillain4fa13f62015-07-06 18:11:54 +01001749 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001750 __ Bind(&true_label);
1751 __ movl(reg, Immediate(1));
1752 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001753}
1754
1755void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001756 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001757}
1758
1759void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001760 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001761}
1762
1763void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001801}
1802
Aart Bike9f37602015-10-09 11:15:55 -07001803void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001805}
1806
1807void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001809}
1810
1811void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001813}
1814
1815void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001835void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001836 LocationSummary* locations =
1837 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001838 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001839 case Primitive::kPrimBoolean:
1840 case Primitive::kPrimByte:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001843 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001844 case Primitive::kPrimLong: {
1845 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001846 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1848 break;
1849 }
1850 case Primitive::kPrimFloat:
1851 case Primitive::kPrimDouble: {
1852 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001853 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001854 locations->SetOut(Location::RequiresRegister());
1855 break;
1856 }
1857 default:
1858 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1859 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001860}
1861
1862void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001863 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001865 Location left = locations->InAt(0);
1866 Location right = locations->InAt(1);
1867
Mark Mendell0c9497d2015-08-21 09:30:05 -04001868 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001869 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001870 Condition less_cond = kLess;
1871
Calin Juravleddb7df22014-11-25 20:56:51 +00001872 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001873 case Primitive::kPrimBoolean:
1874 case Primitive::kPrimByte:
1875 case Primitive::kPrimShort:
1876 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001877 case Primitive::kPrimInt: {
1878 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1879 if (right.IsConstant()) {
1880 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1881 codegen_->Compare32BitValue(left_reg, value);
1882 } else if (right.IsStackSlot()) {
1883 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1884 } else {
1885 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1886 }
1887 break;
1888 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001889 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001890 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1891 if (right.IsConstant()) {
1892 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001893 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001894 } else if (right.IsDoubleStackSlot()) {
1895 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001896 } else {
1897 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1898 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001899 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001900 }
1901 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001902 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1903 if (right.IsConstant()) {
1904 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1905 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1906 } else if (right.IsStackSlot()) {
1907 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1908 } else {
1909 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1910 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001911 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001912 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 break;
1914 }
1915 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001916 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1917 if (right.IsConstant()) {
1918 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1919 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1920 } else if (right.IsDoubleStackSlot()) {
1921 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1922 } else {
1923 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1924 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001925 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001926 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001927 break;
1928 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001929 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001931 }
Aart Bika19616e2016-02-01 18:57:58 -08001932
Calin Juravleddb7df22014-11-25 20:56:51 +00001933 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001934 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001935 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001936
Calin Juravle91debbc2014-11-26 19:01:09 +00001937 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001938 __ movl(out, Immediate(1));
1939 __ jmp(&done);
1940
1941 __ Bind(&less);
1942 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001943
1944 __ Bind(&done);
1945}
1946
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001948 LocationSummary* locations =
1949 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001950 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951}
1952
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001953void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001954 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001955}
1956
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001957void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1958 LocationSummary* locations =
1959 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1960 locations->SetOut(Location::ConstantLocation(constant));
1961}
1962
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001963void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001964 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001965}
1966
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001968 LocationSummary* locations =
1969 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001970 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971}
1972
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001973void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001974 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001975}
1976
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001977void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1980 locations->SetOut(Location::ConstantLocation(constant));
1981}
1982
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001983void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001984 // Will be generated at use site.
1985}
1986
1987void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1990 locations->SetOut(Location::ConstantLocation(constant));
1991}
1992
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001993void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1994 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995 // Will be generated at use site.
1996}
1997
Calin Juravle27df7582015-04-17 19:12:31 +01001998void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1999 memory_barrier->SetLocations(nullptr);
2000}
2001
2002void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002003 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002004}
2005
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002006void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2007 ret->SetLocations(nullptr);
2008}
2009
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002010void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002011 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002012}
2013
2014void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 LocationSummary* locations =
2016 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002017 switch (ret->InputAt(0)->GetType()) {
2018 case Primitive::kPrimBoolean:
2019 case Primitive::kPrimByte:
2020 case Primitive::kPrimChar:
2021 case Primitive::kPrimShort:
2022 case Primitive::kPrimInt:
2023 case Primitive::kPrimNot:
2024 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002025 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002026 break;
2027
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002030 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002031 break;
2032
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002033 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002034 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002035 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036}
2037
2038void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2039 if (kIsDebugBuild) {
2040 switch (ret->InputAt(0)->GetType()) {
2041 case Primitive::kPrimBoolean:
2042 case Primitive::kPrimByte:
2043 case Primitive::kPrimChar:
2044 case Primitive::kPrimShort:
2045 case Primitive::kPrimInt:
2046 case Primitive::kPrimNot:
2047 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002049 break;
2050
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002051 case Primitive::kPrimFloat:
2052 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002053 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002054 XMM0);
2055 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 }
2060 }
2061 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002062}
2063
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002064Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2065 switch (type) {
2066 case Primitive::kPrimBoolean:
2067 case Primitive::kPrimByte:
2068 case Primitive::kPrimChar:
2069 case Primitive::kPrimShort:
2070 case Primitive::kPrimInt:
2071 case Primitive::kPrimNot:
2072 case Primitive::kPrimLong:
2073 return Location::RegisterLocation(RAX);
2074
2075 case Primitive::kPrimVoid:
2076 return Location::NoLocation();
2077
2078 case Primitive::kPrimDouble:
2079 case Primitive::kPrimFloat:
2080 return Location::FpuRegisterLocation(XMM0);
2081 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002082
2083 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002084}
2085
2086Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2087 return Location::RegisterLocation(kMethodRegisterArgument);
2088}
2089
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002090Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002091 switch (type) {
2092 case Primitive::kPrimBoolean:
2093 case Primitive::kPrimByte:
2094 case Primitive::kPrimChar:
2095 case Primitive::kPrimShort:
2096 case Primitive::kPrimInt:
2097 case Primitive::kPrimNot: {
2098 uint32_t index = gp_index_++;
2099 stack_index_++;
2100 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002101 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002102 } else {
2103 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2104 }
2105 }
2106
2107 case Primitive::kPrimLong: {
2108 uint32_t index = gp_index_;
2109 stack_index_ += 2;
2110 if (index < calling_convention.GetNumberOfRegisters()) {
2111 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002112 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002113 } else {
2114 gp_index_ += 2;
2115 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2116 }
2117 }
2118
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002120 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 stack_index_++;
2122 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002123 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002124 } else {
2125 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2126 }
2127 }
2128
2129 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002130 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 stack_index_ += 2;
2132 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002133 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002134 } else {
2135 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2136 }
2137 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002138
2139 case Primitive::kPrimVoid:
2140 LOG(FATAL) << "Unexpected parameter type " << type;
2141 break;
2142 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002143 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144}
2145
Calin Juravle175dc732015-08-25 15:42:32 +01002146void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2147 // The trampoline uses the same calling convention as dex calling conventions,
2148 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2149 // the method_idx.
2150 HandleInvoke(invoke);
2151}
2152
2153void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2154 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2155}
2156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002157void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002158 // Explicit clinit checks triggered by static invokes must have been pruned by
2159 // art::PrepareForRegisterAllocation.
2160 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002161
Mark Mendellfb8d2792015-03-31 22:16:59 -04002162 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002163 if (intrinsic.TryDispatch(invoke)) {
2164 return;
2165 }
2166
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002167 HandleInvoke(invoke);
2168}
2169
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002170static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2171 if (invoke->GetLocations()->Intrinsified()) {
2172 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2173 intrinsic.Dispatch(invoke);
2174 return true;
2175 }
2176 return false;
2177}
2178
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002179void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002180 // Explicit clinit checks triggered by static invokes must have been pruned by
2181 // art::PrepareForRegisterAllocation.
2182 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002183
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002184 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2185 return;
2186 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002187
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002188 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002189 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002190 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002191 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002192}
2193
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002194void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002195 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002196 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002197}
2198
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002199void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002200 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002201 if (intrinsic.TryDispatch(invoke)) {
2202 return;
2203 }
2204
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002205 HandleInvoke(invoke);
2206}
2207
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002208void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002209 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2210 return;
2211 }
2212
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002213 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002214 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002215 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216}
2217
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002218void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2219 HandleInvoke(invoke);
2220 // Add the hidden argument.
2221 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2222}
2223
2224void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2225 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002226 LocationSummary* locations = invoke->GetLocations();
2227 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2228 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002229 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2230 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002231 Location receiver = locations->InAt(0);
2232 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2233
Roland Levillain0d5a2812015-11-13 10:07:31 +00002234 // Set the hidden argument. This is safe to do this here, as RAX
2235 // won't be modified thereafter, before the `call` instruction.
2236 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002237 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239 if (receiver.IsStackSlot()) {
2240 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002241 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242 __ movl(temp, Address(temp, class_offset));
2243 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002247 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002248 // Instead of simply (possibly) unpoisoning `temp` here, we should
2249 // emit a read barrier for the previous class reference load.
2250 // However this is not required in practice, as this is an
2251 // intermediate/temporary reference and because the current
2252 // concurrent copying collector keeps the from-space memory
2253 // intact/accessible until the end of the marking phase (the
2254 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002255 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002257 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002259 __ call(Address(temp,
2260 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261
2262 DCHECK(!codegen_->IsLeafMethod());
2263 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2264}
2265
Roland Levillain88cb1752014-10-20 16:36:47 +01002266void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2269 switch (neg->GetResultType()) {
2270 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002271 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002272 locations->SetInAt(0, Location::RequiresRegister());
2273 locations->SetOut(Location::SameAsFirstInput());
2274 break;
2275
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 case Primitive::kPrimFloat:
2277 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002278 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002279 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002280 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002281 break;
2282
2283 default:
2284 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2285 }
2286}
2287
2288void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2289 LocationSummary* locations = neg->GetLocations();
2290 Location out = locations->Out();
2291 Location in = locations->InAt(0);
2292 switch (neg->GetResultType()) {
2293 case Primitive::kPrimInt:
2294 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002295 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002297 break;
2298
2299 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002300 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002303 break;
2304
Roland Levillain5368c212014-11-27 15:03:41 +00002305 case Primitive::kPrimFloat: {
2306 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002307 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002308 // Implement float negation with an exclusive or with value
2309 // 0x80000000 (mask for bit 31, representing the sign of a
2310 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002311 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002314 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002315
Roland Levillain5368c212014-11-27 15:03:41 +00002316 case Primitive::kPrimDouble: {
2317 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002318 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002319 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002320 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002321 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002322 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002324 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002325 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002326
2327 default:
2328 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2329 }
2330}
2331
Roland Levillaindff1f282014-11-05 14:15:05 +00002332void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2333 LocationSummary* locations =
2334 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2335 Primitive::Type result_type = conversion->GetResultType();
2336 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002337 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002338
David Brazdilb2bd1c52015-03-25 11:17:37 +00002339 // The Java language does not allow treating boolean as an integral type but
2340 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002341
Roland Levillaindff1f282014-11-05 14:15:05 +00002342 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002343 case Primitive::kPrimByte:
2344 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002345 case Primitive::kPrimLong:
2346 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002347 case Primitive::kPrimBoolean:
2348 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002349 case Primitive::kPrimShort:
2350 case Primitive::kPrimInt:
2351 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002352 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002353 locations->SetInAt(0, Location::Any());
2354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361 break;
2362
Roland Levillain01a8d712014-11-14 16:27:39 +00002363 case Primitive::kPrimShort:
2364 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002365 case Primitive::kPrimLong:
2366 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002367 case Primitive::kPrimBoolean:
2368 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002369 case Primitive::kPrimByte:
2370 case Primitive::kPrimInt:
2371 case Primitive::kPrimChar:
2372 // Processing a Dex `int-to-short' instruction.
2373 locations->SetInAt(0, Location::Any());
2374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2375 break;
2376
2377 default:
2378 LOG(FATAL) << "Unexpected type conversion from " << input_type
2379 << " to " << result_type;
2380 }
2381 break;
2382
Roland Levillain946e1432014-11-11 17:35:19 +00002383 case Primitive::kPrimInt:
2384 switch (input_type) {
2385 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002386 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002387 locations->SetInAt(0, Location::Any());
2388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2389 break;
2390
2391 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002392 // Processing a Dex `float-to-int' instruction.
2393 locations->SetInAt(0, Location::RequiresFpuRegister());
2394 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002395 break;
2396
Roland Levillain946e1432014-11-11 17:35:19 +00002397 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002398 // Processing a Dex `double-to-int' instruction.
2399 locations->SetInAt(0, Location::RequiresFpuRegister());
2400 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002401 break;
2402
2403 default:
2404 LOG(FATAL) << "Unexpected type conversion from " << input_type
2405 << " to " << result_type;
2406 }
2407 break;
2408
Roland Levillaindff1f282014-11-05 14:15:05 +00002409 case Primitive::kPrimLong:
2410 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002411 case Primitive::kPrimBoolean:
2412 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002413 case Primitive::kPrimByte:
2414 case Primitive::kPrimShort:
2415 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002416 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002417 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002418 // TODO: We would benefit from a (to-be-implemented)
2419 // Location::RegisterOrStackSlot requirement for this input.
2420 locations->SetInAt(0, Location::RequiresRegister());
2421 locations->SetOut(Location::RequiresRegister());
2422 break;
2423
2424 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002425 // Processing a Dex `float-to-long' instruction.
2426 locations->SetInAt(0, Location::RequiresFpuRegister());
2427 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002428 break;
2429
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002431 // Processing a Dex `double-to-long' instruction.
2432 locations->SetInAt(0, Location::RequiresFpuRegister());
2433 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 break;
2435
2436 default:
2437 LOG(FATAL) << "Unexpected type conversion from " << input_type
2438 << " to " << result_type;
2439 }
2440 break;
2441
Roland Levillain981e4542014-11-14 11:47:14 +00002442 case Primitive::kPrimChar:
2443 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002444 case Primitive::kPrimLong:
2445 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002451 // Processing a Dex `int-to-char' instruction.
2452 locations->SetInAt(0, Location::Any());
2453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2454 break;
2455
2456 default:
2457 LOG(FATAL) << "Unexpected type conversion from " << input_type
2458 << " to " << result_type;
2459 }
2460 break;
2461
Roland Levillaindff1f282014-11-05 14:15:05 +00002462 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002463 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002464 case Primitive::kPrimBoolean:
2465 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002466 case Primitive::kPrimByte:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimInt:
2469 case Primitive::kPrimChar:
2470 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002471 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002472 locations->SetOut(Location::RequiresFpuRegister());
2473 break;
2474
2475 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002476 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002477 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002478 locations->SetOut(Location::RequiresFpuRegister());
2479 break;
2480
Roland Levillaincff13742014-11-17 14:32:17 +00002481 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002482 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002483 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected type conversion from " << input_type
2489 << " to " << result_type;
2490 };
2491 break;
2492
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002494 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002495 case Primitive::kPrimBoolean:
2496 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002497 case Primitive::kPrimByte:
2498 case Primitive::kPrimShort:
2499 case Primitive::kPrimInt:
2500 case Primitive::kPrimChar:
2501 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002502 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002503 locations->SetOut(Location::RequiresFpuRegister());
2504 break;
2505
2506 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002507 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002508 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002509 locations->SetOut(Location::RequiresFpuRegister());
2510 break;
2511
Roland Levillaincff13742014-11-17 14:32:17 +00002512 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002513 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002514 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002515 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002516 break;
2517
2518 default:
2519 LOG(FATAL) << "Unexpected type conversion from " << input_type
2520 << " to " << result_type;
2521 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected type conversion from " << input_type
2526 << " to " << result_type;
2527 }
2528}
2529
2530void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2531 LocationSummary* locations = conversion->GetLocations();
2532 Location out = locations->Out();
2533 Location in = locations->InAt(0);
2534 Primitive::Type result_type = conversion->GetResultType();
2535 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002536 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002538 case Primitive::kPrimByte:
2539 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002540 case Primitive::kPrimLong:
2541 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002542 case Primitive::kPrimBoolean:
2543 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002544 case Primitive::kPrimShort:
2545 case Primitive::kPrimInt:
2546 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002547 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002548 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002550 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002552 Address(CpuRegister(RSP), in.GetStackIndex()));
2553 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002555 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002556 }
2557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563 break;
2564
Roland Levillain01a8d712014-11-14 16:27:39 +00002565 case Primitive::kPrimShort:
2566 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002567 case Primitive::kPrimLong:
2568 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002569 case Primitive::kPrimBoolean:
2570 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002571 case Primitive::kPrimByte:
2572 case Primitive::kPrimInt:
2573 case Primitive::kPrimChar:
2574 // Processing a Dex `int-to-short' instruction.
2575 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002577 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002578 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002579 Address(CpuRegister(RSP), in.GetStackIndex()));
2580 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002581 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002582 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002583 }
2584 break;
2585
2586 default:
2587 LOG(FATAL) << "Unexpected type conversion from " << input_type
2588 << " to " << result_type;
2589 }
2590 break;
2591
Roland Levillain946e1432014-11-11 17:35:19 +00002592 case Primitive::kPrimInt:
2593 switch (input_type) {
2594 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002595 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002596 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002598 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002600 Address(CpuRegister(RSP), in.GetStackIndex()));
2601 } else {
2602 DCHECK(in.IsConstant());
2603 DCHECK(in.GetConstant()->IsLongConstant());
2604 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002606 }
2607 break;
2608
Roland Levillain3f8f9362014-12-02 17:45:01 +00002609 case Primitive::kPrimFloat: {
2610 // Processing a Dex `float-to-int' instruction.
2611 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2612 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002613 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002614
2615 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002616 // if input >= (float)INT_MAX goto done
2617 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002618 __ j(kAboveEqual, &done);
2619 // if input == NaN goto nan
2620 __ j(kUnordered, &nan);
2621 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002622 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002623 __ jmp(&done);
2624 __ Bind(&nan);
2625 // output = 0
2626 __ xorl(output, output);
2627 __ Bind(&done);
2628 break;
2629 }
2630
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631 case Primitive::kPrimDouble: {
2632 // Processing a Dex `double-to-int' instruction.
2633 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2634 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002635 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002636
2637 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002638 // if input >= (double)INT_MAX goto done
2639 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002640 __ j(kAboveEqual, &done);
2641 // if input == NaN goto nan
2642 __ j(kUnordered, &nan);
2643 // output = double-to-int-truncate(input)
2644 __ cvttsd2si(output, input);
2645 __ jmp(&done);
2646 __ Bind(&nan);
2647 // output = 0
2648 __ xorl(output, output);
2649 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002650 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002651 }
Roland Levillain946e1432014-11-11 17:35:19 +00002652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 }
2657 break;
2658
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimLong:
2660 switch (input_type) {
2661 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002662 case Primitive::kPrimBoolean:
2663 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002664 case Primitive::kPrimByte:
2665 case Primitive::kPrimShort:
2666 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002667 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002668 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002669 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002671 break;
2672
Roland Levillain624279f2014-12-04 11:54:28 +00002673 case Primitive::kPrimFloat: {
2674 // Processing a Dex `float-to-long' instruction.
2675 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2676 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002677 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002678
Mark Mendell92e83bf2015-05-07 11:25:03 -04002679 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002680 // if input >= (float)LONG_MAX goto done
2681 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002682 __ j(kAboveEqual, &done);
2683 // if input == NaN goto nan
2684 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002685 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002686 __ cvttss2si(output, input, true);
2687 __ jmp(&done);
2688 __ Bind(&nan);
2689 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002690 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002691 __ Bind(&done);
2692 break;
2693 }
2694
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002695 case Primitive::kPrimDouble: {
2696 // Processing a Dex `double-to-long' instruction.
2697 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2698 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002699 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002700
Mark Mendell92e83bf2015-05-07 11:25:03 -04002701 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002702 // if input >= (double)LONG_MAX goto done
2703 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002704 __ j(kAboveEqual, &done);
2705 // if input == NaN goto nan
2706 __ j(kUnordered, &nan);
2707 // output = double-to-long-truncate(input)
2708 __ cvttsd2si(output, input, true);
2709 __ jmp(&done);
2710 __ Bind(&nan);
2711 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002712 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002713 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002714 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002715 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 }
2721 break;
2722
Roland Levillain981e4542014-11-14 11:47:14 +00002723 case Primitive::kPrimChar:
2724 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002725 case Primitive::kPrimLong:
2726 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002727 case Primitive::kPrimBoolean:
2728 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002729 case Primitive::kPrimByte:
2730 case Primitive::kPrimShort:
2731 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002732 // Processing a Dex `int-to-char' instruction.
2733 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002735 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002737 Address(CpuRegister(RSP), in.GetStackIndex()));
2738 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002739 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002740 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002741 }
2742 break;
2743
2744 default:
2745 LOG(FATAL) << "Unexpected type conversion from " << input_type
2746 << " to " << result_type;
2747 }
2748 break;
2749
Roland Levillaindff1f282014-11-05 14:15:05 +00002750 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002751 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002752 case Primitive::kPrimBoolean:
2753 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002754 case Primitive::kPrimByte:
2755 case Primitive::kPrimShort:
2756 case Primitive::kPrimInt:
2757 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002758 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002759 if (in.IsRegister()) {
2760 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2761 } else if (in.IsConstant()) {
2762 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2763 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002764 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002765 } else {
2766 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2767 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2768 }
Roland Levillaincff13742014-11-17 14:32:17 +00002769 break;
2770
2771 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002772 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002773 if (in.IsRegister()) {
2774 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2775 } else if (in.IsConstant()) {
2776 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2777 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002778 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002779 } else {
2780 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2781 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2782 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002783 break;
2784
Roland Levillaincff13742014-11-17 14:32:17 +00002785 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002786 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002787 if (in.IsFpuRegister()) {
2788 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2789 } else if (in.IsConstant()) {
2790 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2791 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002792 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002793 } else {
2794 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2795 Address(CpuRegister(RSP), in.GetStackIndex()));
2796 }
Roland Levillaincff13742014-11-17 14:32:17 +00002797 break;
2798
2799 default:
2800 LOG(FATAL) << "Unexpected type conversion from " << input_type
2801 << " to " << result_type;
2802 };
2803 break;
2804
Roland Levillaindff1f282014-11-05 14:15:05 +00002805 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002806 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002807 case Primitive::kPrimBoolean:
2808 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002809 case Primitive::kPrimByte:
2810 case Primitive::kPrimShort:
2811 case Primitive::kPrimInt:
2812 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002813 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002814 if (in.IsRegister()) {
2815 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2816 } else if (in.IsConstant()) {
2817 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2818 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002819 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002820 } else {
2821 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2822 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2823 }
Roland Levillaincff13742014-11-17 14:32:17 +00002824 break;
2825
2826 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002827 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002828 if (in.IsRegister()) {
2829 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2830 } else if (in.IsConstant()) {
2831 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2832 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002833 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002834 } else {
2835 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2836 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2837 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002838 break;
2839
Roland Levillaincff13742014-11-17 14:32:17 +00002840 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002841 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002842 if (in.IsFpuRegister()) {
2843 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2844 } else if (in.IsConstant()) {
2845 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2846 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002847 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002848 } else {
2849 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2850 Address(CpuRegister(RSP), in.GetStackIndex()));
2851 }
Roland Levillaincff13742014-11-17 14:32:17 +00002852 break;
2853
2854 default:
2855 LOG(FATAL) << "Unexpected type conversion from " << input_type
2856 << " to " << result_type;
2857 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002858 break;
2859
2860 default:
2861 LOG(FATAL) << "Unexpected type conversion from " << input_type
2862 << " to " << result_type;
2863 }
2864}
2865
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002866void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002867 LocationSummary* locations =
2868 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002869 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002870 case Primitive::kPrimInt: {
2871 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002872 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002874 break;
2875 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002876
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002877 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002878 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002879 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002880 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002882 break;
2883 }
2884
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002885 case Primitive::kPrimDouble:
2886 case Primitive::kPrimFloat: {
2887 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002888 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002889 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002890 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002891 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002892
2893 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002894 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002895 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896}
2897
2898void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2899 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002900 Location first = locations->InAt(0);
2901 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002902 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002903
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002905 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002906 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002907 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2908 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002909 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2910 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002911 } else {
2912 __ leal(out.AsRegister<CpuRegister>(), Address(
2913 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2914 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002915 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002916 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2917 __ addl(out.AsRegister<CpuRegister>(),
2918 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2919 } else {
2920 __ leal(out.AsRegister<CpuRegister>(), Address(
2921 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2922 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002923 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002924 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002925 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002926 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002927 break;
2928 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002930 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002931 if (second.IsRegister()) {
2932 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2933 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002934 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2935 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002936 } else {
2937 __ leaq(out.AsRegister<CpuRegister>(), Address(
2938 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2939 }
2940 } else {
2941 DCHECK(second.IsConstant());
2942 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2943 int32_t int32_value = Low32Bits(value);
2944 DCHECK_EQ(int32_value, value);
2945 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2946 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2947 } else {
2948 __ leaq(out.AsRegister<CpuRegister>(), Address(
2949 first.AsRegister<CpuRegister>(), int32_value));
2950 }
2951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002952 break;
2953 }
2954
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002956 if (second.IsFpuRegister()) {
2957 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2958 } else if (second.IsConstant()) {
2959 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002960 codegen_->LiteralFloatAddress(
2961 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002962 } else {
2963 DCHECK(second.IsStackSlot());
2964 __ addss(first.AsFpuRegister<XmmRegister>(),
2965 Address(CpuRegister(RSP), second.GetStackIndex()));
2966 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002967 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 }
2969
2970 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002971 if (second.IsFpuRegister()) {
2972 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2973 } else if (second.IsConstant()) {
2974 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002975 codegen_->LiteralDoubleAddress(
2976 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002977 } else {
2978 DCHECK(second.IsDoubleStackSlot());
2979 __ addsd(first.AsFpuRegister<XmmRegister>(),
2980 Address(CpuRegister(RSP), second.GetStackIndex()));
2981 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 break;
2983 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002984
2985 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 }
2988}
2989
2990void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002991 LocationSummary* locations =
2992 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002994 case Primitive::kPrimInt: {
2995 locations->SetInAt(0, Location::RequiresRegister());
2996 locations->SetInAt(1, Location::Any());
2997 locations->SetOut(Location::SameAsFirstInput());
2998 break;
2999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003000 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003001 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003002 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003003 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003004 break;
3005 }
Calin Juravle11351682014-10-23 15:38:15 +01003006 case Primitive::kPrimFloat:
3007 case Primitive::kPrimDouble: {
3008 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003009 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003010 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011 break;
Calin Juravle11351682014-10-23 15:38:15 +01003012 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003013 default:
Calin Juravle11351682014-10-23 15:38:15 +01003014 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003015 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003016}
3017
3018void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3019 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003020 Location first = locations->InAt(0);
3021 Location second = locations->InAt(1);
3022 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003024 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003025 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003027 } else if (second.IsConstant()) {
3028 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003030 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003032 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003033 break;
3034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003036 if (second.IsConstant()) {
3037 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3038 DCHECK(IsInt<32>(value));
3039 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3040 } else {
3041 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3042 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003043 break;
3044 }
3045
Calin Juravle11351682014-10-23 15:38:15 +01003046 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003047 if (second.IsFpuRegister()) {
3048 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3049 } else if (second.IsConstant()) {
3050 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003051 codegen_->LiteralFloatAddress(
3052 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003053 } else {
3054 DCHECK(second.IsStackSlot());
3055 __ subss(first.AsFpuRegister<XmmRegister>(),
3056 Address(CpuRegister(RSP), second.GetStackIndex()));
3057 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 break;
Calin Juravle11351682014-10-23 15:38:15 +01003059 }
3060
3061 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003062 if (second.IsFpuRegister()) {
3063 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3064 } else if (second.IsConstant()) {
3065 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003066 codegen_->LiteralDoubleAddress(
3067 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003068 } else {
3069 DCHECK(second.IsDoubleStackSlot());
3070 __ subsd(first.AsFpuRegister<XmmRegister>(),
3071 Address(CpuRegister(RSP), second.GetStackIndex()));
3072 }
Calin Juravle11351682014-10-23 15:38:15 +01003073 break;
3074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003075
3076 default:
Calin Juravle11351682014-10-23 15:38:15 +01003077 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 }
3079}
3080
Calin Juravle34bacdf2014-10-07 20:23:36 +01003081void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3082 LocationSummary* locations =
3083 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3084 switch (mul->GetResultType()) {
3085 case Primitive::kPrimInt: {
3086 locations->SetInAt(0, Location::RequiresRegister());
3087 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003088 if (mul->InputAt(1)->IsIntConstant()) {
3089 // Can use 3 operand multiply.
3090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3091 } else {
3092 locations->SetOut(Location::SameAsFirstInput());
3093 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003094 break;
3095 }
3096 case Primitive::kPrimLong: {
3097 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003098 locations->SetInAt(1, Location::Any());
3099 if (mul->InputAt(1)->IsLongConstant() &&
3100 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003101 // Can use 3 operand multiply.
3102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3103 } else {
3104 locations->SetOut(Location::SameAsFirstInput());
3105 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003106 break;
3107 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003108 case Primitive::kPrimFloat:
3109 case Primitive::kPrimDouble: {
3110 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003111 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003113 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003114 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003115
3116 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003117 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003118 }
3119}
3120
3121void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3122 LocationSummary* locations = mul->GetLocations();
3123 Location first = locations->InAt(0);
3124 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003125 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003127 case Primitive::kPrimInt:
3128 // The constant may have ended up in a register, so test explicitly to avoid
3129 // problems where the output may not be the same as the first operand.
3130 if (mul->InputAt(1)->IsIntConstant()) {
3131 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3132 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3133 } else if (second.IsRegister()) {
3134 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003136 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003137 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003139 __ imull(first.AsRegister<CpuRegister>(),
3140 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141 }
3142 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003143 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003144 // The constant may have ended up in a register, so test explicitly to avoid
3145 // problems where the output may not be the same as the first operand.
3146 if (mul->InputAt(1)->IsLongConstant()) {
3147 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3148 if (IsInt<32>(value)) {
3149 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3150 Immediate(static_cast<int32_t>(value)));
3151 } else {
3152 // Have to use the constant area.
3153 DCHECK(first.Equals(out));
3154 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3155 }
3156 } else if (second.IsRegister()) {
3157 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003158 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003159 } else {
3160 DCHECK(second.IsDoubleStackSlot());
3161 DCHECK(first.Equals(out));
3162 __ imulq(first.AsRegister<CpuRegister>(),
3163 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003164 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 break;
3166 }
3167
Calin Juravleb5bfa962014-10-21 18:02:24 +01003168 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003169 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003170 if (second.IsFpuRegister()) {
3171 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3172 } else if (second.IsConstant()) {
3173 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003174 codegen_->LiteralFloatAddress(
3175 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003176 } else {
3177 DCHECK(second.IsStackSlot());
3178 __ mulss(first.AsFpuRegister<XmmRegister>(),
3179 Address(CpuRegister(RSP), second.GetStackIndex()));
3180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003182 }
3183
3184 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003185 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003186 if (second.IsFpuRegister()) {
3187 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3188 } else if (second.IsConstant()) {
3189 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003190 codegen_->LiteralDoubleAddress(
3191 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003192 } else {
3193 DCHECK(second.IsDoubleStackSlot());
3194 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3195 Address(CpuRegister(RSP), second.GetStackIndex()));
3196 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003197 break;
3198 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199
3200 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 }
3203}
3204
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003205void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3206 uint32_t stack_adjustment, bool is_float) {
3207 if (source.IsStackSlot()) {
3208 DCHECK(is_float);
3209 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3210 } else if (source.IsDoubleStackSlot()) {
3211 DCHECK(!is_float);
3212 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3213 } else {
3214 // Write the value to the temporary location on the stack and load to FP stack.
3215 if (is_float) {
3216 Location stack_temp = Location::StackSlot(temp_offset);
3217 codegen_->Move(stack_temp, source);
3218 __ flds(Address(CpuRegister(RSP), temp_offset));
3219 } else {
3220 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3221 codegen_->Move(stack_temp, source);
3222 __ fldl(Address(CpuRegister(RSP), temp_offset));
3223 }
3224 }
3225}
3226
3227void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3228 Primitive::Type type = rem->GetResultType();
3229 bool is_float = type == Primitive::kPrimFloat;
3230 size_t elem_size = Primitive::ComponentSize(type);
3231 LocationSummary* locations = rem->GetLocations();
3232 Location first = locations->InAt(0);
3233 Location second = locations->InAt(1);
3234 Location out = locations->Out();
3235
3236 // Create stack space for 2 elements.
3237 // TODO: enhance register allocator to ask for stack temporaries.
3238 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3239
3240 // Load the values to the FP stack in reverse order, using temporaries if needed.
3241 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3242 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3243
3244 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003245 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003246 __ Bind(&retry);
3247 __ fprem();
3248
3249 // Move FP status to AX.
3250 __ fstsw();
3251
3252 // And see if the argument reduction is complete. This is signaled by the
3253 // C2 FPU flag bit set to 0.
3254 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3255 __ j(kNotEqual, &retry);
3256
3257 // We have settled on the final value. Retrieve it into an XMM register.
3258 // Store FP top of stack to real stack.
3259 if (is_float) {
3260 __ fsts(Address(CpuRegister(RSP), 0));
3261 } else {
3262 __ fstl(Address(CpuRegister(RSP), 0));
3263 }
3264
3265 // Pop the 2 items from the FP stack.
3266 __ fucompp();
3267
3268 // Load the value from the stack into an XMM register.
3269 DCHECK(out.IsFpuRegister()) << out;
3270 if (is_float) {
3271 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3272 } else {
3273 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3274 }
3275
3276 // And remove the temporary stack space we allocated.
3277 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3278}
3279
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3281 DCHECK(instruction->IsDiv() || instruction->IsRem());
3282
3283 LocationSummary* locations = instruction->GetLocations();
3284 Location second = locations->InAt(1);
3285 DCHECK(second.IsConstant());
3286
3287 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3288 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003289 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003290
3291 DCHECK(imm == 1 || imm == -1);
3292
3293 switch (instruction->GetResultType()) {
3294 case Primitive::kPrimInt: {
3295 if (instruction->IsRem()) {
3296 __ xorl(output_register, output_register);
3297 } else {
3298 __ movl(output_register, input_register);
3299 if (imm == -1) {
3300 __ negl(output_register);
3301 }
3302 }
3303 break;
3304 }
3305
3306 case Primitive::kPrimLong: {
3307 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003308 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003309 } else {
3310 __ movq(output_register, input_register);
3311 if (imm == -1) {
3312 __ negq(output_register);
3313 }
3314 }
3315 break;
3316 }
3317
3318 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003319 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003320 }
3321}
3322
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003323void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003324 LocationSummary* locations = instruction->GetLocations();
3325 Location second = locations->InAt(1);
3326
3327 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3328 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3329
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003330 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003331 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3332 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333
3334 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3335
3336 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003337 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003338 __ testl(numerator, numerator);
3339 __ cmov(kGreaterEqual, tmp, numerator);
3340 int shift = CTZ(imm);
3341 __ sarl(tmp, Immediate(shift));
3342
3343 if (imm < 0) {
3344 __ negl(tmp);
3345 }
3346
3347 __ movl(output_register, tmp);
3348 } else {
3349 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3350 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3351
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003352 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003353 __ addq(rdx, numerator);
3354 __ testq(numerator, numerator);
3355 __ cmov(kGreaterEqual, rdx, numerator);
3356 int shift = CTZ(imm);
3357 __ sarq(rdx, Immediate(shift));
3358
3359 if (imm < 0) {
3360 __ negq(rdx);
3361 }
3362
3363 __ movq(output_register, rdx);
3364 }
3365}
3366
3367void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3368 DCHECK(instruction->IsDiv() || instruction->IsRem());
3369
3370 LocationSummary* locations = instruction->GetLocations();
3371 Location second = locations->InAt(1);
3372
3373 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3374 : locations->GetTemp(0).AsRegister<CpuRegister>();
3375 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3376 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3377 : locations->Out().AsRegister<CpuRegister>();
3378 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3379
3380 DCHECK_EQ(RAX, eax.AsRegister());
3381 DCHECK_EQ(RDX, edx.AsRegister());
3382 if (instruction->IsDiv()) {
3383 DCHECK_EQ(RAX, out.AsRegister());
3384 } else {
3385 DCHECK_EQ(RDX, out.AsRegister());
3386 }
3387
3388 int64_t magic;
3389 int shift;
3390
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003391 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003392 if (instruction->GetResultType() == Primitive::kPrimInt) {
3393 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3394
3395 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3396
3397 __ movl(numerator, eax);
3398
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 __ movl(eax, Immediate(magic));
3400 __ imull(numerator);
3401
3402 if (imm > 0 && magic < 0) {
3403 __ addl(edx, numerator);
3404 } else if (imm < 0 && magic > 0) {
3405 __ subl(edx, numerator);
3406 }
3407
3408 if (shift != 0) {
3409 __ sarl(edx, Immediate(shift));
3410 }
3411
3412 __ movl(eax, edx);
3413 __ shrl(edx, Immediate(31));
3414 __ addl(edx, eax);
3415
3416 if (instruction->IsRem()) {
3417 __ movl(eax, numerator);
3418 __ imull(edx, Immediate(imm));
3419 __ subl(eax, edx);
3420 __ movl(edx, eax);
3421 } else {
3422 __ movl(eax, edx);
3423 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424 } else {
3425 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3426
3427 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3428
3429 CpuRegister rax = eax;
3430 CpuRegister rdx = edx;
3431
3432 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3433
3434 // Save the numerator.
3435 __ movq(numerator, rax);
3436
3437 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003438 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439
3440 // RDX:RAX = magic * numerator
3441 __ imulq(numerator);
3442
3443 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003444 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 __ addq(rdx, numerator);
3446 } else if (imm < 0 && magic > 0) {
3447 // RDX -= numerator
3448 __ subq(rdx, numerator);
3449 }
3450
3451 // Shift if needed.
3452 if (shift != 0) {
3453 __ sarq(rdx, Immediate(shift));
3454 }
3455
3456 // RDX += 1 if RDX < 0
3457 __ movq(rax, rdx);
3458 __ shrq(rdx, Immediate(63));
3459 __ addq(rdx, rax);
3460
3461 if (instruction->IsRem()) {
3462 __ movq(rax, numerator);
3463
3464 if (IsInt<32>(imm)) {
3465 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3466 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003467 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003468 }
3469
3470 __ subq(rax, rdx);
3471 __ movq(rdx, rax);
3472 } else {
3473 __ movq(rax, rdx);
3474 }
3475 }
3476}
3477
Calin Juravlebacfec32014-11-14 15:54:36 +00003478void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3479 DCHECK(instruction->IsDiv() || instruction->IsRem());
3480 Primitive::Type type = instruction->GetResultType();
3481 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3482
3483 bool is_div = instruction->IsDiv();
3484 LocationSummary* locations = instruction->GetLocations();
3485
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3487 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003488
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003491
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003492 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003493 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003494
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495 if (imm == 0) {
3496 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3497 } else if (imm == 1 || imm == -1) {
3498 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003499 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003500 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 } else {
3502 DCHECK(imm <= -2 || imm >= 2);
3503 GenerateDivRemWithAnyConstant(instruction);
3504 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003505 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003506 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003508 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003510
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3512 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3513 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3514 // so it's safe to just use negl instead of more complex comparisons.
3515 if (type == Primitive::kPrimInt) {
3516 __ cmpl(second_reg, Immediate(-1));
3517 __ j(kEqual, slow_path->GetEntryLabel());
3518 // edx:eax <- sign-extended of eax
3519 __ cdq();
3520 // eax = quotient, edx = remainder
3521 __ idivl(second_reg);
3522 } else {
3523 __ cmpq(second_reg, Immediate(-1));
3524 __ j(kEqual, slow_path->GetEntryLabel());
3525 // rdx:rax <- sign-extended of rax
3526 __ cqo();
3527 // rax = quotient, rdx = remainder
3528 __ idivq(second_reg);
3529 }
3530 __ Bind(slow_path->GetExitLabel());
3531 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003532}
3533
Calin Juravle7c4954d2014-10-28 16:57:40 +00003534void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3535 LocationSummary* locations =
3536 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3537 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003538 case Primitive::kPrimInt:
3539 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003540 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003542 locations->SetOut(Location::SameAsFirstInput());
3543 // Intel uses edx:eax as the dividend.
3544 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003545 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3546 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3547 // output and request another temp.
3548 if (div->InputAt(1)->IsConstant()) {
3549 locations->AddTemp(Location::RequiresRegister());
3550 }
Calin Juravled0d48522014-11-04 16:40:20 +00003551 break;
3552 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003553
Calin Juravle7c4954d2014-10-28 16:57:40 +00003554 case Primitive::kPrimFloat:
3555 case Primitive::kPrimDouble: {
3556 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003557 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003558 locations->SetOut(Location::SameAsFirstInput());
3559 break;
3560 }
3561
3562 default:
3563 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3564 }
3565}
3566
3567void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3568 LocationSummary* locations = div->GetLocations();
3569 Location first = locations->InAt(0);
3570 Location second = locations->InAt(1);
3571 DCHECK(first.Equals(locations->Out()));
3572
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 Primitive::Type type = div->GetResultType();
3574 switch (type) {
3575 case Primitive::kPrimInt:
3576 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003577 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003578 break;
3579 }
3580
Calin Juravle7c4954d2014-10-28 16:57:40 +00003581 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003582 if (second.IsFpuRegister()) {
3583 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3584 } else if (second.IsConstant()) {
3585 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003586 codegen_->LiteralFloatAddress(
3587 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003588 } else {
3589 DCHECK(second.IsStackSlot());
3590 __ divss(first.AsFpuRegister<XmmRegister>(),
3591 Address(CpuRegister(RSP), second.GetStackIndex()));
3592 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003593 break;
3594 }
3595
3596 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003597 if (second.IsFpuRegister()) {
3598 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3599 } else if (second.IsConstant()) {
3600 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003601 codegen_->LiteralDoubleAddress(
3602 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003603 } else {
3604 DCHECK(second.IsDoubleStackSlot());
3605 __ divsd(first.AsFpuRegister<XmmRegister>(),
3606 Address(CpuRegister(RSP), second.GetStackIndex()));
3607 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003608 break;
3609 }
3610
3611 default:
3612 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3613 }
3614}
3615
Calin Juravlebacfec32014-11-14 15:54:36 +00003616void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003617 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003618 LocationSummary* locations =
3619 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003620
3621 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 case Primitive::kPrimInt:
3623 case Primitive::kPrimLong: {
3624 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003625 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3627 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3629 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3630 // output and request another temp.
3631 if (rem->InputAt(1)->IsConstant()) {
3632 locations->AddTemp(Location::RequiresRegister());
3633 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636
3637 case Primitive::kPrimFloat:
3638 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003639 locations->SetInAt(0, Location::Any());
3640 locations->SetInAt(1, Location::Any());
3641 locations->SetOut(Location::RequiresFpuRegister());
3642 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003643 break;
3644 }
3645
3646 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003647 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 }
3649}
3650
3651void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3652 Primitive::Type type = rem->GetResultType();
3653 switch (type) {
3654 case Primitive::kPrimInt:
3655 case Primitive::kPrimLong: {
3656 GenerateDivRemIntegral(rem);
3657 break;
3658 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003659 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003660 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003661 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003662 break;
3663 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003664 default:
3665 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3666 }
3667}
3668
Calin Juravled0d48522014-11-04 16:40:20 +00003669void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003670 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3671 ? LocationSummary::kCallOnSlowPath
3672 : LocationSummary::kNoCall;
3673 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003674 locations->SetInAt(0, Location::Any());
3675 if (instruction->HasUses()) {
3676 locations->SetOut(Location::SameAsFirstInput());
3677 }
3678}
3679
3680void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003681 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003682 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3683 codegen_->AddSlowPath(slow_path);
3684
3685 LocationSummary* locations = instruction->GetLocations();
3686 Location value = locations->InAt(0);
3687
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003688 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003689 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003690 case Primitive::kPrimByte:
3691 case Primitive::kPrimChar:
3692 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003693 case Primitive::kPrimInt: {
3694 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003696 __ j(kEqual, slow_path->GetEntryLabel());
3697 } else if (value.IsStackSlot()) {
3698 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3699 __ j(kEqual, slow_path->GetEntryLabel());
3700 } else {
3701 DCHECK(value.IsConstant()) << value;
3702 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3703 __ jmp(slow_path->GetEntryLabel());
3704 }
3705 }
3706 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003707 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003708 case Primitive::kPrimLong: {
3709 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003710 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003711 __ j(kEqual, slow_path->GetEntryLabel());
3712 } else if (value.IsDoubleStackSlot()) {
3713 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3714 __ j(kEqual, slow_path->GetEntryLabel());
3715 } else {
3716 DCHECK(value.IsConstant()) << value;
3717 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3718 __ jmp(slow_path->GetEntryLabel());
3719 }
3720 }
3721 break;
3722 }
3723 default:
3724 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003725 }
Calin Juravled0d48522014-11-04 16:40:20 +00003726}
3727
Calin Juravle9aec02f2014-11-18 23:06:35 +00003728void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3729 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3730
3731 LocationSummary* locations =
3732 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3733
3734 switch (op->GetResultType()) {
3735 case Primitive::kPrimInt:
3736 case Primitive::kPrimLong: {
3737 locations->SetInAt(0, Location::RequiresRegister());
3738 // The shift count needs to be in CL.
3739 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3740 locations->SetOut(Location::SameAsFirstInput());
3741 break;
3742 }
3743 default:
3744 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3745 }
3746}
3747
3748void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3749 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3750
3751 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003752 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 Location second = locations->InAt(1);
3754
3755 switch (op->GetResultType()) {
3756 case Primitive::kPrimInt: {
3757 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 if (op->IsShl()) {
3760 __ shll(first_reg, second_reg);
3761 } else if (op->IsShr()) {
3762 __ sarl(first_reg, second_reg);
3763 } else {
3764 __ shrl(first_reg, second_reg);
3765 }
3766 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003767 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003768 if (op->IsShl()) {
3769 __ shll(first_reg, imm);
3770 } else if (op->IsShr()) {
3771 __ sarl(first_reg, imm);
3772 } else {
3773 __ shrl(first_reg, imm);
3774 }
3775 }
3776 break;
3777 }
3778 case Primitive::kPrimLong: {
3779 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003781 if (op->IsShl()) {
3782 __ shlq(first_reg, second_reg);
3783 } else if (op->IsShr()) {
3784 __ sarq(first_reg, second_reg);
3785 } else {
3786 __ shrq(first_reg, second_reg);
3787 }
3788 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003789 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003790 if (op->IsShl()) {
3791 __ shlq(first_reg, imm);
3792 } else if (op->IsShr()) {
3793 __ sarq(first_reg, imm);
3794 } else {
3795 __ shrq(first_reg, imm);
3796 }
3797 }
3798 break;
3799 }
3800 default:
3801 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003802 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003803 }
3804}
3805
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003806void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3809
3810 switch (ror->GetResultType()) {
3811 case Primitive::kPrimInt:
3812 case Primitive::kPrimLong: {
3813 locations->SetInAt(0, Location::RequiresRegister());
3814 // The shift count needs to be in CL (unless it is a constant).
3815 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3816 locations->SetOut(Location::SameAsFirstInput());
3817 break;
3818 }
3819 default:
3820 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3821 UNREACHABLE();
3822 }
3823}
3824
3825void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3826 LocationSummary* locations = ror->GetLocations();
3827 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3828 Location second = locations->InAt(1);
3829
3830 switch (ror->GetResultType()) {
3831 case Primitive::kPrimInt:
3832 if (second.IsRegister()) {
3833 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3834 __ rorl(first_reg, second_reg);
3835 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003836 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003837 __ rorl(first_reg, imm);
3838 }
3839 break;
3840 case Primitive::kPrimLong:
3841 if (second.IsRegister()) {
3842 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3843 __ rorq(first_reg, second_reg);
3844 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003845 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003846 __ rorq(first_reg, imm);
3847 }
3848 break;
3849 default:
3850 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3851 UNREACHABLE();
3852 }
3853}
3854
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3856 HandleShift(shl);
3857}
3858
3859void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3860 HandleShift(shl);
3861}
3862
3863void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3864 HandleShift(shr);
3865}
3866
3867void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3868 HandleShift(shr);
3869}
3870
3871void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3872 HandleShift(ushr);
3873}
3874
3875void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3876 HandleShift(ushr);
3877}
3878
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003879void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003880 LocationSummary* locations =
3881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003882 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003883 if (instruction->IsStringAlloc()) {
3884 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3885 } else {
3886 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3887 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3888 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003889 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003890}
3891
3892void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003893 // Note: if heap poisoning is enabled, the entry point takes cares
3894 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003895 if (instruction->IsStringAlloc()) {
3896 // String is allocated through StringFactory. Call NewEmptyString entry point.
3897 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3898 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3899 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3900 __ call(Address(temp, code_offset.SizeValue()));
3901 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3902 } else {
3903 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3904 instruction,
3905 instruction->GetDexPc(),
3906 nullptr);
3907 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3908 DCHECK(!codegen_->IsLeafMethod());
3909 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003910}
3911
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003912void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3913 LocationSummary* locations =
3914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3915 InvokeRuntimeCallingConvention calling_convention;
3916 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003917 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003918 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003919 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003920}
3921
3922void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3923 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003924 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3925 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003926 // Note: if heap poisoning is enabled, the entry point takes cares
3927 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003928 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3929 instruction,
3930 instruction->GetDexPc(),
3931 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003932 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003933
3934 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003935}
3936
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003937void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003938 LocationSummary* locations =
3939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003940 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3941 if (location.IsStackSlot()) {
3942 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3943 } else if (location.IsDoubleStackSlot()) {
3944 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3945 }
3946 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003947}
3948
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003949void InstructionCodeGeneratorX86_64::VisitParameterValue(
3950 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003951 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003952}
3953
3954void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3955 LocationSummary* locations =
3956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3957 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3958}
3959
3960void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3961 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3962 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003963}
3964
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003965void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3966 LocationSummary* locations =
3967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3968 locations->SetInAt(0, Location::RequiresRegister());
3969 locations->SetOut(Location::RequiresRegister());
3970}
3971
3972void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3973 LocationSummary* locations = instruction->GetLocations();
3974 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003975 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003976 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3977 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
3978 } else {
3979 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3980 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
3981 }
3982 __ movq(locations->Out().AsRegister<CpuRegister>(),
3983 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
3984}
3985
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003986void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003987 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003988 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003989 locations->SetInAt(0, Location::RequiresRegister());
3990 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003991}
3992
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003993void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3994 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003995 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3996 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003997 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003998 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003999 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004000 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004001 break;
4002
4003 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004004 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004005 break;
4006
4007 default:
4008 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4009 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004010}
4011
David Brazdil66d126e2015-04-03 16:02:44 +01004012void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4013 LocationSummary* locations =
4014 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4015 locations->SetInAt(0, Location::RequiresRegister());
4016 locations->SetOut(Location::SameAsFirstInput());
4017}
4018
4019void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004020 LocationSummary* locations = bool_not->GetLocations();
4021 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4022 locations->Out().AsRegister<CpuRegister>().AsRegister());
4023 Location out = locations->Out();
4024 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4025}
4026
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004027void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004028 LocationSummary* locations =
4029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4031 locations->SetInAt(i, Location::Any());
4032 }
4033 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004034}
4035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004036void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004037 LOG(FATAL) << "Unimplemented";
4038}
4039
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004040void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004041 /*
4042 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004043 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004044 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4045 */
4046 switch (kind) {
4047 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004048 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004049 break;
4050 }
4051 case MemBarrierKind::kAnyStore:
4052 case MemBarrierKind::kLoadAny:
4053 case MemBarrierKind::kStoreStore: {
4054 // nop
4055 break;
4056 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004057 case MemBarrierKind::kNTStoreStore:
4058 // Non-Temporal Store/Store needs an explicit fence.
4059 MemoryFence(/* non-temporal */ true);
4060 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004061 }
4062}
4063
4064void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4065 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4066
Roland Levillain0d5a2812015-11-13 10:07:31 +00004067 bool object_field_get_with_read_barrier =
4068 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004069 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004070 new (GetGraph()->GetArena()) LocationSummary(instruction,
4071 object_field_get_with_read_barrier ?
4072 LocationSummary::kCallOnSlowPath :
4073 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004074 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004075 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4076 locations->SetOut(Location::RequiresFpuRegister());
4077 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004078 // The output overlaps for an object field get when read barriers
4079 // are enabled: we do not want the move to overwrite the object's
4080 // location, as we need it to emit the read barrier.
4081 locations->SetOut(
4082 Location::RequiresRegister(),
4083 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004084 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004085 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4086 // We need a temporary register for the read barrier marking slow
4087 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4088 locations->AddTemp(Location::RequiresRegister());
4089 }
Calin Juravle52c48962014-12-16 17:02:57 +00004090}
4091
4092void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4093 const FieldInfo& field_info) {
4094 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4095
4096 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004097 Location base_loc = locations->InAt(0);
4098 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004099 Location out = locations->Out();
4100 bool is_volatile = field_info.IsVolatile();
4101 Primitive::Type field_type = field_info.GetFieldType();
4102 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4103
4104 switch (field_type) {
4105 case Primitive::kPrimBoolean: {
4106 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4107 break;
4108 }
4109
4110 case Primitive::kPrimByte: {
4111 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4112 break;
4113 }
4114
4115 case Primitive::kPrimShort: {
4116 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4117 break;
4118 }
4119
4120 case Primitive::kPrimChar: {
4121 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4122 break;
4123 }
4124
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004125 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004126 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4127 break;
4128 }
4129
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004130 case Primitive::kPrimNot: {
4131 // /* HeapReference<Object> */ out = *(base + offset)
4132 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4133 Location temp_loc = locations->GetTemp(0);
4134 // Note that a potential implicit null check is handled in this
4135 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4136 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4137 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4138 if (is_volatile) {
4139 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4140 }
4141 } else {
4142 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4143 codegen_->MaybeRecordImplicitNullCheck(instruction);
4144 if (is_volatile) {
4145 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4146 }
4147 // If read barriers are enabled, emit read barriers other than
4148 // Baker's using a slow path (and also unpoison the loaded
4149 // reference, if heap poisoning is enabled).
4150 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4151 }
4152 break;
4153 }
4154
Calin Juravle52c48962014-12-16 17:02:57 +00004155 case Primitive::kPrimLong: {
4156 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4157 break;
4158 }
4159
4160 case Primitive::kPrimFloat: {
4161 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4162 break;
4163 }
4164
4165 case Primitive::kPrimDouble: {
4166 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4167 break;
4168 }
4169
4170 case Primitive::kPrimVoid:
4171 LOG(FATAL) << "Unreachable type " << field_type;
4172 UNREACHABLE();
4173 }
4174
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004175 if (field_type == Primitive::kPrimNot) {
4176 // Potential implicit null checks, in the case of reference
4177 // fields, are handled in the previous switch statement.
4178 } else {
4179 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004180 }
Roland Levillain4d027112015-07-01 15:41:14 +01004181
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004182 if (is_volatile) {
4183 if (field_type == Primitive::kPrimNot) {
4184 // Memory barriers, in the case of references, are also handled
4185 // in the previous switch statement.
4186 } else {
4187 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4188 }
Roland Levillain4d027112015-07-01 15:41:14 +01004189 }
Calin Juravle52c48962014-12-16 17:02:57 +00004190}
4191
4192void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4193 const FieldInfo& field_info) {
4194 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4195
4196 LocationSummary* locations =
4197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004198 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004199 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004200 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004201 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004202
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004203 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004204 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004205 if (is_volatile) {
4206 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4207 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4208 } else {
4209 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4210 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004211 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004212 if (is_volatile) {
4213 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4214 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4215 } else {
4216 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4217 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004218 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004219 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004220 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004221 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004222 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004223 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4224 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004225 locations->AddTemp(Location::RequiresRegister());
4226 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004227}
4228
Calin Juravle52c48962014-12-16 17:02:57 +00004229void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004230 const FieldInfo& field_info,
4231 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004232 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4233
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004234 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004235 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4236 Location value = locations->InAt(1);
4237 bool is_volatile = field_info.IsVolatile();
4238 Primitive::Type field_type = field_info.GetFieldType();
4239 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4240
4241 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004242 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004243 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244
Mark Mendellea5af682015-10-22 17:35:49 -04004245 bool maybe_record_implicit_null_check_done = false;
4246
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247 switch (field_type) {
4248 case Primitive::kPrimBoolean:
4249 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004250 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004251 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004252 __ movb(Address(base, offset), Immediate(v));
4253 } else {
4254 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4255 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004256 break;
4257 }
4258
4259 case Primitive::kPrimShort:
4260 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004261 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004262 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004263 __ movw(Address(base, offset), Immediate(v));
4264 } else {
4265 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267 break;
4268 }
4269
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004270 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004271 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004272 if (value.IsConstant()) {
4273 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004274 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4275 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4276 // Note: if heap poisoning is enabled, no need to poison
4277 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004278 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004279 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004280 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4281 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4282 __ movl(temp, value.AsRegister<CpuRegister>());
4283 __ PoisonHeapReference(temp);
4284 __ movl(Address(base, offset), temp);
4285 } else {
4286 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4287 }
Mark Mendell40741f32015-04-20 22:10:34 -04004288 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289 break;
4290 }
4291
4292 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004293 if (value.IsConstant()) {
4294 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004295 codegen_->MoveInt64ToAddress(Address(base, offset),
4296 Address(base, offset + sizeof(int32_t)),
4297 v,
4298 instruction);
4299 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004300 } else {
4301 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4302 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 break;
4304 }
4305
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004306 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004307 if (value.IsConstant()) {
4308 int32_t v =
4309 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4310 __ movl(Address(base, offset), Immediate(v));
4311 } else {
4312 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4313 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004314 break;
4315 }
4316
4317 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004318 if (value.IsConstant()) {
4319 int64_t v =
4320 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4321 codegen_->MoveInt64ToAddress(Address(base, offset),
4322 Address(base, offset + sizeof(int32_t)),
4323 v,
4324 instruction);
4325 maybe_record_implicit_null_check_done = true;
4326 } else {
4327 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4328 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004329 break;
4330 }
4331
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004332 case Primitive::kPrimVoid:
4333 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004334 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004335 }
Calin Juravle52c48962014-12-16 17:02:57 +00004336
Mark Mendellea5af682015-10-22 17:35:49 -04004337 if (!maybe_record_implicit_null_check_done) {
4338 codegen_->MaybeRecordImplicitNullCheck(instruction);
4339 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004340
4341 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4342 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4343 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004344 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004345 }
4346
Calin Juravle52c48962014-12-16 17:02:57 +00004347 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004348 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004349 }
4350}
4351
4352void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4353 HandleFieldSet(instruction, instruction->GetFieldInfo());
4354}
4355
4356void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004357 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358}
4359
4360void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004361 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362}
4363
4364void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004365 HandleFieldGet(instruction, instruction->GetFieldInfo());
4366}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367
Calin Juravle52c48962014-12-16 17:02:57 +00004368void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4369 HandleFieldGet(instruction);
4370}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371
Calin Juravle52c48962014-12-16 17:02:57 +00004372void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4373 HandleFieldGet(instruction, instruction->GetFieldInfo());
4374}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375
Calin Juravle52c48962014-12-16 17:02:57 +00004376void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4377 HandleFieldSet(instruction, instruction->GetFieldInfo());
4378}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004379
Calin Juravle52c48962014-12-16 17:02:57 +00004380void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004381 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004382}
4383
Calin Juravlee460d1d2015-09-29 04:52:17 +01004384void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4385 HUnresolvedInstanceFieldGet* instruction) {
4386 FieldAccessCallingConventionX86_64 calling_convention;
4387 codegen_->CreateUnresolvedFieldLocationSummary(
4388 instruction, instruction->GetFieldType(), calling_convention);
4389}
4390
4391void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4392 HUnresolvedInstanceFieldGet* instruction) {
4393 FieldAccessCallingConventionX86_64 calling_convention;
4394 codegen_->GenerateUnresolvedFieldAccess(instruction,
4395 instruction->GetFieldType(),
4396 instruction->GetFieldIndex(),
4397 instruction->GetDexPc(),
4398 calling_convention);
4399}
4400
4401void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4402 HUnresolvedInstanceFieldSet* instruction) {
4403 FieldAccessCallingConventionX86_64 calling_convention;
4404 codegen_->CreateUnresolvedFieldLocationSummary(
4405 instruction, instruction->GetFieldType(), calling_convention);
4406}
4407
4408void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4409 HUnresolvedInstanceFieldSet* instruction) {
4410 FieldAccessCallingConventionX86_64 calling_convention;
4411 codegen_->GenerateUnresolvedFieldAccess(instruction,
4412 instruction->GetFieldType(),
4413 instruction->GetFieldIndex(),
4414 instruction->GetDexPc(),
4415 calling_convention);
4416}
4417
4418void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4419 HUnresolvedStaticFieldGet* instruction) {
4420 FieldAccessCallingConventionX86_64 calling_convention;
4421 codegen_->CreateUnresolvedFieldLocationSummary(
4422 instruction, instruction->GetFieldType(), calling_convention);
4423}
4424
4425void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4426 HUnresolvedStaticFieldGet* instruction) {
4427 FieldAccessCallingConventionX86_64 calling_convention;
4428 codegen_->GenerateUnresolvedFieldAccess(instruction,
4429 instruction->GetFieldType(),
4430 instruction->GetFieldIndex(),
4431 instruction->GetDexPc(),
4432 calling_convention);
4433}
4434
4435void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4436 HUnresolvedStaticFieldSet* instruction) {
4437 FieldAccessCallingConventionX86_64 calling_convention;
4438 codegen_->CreateUnresolvedFieldLocationSummary(
4439 instruction, instruction->GetFieldType(), calling_convention);
4440}
4441
4442void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4443 HUnresolvedStaticFieldSet* instruction) {
4444 FieldAccessCallingConventionX86_64 calling_convention;
4445 codegen_->GenerateUnresolvedFieldAccess(instruction,
4446 instruction->GetFieldType(),
4447 instruction->GetFieldIndex(),
4448 instruction->GetDexPc(),
4449 calling_convention);
4450}
4451
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004453 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4454 ? LocationSummary::kCallOnSlowPath
4455 : LocationSummary::kNoCall;
4456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4457 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004458 ? Location::RequiresRegister()
4459 : Location::Any();
4460 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004461 if (instruction->HasUses()) {
4462 locations->SetOut(Location::SameAsFirstInput());
4463 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464}
4465
Calin Juravle2ae48182016-03-16 14:05:09 +00004466void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4467 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004468 return;
4469 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004470 LocationSummary* locations = instruction->GetLocations();
4471 Location obj = locations->InAt(0);
4472
4473 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004474 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004475}
4476
Calin Juravle2ae48182016-03-16 14:05:09 +00004477void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004478 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004479 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004480
4481 LocationSummary* locations = instruction->GetLocations();
4482 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483
4484 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004485 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004486 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004488 } else {
4489 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004490 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004491 __ jmp(slow_path->GetEntryLabel());
4492 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493 }
4494 __ j(kEqual, slow_path->GetEntryLabel());
4495}
4496
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004497void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004498 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004499}
4500
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004501void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004502 bool object_array_get_with_read_barrier =
4503 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004504 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004505 new (GetGraph()->GetArena()) LocationSummary(instruction,
4506 object_array_get_with_read_barrier ?
4507 LocationSummary::kCallOnSlowPath :
4508 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004509 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004510 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004511 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4512 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4513 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004514 // The output overlaps for an object array get when read barriers
4515 // are enabled: we do not want the move to overwrite the array's
4516 // location, as we need it to emit the read barrier.
4517 locations->SetOut(
4518 Location::RequiresRegister(),
4519 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004520 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004521 // We need a temporary register for the read barrier marking slow
4522 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4523 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4524 locations->AddTemp(Location::RequiresRegister());
4525 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004526}
4527
4528void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4529 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004530 Location obj_loc = locations->InAt(0);
4531 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004533 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004535 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004536 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537 case Primitive::kPrimBoolean: {
4538 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004539 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 if (index.IsConstant()) {
4541 __ movzxb(out, Address(obj,
4542 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4543 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004544 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545 }
4546 break;
4547 }
4548
4549 case Primitive::kPrimByte: {
4550 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004551 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 if (index.IsConstant()) {
4553 __ movsxb(out, Address(obj,
4554 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4555 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004556 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004557 }
4558 break;
4559 }
4560
4561 case Primitive::kPrimShort: {
4562 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004563 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004564 if (index.IsConstant()) {
4565 __ movsxw(out, Address(obj,
4566 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4567 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004568 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 }
4570 break;
4571 }
4572
4573 case Primitive::kPrimChar: {
4574 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004575 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 if (index.IsConstant()) {
4577 __ movzxw(out, Address(obj,
4578 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4579 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004580 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004581 }
4582 break;
4583 }
4584
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004585 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004587 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 if (index.IsConstant()) {
4589 __ movl(out, Address(obj,
4590 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4591 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004592 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004593 }
4594 break;
4595 }
4596
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004597 case Primitive::kPrimNot: {
4598 static_assert(
4599 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4600 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4602 // /* HeapReference<Object> */ out =
4603 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4604 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4605 Location temp = locations->GetTemp(0);
4606 // Note that a potential implicit null check is handled in this
4607 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4608 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4609 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4610 } else {
4611 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4612 if (index.IsConstant()) {
4613 uint32_t offset =
4614 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4615 __ movl(out, Address(obj, offset));
4616 codegen_->MaybeRecordImplicitNullCheck(instruction);
4617 // If read barriers are enabled, emit read barriers other than
4618 // Baker's using a slow path (and also unpoison the loaded
4619 // reference, if heap poisoning is enabled).
4620 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4621 } else {
4622 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4623 codegen_->MaybeRecordImplicitNullCheck(instruction);
4624 // If read barriers are enabled, emit read barriers other than
4625 // Baker's using a slow path (and also unpoison the loaded
4626 // reference, if heap poisoning is enabled).
4627 codegen_->MaybeGenerateReadBarrierSlow(
4628 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4629 }
4630 }
4631 break;
4632 }
4633
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004634 case Primitive::kPrimLong: {
4635 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004636 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637 if (index.IsConstant()) {
4638 __ movq(out, Address(obj,
4639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4640 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004641 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004642 }
4643 break;
4644 }
4645
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004646 case Primitive::kPrimFloat: {
4647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004648 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004649 if (index.IsConstant()) {
4650 __ movss(out, Address(obj,
4651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4652 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004653 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004654 }
4655 break;
4656 }
4657
4658 case Primitive::kPrimDouble: {
4659 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004660 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004661 if (index.IsConstant()) {
4662 __ movsd(out, Address(obj,
4663 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4664 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004665 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004666 }
4667 break;
4668 }
4669
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004671 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004672 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 }
Roland Levillain4d027112015-07-01 15:41:14 +01004674
4675 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004676 // Potential implicit null checks, in the case of reference
4677 // arrays, are handled in the previous switch statement.
4678 } else {
4679 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004680 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004681}
4682
4683void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004684 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004685
4686 bool needs_write_barrier =
4687 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004688 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004689 bool object_array_set_with_read_barrier =
4690 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004691
Nicolas Geoffray39468442014-09-02 15:17:15 +01004692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004693 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004694 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004695 LocationSummary::kCallOnSlowPath :
4696 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004698 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004699 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4700 if (Primitive::IsFloatingPointType(value_type)) {
4701 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004702 } else {
4703 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4704 }
4705
4706 if (needs_write_barrier) {
4707 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004708
4709 // This first temporary register is possibly used for heap
4710 // reference poisoning and/or read barrier emission too.
4711 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004712 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004713 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714}
4715
4716void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4717 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004718 Location array_loc = locations->InAt(0);
4719 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004720 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004721 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004722 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004723 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004724 bool needs_write_barrier =
4725 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004726 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4727 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4728 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729
4730 switch (value_type) {
4731 case Primitive::kPrimBoolean:
4732 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004733 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4734 Address address = index.IsConstant()
4735 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4736 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4737 if (value.IsRegister()) {
4738 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004740 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004741 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004742 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 break;
4744 }
4745
4746 case Primitive::kPrimShort:
4747 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004748 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4749 Address address = index.IsConstant()
4750 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4751 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4752 if (value.IsRegister()) {
4753 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004755 DCHECK(value.IsConstant()) << value;
4756 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004757 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004758 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 break;
4760 }
4761
4762 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4764 Address address = index.IsConstant()
4765 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4766 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004767
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004768 if (!value.IsRegister()) {
4769 // Just setting null.
4770 DCHECK(instruction->InputAt(2)->IsNullConstant());
4771 DCHECK(value.IsConstant()) << value;
4772 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004773 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004775 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004776 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004777 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004778
4779 DCHECK(needs_write_barrier);
4780 CpuRegister register_value = value.AsRegister<CpuRegister>();
4781 NearLabel done, not_null, do_put;
4782 SlowPathCode* slow_path = nullptr;
4783 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004784 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004785 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4786 codegen_->AddSlowPath(slow_path);
4787 if (instruction->GetValueCanBeNull()) {
4788 __ testl(register_value, register_value);
4789 __ j(kNotEqual, &not_null);
4790 __ movl(address, Immediate(0));
4791 codegen_->MaybeRecordImplicitNullCheck(instruction);
4792 __ jmp(&done);
4793 __ Bind(&not_null);
4794 }
4795
Roland Levillain0d5a2812015-11-13 10:07:31 +00004796 if (kEmitCompilerReadBarrier) {
4797 // When read barriers are enabled, the type checking
4798 // instrumentation requires two read barriers:
4799 //
4800 // __ movl(temp2, temp);
4801 // // /* HeapReference<Class> */ temp = temp->component_type_
4802 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004803 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004804 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4805 //
4806 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4807 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004808 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004809 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4810 //
4811 // __ cmpl(temp, temp2);
4812 //
4813 // However, the second read barrier may trash `temp`, as it
4814 // is a temporary register, and as such would not be saved
4815 // along with live registers before calling the runtime (nor
4816 // restored afterwards). So in this case, we bail out and
4817 // delegate the work to the array set slow path.
4818 //
4819 // TODO: Extend the register allocator to support a new
4820 // "(locally) live temp" location so as to avoid always
4821 // going into the slow path when read barriers are enabled.
4822 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004824 // /* HeapReference<Class> */ temp = array->klass_
4825 __ movl(temp, Address(array, class_offset));
4826 codegen_->MaybeRecordImplicitNullCheck(instruction);
4827 __ MaybeUnpoisonHeapReference(temp);
4828
4829 // /* HeapReference<Class> */ temp = temp->component_type_
4830 __ movl(temp, Address(temp, component_offset));
4831 // If heap poisoning is enabled, no need to unpoison `temp`
4832 // nor the object reference in `register_value->klass`, as
4833 // we are comparing two poisoned references.
4834 __ cmpl(temp, Address(register_value, class_offset));
4835
4836 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4837 __ j(kEqual, &do_put);
4838 // If heap poisoning is enabled, the `temp` reference has
4839 // not been unpoisoned yet; unpoison it now.
4840 __ MaybeUnpoisonHeapReference(temp);
4841
4842 // /* HeapReference<Class> */ temp = temp->super_class_
4843 __ movl(temp, Address(temp, super_offset));
4844 // If heap poisoning is enabled, no need to unpoison
4845 // `temp`, as we are comparing against null below.
4846 __ testl(temp, temp);
4847 __ j(kNotEqual, slow_path->GetEntryLabel());
4848 __ Bind(&do_put);
4849 } else {
4850 __ j(kNotEqual, slow_path->GetEntryLabel());
4851 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004852 }
4853 }
4854
4855 if (kPoisonHeapReferences) {
4856 __ movl(temp, register_value);
4857 __ PoisonHeapReference(temp);
4858 __ movl(address, temp);
4859 } else {
4860 __ movl(address, register_value);
4861 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004862 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004863 codegen_->MaybeRecordImplicitNullCheck(instruction);
4864 }
4865
4866 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4867 codegen_->MarkGCCard(
4868 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4869 __ Bind(&done);
4870
4871 if (slow_path != nullptr) {
4872 __ Bind(slow_path->GetExitLabel());
4873 }
4874
4875 break;
4876 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004877
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004878 case Primitive::kPrimInt: {
4879 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4880 Address address = index.IsConstant()
4881 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4882 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4883 if (value.IsRegister()) {
4884 __ movl(address, value.AsRegister<CpuRegister>());
4885 } else {
4886 DCHECK(value.IsConstant()) << value;
4887 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4888 __ movl(address, Immediate(v));
4889 }
4890 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 break;
4892 }
4893
4894 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004895 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4896 Address address = index.IsConstant()
4897 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4898 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4899 if (value.IsRegister()) {
4900 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004904 Address address_high = index.IsConstant()
4905 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4906 offset + sizeof(int32_t))
4907 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4908 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004909 }
4910 break;
4911 }
4912
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004913 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004914 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4915 Address address = index.IsConstant()
4916 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4917 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004918 if (value.IsFpuRegister()) {
4919 __ movss(address, value.AsFpuRegister<XmmRegister>());
4920 } else {
4921 DCHECK(value.IsConstant());
4922 int32_t v =
4923 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4924 __ movl(address, Immediate(v));
4925 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004926 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004927 break;
4928 }
4929
4930 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004931 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4932 Address address = index.IsConstant()
4933 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4934 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004935 if (value.IsFpuRegister()) {
4936 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4937 codegen_->MaybeRecordImplicitNullCheck(instruction);
4938 } else {
4939 int64_t v =
4940 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4941 Address address_high = index.IsConstant()
4942 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4943 offset + sizeof(int32_t))
4944 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4945 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4946 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004947 break;
4948 }
4949
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004950 case Primitive::kPrimVoid:
4951 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004952 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004953 }
4954}
4955
4956void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004957 LocationSummary* locations =
4958 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004959 locations->SetInAt(0, Location::RequiresRegister());
4960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004961}
4962
4963void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4964 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004965 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004966 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4967 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004969 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970}
4971
4972void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004973 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4974 ? LocationSummary::kCallOnSlowPath
4975 : LocationSummary::kNoCall;
4976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004977 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004978 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004979 if (instruction->HasUses()) {
4980 locations->SetOut(Location::SameAsFirstInput());
4981 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004982}
4983
4984void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4985 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004986 Location index_loc = locations->InAt(0);
4987 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004988 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004989 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004990
Mark Mendell99dbd682015-04-22 16:18:52 -04004991 if (length_loc.IsConstant()) {
4992 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4993 if (index_loc.IsConstant()) {
4994 // BCE will remove the bounds check if we are guarenteed to pass.
4995 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4996 if (index < 0 || index >= length) {
4997 codegen_->AddSlowPath(slow_path);
4998 __ jmp(slow_path->GetEntryLabel());
4999 } else {
5000 // Some optimization after BCE may have generated this, and we should not
5001 // generate a bounds check if it is a valid range.
5002 }
5003 return;
5004 }
5005
5006 // We have to reverse the jump condition because the length is the constant.
5007 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5008 __ cmpl(index_reg, Immediate(length));
5009 codegen_->AddSlowPath(slow_path);
5010 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005011 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005012 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5013 if (index_loc.IsConstant()) {
5014 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5015 __ cmpl(length, Immediate(value));
5016 } else {
5017 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5018 }
5019 codegen_->AddSlowPath(slow_path);
5020 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005021 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005022}
5023
5024void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5025 CpuRegister card,
5026 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005027 CpuRegister value,
5028 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005029 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005030 if (value_can_be_null) {
5031 __ testl(value, value);
5032 __ j(kEqual, &is_null);
5033 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005034 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5035 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 __ movq(temp, object);
5037 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005038 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005039 if (value_can_be_null) {
5040 __ Bind(&is_null);
5041 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042}
5043
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005044void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005045 LOG(FATAL) << "Unimplemented";
5046}
5047
5048void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005049 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5050}
5051
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005052void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5054}
5055
5056void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005057 HBasicBlock* block = instruction->GetBlock();
5058 if (block->GetLoopInformation() != nullptr) {
5059 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5060 // The back edge will generate the suspend check.
5061 return;
5062 }
5063 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5064 // The goto will generate the suspend check.
5065 return;
5066 }
5067 GenerateSuspendCheck(instruction, nullptr);
5068}
5069
5070void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5071 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005072 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005073 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5074 if (slow_path == nullptr) {
5075 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5076 instruction->SetSlowPath(slow_path);
5077 codegen_->AddSlowPath(slow_path);
5078 if (successor != nullptr) {
5079 DCHECK(successor->IsLoopHeader());
5080 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5081 }
5082 } else {
5083 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5084 }
5085
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005086 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5087 /* no_rip */ true),
5088 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005089 if (successor == nullptr) {
5090 __ j(kNotEqual, slow_path->GetEntryLabel());
5091 __ Bind(slow_path->GetReturnLabel());
5092 } else {
5093 __ j(kEqual, codegen_->GetLabelOf(successor));
5094 __ jmp(slow_path->GetEntryLabel());
5095 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005096}
5097
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005098X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5099 return codegen_->GetAssembler();
5100}
5101
5102void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005103 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005104 Location source = move->GetSource();
5105 Location destination = move->GetDestination();
5106
5107 if (source.IsRegister()) {
5108 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005109 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005110 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005111 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005112 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005113 } else {
5114 DCHECK(destination.IsDoubleStackSlot());
5115 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005116 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005117 }
5118 } else if (source.IsStackSlot()) {
5119 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005121 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005122 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005123 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005124 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005125 } else {
5126 DCHECK(destination.IsStackSlot());
5127 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5128 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5129 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005130 } else if (source.IsDoubleStackSlot()) {
5131 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005132 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005133 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005134 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005135 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5136 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005137 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005138 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005139 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5140 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5141 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005142 } else if (source.IsConstant()) {
5143 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005144 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5145 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005146 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005147 if (value == 0) {
5148 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5149 } else {
5150 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5151 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005152 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005153 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005154 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005155 }
5156 } else if (constant->IsLongConstant()) {
5157 int64_t value = constant->AsLongConstant()->GetValue();
5158 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005159 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005160 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005161 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005162 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005163 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005164 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005165 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005166 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005167 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005168 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005169 } else {
5170 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005171 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005172 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5173 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005174 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005175 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005176 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005177 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005178 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005179 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005180 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005181 } else {
5182 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005183 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005185 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005186 } else if (source.IsFpuRegister()) {
5187 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005188 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005189 } else if (destination.IsStackSlot()) {
5190 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005191 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005192 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005193 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005194 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005195 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005196 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005197 }
5198}
5199
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005200void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005201 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005202 __ movl(Address(CpuRegister(RSP), mem), reg);
5203 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005204}
5205
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005206void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005207 ScratchRegisterScope ensure_scratch(
5208 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5209
5210 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5211 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5212 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5213 Address(CpuRegister(RSP), mem2 + stack_offset));
5214 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5215 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5216 CpuRegister(ensure_scratch.GetRegister()));
5217}
5218
Mark Mendell8a1c7282015-06-29 15:41:28 -04005219void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5220 __ movq(CpuRegister(TMP), reg1);
5221 __ movq(reg1, reg2);
5222 __ movq(reg2, CpuRegister(TMP));
5223}
5224
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005225void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5226 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5227 __ movq(Address(CpuRegister(RSP), mem), reg);
5228 __ movq(reg, CpuRegister(TMP));
5229}
5230
5231void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5232 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005233 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005234
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005235 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5236 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5237 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5238 Address(CpuRegister(RSP), mem2 + stack_offset));
5239 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5240 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5241 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005242}
5243
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005244void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5245 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5246 __ movss(Address(CpuRegister(RSP), mem), reg);
5247 __ movd(reg, CpuRegister(TMP));
5248}
5249
5250void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5251 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5252 __ movsd(Address(CpuRegister(RSP), mem), reg);
5253 __ movd(reg, CpuRegister(TMP));
5254}
5255
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005256void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005257 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005258 Location source = move->GetSource();
5259 Location destination = move->GetDestination();
5260
5261 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005262 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005263 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005264 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005265 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005266 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005267 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005268 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5269 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005270 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005271 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005272 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005273 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5274 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005275 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005276 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5277 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5278 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005283 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005284 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005285 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005286 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005287 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005289 }
5290}
5291
5292
5293void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5294 __ pushq(CpuRegister(reg));
5295}
5296
5297
5298void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5299 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005300}
5301
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005302void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005303 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005304 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5305 Immediate(mirror::Class::kStatusInitialized));
5306 __ j(kLess, slow_path->GetEntryLabel());
5307 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005308 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005309}
5310
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005311void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005312 InvokeRuntimeCallingConvention calling_convention;
5313 CodeGenerator::CreateLoadClassLocationSummary(
5314 cls,
5315 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005316 Location::RegisterLocation(RAX),
5317 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005318}
5319
5320void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005321 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005322 if (cls->NeedsAccessCheck()) {
5323 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5324 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5325 cls,
5326 cls->GetDexPc(),
5327 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005328 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005329 return;
5330 }
5331
Roland Levillain0d5a2812015-11-13 10:07:31 +00005332 Location out_loc = locations->Out();
5333 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005334 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335
Calin Juravle580b6092015-10-06 17:35:58 +01005336 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005337 DCHECK(!cls->CanCallRuntime());
5338 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005339 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5340 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005341 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005342 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 // /* GcRoot<mirror::Class>[] */ out =
5344 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5345 __ movq(out, Address(current_method,
5346 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005347 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005348 GenerateGcRootFieldLoad(
5349 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01005350
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005351 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5352 DCHECK(cls->CanCallRuntime());
5353 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5354 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5355 codegen_->AddSlowPath(slow_path);
5356 if (!cls->IsInDexCache()) {
5357 __ testl(out, out);
5358 __ j(kEqual, slow_path->GetEntryLabel());
5359 }
5360 if (cls->MustGenerateClinitCheck()) {
5361 GenerateClassInitializationCheck(slow_path, out);
5362 } else {
5363 __ Bind(slow_path->GetExitLabel());
5364 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005365 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005366 }
5367}
5368
5369void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5370 LocationSummary* locations =
5371 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5372 locations->SetInAt(0, Location::RequiresRegister());
5373 if (check->HasUses()) {
5374 locations->SetOut(Location::SameAsFirstInput());
5375 }
5376}
5377
5378void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005379 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005380 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005381 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005382 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005383 GenerateClassInitializationCheck(slow_path,
5384 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385}
5386
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005387HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5388 HLoadString::LoadKind desired_string_load_kind) {
5389 if (kEmitCompilerReadBarrier) {
5390 switch (desired_string_load_kind) {
5391 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5392 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5393 case HLoadString::LoadKind::kBootImageAddress:
5394 // TODO: Implement for read barrier.
5395 return HLoadString::LoadKind::kDexCacheViaMethod;
5396 default:
5397 break;
5398 }
5399 }
5400 switch (desired_string_load_kind) {
5401 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5402 DCHECK(!GetCompilerOptions().GetCompilePic());
5403 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5404 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5405 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5406 DCHECK(GetCompilerOptions().GetCompilePic());
5407 break;
5408 case HLoadString::LoadKind::kBootImageAddress:
5409 break;
5410 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005411 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005412 break;
5413 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005414 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005415 break;
5416 case HLoadString::LoadKind::kDexCacheViaMethod:
5417 break;
5418 }
5419 return desired_string_load_kind;
5420}
5421
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005422void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005423 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005424 ? LocationSummary::kCallOnSlowPath
5425 : LocationSummary::kNoCall;
5426 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005427 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5428 locations->SetInAt(0, Location::RequiresRegister());
5429 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005430 locations->SetOut(Location::RequiresRegister());
5431}
5432
5433void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005434 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005435 Location out_loc = locations->Out();
5436 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005437
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005438 switch (load->GetLoadKind()) {
5439 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5440 DCHECK(!kEmitCompilerReadBarrier);
5441 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5442 codegen_->RecordStringPatch(load);
5443 return; // No dex cache slow path.
5444 }
5445 case HLoadString::LoadKind::kBootImageAddress: {
5446 DCHECK(!kEmitCompilerReadBarrier);
5447 DCHECK_NE(load->GetAddress(), 0u);
5448 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5449 __ movl(out, Immediate(address)); // Zero-extended.
5450 codegen_->RecordSimplePatch();
5451 return; // No dex cache slow path.
5452 }
5453 case HLoadString::LoadKind::kDexCacheAddress: {
5454 DCHECK_NE(load->GetAddress(), 0u);
5455 if (IsUint<32>(load->GetAddress())) {
5456 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5457 GenerateGcRootFieldLoad(load, out_loc, address);
5458 } else {
5459 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5460 __ movq(out, Immediate(load->GetAddress()));
5461 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5462 }
5463 break;
5464 }
5465 case HLoadString::LoadKind::kDexCachePcRelative: {
5466 uint32_t offset = load->GetDexCacheElementOffset();
5467 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5468 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5469 /* no_rip */ false);
5470 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5471 break;
5472 }
5473 case HLoadString::LoadKind::kDexCacheViaMethod: {
5474 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5475
5476 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5477 GenerateGcRootFieldLoad(
5478 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5479 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5480 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5481 // /* GcRoot<mirror::String> */ out = out[string_index]
5482 GenerateGcRootFieldLoad(
5483 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5484 break;
5485 }
5486 default:
5487 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5488 UNREACHABLE();
5489 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005490
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005491 if (!load->IsInDexCache()) {
5492 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5493 codegen_->AddSlowPath(slow_path);
5494 __ testl(out, out);
5495 __ j(kEqual, slow_path->GetEntryLabel());
5496 __ Bind(slow_path->GetExitLabel());
5497 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005498}
5499
David Brazdilcb1c0552015-08-04 16:22:25 +01005500static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005501 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5502 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005503}
5504
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005505void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5506 LocationSummary* locations =
5507 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5508 locations->SetOut(Location::RequiresRegister());
5509}
5510
5511void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005512 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5513}
5514
5515void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5516 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5517}
5518
5519void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5520 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005521}
5522
5523void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5524 LocationSummary* locations =
5525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5526 InvokeRuntimeCallingConvention calling_convention;
5527 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5528}
5529
5530void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005531 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5532 instruction,
5533 instruction->GetDexPc(),
5534 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005535 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005536}
5537
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005538static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5539 return kEmitCompilerReadBarrier &&
5540 (kUseBakerReadBarrier ||
5541 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5542 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5543 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5544}
5545
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005546void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005547 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005548 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5549 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005550 case TypeCheckKind::kExactCheck:
5551 case TypeCheckKind::kAbstractClassCheck:
5552 case TypeCheckKind::kClassHierarchyCheck:
5553 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005554 call_kind =
5555 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005556 break;
5557 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005558 case TypeCheckKind::kUnresolvedCheck:
5559 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005560 call_kind = LocationSummary::kCallOnSlowPath;
5561 break;
5562 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005563
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005565 locations->SetInAt(0, Location::RequiresRegister());
5566 locations->SetInAt(1, Location::Any());
5567 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5568 locations->SetOut(Location::RequiresRegister());
5569 // When read barriers are enabled, we need a temporary register for
5570 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005571 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005572 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005574}
5575
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005576void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005577 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005578 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005579 Location obj_loc = locations->InAt(0);
5580 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005581 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005582 Location out_loc = locations->Out();
5583 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005584 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005585 locations->GetTemp(0) :
5586 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005587 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005588 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5589 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5590 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005591 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005592 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005593
5594 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005595 // Avoid null check if we know obj is not null.
5596 if (instruction->MustDoNullCheck()) {
5597 __ testl(obj, obj);
5598 __ j(kEqual, &zero);
5599 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005600
Roland Levillain0d5a2812015-11-13 10:07:31 +00005601 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005602 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005603
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005604 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005605 case TypeCheckKind::kExactCheck: {
5606 if (cls.IsRegister()) {
5607 __ cmpl(out, cls.AsRegister<CpuRegister>());
5608 } else {
5609 DCHECK(cls.IsStackSlot()) << cls;
5610 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5611 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005612 if (zero.IsLinked()) {
5613 // Classes must be equal for the instanceof to succeed.
5614 __ j(kNotEqual, &zero);
5615 __ movl(out, Immediate(1));
5616 __ jmp(&done);
5617 } else {
5618 __ setcc(kEqual, out);
5619 // setcc only sets the low byte.
5620 __ andl(out, Immediate(1));
5621 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622 break;
5623 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005624
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625 case TypeCheckKind::kAbstractClassCheck: {
5626 // If the class is abstract, we eagerly fetch the super class of the
5627 // object to avoid doing a comparison we know will fail.
5628 NearLabel loop, success;
5629 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005630 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005631 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 __ testl(out, out);
5633 // If `out` is null, we use it for the result, and jump to `done`.
5634 __ j(kEqual, &done);
5635 if (cls.IsRegister()) {
5636 __ cmpl(out, cls.AsRegister<CpuRegister>());
5637 } else {
5638 DCHECK(cls.IsStackSlot()) << cls;
5639 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5640 }
5641 __ j(kNotEqual, &loop);
5642 __ movl(out, Immediate(1));
5643 if (zero.IsLinked()) {
5644 __ jmp(&done);
5645 }
5646 break;
5647 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005648
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005649 case TypeCheckKind::kClassHierarchyCheck: {
5650 // Walk over the class hierarchy to find a match.
5651 NearLabel loop, success;
5652 __ Bind(&loop);
5653 if (cls.IsRegister()) {
5654 __ cmpl(out, cls.AsRegister<CpuRegister>());
5655 } else {
5656 DCHECK(cls.IsStackSlot()) << cls;
5657 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5658 }
5659 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005660 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005661 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005662 __ testl(out, out);
5663 __ j(kNotEqual, &loop);
5664 // If `out` is null, we use it for the result, and jump to `done`.
5665 __ jmp(&done);
5666 __ Bind(&success);
5667 __ movl(out, Immediate(1));
5668 if (zero.IsLinked()) {
5669 __ jmp(&done);
5670 }
5671 break;
5672 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005673
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005674 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005675 // Do an exact check.
5676 NearLabel exact_check;
5677 if (cls.IsRegister()) {
5678 __ cmpl(out, cls.AsRegister<CpuRegister>());
5679 } else {
5680 DCHECK(cls.IsStackSlot()) << cls;
5681 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5682 }
5683 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005684 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005685 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005686 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005687 __ testl(out, out);
5688 // If `out` is null, we use it for the result, and jump to `done`.
5689 __ j(kEqual, &done);
5690 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5691 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005692 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005693 __ movl(out, Immediate(1));
5694 __ jmp(&done);
5695 break;
5696 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005697
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 case TypeCheckKind::kArrayCheck: {
5699 if (cls.IsRegister()) {
5700 __ cmpl(out, cls.AsRegister<CpuRegister>());
5701 } else {
5702 DCHECK(cls.IsStackSlot()) << cls;
5703 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5704 }
5705 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5707 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 codegen_->AddSlowPath(slow_path);
5709 __ j(kNotEqual, slow_path->GetEntryLabel());
5710 __ movl(out, Immediate(1));
5711 if (zero.IsLinked()) {
5712 __ jmp(&done);
5713 }
5714 break;
5715 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005716
Calin Juravle98893e12015-10-02 21:05:03 +01005717 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005718 case TypeCheckKind::kInterfaceCheck: {
5719 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005720 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005721 // cases.
5722 //
5723 // We cannot directly call the InstanceofNonTrivial runtime
5724 // entry point without resorting to a type checking slow path
5725 // here (i.e. by calling InvokeRuntime directly), as it would
5726 // require to assign fixed registers for the inputs of this
5727 // HInstanceOf instruction (following the runtime calling
5728 // convention), which might be cluttered by the potential first
5729 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005730 //
5731 // TODO: Introduce a new runtime entry point taking the object
5732 // to test (instead of its class) as argument, and let it deal
5733 // with the read barrier issues. This will let us refactor this
5734 // case of the `switch` code as it was previously (with a direct
5735 // call to the runtime not using a type checking slow path).
5736 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 DCHECK(locations->OnlyCallsOnSlowPath());
5738 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5739 /* is_fatal */ false);
5740 codegen_->AddSlowPath(slow_path);
5741 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 if (zero.IsLinked()) {
5743 __ jmp(&done);
5744 }
5745 break;
5746 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005747 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005749 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005750 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 __ xorl(out, out);
5752 }
5753
5754 if (done.IsLinked()) {
5755 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005756 }
5757
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005758 if (slow_path != nullptr) {
5759 __ Bind(slow_path->GetExitLabel());
5760 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005761}
5762
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005763void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5765 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5767 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 case TypeCheckKind::kExactCheck:
5769 case TypeCheckKind::kAbstractClassCheck:
5770 case TypeCheckKind::kClassHierarchyCheck:
5771 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005772 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5773 LocationSummary::kCallOnSlowPath :
5774 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 break;
5776 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777 case TypeCheckKind::kUnresolvedCheck:
5778 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779 call_kind = LocationSummary::kCallOnSlowPath;
5780 break;
5781 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5783 locations->SetInAt(0, Location::RequiresRegister());
5784 locations->SetInAt(1, Location::Any());
5785 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5786 locations->AddTemp(Location::RequiresRegister());
5787 // When read barriers are enabled, we need an additional temporary
5788 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005789 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005791 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005792}
5793
5794void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005795 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005796 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005797 Location obj_loc = locations->InAt(0);
5798 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005799 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005800 Location temp_loc = locations->GetTemp(0);
5801 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005802 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005803 locations->GetTemp(1) :
5804 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5806 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5807 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5808 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809
Roland Levillain0d5a2812015-11-13 10:07:31 +00005810 bool is_type_check_slow_path_fatal =
5811 (type_check_kind == TypeCheckKind::kExactCheck ||
5812 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5813 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5814 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5815 !instruction->CanThrowIntoCatchBlock();
5816 SlowPathCode* type_check_slow_path =
5817 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5818 is_type_check_slow_path_fatal);
5819 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005820
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822 case TypeCheckKind::kExactCheck:
5823 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005824 NearLabel done;
5825 // Avoid null check if we know obj is not null.
5826 if (instruction->MustDoNullCheck()) {
5827 __ testl(obj, obj);
5828 __ j(kEqual, &done);
5829 }
5830
5831 // /* HeapReference<Class> */ temp = obj->klass_
5832 GenerateReferenceLoadTwoRegisters(
5833 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5834
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005835 if (cls.IsRegister()) {
5836 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5837 } else {
5838 DCHECK(cls.IsStackSlot()) << cls;
5839 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5840 }
5841 // Jump to slow path for throwing the exception or doing a
5842 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005844 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 break;
5846 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005848 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005849 NearLabel done;
5850 // Avoid null check if we know obj is not null.
5851 if (instruction->MustDoNullCheck()) {
5852 __ testl(obj, obj);
5853 __ j(kEqual, &done);
5854 }
5855
5856 // /* HeapReference<Class> */ temp = obj->klass_
5857 GenerateReferenceLoadTwoRegisters(
5858 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5859
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 // If the class is abstract, we eagerly fetch the super class of the
5861 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005865 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866
5867 // If the class reference currently in `temp` is not null, jump
5868 // to the `compare_classes` label to compare it with the checked
5869 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005871 __ j(kNotEqual, &compare_classes);
5872 // Otherwise, jump to the slow path to throw the exception.
5873 //
5874 // But before, move back the object's class into `temp` before
5875 // going into the slow path, as it has been overwritten in the
5876 // meantime.
5877 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005878 GenerateReferenceLoadTwoRegisters(
5879 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 __ jmp(type_check_slow_path->GetEntryLabel());
5881
5882 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 if (cls.IsRegister()) {
5884 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5885 } else {
5886 DCHECK(cls.IsStackSlot()) << cls;
5887 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5888 }
5889 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005890 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 break;
5892 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005894 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005895 NearLabel done;
5896 // Avoid null check if we know obj is not null.
5897 if (instruction->MustDoNullCheck()) {
5898 __ testl(obj, obj);
5899 __ j(kEqual, &done);
5900 }
5901
5902 // /* HeapReference<Class> */ temp = obj->klass_
5903 GenerateReferenceLoadTwoRegisters(
5904 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5905
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005907 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 __ Bind(&loop);
5909 if (cls.IsRegister()) {
5910 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5911 } else {
5912 DCHECK(cls.IsStackSlot()) << cls;
5913 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5914 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005915 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005916
Roland Levillain0d5a2812015-11-13 10:07:31 +00005917 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005918 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919
5920 // If the class reference currently in `temp` is not null, jump
5921 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 __ testl(temp, temp);
5923 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005924 // Otherwise, jump to the slow path to throw the exception.
5925 //
5926 // But before, move back the object's class into `temp` before
5927 // going into the slow path, as it has been overwritten in the
5928 // meantime.
5929 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005930 GenerateReferenceLoadTwoRegisters(
5931 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005933 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 break;
5935 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005938 // We cannot use a NearLabel here, as its range might be too
5939 // short in some cases when read barriers are enabled. This has
5940 // been observed for instance when the code emitted for this
5941 // case uses high x86-64 registers (R8-R15).
5942 Label done;
5943 // Avoid null check if we know obj is not null.
5944 if (instruction->MustDoNullCheck()) {
5945 __ testl(obj, obj);
5946 __ j(kEqual, &done);
5947 }
5948
5949 // /* HeapReference<Class> */ temp = obj->klass_
5950 GenerateReferenceLoadTwoRegisters(
5951 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5952
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005953 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005955 if (cls.IsRegister()) {
5956 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5957 } else {
5958 DCHECK(cls.IsStackSlot()) << cls;
5959 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5960 }
5961 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005962
5963 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005965 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005966
5967 // If the component type is not null (i.e. the object is indeed
5968 // an array), jump to label `check_non_primitive_component_type`
5969 // to further check that this component type is not a primitive
5970 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005971 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972 __ j(kNotEqual, &check_non_primitive_component_type);
5973 // Otherwise, jump to the slow path to throw the exception.
5974 //
5975 // But before, move back the object's class into `temp` before
5976 // going into the slow path, as it has been overwritten in the
5977 // meantime.
5978 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005979 GenerateReferenceLoadTwoRegisters(
5980 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981 __ jmp(type_check_slow_path->GetEntryLabel());
5982
5983 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005984 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005985 __ j(kEqual, &done);
5986 // Same comment as above regarding `temp` and the slow path.
5987 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005988 GenerateReferenceLoadTwoRegisters(
5989 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005991 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 break;
5993 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994
Calin Juravle98893e12015-10-02 21:05:03 +01005995 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00005997 NearLabel done;
5998 // Avoid null check if we know obj is not null.
5999 if (instruction->MustDoNullCheck()) {
6000 __ testl(obj, obj);
6001 __ j(kEqual, &done);
6002 }
6003
6004 // /* HeapReference<Class> */ temp = obj->klass_
6005 GenerateReferenceLoadTwoRegisters(
6006 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6007
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006008 // We always go into the type check slow path for the unresolved
6009 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010 //
6011 // We cannot directly call the CheckCast runtime entry point
6012 // without resorting to a type checking slow path here (i.e. by
6013 // calling InvokeRuntime directly), as it would require to
6014 // assign fixed registers for the inputs of this HInstanceOf
6015 // instruction (following the runtime calling convention), which
6016 // might be cluttered by the potential first read barrier
6017 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006018 //
6019 // TODO: Introduce a new runtime entry point taking the object
6020 // to test (instead of its class) as argument, and let it deal
6021 // with the read barrier issues. This will let us refactor this
6022 // case of the `switch` code as it was previously (with a direct
6023 // call to the runtime not using a type checking slow path).
6024 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006026 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006027 break;
6028 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029
Roland Levillain0d5a2812015-11-13 10:07:31 +00006030 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006031}
6032
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006033void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6034 LocationSummary* locations =
6035 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6036 InvokeRuntimeCallingConvention calling_convention;
6037 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6038}
6039
6040void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006041 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6042 : QUICK_ENTRY_POINT(pUnlockObject),
6043 instruction,
6044 instruction->GetDexPc(),
6045 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006046 if (instruction->IsEnter()) {
6047 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6048 } else {
6049 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6050 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006051}
6052
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006053void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6054void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6055void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6056
6057void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6058 LocationSummary* locations =
6059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6060 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6061 || instruction->GetResultType() == Primitive::kPrimLong);
6062 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006063 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006064 locations->SetOut(Location::SameAsFirstInput());
6065}
6066
6067void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6068 HandleBitwiseOperation(instruction);
6069}
6070
6071void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6072 HandleBitwiseOperation(instruction);
6073}
6074
6075void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6076 HandleBitwiseOperation(instruction);
6077}
6078
6079void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6080 LocationSummary* locations = instruction->GetLocations();
6081 Location first = locations->InAt(0);
6082 Location second = locations->InAt(1);
6083 DCHECK(first.Equals(locations->Out()));
6084
6085 if (instruction->GetResultType() == Primitive::kPrimInt) {
6086 if (second.IsRegister()) {
6087 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006088 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006089 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006090 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006091 } else {
6092 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006093 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006094 }
6095 } else if (second.IsConstant()) {
6096 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6097 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006098 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006099 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006100 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006101 } else {
6102 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006103 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006104 }
6105 } else {
6106 Address address(CpuRegister(RSP), second.GetStackIndex());
6107 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006108 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006109 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006110 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006111 } else {
6112 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006113 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006114 }
6115 }
6116 } else {
6117 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006118 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6119 bool second_is_constant = false;
6120 int64_t value = 0;
6121 if (second.IsConstant()) {
6122 second_is_constant = true;
6123 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006124 }
Mark Mendell40741f32015-04-20 22:10:34 -04006125 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006126
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006127 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006128 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006129 if (is_int32_value) {
6130 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6131 } else {
6132 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6133 }
6134 } else if (second.IsDoubleStackSlot()) {
6135 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006136 } else {
6137 __ andq(first_reg, second.AsRegister<CpuRegister>());
6138 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006139 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006140 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006141 if (is_int32_value) {
6142 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6143 } else {
6144 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6145 }
6146 } else if (second.IsDoubleStackSlot()) {
6147 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006148 } else {
6149 __ orq(first_reg, second.AsRegister<CpuRegister>());
6150 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006151 } else {
6152 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006153 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006154 if (is_int32_value) {
6155 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6156 } else {
6157 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6158 }
6159 } else if (second.IsDoubleStackSlot()) {
6160 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006161 } else {
6162 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6163 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006164 }
6165 }
6166}
6167
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006168void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6169 Location out,
6170 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006171 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006172 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6173 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006174 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006175 if (kUseBakerReadBarrier) {
6176 // Load with fast path based Baker's read barrier.
6177 // /* HeapReference<Object> */ out = *(out + offset)
6178 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006179 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006180 } else {
6181 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006182 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006183 // in the following move operation, as we will need it for the
6184 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006185 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006186 // /* HeapReference<Object> */ out = *(out + offset)
6187 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006188 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006189 }
6190 } else {
6191 // Plain load with no read barrier.
6192 // /* HeapReference<Object> */ out = *(out + offset)
6193 __ movl(out_reg, Address(out_reg, offset));
6194 __ MaybeUnpoisonHeapReference(out_reg);
6195 }
6196}
6197
6198void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6199 Location out,
6200 Location obj,
6201 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006202 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006203 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6204 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6205 if (kEmitCompilerReadBarrier) {
6206 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006207 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006208 // Load with fast path based Baker's read barrier.
6209 // /* HeapReference<Object> */ out = *(obj + offset)
6210 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006211 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006212 } else {
6213 // Load with slow path based read barrier.
6214 // /* HeapReference<Object> */ out = *(obj + offset)
6215 __ movl(out_reg, Address(obj_reg, offset));
6216 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6217 }
6218 } else {
6219 // Plain load with no read barrier.
6220 // /* HeapReference<Object> */ out = *(obj + offset)
6221 __ movl(out_reg, Address(obj_reg, offset));
6222 __ MaybeUnpoisonHeapReference(out_reg);
6223 }
6224}
6225
6226void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6227 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006228 const Address& address,
6229 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006230 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6231 if (kEmitCompilerReadBarrier) {
6232 if (kUseBakerReadBarrier) {
6233 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6234 // Baker's read barrier are used:
6235 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006236 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006237 // if (Thread::Current()->GetIsGcMarking()) {
6238 // root = ReadBarrier::Mark(root)
6239 // }
6240
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 // /* GcRoot<mirror::Object> */ root = *address
6242 __ movl(root_reg, address);
6243 if (fixup_label != nullptr) {
6244 __ Bind(fixup_label);
6245 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006246 static_assert(
6247 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6248 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6249 "have different sizes.");
6250 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6251 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6252 "have different sizes.");
6253
6254 // Slow path used to mark the GC root `root`.
6255 SlowPathCode* slow_path =
6256 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6257 codegen_->AddSlowPath(slow_path);
6258
6259 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6260 /* no_rip */ true),
6261 Immediate(0));
6262 __ j(kNotEqual, slow_path->GetEntryLabel());
6263 __ Bind(slow_path->GetExitLabel());
6264 } else {
6265 // GC root loaded through a slow path for read barriers other
6266 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006267 // /* GcRoot<mirror::Object>* */ root = address
6268 __ leaq(root_reg, address);
6269 if (fixup_label != nullptr) {
6270 __ Bind(fixup_label);
6271 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006272 // /* mirror::Object* */ root = root->Read()
6273 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6274 }
6275 } else {
6276 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006277 // /* GcRoot<mirror::Object> */ root = *address
6278 __ movl(root_reg, address);
6279 if (fixup_label != nullptr) {
6280 __ Bind(fixup_label);
6281 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006282 // Note that GC roots are not affected by heap poisoning, thus we
6283 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006284 }
6285}
6286
6287void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6288 Location ref,
6289 CpuRegister obj,
6290 uint32_t offset,
6291 Location temp,
6292 bool needs_null_check) {
6293 DCHECK(kEmitCompilerReadBarrier);
6294 DCHECK(kUseBakerReadBarrier);
6295
6296 // /* HeapReference<Object> */ ref = *(obj + offset)
6297 Address src(obj, offset);
6298 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6299}
6300
6301void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6302 Location ref,
6303 CpuRegister obj,
6304 uint32_t data_offset,
6305 Location index,
6306 Location temp,
6307 bool needs_null_check) {
6308 DCHECK(kEmitCompilerReadBarrier);
6309 DCHECK(kUseBakerReadBarrier);
6310
6311 // /* HeapReference<Object> */ ref =
6312 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6313 Address src = index.IsConstant() ?
6314 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6315 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6316 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6317}
6318
6319void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6320 Location ref,
6321 CpuRegister obj,
6322 const Address& src,
6323 Location temp,
6324 bool needs_null_check) {
6325 DCHECK(kEmitCompilerReadBarrier);
6326 DCHECK(kUseBakerReadBarrier);
6327
6328 // In slow path based read barriers, the read barrier call is
6329 // inserted after the original load. However, in fast path based
6330 // Baker's read barriers, we need to perform the load of
6331 // mirror::Object::monitor_ *before* the original reference load.
6332 // This load-load ordering is required by the read barrier.
6333 // The fast path/slow path (for Baker's algorithm) should look like:
6334 //
6335 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6336 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6337 // HeapReference<Object> ref = *src; // Original reference load.
6338 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6339 // if (is_gray) {
6340 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6341 // }
6342 //
6343 // Note: the original implementation in ReadBarrier::Barrier is
6344 // slightly more complex as:
6345 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006346 // the high-bits of rb_state, which are expected to be all zeroes
6347 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6348 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006349 // - it performs additional checks that we do not do here for
6350 // performance reasons.
6351
6352 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6353 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6354 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6355
6356 // /* int32_t */ monitor = obj->monitor_
6357 __ movl(temp_reg, Address(obj, monitor_offset));
6358 if (needs_null_check) {
6359 MaybeRecordImplicitNullCheck(instruction);
6360 }
6361 // /* LockWord */ lock_word = LockWord(monitor)
6362 static_assert(sizeof(LockWord) == sizeof(int32_t),
6363 "art::LockWord and int32_t have different sizes.");
6364 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6365 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6366 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6367 static_assert(
6368 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6369 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6370
6371 // Load fence to prevent load-load reordering.
6372 // Note that this is a no-op, thanks to the x86-64 memory model.
6373 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6374
6375 // The actual reference load.
6376 // /* HeapReference<Object> */ ref = *src
6377 __ movl(ref_reg, src);
6378
6379 // Object* ref = ref_addr->AsMirrorPtr()
6380 __ MaybeUnpoisonHeapReference(ref_reg);
6381
6382 // Slow path used to mark the object `ref` when it is gray.
6383 SlowPathCode* slow_path =
6384 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6385 AddSlowPath(slow_path);
6386
6387 // if (rb_state == ReadBarrier::gray_ptr_)
6388 // ref = ReadBarrier::Mark(ref);
6389 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6390 __ j(kEqual, slow_path->GetEntryLabel());
6391 __ Bind(slow_path->GetExitLabel());
6392}
6393
6394void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6395 Location out,
6396 Location ref,
6397 Location obj,
6398 uint32_t offset,
6399 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 DCHECK(kEmitCompilerReadBarrier);
6401
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006402 // Insert a slow path based read barrier *after* the reference load.
6403 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 // If heap poisoning is enabled, the unpoisoning of the loaded
6405 // reference will be carried out by the runtime within the slow
6406 // path.
6407 //
6408 // Note that `ref` currently does not get unpoisoned (when heap
6409 // poisoning is enabled), which is alright as the `ref` argument is
6410 // not used by the artReadBarrierSlow entry point.
6411 //
6412 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6413 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6414 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6415 AddSlowPath(slow_path);
6416
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 __ jmp(slow_path->GetEntryLabel());
6418 __ Bind(slow_path->GetExitLabel());
6419}
6420
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006421void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6422 Location out,
6423 Location ref,
6424 Location obj,
6425 uint32_t offset,
6426 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006428 // Baker's read barriers shall be handled by the fast path
6429 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6430 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 // If heap poisoning is enabled, unpoisoning will be taken care of
6432 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006433 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434 } else if (kPoisonHeapReferences) {
6435 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6436 }
6437}
6438
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006439void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6440 Location out,
6441 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442 DCHECK(kEmitCompilerReadBarrier);
6443
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006444 // Insert a slow path based read barrier *after* the GC root load.
6445 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 // Note that GC roots are not affected by heap poisoning, so we do
6447 // not need to do anything special for this here.
6448 SlowPathCode* slow_path =
6449 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6450 AddSlowPath(slow_path);
6451
Roland Levillain0d5a2812015-11-13 10:07:31 +00006452 __ jmp(slow_path->GetEntryLabel());
6453 __ Bind(slow_path->GetExitLabel());
6454}
6455
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006456void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006457 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006458 LOG(FATAL) << "Unreachable";
6459}
6460
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006461void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006462 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006463 LOG(FATAL) << "Unreachable";
6464}
6465
Mark Mendellfe57faa2015-09-18 09:26:15 -04006466// Simple implementation of packed switch - generate cascaded compare/jumps.
6467void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6468 LocationSummary* locations =
6469 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6470 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006471 locations->AddTemp(Location::RequiresRegister());
6472 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006473}
6474
6475void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6476 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006477 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006478 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006479 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6480 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6481 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006482 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6483
6484 // Should we generate smaller inline compare/jumps?
6485 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6486 // Figure out the correct compare values and jump conditions.
6487 // Handle the first compare/branch as a special case because it might
6488 // jump to the default case.
6489 DCHECK_GT(num_entries, 2u);
6490 Condition first_condition;
6491 uint32_t index;
6492 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6493 if (lower_bound != 0) {
6494 first_condition = kLess;
6495 __ cmpl(value_reg_in, Immediate(lower_bound));
6496 __ j(first_condition, codegen_->GetLabelOf(default_block));
6497 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6498
6499 index = 1;
6500 } else {
6501 // Handle all the compare/jumps below.
6502 first_condition = kBelow;
6503 index = 0;
6504 }
6505
6506 // Handle the rest of the compare/jumps.
6507 for (; index + 1 < num_entries; index += 2) {
6508 int32_t compare_to_value = lower_bound + index + 1;
6509 __ cmpl(value_reg_in, Immediate(compare_to_value));
6510 // Jump to successors[index] if value < case_value[index].
6511 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6512 // Jump to successors[index + 1] if value == case_value[index + 1].
6513 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6514 }
6515
6516 if (index != num_entries) {
6517 // There are an odd number of entries. Handle the last one.
6518 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006519 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006520 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6521 }
6522
6523 // And the default for any other value.
6524 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6525 __ jmp(codegen_->GetLabelOf(default_block));
6526 }
6527 return;
6528 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006529
6530 // Remove the bias, if needed.
6531 Register value_reg_out = value_reg_in.AsRegister();
6532 if (lower_bound != 0) {
6533 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6534 value_reg_out = temp_reg.AsRegister();
6535 }
6536 CpuRegister value_reg(value_reg_out);
6537
6538 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006539 __ cmpl(value_reg, Immediate(num_entries - 1));
6540 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006541
Mark Mendell9c86b482015-09-18 13:36:07 -04006542 // We are in the range of the table.
6543 // Load the address of the jump table in the constant area.
6544 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006545
Mark Mendell9c86b482015-09-18 13:36:07 -04006546 // Load the (signed) offset from the jump table.
6547 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6548
6549 // Add the offset to the address of the table base.
6550 __ addq(temp_reg, base_reg);
6551
6552 // And jump.
6553 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006554}
6555
Aart Bikc5d47542016-01-27 17:00:35 -08006556void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6557 if (value == 0) {
6558 __ xorl(dest, dest);
6559 } else {
6560 __ movl(dest, Immediate(value));
6561 }
6562}
6563
Mark Mendell92e83bf2015-05-07 11:25:03 -04006564void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6565 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006566 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006567 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006568 } else if (IsUint<32>(value)) {
6569 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006570 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6571 } else {
6572 __ movq(dest, Immediate(value));
6573 }
6574}
6575
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006576void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6577 if (value == 0) {
6578 __ xorps(dest, dest);
6579 } else {
6580 __ movss(dest, LiteralInt32Address(value));
6581 }
6582}
6583
6584void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6585 if (value == 0) {
6586 __ xorpd(dest, dest);
6587 } else {
6588 __ movsd(dest, LiteralInt64Address(value));
6589 }
6590}
6591
6592void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6593 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6594}
6595
6596void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6597 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6598}
6599
Aart Bika19616e2016-02-01 18:57:58 -08006600void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6601 if (value == 0) {
6602 __ testl(dest, dest);
6603 } else {
6604 __ cmpl(dest, Immediate(value));
6605 }
6606}
6607
6608void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6609 if (IsInt<32>(value)) {
6610 if (value == 0) {
6611 __ testq(dest, dest);
6612 } else {
6613 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6614 }
6615 } else {
6616 // Value won't fit in an int.
6617 __ cmpq(dest, LiteralInt64Address(value));
6618 }
6619}
6620
Mark Mendellcfa410b2015-05-25 16:02:44 -04006621void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6622 DCHECK(dest.IsDoubleStackSlot());
6623 if (IsInt<32>(value)) {
6624 // Can move directly as an int32 constant.
6625 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6626 Immediate(static_cast<int32_t>(value)));
6627 } else {
6628 Load64BitValue(CpuRegister(TMP), value);
6629 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6630 }
6631}
6632
Mark Mendell9c86b482015-09-18 13:36:07 -04006633/**
6634 * Class to handle late fixup of offsets into constant area.
6635 */
6636class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6637 public:
6638 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6639 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6640
6641 protected:
6642 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6643
6644 CodeGeneratorX86_64* codegen_;
6645
6646 private:
6647 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6648 // Patch the correct offset for the instruction. We use the address of the
6649 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6650 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6651 int32_t relative_position = constant_offset - pos;
6652
6653 // Patch in the right value.
6654 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6655 }
6656
6657 // Location in constant area that the fixup refers to.
6658 size_t offset_into_constant_area_;
6659};
6660
6661/**
6662 t * Class to handle late fixup of offsets to a jump table that will be created in the
6663 * constant area.
6664 */
6665class JumpTableRIPFixup : public RIPFixup {
6666 public:
6667 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6668 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6669
6670 void CreateJumpTable() {
6671 X86_64Assembler* assembler = codegen_->GetAssembler();
6672
6673 // Ensure that the reference to the jump table has the correct offset.
6674 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6675 SetOffset(offset_in_constant_table);
6676
6677 // Compute the offset from the start of the function to this jump table.
6678 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6679
6680 // Populate the jump table with the correct values for the jump table.
6681 int32_t num_entries = switch_instr_->GetNumEntries();
6682 HBasicBlock* block = switch_instr_->GetBlock();
6683 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6684 // The value that we want is the target offset - the position of the table.
6685 for (int32_t i = 0; i < num_entries; i++) {
6686 HBasicBlock* b = successors[i];
6687 Label* l = codegen_->GetLabelOf(b);
6688 DCHECK(l->IsBound());
6689 int32_t offset_to_block = l->Position() - current_table_offset;
6690 assembler->AppendInt32(offset_to_block);
6691 }
6692 }
6693
6694 private:
6695 const HPackedSwitch* switch_instr_;
6696};
6697
Mark Mendellf55c3e02015-03-26 21:07:46 -04006698void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6699 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006700 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006701 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6702 // 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 -04006703 assembler->Align(4, 0);
6704 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006705
6706 // Populate any jump tables.
6707 for (auto jump_table : fixups_to_jump_tables_) {
6708 jump_table->CreateJumpTable();
6709 }
6710
6711 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006712 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006713 }
6714
6715 // And finish up.
6716 CodeGenerator::Finalize(allocator);
6717}
6718
Mark Mendellf55c3e02015-03-26 21:07:46 -04006719Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6720 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6721 return Address::RIP(fixup);
6722}
6723
6724Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6725 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6726 return Address::RIP(fixup);
6727}
6728
6729Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6730 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6731 return Address::RIP(fixup);
6732}
6733
6734Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6735 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6736 return Address::RIP(fixup);
6737}
6738
Andreas Gampe85b62f22015-09-09 13:15:38 -07006739// TODO: trg as memory.
6740void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6741 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006742 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006743 return;
6744 }
6745
6746 DCHECK_NE(type, Primitive::kPrimVoid);
6747
6748 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6749 if (trg.Equals(return_loc)) {
6750 return;
6751 }
6752
6753 // Let the parallel move resolver take care of all of this.
6754 HParallelMove parallel_move(GetGraph()->GetArena());
6755 parallel_move.AddMove(return_loc, trg, type, nullptr);
6756 GetMoveResolver()->EmitNativeCode(&parallel_move);
6757}
6758
Mark Mendell9c86b482015-09-18 13:36:07 -04006759Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6760 // Create a fixup to be used to create and address the jump table.
6761 JumpTableRIPFixup* table_fixup =
6762 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6763
6764 // We have to populate the jump tables.
6765 fixups_to_jump_tables_.push_back(table_fixup);
6766 return Address::RIP(table_fixup);
6767}
6768
Mark Mendellea5af682015-10-22 17:35:49 -04006769void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6770 const Address& addr_high,
6771 int64_t v,
6772 HInstruction* instruction) {
6773 if (IsInt<32>(v)) {
6774 int32_t v_32 = v;
6775 __ movq(addr_low, Immediate(v_32));
6776 MaybeRecordImplicitNullCheck(instruction);
6777 } else {
6778 // Didn't fit in a register. Do it in pieces.
6779 int32_t low_v = Low32Bits(v);
6780 int32_t high_v = High32Bits(v);
6781 __ movl(addr_low, Immediate(low_v));
6782 MaybeRecordImplicitNullCheck(instruction);
6783 __ movl(addr_high, Immediate(high_v));
6784 }
6785}
6786
Roland Levillain4d027112015-07-01 15:41:14 +01006787#undef __
6788
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006789} // namespace x86_64
6790} // namespace art