blob: 18ab5958e36aa48def6e7ec84f809c0a3df2aceb [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"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080023#include "intrinsics.h"
24#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010027#include "mirror/object_reference.h"
28#include "thread.h"
29#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/managed_register_x86_64.h"
33
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034namespace art {
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace x86_64 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038// Some x86_64 instructions require a register to be available as temp.
39static constexpr Register TMP = R11;
40
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010042static constexpr Register kMethodRegisterArgument = RDI;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000044static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Roland Levillain62a46b22015-06-01 18:24:13 +010049#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Alexandre Rames8158f282015-08-07 10:26:17 +010050#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x), true)
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010052class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055
Alexandre Rames2ed20af2015-03-06 13:55:35 +000056 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010057 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010059 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
60 instruction_,
61 instruction_->GetDexPc(),
62 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 }
64
Alexandre Rames8158f282015-08-07 10:26:17 +010065 bool IsFatal() const OVERRIDE { return true; }
66
Alexandre Rames9931f312015-06-19 14:47:01 +010067 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
68
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010070 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
72};
73
Calin Juravled0d48522014-11-04 16:40:20 +000074class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
75 public:
76 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
77
Alexandre Rames2ed20af2015-03-06 13:55:35 +000078 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010079 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000080 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010081 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
82 instruction_,
83 instruction_->GetDexPc(),
84 this);
Calin Juravled0d48522014-11-04 16:40:20 +000085 }
86
Alexandre Rames8158f282015-08-07 10:26:17 +010087 bool IsFatal() const OVERRIDE { return true; }
88
Alexandre Rames9931f312015-06-19 14:47:01 +010089 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
90
Calin Juravled0d48522014-11-04 16:40:20 +000091 private:
92 HDivZeroCheck* const instruction_;
93 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
94};
95
Calin Juravlebacfec32014-11-14 15:54:36 +000096class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000097 public:
Roland Levillain3887c462015-08-12 18:15:42 +010098 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +000099 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000100
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000101 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000102 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000103 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000104 if (is_div_) {
105 __ negl(cpu_reg_);
106 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400107 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 }
109
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 } else {
111 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negq(cpu_reg_);
114 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400115 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000116 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000125 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 const bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100132 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000135 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000138 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
140 instruction_,
141 instruction_->GetDexPc(),
142 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100156 HBasicBlock* GetSuccessor() const {
157 return successor_;
158 }
159
Alexandre Rames9931f312015-06-19 14:47:01 +0100160 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
161
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162 private:
163 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100164 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 Label return_label_;
166
167 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
168};
169
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100170class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100172 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
173 Location index_location,
174 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100175 : instruction_(instruction),
176 index_location_(index_location),
177 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100178
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100180 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100181 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000182 // We're moving two locations to locations that could overlap, so we need a parallel
183 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000185 codegen->EmitParallelMoves(
186 index_location_,
187 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100188 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000189 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100190 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
191 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100192 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
193 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 }
195
Alexandre Rames8158f282015-08-07 10:26:17 +0100196 bool IsFatal() const OVERRIDE { return true; }
197
Alexandre Rames9931f312015-06-19 14:47:01 +0100198 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
199
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100201 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100202 const Location index_location_;
203 const Location length_location_;
204
205 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
206};
207
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 LoadClassSlowPathX86_64(HLoadClass* cls,
211 HInstruction* at,
212 uint32_t dex_pc,
213 bool do_clinit)
214 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
215 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
216 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100217
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000218 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
221 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000223 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100227 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
228 : QUICK_ENTRY_POINT(pInitializeType),
229 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000231 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000233 if (out.IsValid()) {
234 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
235 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 }
237
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000238 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 __ jmp(GetExitLabel());
240 }
241
Alexandre Rames9931f312015-06-19 14:47:01 +0100242 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
243
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 // The class this slow path will load.
246 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 // The instruction where this slow path is happening.
249 // (Might be the load class or an initialization check).
250 HInstruction* const at_;
251
252 // The dex PC of `at_`.
253 const uint32_t dex_pc_;
254
255 // Whether to initialize the class.
256 const bool do_clinit_;
257
258 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100259};
260
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
262 public:
263 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
264
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000265 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 LocationSummary* locations = instruction_->GetLocations();
267 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
268
269 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
270 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000272
273 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000275 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100276 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
277 instruction_,
278 instruction_->GetDexPc(),
279 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000281 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000282 __ jmp(GetExitLabel());
283 }
284
Alexandre Rames9931f312015-06-19 14:47:01 +0100285 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
286
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000287 private:
288 HLoadString* const instruction_;
289
290 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
291};
292
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
294 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 TypeCheckSlowPathX86_64(HInstruction* instruction,
296 Location class_to_check,
297 Location object_class,
298 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 class_to_check_(class_to_check),
301 object_class_(object_class),
302 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000306 DCHECK(instruction_->IsCheckCast()
307 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
309 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
310 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000311 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
313 // We're moving two locations to locations that could overlap, so we need a parallel
314 // move resolver.
315 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000316 codegen->EmitParallelMoves(
317 class_to_check_,
318 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100319 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000320 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100321 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
322 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100325 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
326 instruction_,
327 dex_pc_,
328 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 } else {
330 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100331 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
332 instruction_,
333 dex_pc_,
334 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336
337 if (instruction_->IsInstanceOf()) {
338 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
339 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000341 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342 __ jmp(GetExitLabel());
343 }
344
Alexandre Rames9931f312015-06-19 14:47:01 +0100345 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
346
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 HInstruction* const instruction_;
349 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000350 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352
353 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
354};
355
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700356class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
357 public:
358 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
359 : instruction_(instruction) {}
360
361 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100362 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363 __ Bind(GetEntryLabel());
364 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 DCHECK(instruction_->IsDeoptimize());
366 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100367 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
368 deoptimize,
369 deoptimize->GetDexPc(),
370 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 }
372
Alexandre Rames9931f312015-06-19 14:47:01 +0100373 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
374
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 private:
376 HInstruction* const instruction_;
377 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
378};
379
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100380#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100381#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100382
Roland Levillain4fa13f62015-07-06 18:11:54 +0100383inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700384 switch (cond) {
385 case kCondEQ: return kEqual;
386 case kCondNE: return kNotEqual;
387 case kCondLT: return kLess;
388 case kCondLE: return kLessEqual;
389 case kCondGT: return kGreater;
390 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700391 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100392 LOG(FATAL) << "Unreachable";
393 UNREACHABLE();
394}
395
396inline Condition X86_64FPCondition(IfCondition cond) {
397 switch (cond) {
398 case kCondEQ: return kEqual;
399 case kCondNE: return kNotEqual;
400 case kCondLT: return kBelow;
401 case kCondLE: return kBelowEqual;
402 case kCondGT: return kAbove;
403 case kCondGE: return kAboveEqual;
404 };
405 LOG(FATAL) << "Unreachable";
406 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700407}
408
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800409void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100410 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800411 // All registers are assumed to be correctly set up.
412
413 // TODO: Implement all kinds of calls:
414 // 1) boot -> boot
415 // 2) app -> boot
416 // 3) app -> app
417 //
418 // Currently we implement the app -> app logic, which looks up in the resolve cache.
419
Jeff Hao848f70a2014-01-15 13:49:50 -0800420 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100421 CpuRegister reg = temp.AsRegister<CpuRegister>();
Jeff Hao848f70a2014-01-15 13:49:50 -0800422 // temp = thread->string_init_entrypoint
Jeff Haocad65422015-06-18 21:16:08 -0700423 __ gs()->movq(reg, Address::Absolute(invoke->GetStringInitOffset(), true));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000424 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100425 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000426 kX86_64WordSize).SizeValue()));
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100427 } else if (invoke->IsRecursive()) {
428 __ call(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000429 } else {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100430 CpuRegister reg = temp.AsRegister<CpuRegister>();
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100431 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
432 Register method_reg;
433 if (current_method.IsRegister()) {
434 method_reg = current_method.AsRegister<Register>();
435 } else {
436 DCHECK(invoke->GetLocations()->Intrinsified());
437 DCHECK(!current_method.IsValid());
438 method_reg = reg.AsRegister();
439 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
440 }
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100441 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100442 __ movl(reg, Address(CpuRegister(method_reg),
443 ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100444 // temp = temp[index_in_cache]
445 __ movq(reg, Address(
446 reg, CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
447 // (temp + offset_of_quick_compiled_code)()
448 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
449 kX86_64WordSize).SizeValue()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000450 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800451
452 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800453}
454
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100455void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100456 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100457}
458
459void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100460 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461}
462
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100463size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
464 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
465 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100466}
467
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100468size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
469 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
470 return kX86_64WordSize;
471}
472
473size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
474 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
475 return kX86_64WordSize;
476}
477
478size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
479 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
480 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100481}
482
Alexandre Rames8158f282015-08-07 10:26:17 +0100483void CodeGeneratorX86_64::InvokeRuntime(Address entry_point,
484 HInstruction* instruction,
485 uint32_t dex_pc,
486 SlowPathCode* slow_path) {
487 // Ensure that the call kind indication given to the register allocator is
488 // coherent with the runtime call generated.
489 if (slow_path == nullptr) {
490 DCHECK(instruction->GetLocations()->WillCall());
491 } else {
492 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal());
493 }
494
495 __ gs()->call(entry_point);
496 RecordPcInfo(instruction, dex_pc, slow_path);
497 DCHECK(instruction->IsSuspendCheck()
498 || instruction->IsBoundsCheck()
499 || instruction->IsNullCheck()
500 || instruction->IsDivZeroCheck()
501 || !IsLeafMethod());
502}
503
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000504static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000505// Use a fake return address register to mimic Quick.
506static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400507CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
508 const X86_64InstructionSetFeatures& isa_features,
509 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000510 : CodeGenerator(graph,
511 kNumberOfCpuRegisters,
512 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000513 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000514 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
515 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000516 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000517 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
518 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000519 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100520 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100521 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000522 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400523 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400524 isa_features_(isa_features),
525 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000526 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
527}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100528
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100529InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
530 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 : HGraphVisitor(graph),
532 assembler_(codegen->GetAssembler()),
533 codegen_(codegen) {}
534
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100535Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 switch (type) {
537 case Primitive::kPrimLong:
538 case Primitive::kPrimByte:
539 case Primitive::kPrimBoolean:
540 case Primitive::kPrimChar:
541 case Primitive::kPrimShort:
542 case Primitive::kPrimInt:
543 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100544 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100546 }
547
548 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100549 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100550 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100551 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100553
554 case Primitive::kPrimVoid:
555 LOG(FATAL) << "Unreachable type " << type;
556 }
557
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100558 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100559}
560
Nicolas Geoffray98893962015-01-21 12:32:32 +0000561void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100562 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100563 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000565 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100566 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000567
Nicolas Geoffray98893962015-01-21 12:32:32 +0000568 if (is_baseline) {
569 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
570 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
571 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000572 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
573 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
574 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000575 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576}
577
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100578static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100579 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100580}
David Srbecky9d8606d2015-04-12 09:35:32 +0100581
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100582static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100583 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100584}
585
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100587 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000588 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100589 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700590 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000591 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100592
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000593 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100594 __ testq(CpuRegister(RAX), Address(
595 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100596 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100597 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000598
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000599 if (HasEmptyFrame()) {
600 return;
601 }
602
Nicolas Geoffray98893962015-01-21 12:32:32 +0000603 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000604 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000605 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000606 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100607 __ cfi().AdjustCFAOffset(kX86_64WordSize);
608 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000609 }
610 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100611
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100612 int adjust = GetFrameSize() - GetCoreSpillSize();
613 __ subq(CpuRegister(RSP), Immediate(adjust));
614 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000615 uint32_t xmm_spill_location = GetFpuSpillStart();
616 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100617
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000618 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
619 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100620 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
621 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
622 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000623 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100624 }
625
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100627 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628}
629
630void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100631 __ cfi().RememberState();
632 if (!HasEmptyFrame()) {
633 uint32_t xmm_spill_location = GetFpuSpillStart();
634 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
635 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
636 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
637 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
638 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
639 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
640 }
641 }
642
643 int adjust = GetFrameSize() - GetCoreSpillSize();
644 __ addq(CpuRegister(RSP), Immediate(adjust));
645 __ cfi().AdjustCFAOffset(-adjust);
646
647 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
648 Register reg = kCoreCalleeSaves[i];
649 if (allocated_registers_.ContainsCoreRegister(reg)) {
650 __ popq(CpuRegister(reg));
651 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
652 __ cfi().Restore(DWARFReg(reg));
653 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000654 }
655 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100656 __ ret();
657 __ cfi().RestoreState();
658 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100659}
660
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100661void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
662 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663}
664
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100665Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
666 switch (load->GetType()) {
667 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100669 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100670
671 case Primitive::kPrimInt:
672 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100673 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675
676 case Primitive::kPrimBoolean:
677 case Primitive::kPrimByte:
678 case Primitive::kPrimChar:
679 case Primitive::kPrimShort:
680 case Primitive::kPrimVoid:
681 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700682 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683 }
684
685 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700686 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100687}
688
689void CodeGeneratorX86_64::Move(Location destination, Location source) {
690 if (source.Equals(destination)) {
691 return;
692 }
693 if (destination.IsRegister()) {
694 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000695 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100696 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000697 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100698 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000699 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100700 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100701 } else {
702 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 Address(CpuRegister(RSP), source.GetStackIndex()));
705 }
706 } else if (destination.IsFpuRegister()) {
707 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000708 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000712 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 Address(CpuRegister(RSP), source.GetStackIndex()));
714 } else {
715 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000716 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100718 }
719 } else if (destination.IsStackSlot()) {
720 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 } else if (source.IsFpuRegister()) {
724 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500726 } else if (source.IsConstant()) {
727 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000728 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500729 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500731 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000732 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
733 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100734 }
735 } else {
736 DCHECK(destination.IsDoubleStackSlot());
737 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000739 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 } else if (source.IsFpuRegister()) {
741 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000742 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500743 } else if (source.IsConstant()) {
744 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800745 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500746 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000747 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500748 } else {
749 DCHECK(constant->IsLongConstant());
750 value = constant->AsLongConstant()->GetValue();
751 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400752 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 } else {
754 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000755 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
756 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100757 }
758 }
759}
760
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100761void CodeGeneratorX86_64::Move(HInstruction* instruction,
762 Location location,
763 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000764 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100765 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700766 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100767 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000768 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100769 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000770 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000771 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
772 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000773 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000774 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000775 } else if (location.IsStackSlot()) {
776 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
777 } else {
778 DCHECK(location.IsConstant());
779 DCHECK_EQ(location.GetConstant(), const_to_move);
780 }
781 } else if (const_to_move->IsLongConstant()) {
782 int64_t value = const_to_move->AsLongConstant()->GetValue();
783 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400784 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000785 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400786 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 } else {
788 DCHECK(location.IsConstant());
789 DCHECK_EQ(location.GetConstant(), const_to_move);
790 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100791 }
Roland Levillain476df552014-10-09 17:51:36 +0100792 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100793 switch (instruction->GetType()) {
794 case Primitive::kPrimBoolean:
795 case Primitive::kPrimByte:
796 case Primitive::kPrimChar:
797 case Primitive::kPrimShort:
798 case Primitive::kPrimInt:
799 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100800 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100801 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
802 break;
803
804 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000806 Move(location,
807 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100808 break;
809
810 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100811 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100812 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000813 } else if (instruction->IsTemporary()) {
814 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
815 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100816 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100817 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100818 switch (instruction->GetType()) {
819 case Primitive::kPrimBoolean:
820 case Primitive::kPrimByte:
821 case Primitive::kPrimChar:
822 case Primitive::kPrimShort:
823 case Primitive::kPrimInt:
824 case Primitive::kPrimNot:
825 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100826 case Primitive::kPrimFloat:
827 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000828 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100829 break;
830
831 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100832 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100833 }
834 }
835}
836
David Brazdilfc6a86a2015-06-26 10:33:45 +0000837void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 DCHECK(!successor->IsExitBlock());
839
840 HBasicBlock* block = got->GetBlock();
841 HInstruction* previous = got->GetPrevious();
842
843 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000844 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
846 return;
847 }
848
849 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
850 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
851 }
852 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100853 __ jmp(codegen_->GetLabelOf(successor));
854 }
855}
856
David Brazdilfc6a86a2015-06-26 10:33:45 +0000857void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
858 got->SetLocations(nullptr);
859}
860
861void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
862 HandleGoto(got, got->GetSuccessor());
863}
864
865void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
866 try_boundary->SetLocations(nullptr);
867}
868
869void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
870 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
871 if (!successor->IsExitBlock()) {
872 HandleGoto(try_boundary, successor);
873 }
874}
875
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100876void LocationsBuilderX86_64::VisitExit(HExit* exit) {
877 exit->SetLocations(nullptr);
878}
879
880void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700881 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882}
883
Mark Mendellc4701932015-04-10 13:18:51 -0400884void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
885 Label* true_label,
886 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100887 if (cond->IsFPConditionTrueIfNaN()) {
888 __ j(kUnordered, true_label);
889 } else if (cond->IsFPConditionFalseIfNaN()) {
890 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400891 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100892 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400893}
894
895void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
896 HCondition* condition,
897 Label* true_target,
898 Label* false_target,
899 Label* always_true_target) {
900 LocationSummary* locations = condition->GetLocations();
901 Location left = locations->InAt(0);
902 Location right = locations->InAt(1);
903
904 // We don't want true_target as a nullptr.
905 if (true_target == nullptr) {
906 true_target = always_true_target;
907 }
908 bool falls_through = (false_target == nullptr);
909
910 // FP compares don't like null false_targets.
911 if (false_target == nullptr) {
912 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
913 }
914
915 Primitive::Type type = condition->InputAt(0)->GetType();
916 switch (type) {
917 case Primitive::kPrimLong: {
918 CpuRegister left_reg = left.AsRegister<CpuRegister>();
919 if (right.IsConstant()) {
920 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
921 if (IsInt<32>(value)) {
922 if (value == 0) {
923 __ testq(left_reg, left_reg);
924 } else {
925 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
926 }
927 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -0400929 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
930 }
931 } else if (right.IsDoubleStackSlot()) {
932 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
933 } else {
934 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
935 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100936 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400937 break;
938 }
939 case Primitive::kPrimFloat: {
940 if (right.IsFpuRegister()) {
941 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
942 } else if (right.IsConstant()) {
943 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
944 codegen_->LiteralFloatAddress(
945 right.GetConstant()->AsFloatConstant()->GetValue()));
946 } else {
947 DCHECK(right.IsStackSlot());
948 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
949 Address(CpuRegister(RSP), right.GetStackIndex()));
950 }
951 GenerateFPJumps(condition, true_target, false_target);
952 break;
953 }
954 case Primitive::kPrimDouble: {
955 if (right.IsFpuRegister()) {
956 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
957 } else if (right.IsConstant()) {
958 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
959 codegen_->LiteralDoubleAddress(
960 right.GetConstant()->AsDoubleConstant()->GetValue()));
961 } else {
962 DCHECK(right.IsDoubleStackSlot());
963 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
964 Address(CpuRegister(RSP), right.GetStackIndex()));
965 }
966 GenerateFPJumps(condition, true_target, false_target);
967 break;
968 }
969 default:
970 LOG(FATAL) << "Unexpected condition type " << type;
971 }
972
973 if (!falls_through) {
974 __ jmp(false_target);
975 }
976}
977
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700978void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
979 Label* true_target,
980 Label* false_target,
981 Label* always_true_target) {
982 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100983 if (cond->IsIntConstant()) {
984 // Constant condition, statically compared against 1.
985 int32_t cond_value = cond->AsIntConstant()->GetValue();
986 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700987 if (always_true_target != nullptr) {
988 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100989 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100990 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100991 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100992 DCHECK_EQ(cond_value, 0);
993 }
994 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100995 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100996 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
997 // Moves do not affect the eflags register, so if the condition is
998 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -0400999 // again. We can't use the eflags on FP conditions if they are
1000 // materialized due to the complex branching.
1001 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001002 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001003 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1004 && !Primitive::IsFloatingPointType(type);
1005
Roland Levillain4fa13f62015-07-06 18:11:54 +01001006 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001007 if (!eflags_set) {
1008 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001009 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001010 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001011 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001012 } else {
1013 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1014 Immediate(0));
1015 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001016 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001017 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001018 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001019 }
1020 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001021 // Condition has not been materialized, use its inputs as the
1022 // comparison and its condition as the branch condition.
1023
Mark Mendellc4701932015-04-10 13:18:51 -04001024 // Is this a long or FP comparison that has been folded into the HCondition?
1025 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001026 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001027 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1028 true_target, false_target, always_true_target);
1029 return;
1030 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001031
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001032 Location lhs = cond->GetLocations()->InAt(0);
1033 Location rhs = cond->GetLocations()->InAt(1);
1034 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001036 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001037 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001038 if (constant == 0) {
1039 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1040 } else {
1041 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1042 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001044 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001045 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1046 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001047 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001048 }
Dave Allison20dfc792014-06-16 20:44:29 -07001049 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001050 if (false_target != nullptr) {
1051 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052 }
1053}
1054
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001055void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1056 LocationSummary* locations =
1057 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1058 HInstruction* cond = if_instr->InputAt(0);
1059 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1060 locations->SetInAt(0, Location::Any());
1061 }
1062}
1063
1064void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1065 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1066 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1067 Label* always_true_target = true_target;
1068 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1069 if_instr->IfTrueSuccessor())) {
1070 always_true_target = nullptr;
1071 }
1072 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1073 if_instr->IfFalseSuccessor())) {
1074 false_target = nullptr;
1075 }
1076 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1077}
1078
1079void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1080 LocationSummary* locations = new (GetGraph()->GetArena())
1081 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1082 HInstruction* cond = deoptimize->InputAt(0);
1083 DCHECK(cond->IsCondition());
1084 if (cond->AsCondition()->NeedsMaterialization()) {
1085 locations->SetInAt(0, Location::Any());
1086 }
1087}
1088
1089void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1090 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1091 DeoptimizationSlowPathX86_64(deoptimize);
1092 codegen_->AddSlowPath(slow_path);
1093 Label* slow_path_entry = slow_path->GetEntryLabel();
1094 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1095}
1096
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1098 local->SetLocations(nullptr);
1099}
1100
1101void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1102 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1103}
1104
1105void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1106 local->SetLocations(nullptr);
1107}
1108
1109void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1110 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001111 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112}
1113
1114void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001115 LocationSummary* locations =
1116 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117 switch (store->InputAt(1)->GetType()) {
1118 case Primitive::kPrimBoolean:
1119 case Primitive::kPrimByte:
1120 case Primitive::kPrimChar:
1121 case Primitive::kPrimShort:
1122 case Primitive::kPrimInt:
1123 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001124 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1126 break;
1127
1128 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001129 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1131 break;
1132
1133 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001134 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136}
1137
1138void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001139 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001140}
1141
Roland Levillain0d37cd02015-05-27 16:39:19 +01001142void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001143 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001144 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001145 // Handle the long/FP comparisons made in instruction simplification.
1146 switch (cond->InputAt(0)->GetType()) {
1147 case Primitive::kPrimLong:
1148 locations->SetInAt(0, Location::RequiresRegister());
1149 locations->SetInAt(1, Location::Any());
1150 break;
1151 case Primitive::kPrimFloat:
1152 case Primitive::kPrimDouble:
1153 locations->SetInAt(0, Location::RequiresFpuRegister());
1154 locations->SetInAt(1, Location::Any());
1155 break;
1156 default:
1157 locations->SetInAt(0, Location::RequiresRegister());
1158 locations->SetInAt(1, Location::Any());
1159 break;
1160 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001161 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001162 locations->SetOut(Location::RequiresRegister());
1163 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001164}
1165
Roland Levillain0d37cd02015-05-27 16:39:19 +01001166void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001167 if (!cond->NeedsMaterialization()) {
1168 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001169 }
Mark Mendellc4701932015-04-10 13:18:51 -04001170
1171 LocationSummary* locations = cond->GetLocations();
1172 Location lhs = locations->InAt(0);
1173 Location rhs = locations->InAt(1);
1174 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1175 Label true_label, false_label;
1176
1177 switch (cond->InputAt(0)->GetType()) {
1178 default:
1179 // Integer case.
1180
1181 // Clear output register: setcc only sets the low byte.
1182 __ xorl(reg, reg);
1183
1184 if (rhs.IsRegister()) {
1185 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1186 } else if (rhs.IsConstant()) {
1187 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1188 if (constant == 0) {
1189 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1190 } else {
1191 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1192 }
1193 } else {
1194 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1195 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001196 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001197 return;
1198 case Primitive::kPrimLong:
1199 // Clear output register: setcc only sets the low byte.
1200 __ xorl(reg, reg);
1201
1202 if (rhs.IsRegister()) {
1203 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1204 } else if (rhs.IsConstant()) {
1205 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1206 if (IsInt<32>(value)) {
1207 if (value == 0) {
1208 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1209 } else {
1210 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1211 }
1212 } else {
1213 // Value won't fit in an int.
1214 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1215 }
1216 } else {
1217 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1218 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001220 return;
1221 case Primitive::kPrimFloat: {
1222 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1223 if (rhs.IsConstant()) {
1224 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1225 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1226 } else if (rhs.IsStackSlot()) {
1227 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1228 } else {
1229 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1230 }
1231 GenerateFPJumps(cond, &true_label, &false_label);
1232 break;
1233 }
1234 case Primitive::kPrimDouble: {
1235 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1236 if (rhs.IsConstant()) {
1237 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1238 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1239 } else if (rhs.IsDoubleStackSlot()) {
1240 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1241 } else {
1242 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1243 }
1244 GenerateFPJumps(cond, &true_label, &false_label);
1245 break;
1246 }
1247 }
1248
1249 // Convert the jumps into the result.
1250 Label done_label;
1251
Roland Levillain4fa13f62015-07-06 18:11:54 +01001252 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001253 __ Bind(&false_label);
1254 __ xorl(reg, reg);
1255 __ jmp(&done_label);
1256
Roland Levillain4fa13f62015-07-06 18:11:54 +01001257 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001258 __ Bind(&true_label);
1259 __ movl(reg, Immediate(1));
1260 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001261}
1262
1263void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1264 VisitCondition(comp);
1265}
1266
1267void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1268 VisitCondition(comp);
1269}
1270
1271void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1272 VisitCondition(comp);
1273}
1274
1275void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1276 VisitCondition(comp);
1277}
1278
1279void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1280 VisitCondition(comp);
1281}
1282
1283void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1284 VisitCondition(comp);
1285}
1286
1287void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1288 VisitCondition(comp);
1289}
1290
1291void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1292 VisitCondition(comp);
1293}
1294
1295void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1296 VisitCondition(comp);
1297}
1298
1299void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1300 VisitCondition(comp);
1301}
1302
1303void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1304 VisitCondition(comp);
1305}
1306
1307void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1308 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309}
1310
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001311void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001312 LocationSummary* locations =
1313 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001314 switch (compare->InputAt(0)->GetType()) {
1315 case Primitive::kPrimLong: {
1316 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001317 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1319 break;
1320 }
1321 case Primitive::kPrimFloat:
1322 case Primitive::kPrimDouble: {
1323 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001324 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001325 locations->SetOut(Location::RequiresRegister());
1326 break;
1327 }
1328 default:
1329 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1330 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001331}
1332
1333void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001334 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001335 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001336 Location left = locations->InAt(0);
1337 Location right = locations->InAt(1);
1338
1339 Label less, greater, done;
1340 Primitive::Type type = compare->InputAt(0)->GetType();
1341 switch (type) {
1342 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001343 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1344 if (right.IsConstant()) {
1345 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001346 if (IsInt<32>(value)) {
1347 if (value == 0) {
1348 __ testq(left_reg, left_reg);
1349 } else {
1350 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1351 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001352 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001353 // Value won't fit in an int.
1354 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001355 }
Mark Mendell40741f32015-04-20 22:10:34 -04001356 } else if (right.IsDoubleStackSlot()) {
1357 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001358 } else {
1359 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1360 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001361 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001362 }
1363 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001364 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1365 if (right.IsConstant()) {
1366 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1367 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1368 } else if (right.IsStackSlot()) {
1369 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1370 } else {
1371 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1372 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001373 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1374 break;
1375 }
1376 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001377 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1378 if (right.IsConstant()) {
1379 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1380 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1381 } else if (right.IsDoubleStackSlot()) {
1382 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1383 } else {
1384 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1385 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001386 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1387 break;
1388 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001389 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001390 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001391 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001392 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001393 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001394 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001395
Calin Juravle91debbc2014-11-26 19:01:09 +00001396 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001397 __ movl(out, Immediate(1));
1398 __ jmp(&done);
1399
1400 __ Bind(&less);
1401 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001402
1403 __ Bind(&done);
1404}
1405
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001406void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001407 LocationSummary* locations =
1408 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001409 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001410}
1411
1412void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001413 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001414 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001415}
1416
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001417void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1418 LocationSummary* locations =
1419 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1420 locations->SetOut(Location::ConstantLocation(constant));
1421}
1422
1423void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1424 // Will be generated at use site.
1425 UNUSED(constant);
1426}
1427
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001428void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001429 LocationSummary* locations =
1430 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001431 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001432}
1433
1434void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001435 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001436 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001437}
1438
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001439void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1440 LocationSummary* locations =
1441 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1442 locations->SetOut(Location::ConstantLocation(constant));
1443}
1444
1445void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1446 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001447 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001448}
1449
1450void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1451 LocationSummary* locations =
1452 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1453 locations->SetOut(Location::ConstantLocation(constant));
1454}
1455
1456void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1457 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001458 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001459}
1460
Calin Juravle27df7582015-04-17 19:12:31 +01001461void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1462 memory_barrier->SetLocations(nullptr);
1463}
1464
1465void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1466 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1467}
1468
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001469void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1470 ret->SetLocations(nullptr);
1471}
1472
1473void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001474 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001475 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001476}
1477
1478void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001481 switch (ret->InputAt(0)->GetType()) {
1482 case Primitive::kPrimBoolean:
1483 case Primitive::kPrimByte:
1484 case Primitive::kPrimChar:
1485 case Primitive::kPrimShort:
1486 case Primitive::kPrimInt:
1487 case Primitive::kPrimNot:
1488 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001489 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001490 break;
1491
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001492 case Primitive::kPrimFloat:
1493 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001494 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001495 break;
1496
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001497 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001498 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001499 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001500}
1501
1502void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1503 if (kIsDebugBuild) {
1504 switch (ret->InputAt(0)->GetType()) {
1505 case Primitive::kPrimBoolean:
1506 case Primitive::kPrimByte:
1507 case Primitive::kPrimChar:
1508 case Primitive::kPrimShort:
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimNot:
1511 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001512 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001513 break;
1514
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001515 case Primitive::kPrimFloat:
1516 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001517 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001518 XMM0);
1519 break;
1520
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001521 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001522 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001523 }
1524 }
1525 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001526}
1527
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001528Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1529 switch (type) {
1530 case Primitive::kPrimBoolean:
1531 case Primitive::kPrimByte:
1532 case Primitive::kPrimChar:
1533 case Primitive::kPrimShort:
1534 case Primitive::kPrimInt:
1535 case Primitive::kPrimNot:
1536 case Primitive::kPrimLong:
1537 return Location::RegisterLocation(RAX);
1538
1539 case Primitive::kPrimVoid:
1540 return Location::NoLocation();
1541
1542 case Primitive::kPrimDouble:
1543 case Primitive::kPrimFloat:
1544 return Location::FpuRegisterLocation(XMM0);
1545 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001546
1547 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001548}
1549
1550Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1551 return Location::RegisterLocation(kMethodRegisterArgument);
1552}
1553
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001554Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001555 switch (type) {
1556 case Primitive::kPrimBoolean:
1557 case Primitive::kPrimByte:
1558 case Primitive::kPrimChar:
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimNot: {
1562 uint32_t index = gp_index_++;
1563 stack_index_++;
1564 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001565 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001566 } else {
1567 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1568 }
1569 }
1570
1571 case Primitive::kPrimLong: {
1572 uint32_t index = gp_index_;
1573 stack_index_ += 2;
1574 if (index < calling_convention.GetNumberOfRegisters()) {
1575 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001576 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001577 } else {
1578 gp_index_ += 2;
1579 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1580 }
1581 }
1582
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001583 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001584 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001585 stack_index_++;
1586 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001587 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001588 } else {
1589 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1590 }
1591 }
1592
1593 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001594 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001595 stack_index_ += 2;
1596 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001597 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001598 } else {
1599 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1600 }
1601 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001602
1603 case Primitive::kPrimVoid:
1604 LOG(FATAL) << "Unexpected parameter type " << type;
1605 break;
1606 }
1607 return Location();
1608}
1609
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001610void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001611 // When we do not run baseline, explicit clinit checks triggered by static
1612 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1613 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001614
Mark Mendellfb8d2792015-03-31 22:16:59 -04001615 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001616 if (intrinsic.TryDispatch(invoke)) {
1617 return;
1618 }
1619
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001620 HandleInvoke(invoke);
1621}
1622
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001623static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1624 if (invoke->GetLocations()->Intrinsified()) {
1625 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1626 intrinsic.Dispatch(invoke);
1627 return true;
1628 }
1629 return false;
1630}
1631
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001632void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001633 // When we do not run baseline, explicit clinit checks triggered by static
1634 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1635 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001636
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001637 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1638 return;
1639 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001640
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001641 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001642 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001643 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001644 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001645}
1646
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001647void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001648 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001649 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001650}
1651
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001652void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001653 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001654 if (intrinsic.TryDispatch(invoke)) {
1655 return;
1656 }
1657
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001658 HandleInvoke(invoke);
1659}
1660
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001661void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001662 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1663 return;
1664 }
1665
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001667 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1668 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001669 LocationSummary* locations = invoke->GetLocations();
1670 Location receiver = locations->InAt(0);
1671 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1672 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001673 DCHECK(receiver.IsRegister());
1674 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001675 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001676 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001677 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001678 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001679 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001680 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001681 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001682
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001683 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001684 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001685}
1686
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001687void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1688 HandleInvoke(invoke);
1689 // Add the hidden argument.
1690 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1691}
1692
1693void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1694 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001695 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001696 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1697 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001698 LocationSummary* locations = invoke->GetLocations();
1699 Location receiver = locations->InAt(0);
1700 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1701
1702 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001703 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1704 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001705
1706 // temp = object->GetClass();
1707 if (receiver.IsStackSlot()) {
1708 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1709 __ movl(temp, Address(temp, class_offset));
1710 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001712 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001713 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001714 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001715 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001716 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001717 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001718 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001719 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001720
1721 DCHECK(!codegen_->IsLeafMethod());
1722 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1723}
1724
Roland Levillain88cb1752014-10-20 16:36:47 +01001725void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1726 LocationSummary* locations =
1727 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1728 switch (neg->GetResultType()) {
1729 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001730 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001731 locations->SetInAt(0, Location::RequiresRegister());
1732 locations->SetOut(Location::SameAsFirstInput());
1733 break;
1734
Roland Levillain88cb1752014-10-20 16:36:47 +01001735 case Primitive::kPrimFloat:
1736 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001737 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001738 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001739 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001740 break;
1741
1742 default:
1743 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1744 }
1745}
1746
1747void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1748 LocationSummary* locations = neg->GetLocations();
1749 Location out = locations->Out();
1750 Location in = locations->InAt(0);
1751 switch (neg->GetResultType()) {
1752 case Primitive::kPrimInt:
1753 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001754 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001756 break;
1757
1758 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001759 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001760 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001762 break;
1763
Roland Levillain5368c212014-11-27 15:03:41 +00001764 case Primitive::kPrimFloat: {
1765 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001766 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001767 // Implement float negation with an exclusive or with value
1768 // 0x80000000 (mask for bit 31, representing the sign of a
1769 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001770 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001771 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001772 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001773 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001774
Roland Levillain5368c212014-11-27 15:03:41 +00001775 case Primitive::kPrimDouble: {
1776 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001777 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001778 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001779 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001780 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001781 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001782 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001783 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001784 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001785
1786 default:
1787 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1788 }
1789}
1790
Roland Levillaindff1f282014-11-05 14:15:05 +00001791void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1792 LocationSummary* locations =
1793 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1794 Primitive::Type result_type = conversion->GetResultType();
1795 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001796 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001797
David Brazdilb2bd1c52015-03-25 11:17:37 +00001798 // The Java language does not allow treating boolean as an integral type but
1799 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001800
Roland Levillaindff1f282014-11-05 14:15:05 +00001801 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001802 case Primitive::kPrimByte:
1803 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001804 case Primitive::kPrimBoolean:
1805 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001806 case Primitive::kPrimShort:
1807 case Primitive::kPrimInt:
1808 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001809 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001810 locations->SetInAt(0, Location::Any());
1811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1812 break;
1813
1814 default:
1815 LOG(FATAL) << "Unexpected type conversion from " << input_type
1816 << " to " << result_type;
1817 }
1818 break;
1819
Roland Levillain01a8d712014-11-14 16:27:39 +00001820 case Primitive::kPrimShort:
1821 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001822 case Primitive::kPrimBoolean:
1823 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001824 case Primitive::kPrimByte:
1825 case Primitive::kPrimInt:
1826 case Primitive::kPrimChar:
1827 // Processing a Dex `int-to-short' instruction.
1828 locations->SetInAt(0, Location::Any());
1829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1830 break;
1831
1832 default:
1833 LOG(FATAL) << "Unexpected type conversion from " << input_type
1834 << " to " << result_type;
1835 }
1836 break;
1837
Roland Levillain946e1432014-11-11 17:35:19 +00001838 case Primitive::kPrimInt:
1839 switch (input_type) {
1840 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001841 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001842 locations->SetInAt(0, Location::Any());
1843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1844 break;
1845
1846 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001847 // Processing a Dex `float-to-int' instruction.
1848 locations->SetInAt(0, Location::RequiresFpuRegister());
1849 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001850 break;
1851
Roland Levillain946e1432014-11-11 17:35:19 +00001852 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001853 // Processing a Dex `double-to-int' instruction.
1854 locations->SetInAt(0, Location::RequiresFpuRegister());
1855 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001856 break;
1857
1858 default:
1859 LOG(FATAL) << "Unexpected type conversion from " << input_type
1860 << " to " << result_type;
1861 }
1862 break;
1863
Roland Levillaindff1f282014-11-05 14:15:05 +00001864 case Primitive::kPrimLong:
1865 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001866 case Primitive::kPrimBoolean:
1867 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001868 case Primitive::kPrimByte:
1869 case Primitive::kPrimShort:
1870 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001871 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001872 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001873 // TODO: We would benefit from a (to-be-implemented)
1874 // Location::RegisterOrStackSlot requirement for this input.
1875 locations->SetInAt(0, Location::RequiresRegister());
1876 locations->SetOut(Location::RequiresRegister());
1877 break;
1878
1879 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001880 // Processing a Dex `float-to-long' instruction.
1881 locations->SetInAt(0, Location::RequiresFpuRegister());
1882 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001883 break;
1884
Roland Levillaindff1f282014-11-05 14:15:05 +00001885 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001886 // Processing a Dex `double-to-long' instruction.
1887 locations->SetInAt(0, Location::RequiresFpuRegister());
1888 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001889 break;
1890
1891 default:
1892 LOG(FATAL) << "Unexpected type conversion from " << input_type
1893 << " to " << result_type;
1894 }
1895 break;
1896
Roland Levillain981e4542014-11-14 11:47:14 +00001897 case Primitive::kPrimChar:
1898 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001899 case Primitive::kPrimBoolean:
1900 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001901 case Primitive::kPrimByte:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001904 // Processing a Dex `int-to-char' instruction.
1905 locations->SetInAt(0, Location::Any());
1906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1907 break;
1908
1909 default:
1910 LOG(FATAL) << "Unexpected type conversion from " << input_type
1911 << " to " << result_type;
1912 }
1913 break;
1914
Roland Levillaindff1f282014-11-05 14:15:05 +00001915 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001916 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001917 case Primitive::kPrimBoolean:
1918 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001919 case Primitive::kPrimByte:
1920 case Primitive::kPrimShort:
1921 case Primitive::kPrimInt:
1922 case Primitive::kPrimChar:
1923 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001924 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001925 locations->SetOut(Location::RequiresFpuRegister());
1926 break;
1927
1928 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001929 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001930 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001931 locations->SetOut(Location::RequiresFpuRegister());
1932 break;
1933
Roland Levillaincff13742014-11-17 14:32:17 +00001934 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001935 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001936 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001937 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001938 break;
1939
1940 default:
1941 LOG(FATAL) << "Unexpected type conversion from " << input_type
1942 << " to " << result_type;
1943 };
1944 break;
1945
Roland Levillaindff1f282014-11-05 14:15:05 +00001946 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001947 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001948 case Primitive::kPrimBoolean:
1949 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001950 case Primitive::kPrimByte:
1951 case Primitive::kPrimShort:
1952 case Primitive::kPrimInt:
1953 case Primitive::kPrimChar:
1954 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001955 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001956 locations->SetOut(Location::RequiresFpuRegister());
1957 break;
1958
1959 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001960 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001961 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001962 locations->SetOut(Location::RequiresFpuRegister());
1963 break;
1964
Roland Levillaincff13742014-11-17 14:32:17 +00001965 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001966 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001967 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001968 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected type conversion from " << input_type
1973 << " to " << result_type;
1974 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001975 break;
1976
1977 default:
1978 LOG(FATAL) << "Unexpected type conversion from " << input_type
1979 << " to " << result_type;
1980 }
1981}
1982
1983void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1984 LocationSummary* locations = conversion->GetLocations();
1985 Location out = locations->Out();
1986 Location in = locations->InAt(0);
1987 Primitive::Type result_type = conversion->GetResultType();
1988 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001989 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001990 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001991 case Primitive::kPrimByte:
1992 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001993 case Primitive::kPrimBoolean:
1994 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001995 case Primitive::kPrimShort:
1996 case Primitive::kPrimInt:
1997 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001998 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001999 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002001 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002002 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002003 Address(CpuRegister(RSP), in.GetStackIndex()));
2004 } else {
2005 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002006 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002007 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2008 }
2009 break;
2010
2011 default:
2012 LOG(FATAL) << "Unexpected type conversion from " << input_type
2013 << " to " << result_type;
2014 }
2015 break;
2016
Roland Levillain01a8d712014-11-14 16:27:39 +00002017 case Primitive::kPrimShort:
2018 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002019 case Primitive::kPrimBoolean:
2020 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002021 case Primitive::kPrimByte:
2022 case Primitive::kPrimInt:
2023 case Primitive::kPrimChar:
2024 // Processing a Dex `int-to-short' instruction.
2025 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002027 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002028 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002029 Address(CpuRegister(RSP), in.GetStackIndex()));
2030 } else {
2031 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002032 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002033 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2034 }
2035 break;
2036
2037 default:
2038 LOG(FATAL) << "Unexpected type conversion from " << input_type
2039 << " to " << result_type;
2040 }
2041 break;
2042
Roland Levillain946e1432014-11-11 17:35:19 +00002043 case Primitive::kPrimInt:
2044 switch (input_type) {
2045 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002046 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002047 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002049 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002051 Address(CpuRegister(RSP), in.GetStackIndex()));
2052 } else {
2053 DCHECK(in.IsConstant());
2054 DCHECK(in.GetConstant()->IsLongConstant());
2055 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002057 }
2058 break;
2059
Roland Levillain3f8f9362014-12-02 17:45:01 +00002060 case Primitive::kPrimFloat: {
2061 // Processing a Dex `float-to-int' instruction.
2062 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2063 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain3f8f9362014-12-02 17:45:01 +00002064 Label done, nan;
2065
2066 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002067 // if input >= (float)INT_MAX goto done
2068 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002069 __ j(kAboveEqual, &done);
2070 // if input == NaN goto nan
2071 __ j(kUnordered, &nan);
2072 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002073 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002074 __ jmp(&done);
2075 __ Bind(&nan);
2076 // output = 0
2077 __ xorl(output, output);
2078 __ Bind(&done);
2079 break;
2080 }
2081
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002082 case Primitive::kPrimDouble: {
2083 // Processing a Dex `double-to-int' instruction.
2084 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2085 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002086 Label done, nan;
2087
2088 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002089 // if input >= (double)INT_MAX goto done
2090 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002091 __ j(kAboveEqual, &done);
2092 // if input == NaN goto nan
2093 __ j(kUnordered, &nan);
2094 // output = double-to-int-truncate(input)
2095 __ cvttsd2si(output, input);
2096 __ jmp(&done);
2097 __ Bind(&nan);
2098 // output = 0
2099 __ xorl(output, output);
2100 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002101 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002102 }
Roland Levillain946e1432014-11-11 17:35:19 +00002103
2104 default:
2105 LOG(FATAL) << "Unexpected type conversion from " << input_type
2106 << " to " << result_type;
2107 }
2108 break;
2109
Roland Levillaindff1f282014-11-05 14:15:05 +00002110 case Primitive::kPrimLong:
2111 switch (input_type) {
2112 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002113 case Primitive::kPrimBoolean:
2114 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002115 case Primitive::kPrimByte:
2116 case Primitive::kPrimShort:
2117 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002118 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002119 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002120 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002122 break;
2123
Roland Levillain624279f2014-12-04 11:54:28 +00002124 case Primitive::kPrimFloat: {
2125 // Processing a Dex `float-to-long' instruction.
2126 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2127 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain624279f2014-12-04 11:54:28 +00002128 Label done, nan;
2129
Mark Mendell92e83bf2015-05-07 11:25:03 -04002130 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002131 // if input >= (float)LONG_MAX goto done
2132 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002133 __ j(kAboveEqual, &done);
2134 // if input == NaN goto nan
2135 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002137 __ cvttss2si(output, input, true);
2138 __ jmp(&done);
2139 __ Bind(&nan);
2140 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002141 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002142 __ Bind(&done);
2143 break;
2144 }
2145
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002146 case Primitive::kPrimDouble: {
2147 // Processing a Dex `double-to-long' instruction.
2148 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2149 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002150 Label done, nan;
2151
Mark Mendell92e83bf2015-05-07 11:25:03 -04002152 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002153 // if input >= (double)LONG_MAX goto done
2154 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002155 __ j(kAboveEqual, &done);
2156 // if input == NaN goto nan
2157 __ j(kUnordered, &nan);
2158 // output = double-to-long-truncate(input)
2159 __ cvttsd2si(output, input, true);
2160 __ jmp(&done);
2161 __ Bind(&nan);
2162 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002163 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002165 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002166 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002167
2168 default:
2169 LOG(FATAL) << "Unexpected type conversion from " << input_type
2170 << " to " << result_type;
2171 }
2172 break;
2173
Roland Levillain981e4542014-11-14 11:47:14 +00002174 case Primitive::kPrimChar:
2175 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002176 case Primitive::kPrimBoolean:
2177 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002178 case Primitive::kPrimByte:
2179 case Primitive::kPrimShort:
2180 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002181 // Processing a Dex `int-to-char' instruction.
2182 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002183 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002184 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002185 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002186 Address(CpuRegister(RSP), in.GetStackIndex()));
2187 } else {
2188 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002189 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002190 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2191 }
2192 break;
2193
2194 default:
2195 LOG(FATAL) << "Unexpected type conversion from " << input_type
2196 << " to " << result_type;
2197 }
2198 break;
2199
Roland Levillaindff1f282014-11-05 14:15:05 +00002200 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002201 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002202 case Primitive::kPrimBoolean:
2203 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002204 case Primitive::kPrimByte:
2205 case Primitive::kPrimShort:
2206 case Primitive::kPrimInt:
2207 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002208 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002209 if (in.IsRegister()) {
2210 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2211 } else if (in.IsConstant()) {
2212 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2213 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2214 if (v == 0) {
2215 __ xorps(dest, dest);
2216 } else {
2217 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2218 }
2219 } else {
2220 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2221 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2222 }
Roland Levillaincff13742014-11-17 14:32:17 +00002223 break;
2224
2225 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002226 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002227 if (in.IsRegister()) {
2228 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2229 } else if (in.IsConstant()) {
2230 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2231 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2232 if (v == 0) {
2233 __ xorps(dest, dest);
2234 } else {
2235 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2236 }
2237 } else {
2238 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2239 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2240 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002241 break;
2242
Roland Levillaincff13742014-11-17 14:32:17 +00002243 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002244 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002245 if (in.IsFpuRegister()) {
2246 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2247 } else if (in.IsConstant()) {
2248 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2249 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2250 if (bit_cast<int64_t, double>(v) == 0) {
2251 __ xorps(dest, dest);
2252 } else {
2253 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2254 }
2255 } else {
2256 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2257 Address(CpuRegister(RSP), in.GetStackIndex()));
2258 }
Roland Levillaincff13742014-11-17 14:32:17 +00002259 break;
2260
2261 default:
2262 LOG(FATAL) << "Unexpected type conversion from " << input_type
2263 << " to " << result_type;
2264 };
2265 break;
2266
Roland Levillaindff1f282014-11-05 14:15:05 +00002267 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002268 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002269 case Primitive::kPrimBoolean:
2270 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002271 case Primitive::kPrimByte:
2272 case Primitive::kPrimShort:
2273 case Primitive::kPrimInt:
2274 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002275 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002276 if (in.IsRegister()) {
2277 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2278 } else if (in.IsConstant()) {
2279 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2280 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2281 if (v == 0) {
2282 __ xorpd(dest, dest);
2283 } else {
2284 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2285 }
2286 } else {
2287 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2288 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2289 }
Roland Levillaincff13742014-11-17 14:32:17 +00002290 break;
2291
2292 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002293 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002294 if (in.IsRegister()) {
2295 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2296 } else if (in.IsConstant()) {
2297 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2298 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2299 if (v == 0) {
2300 __ xorpd(dest, dest);
2301 } else {
2302 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2303 }
2304 } else {
2305 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2306 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2307 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002308 break;
2309
Roland Levillaincff13742014-11-17 14:32:17 +00002310 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002311 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002312 if (in.IsFpuRegister()) {
2313 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2314 } else if (in.IsConstant()) {
2315 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2316 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2317 if (bit_cast<int32_t, float>(v) == 0) {
2318 __ xorpd(dest, dest);
2319 } else {
2320 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2321 }
2322 } else {
2323 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2324 Address(CpuRegister(RSP), in.GetStackIndex()));
2325 }
Roland Levillaincff13742014-11-17 14:32:17 +00002326 break;
2327
2328 default:
2329 LOG(FATAL) << "Unexpected type conversion from " << input_type
2330 << " to " << result_type;
2331 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002332 break;
2333
2334 default:
2335 LOG(FATAL) << "Unexpected type conversion from " << input_type
2336 << " to " << result_type;
2337 }
2338}
2339
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002340void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002341 LocationSummary* locations =
2342 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002343 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002344 case Primitive::kPrimInt: {
2345 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002346 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002348 break;
2349 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002350
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002351 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002352 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002353 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002354 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002356 break;
2357 }
2358
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002359 case Primitive::kPrimDouble:
2360 case Primitive::kPrimFloat: {
2361 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002362 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002363 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002364 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002365 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002366
2367 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002368 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002369 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002370}
2371
2372void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2373 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002374 Location first = locations->InAt(0);
2375 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002376 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002377
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002378 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002379 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002380 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002381 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2382 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002383 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2384 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002385 } else {
2386 __ leal(out.AsRegister<CpuRegister>(), Address(
2387 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2388 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002389 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002390 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2391 __ addl(out.AsRegister<CpuRegister>(),
2392 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2393 } else {
2394 __ leal(out.AsRegister<CpuRegister>(), Address(
2395 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2396 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002397 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002398 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002399 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002400 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002401 break;
2402 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002403
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002404 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002405 if (second.IsRegister()) {
2406 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2407 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002408 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2409 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002410 } else {
2411 __ leaq(out.AsRegister<CpuRegister>(), Address(
2412 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2413 }
2414 } else {
2415 DCHECK(second.IsConstant());
2416 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2417 int32_t int32_value = Low32Bits(value);
2418 DCHECK_EQ(int32_value, value);
2419 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2420 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2421 } else {
2422 __ leaq(out.AsRegister<CpuRegister>(), Address(
2423 first.AsRegister<CpuRegister>(), int32_value));
2424 }
2425 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002426 break;
2427 }
2428
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002429 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002430 if (second.IsFpuRegister()) {
2431 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2432 } else if (second.IsConstant()) {
2433 __ addss(first.AsFpuRegister<XmmRegister>(),
2434 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2435 } else {
2436 DCHECK(second.IsStackSlot());
2437 __ addss(first.AsFpuRegister<XmmRegister>(),
2438 Address(CpuRegister(RSP), second.GetStackIndex()));
2439 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002440 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002441 }
2442
2443 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002444 if (second.IsFpuRegister()) {
2445 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2446 } else if (second.IsConstant()) {
2447 __ addsd(first.AsFpuRegister<XmmRegister>(),
2448 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2449 } else {
2450 DCHECK(second.IsDoubleStackSlot());
2451 __ addsd(first.AsFpuRegister<XmmRegister>(),
2452 Address(CpuRegister(RSP), second.GetStackIndex()));
2453 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002454 break;
2455 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002456
2457 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002458 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002459 }
2460}
2461
2462void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002463 LocationSummary* locations =
2464 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002465 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002466 case Primitive::kPrimInt: {
2467 locations->SetInAt(0, Location::RequiresRegister());
2468 locations->SetInAt(1, Location::Any());
2469 locations->SetOut(Location::SameAsFirstInput());
2470 break;
2471 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002472 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002473 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002474 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002475 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002476 break;
2477 }
Calin Juravle11351682014-10-23 15:38:15 +01002478 case Primitive::kPrimFloat:
2479 case Primitive::kPrimDouble: {
2480 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002481 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002482 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002483 break;
Calin Juravle11351682014-10-23 15:38:15 +01002484 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002485 default:
Calin Juravle11351682014-10-23 15:38:15 +01002486 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002487 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002488}
2489
2490void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2491 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002492 Location first = locations->InAt(0);
2493 Location second = locations->InAt(1);
2494 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002495 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002496 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002497 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002498 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002499 } else if (second.IsConstant()) {
2500 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002502 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002504 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002505 break;
2506 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002507 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002508 if (second.IsConstant()) {
2509 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2510 DCHECK(IsInt<32>(value));
2511 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2512 } else {
2513 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2514 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002515 break;
2516 }
2517
Calin Juravle11351682014-10-23 15:38:15 +01002518 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002519 if (second.IsFpuRegister()) {
2520 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2521 } else if (second.IsConstant()) {
2522 __ subss(first.AsFpuRegister<XmmRegister>(),
2523 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2524 } else {
2525 DCHECK(second.IsStackSlot());
2526 __ subss(first.AsFpuRegister<XmmRegister>(),
2527 Address(CpuRegister(RSP), second.GetStackIndex()));
2528 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002529 break;
Calin Juravle11351682014-10-23 15:38:15 +01002530 }
2531
2532 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002533 if (second.IsFpuRegister()) {
2534 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2535 } else if (second.IsConstant()) {
2536 __ subsd(first.AsFpuRegister<XmmRegister>(),
2537 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2538 } else {
2539 DCHECK(second.IsDoubleStackSlot());
2540 __ subsd(first.AsFpuRegister<XmmRegister>(),
2541 Address(CpuRegister(RSP), second.GetStackIndex()));
2542 }
Calin Juravle11351682014-10-23 15:38:15 +01002543 break;
2544 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002545
2546 default:
Calin Juravle11351682014-10-23 15:38:15 +01002547 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002548 }
2549}
2550
Calin Juravle34bacdf2014-10-07 20:23:36 +01002551void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2552 LocationSummary* locations =
2553 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2554 switch (mul->GetResultType()) {
2555 case Primitive::kPrimInt: {
2556 locations->SetInAt(0, Location::RequiresRegister());
2557 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002558 if (mul->InputAt(1)->IsIntConstant()) {
2559 // Can use 3 operand multiply.
2560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2561 } else {
2562 locations->SetOut(Location::SameAsFirstInput());
2563 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002564 break;
2565 }
2566 case Primitive::kPrimLong: {
2567 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002568 locations->SetInAt(1, Location::Any());
2569 if (mul->InputAt(1)->IsLongConstant() &&
2570 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002571 // Can use 3 operand multiply.
2572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2573 } else {
2574 locations->SetOut(Location::SameAsFirstInput());
2575 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002576 break;
2577 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002578 case Primitive::kPrimFloat:
2579 case Primitive::kPrimDouble: {
2580 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002581 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002582 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002583 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002584 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002585
2586 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002587 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002588 }
2589}
2590
2591void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2592 LocationSummary* locations = mul->GetLocations();
2593 Location first = locations->InAt(0);
2594 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002595 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002596 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002597 case Primitive::kPrimInt:
2598 // The constant may have ended up in a register, so test explicitly to avoid
2599 // problems where the output may not be the same as the first operand.
2600 if (mul->InputAt(1)->IsIntConstant()) {
2601 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2602 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2603 } else if (second.IsRegister()) {
2604 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002606 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002607 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002608 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002609 __ imull(first.AsRegister<CpuRegister>(),
2610 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002611 }
2612 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002613 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002614 // The constant may have ended up in a register, so test explicitly to avoid
2615 // problems where the output may not be the same as the first operand.
2616 if (mul->InputAt(1)->IsLongConstant()) {
2617 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2618 if (IsInt<32>(value)) {
2619 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2620 Immediate(static_cast<int32_t>(value)));
2621 } else {
2622 // Have to use the constant area.
2623 DCHECK(first.Equals(out));
2624 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2625 }
2626 } else if (second.IsRegister()) {
2627 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002628 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002629 } else {
2630 DCHECK(second.IsDoubleStackSlot());
2631 DCHECK(first.Equals(out));
2632 __ imulq(first.AsRegister<CpuRegister>(),
2633 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002634 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002635 break;
2636 }
2637
Calin Juravleb5bfa962014-10-21 18:02:24 +01002638 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002639 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002640 if (second.IsFpuRegister()) {
2641 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2642 } else if (second.IsConstant()) {
2643 __ mulss(first.AsFpuRegister<XmmRegister>(),
2644 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2645 } else {
2646 DCHECK(second.IsStackSlot());
2647 __ mulss(first.AsFpuRegister<XmmRegister>(),
2648 Address(CpuRegister(RSP), second.GetStackIndex()));
2649 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002651 }
2652
2653 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002654 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002655 if (second.IsFpuRegister()) {
2656 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2657 } else if (second.IsConstant()) {
2658 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2659 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2660 } else {
2661 DCHECK(second.IsDoubleStackSlot());
2662 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2663 Address(CpuRegister(RSP), second.GetStackIndex()));
2664 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002665 break;
2666 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002667
2668 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002669 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002670 }
2671}
2672
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002673void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2674 uint32_t stack_adjustment, bool is_float) {
2675 if (source.IsStackSlot()) {
2676 DCHECK(is_float);
2677 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2678 } else if (source.IsDoubleStackSlot()) {
2679 DCHECK(!is_float);
2680 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2681 } else {
2682 // Write the value to the temporary location on the stack and load to FP stack.
2683 if (is_float) {
2684 Location stack_temp = Location::StackSlot(temp_offset);
2685 codegen_->Move(stack_temp, source);
2686 __ flds(Address(CpuRegister(RSP), temp_offset));
2687 } else {
2688 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2689 codegen_->Move(stack_temp, source);
2690 __ fldl(Address(CpuRegister(RSP), temp_offset));
2691 }
2692 }
2693}
2694
2695void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2696 Primitive::Type type = rem->GetResultType();
2697 bool is_float = type == Primitive::kPrimFloat;
2698 size_t elem_size = Primitive::ComponentSize(type);
2699 LocationSummary* locations = rem->GetLocations();
2700 Location first = locations->InAt(0);
2701 Location second = locations->InAt(1);
2702 Location out = locations->Out();
2703
2704 // Create stack space for 2 elements.
2705 // TODO: enhance register allocator to ask for stack temporaries.
2706 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2707
2708 // Load the values to the FP stack in reverse order, using temporaries if needed.
2709 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2710 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2711
2712 // Loop doing FPREM until we stabilize.
2713 Label retry;
2714 __ Bind(&retry);
2715 __ fprem();
2716
2717 // Move FP status to AX.
2718 __ fstsw();
2719
2720 // And see if the argument reduction is complete. This is signaled by the
2721 // C2 FPU flag bit set to 0.
2722 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2723 __ j(kNotEqual, &retry);
2724
2725 // We have settled on the final value. Retrieve it into an XMM register.
2726 // Store FP top of stack to real stack.
2727 if (is_float) {
2728 __ fsts(Address(CpuRegister(RSP), 0));
2729 } else {
2730 __ fstl(Address(CpuRegister(RSP), 0));
2731 }
2732
2733 // Pop the 2 items from the FP stack.
2734 __ fucompp();
2735
2736 // Load the value from the stack into an XMM register.
2737 DCHECK(out.IsFpuRegister()) << out;
2738 if (is_float) {
2739 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2740 } else {
2741 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2742 }
2743
2744 // And remove the temporary stack space we allocated.
2745 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2746}
2747
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002748void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2749 DCHECK(instruction->IsDiv() || instruction->IsRem());
2750
2751 LocationSummary* locations = instruction->GetLocations();
2752 Location second = locations->InAt(1);
2753 DCHECK(second.IsConstant());
2754
2755 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2756 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002757 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002758
2759 DCHECK(imm == 1 || imm == -1);
2760
2761 switch (instruction->GetResultType()) {
2762 case Primitive::kPrimInt: {
2763 if (instruction->IsRem()) {
2764 __ xorl(output_register, output_register);
2765 } else {
2766 __ movl(output_register, input_register);
2767 if (imm == -1) {
2768 __ negl(output_register);
2769 }
2770 }
2771 break;
2772 }
2773
2774 case Primitive::kPrimLong: {
2775 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002776 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002777 } else {
2778 __ movq(output_register, input_register);
2779 if (imm == -1) {
2780 __ negq(output_register);
2781 }
2782 }
2783 break;
2784 }
2785
2786 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002787 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002788 }
2789}
2790
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002791void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002792 LocationSummary* locations = instruction->GetLocations();
2793 Location second = locations->InAt(1);
2794
2795 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2796 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2797
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002798 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002799
2800 DCHECK(IsPowerOfTwo(std::abs(imm)));
2801
2802 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2803
2804 if (instruction->GetResultType() == Primitive::kPrimInt) {
2805 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2806 __ testl(numerator, numerator);
2807 __ cmov(kGreaterEqual, tmp, numerator);
2808 int shift = CTZ(imm);
2809 __ sarl(tmp, Immediate(shift));
2810
2811 if (imm < 0) {
2812 __ negl(tmp);
2813 }
2814
2815 __ movl(output_register, tmp);
2816 } else {
2817 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2818 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2819
Mark Mendell92e83bf2015-05-07 11:25:03 -04002820 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002821 __ addq(rdx, numerator);
2822 __ testq(numerator, numerator);
2823 __ cmov(kGreaterEqual, rdx, numerator);
2824 int shift = CTZ(imm);
2825 __ sarq(rdx, Immediate(shift));
2826
2827 if (imm < 0) {
2828 __ negq(rdx);
2829 }
2830
2831 __ movq(output_register, rdx);
2832 }
2833}
2834
2835void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2836 DCHECK(instruction->IsDiv() || instruction->IsRem());
2837
2838 LocationSummary* locations = instruction->GetLocations();
2839 Location second = locations->InAt(1);
2840
2841 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2842 : locations->GetTemp(0).AsRegister<CpuRegister>();
2843 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2844 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2845 : locations->Out().AsRegister<CpuRegister>();
2846 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2847
2848 DCHECK_EQ(RAX, eax.AsRegister());
2849 DCHECK_EQ(RDX, edx.AsRegister());
2850 if (instruction->IsDiv()) {
2851 DCHECK_EQ(RAX, out.AsRegister());
2852 } else {
2853 DCHECK_EQ(RDX, out.AsRegister());
2854 }
2855
2856 int64_t magic;
2857 int shift;
2858
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002859 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002860 if (instruction->GetResultType() == Primitive::kPrimInt) {
2861 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2862
2863 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2864
2865 __ movl(numerator, eax);
2866
2867 Label no_div;
2868 Label end;
2869 __ testl(eax, eax);
2870 __ j(kNotEqual, &no_div);
2871
2872 __ xorl(out, out);
2873 __ jmp(&end);
2874
2875 __ Bind(&no_div);
2876
2877 __ movl(eax, Immediate(magic));
2878 __ imull(numerator);
2879
2880 if (imm > 0 && magic < 0) {
2881 __ addl(edx, numerator);
2882 } else if (imm < 0 && magic > 0) {
2883 __ subl(edx, numerator);
2884 }
2885
2886 if (shift != 0) {
2887 __ sarl(edx, Immediate(shift));
2888 }
2889
2890 __ movl(eax, edx);
2891 __ shrl(edx, Immediate(31));
2892 __ addl(edx, eax);
2893
2894 if (instruction->IsRem()) {
2895 __ movl(eax, numerator);
2896 __ imull(edx, Immediate(imm));
2897 __ subl(eax, edx);
2898 __ movl(edx, eax);
2899 } else {
2900 __ movl(eax, edx);
2901 }
2902 __ Bind(&end);
2903 } else {
2904 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2905
2906 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2907
2908 CpuRegister rax = eax;
2909 CpuRegister rdx = edx;
2910
2911 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2912
2913 // Save the numerator.
2914 __ movq(numerator, rax);
2915
2916 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002917 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002918
2919 // RDX:RAX = magic * numerator
2920 __ imulq(numerator);
2921
2922 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002923 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002924 __ addq(rdx, numerator);
2925 } else if (imm < 0 && magic > 0) {
2926 // RDX -= numerator
2927 __ subq(rdx, numerator);
2928 }
2929
2930 // Shift if needed.
2931 if (shift != 0) {
2932 __ sarq(rdx, Immediate(shift));
2933 }
2934
2935 // RDX += 1 if RDX < 0
2936 __ movq(rax, rdx);
2937 __ shrq(rdx, Immediate(63));
2938 __ addq(rdx, rax);
2939
2940 if (instruction->IsRem()) {
2941 __ movq(rax, numerator);
2942
2943 if (IsInt<32>(imm)) {
2944 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2945 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002946 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002947 }
2948
2949 __ subq(rax, rdx);
2950 __ movq(rdx, rax);
2951 } else {
2952 __ movq(rax, rdx);
2953 }
2954 }
2955}
2956
Calin Juravlebacfec32014-11-14 15:54:36 +00002957void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2958 DCHECK(instruction->IsDiv() || instruction->IsRem());
2959 Primitive::Type type = instruction->GetResultType();
2960 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2961
2962 bool is_div = instruction->IsDiv();
2963 LocationSummary* locations = instruction->GetLocations();
2964
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002965 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2966 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002967
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002969 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002970
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002971 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002972 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002973
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002974 if (imm == 0) {
2975 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2976 } else if (imm == 1 || imm == -1) {
2977 DivRemOneOrMinusOne(instruction);
2978 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002979 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002980 } else {
2981 DCHECK(imm <= -2 || imm >= 2);
2982 GenerateDivRemWithAnyConstant(instruction);
2983 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002984 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002985 SlowPathCodeX86_64* slow_path =
2986 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2987 out.AsRegister(), type, is_div);
2988 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002989
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002990 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2991 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2992 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2993 // so it's safe to just use negl instead of more complex comparisons.
2994 if (type == Primitive::kPrimInt) {
2995 __ cmpl(second_reg, Immediate(-1));
2996 __ j(kEqual, slow_path->GetEntryLabel());
2997 // edx:eax <- sign-extended of eax
2998 __ cdq();
2999 // eax = quotient, edx = remainder
3000 __ idivl(second_reg);
3001 } else {
3002 __ cmpq(second_reg, Immediate(-1));
3003 __ j(kEqual, slow_path->GetEntryLabel());
3004 // rdx:rax <- sign-extended of rax
3005 __ cqo();
3006 // rax = quotient, rdx = remainder
3007 __ idivq(second_reg);
3008 }
3009 __ Bind(slow_path->GetExitLabel());
3010 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003011}
3012
Calin Juravle7c4954d2014-10-28 16:57:40 +00003013void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3014 LocationSummary* locations =
3015 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3016 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003017 case Primitive::kPrimInt:
3018 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003019 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003020 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003021 locations->SetOut(Location::SameAsFirstInput());
3022 // Intel uses edx:eax as the dividend.
3023 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003024 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3025 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3026 // output and request another temp.
3027 if (div->InputAt(1)->IsConstant()) {
3028 locations->AddTemp(Location::RequiresRegister());
3029 }
Calin Juravled0d48522014-11-04 16:40:20 +00003030 break;
3031 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003032
Calin Juravle7c4954d2014-10-28 16:57:40 +00003033 case Primitive::kPrimFloat:
3034 case Primitive::kPrimDouble: {
3035 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003036 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003037 locations->SetOut(Location::SameAsFirstInput());
3038 break;
3039 }
3040
3041 default:
3042 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3043 }
3044}
3045
3046void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3047 LocationSummary* locations = div->GetLocations();
3048 Location first = locations->InAt(0);
3049 Location second = locations->InAt(1);
3050 DCHECK(first.Equals(locations->Out()));
3051
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003052 Primitive::Type type = div->GetResultType();
3053 switch (type) {
3054 case Primitive::kPrimInt:
3055 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003056 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003057 break;
3058 }
3059
Calin Juravle7c4954d2014-10-28 16:57:40 +00003060 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003061 if (second.IsFpuRegister()) {
3062 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3063 } else if (second.IsConstant()) {
3064 __ divss(first.AsFpuRegister<XmmRegister>(),
3065 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3066 } else {
3067 DCHECK(second.IsStackSlot());
3068 __ divss(first.AsFpuRegister<XmmRegister>(),
3069 Address(CpuRegister(RSP), second.GetStackIndex()));
3070 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003071 break;
3072 }
3073
3074 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003075 if (second.IsFpuRegister()) {
3076 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3077 } else if (second.IsConstant()) {
3078 __ divsd(first.AsFpuRegister<XmmRegister>(),
3079 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3080 } else {
3081 DCHECK(second.IsDoubleStackSlot());
3082 __ divsd(first.AsFpuRegister<XmmRegister>(),
3083 Address(CpuRegister(RSP), second.GetStackIndex()));
3084 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003085 break;
3086 }
3087
3088 default:
3089 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3090 }
3091}
3092
Calin Juravlebacfec32014-11-14 15:54:36 +00003093void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003094 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003097
3098 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 case Primitive::kPrimInt:
3100 case Primitive::kPrimLong: {
3101 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003102 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003103 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3104 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003105 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3106 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3107 // output and request another temp.
3108 if (rem->InputAt(1)->IsConstant()) {
3109 locations->AddTemp(Location::RequiresRegister());
3110 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003111 break;
3112 }
3113
3114 case Primitive::kPrimFloat:
3115 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003116 locations->SetInAt(0, Location::Any());
3117 locations->SetInAt(1, Location::Any());
3118 locations->SetOut(Location::RequiresFpuRegister());
3119 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003120 break;
3121 }
3122
3123 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003124 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003125 }
3126}
3127
3128void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3129 Primitive::Type type = rem->GetResultType();
3130 switch (type) {
3131 case Primitive::kPrimInt:
3132 case Primitive::kPrimLong: {
3133 GenerateDivRemIntegral(rem);
3134 break;
3135 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003136 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003137 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003138 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003139 break;
3140 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003141 default:
3142 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3143 }
3144}
3145
Calin Juravled0d48522014-11-04 16:40:20 +00003146void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3147 LocationSummary* locations =
3148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3149 locations->SetInAt(0, Location::Any());
3150 if (instruction->HasUses()) {
3151 locations->SetOut(Location::SameAsFirstInput());
3152 }
3153}
3154
3155void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3156 SlowPathCodeX86_64* slow_path =
3157 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3158 codegen_->AddSlowPath(slow_path);
3159
3160 LocationSummary* locations = instruction->GetLocations();
3161 Location value = locations->InAt(0);
3162
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003163 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003164 case Primitive::kPrimByte:
3165 case Primitive::kPrimChar:
3166 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003167 case Primitive::kPrimInt: {
3168 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003169 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003170 __ j(kEqual, slow_path->GetEntryLabel());
3171 } else if (value.IsStackSlot()) {
3172 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3173 __ j(kEqual, slow_path->GetEntryLabel());
3174 } else {
3175 DCHECK(value.IsConstant()) << value;
3176 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3177 __ jmp(slow_path->GetEntryLabel());
3178 }
3179 }
3180 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003181 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003182 case Primitive::kPrimLong: {
3183 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003184 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003185 __ j(kEqual, slow_path->GetEntryLabel());
3186 } else if (value.IsDoubleStackSlot()) {
3187 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3188 __ j(kEqual, slow_path->GetEntryLabel());
3189 } else {
3190 DCHECK(value.IsConstant()) << value;
3191 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3192 __ jmp(slow_path->GetEntryLabel());
3193 }
3194 }
3195 break;
3196 }
3197 default:
3198 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003199 }
Calin Juravled0d48522014-11-04 16:40:20 +00003200}
3201
Calin Juravle9aec02f2014-11-18 23:06:35 +00003202void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3203 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3204
3205 LocationSummary* locations =
3206 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3207
3208 switch (op->GetResultType()) {
3209 case Primitive::kPrimInt:
3210 case Primitive::kPrimLong: {
3211 locations->SetInAt(0, Location::RequiresRegister());
3212 // The shift count needs to be in CL.
3213 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3214 locations->SetOut(Location::SameAsFirstInput());
3215 break;
3216 }
3217 default:
3218 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3219 }
3220}
3221
3222void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3223 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3224
3225 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003227 Location second = locations->InAt(1);
3228
3229 switch (op->GetResultType()) {
3230 case Primitive::kPrimInt: {
3231 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003232 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003233 if (op->IsShl()) {
3234 __ shll(first_reg, second_reg);
3235 } else if (op->IsShr()) {
3236 __ sarl(first_reg, second_reg);
3237 } else {
3238 __ shrl(first_reg, second_reg);
3239 }
3240 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003241 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003242 if (op->IsShl()) {
3243 __ shll(first_reg, imm);
3244 } else if (op->IsShr()) {
3245 __ sarl(first_reg, imm);
3246 } else {
3247 __ shrl(first_reg, imm);
3248 }
3249 }
3250 break;
3251 }
3252 case Primitive::kPrimLong: {
3253 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003255 if (op->IsShl()) {
3256 __ shlq(first_reg, second_reg);
3257 } else if (op->IsShr()) {
3258 __ sarq(first_reg, second_reg);
3259 } else {
3260 __ shrq(first_reg, second_reg);
3261 }
3262 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003263 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003264 if (op->IsShl()) {
3265 __ shlq(first_reg, imm);
3266 } else if (op->IsShr()) {
3267 __ sarq(first_reg, imm);
3268 } else {
3269 __ shrq(first_reg, imm);
3270 }
3271 }
3272 break;
3273 }
3274 default:
3275 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3276 }
3277}
3278
3279void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3280 HandleShift(shl);
3281}
3282
3283void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3284 HandleShift(shl);
3285}
3286
3287void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3288 HandleShift(shr);
3289}
3290
3291void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3292 HandleShift(shr);
3293}
3294
3295void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3296 HandleShift(ushr);
3297}
3298
3299void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3300 HandleShift(ushr);
3301}
3302
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003303void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003304 LocationSummary* locations =
3305 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003307 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003308 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003309 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003310}
3311
3312void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3313 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003314 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3315 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003316 // Note: if heap poisoning is enabled, the entry point takes cares
3317 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003318
3319 codegen_->InvokeRuntime(
3320 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3321 instruction,
3322 instruction->GetDexPc(),
3323 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003324
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003325 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003326}
3327
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003328void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3329 LocationSummary* locations =
3330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3331 InvokeRuntimeCallingConvention calling_convention;
3332 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003333 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003334 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003335 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003336}
3337
3338void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3339 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003340 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3341 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003342
Roland Levillain4d027112015-07-01 15:41:14 +01003343 // Note: if heap poisoning is enabled, the entry point takes cares
3344 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003345 codegen_->InvokeRuntime(
3346 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3347 instruction,
3348 instruction->GetDexPc(),
3349 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003350
3351 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003352}
3353
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003354void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003355 LocationSummary* locations =
3356 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003357 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3358 if (location.IsStackSlot()) {
3359 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3360 } else if (location.IsDoubleStackSlot()) {
3361 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3362 }
3363 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003364}
3365
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003366void InstructionCodeGeneratorX86_64::VisitParameterValue(
3367 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003368 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003369}
3370
3371void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3374 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3375}
3376
3377void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3378 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3379 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003380}
3381
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003382void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003383 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003384 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003385 locations->SetInAt(0, Location::RequiresRegister());
3386 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003387}
3388
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003389void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3390 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3392 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003393 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003394 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003395 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003396 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003397 break;
3398
3399 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003400 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003401 break;
3402
3403 default:
3404 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3405 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003406}
3407
David Brazdil66d126e2015-04-03 16:02:44 +01003408void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3409 LocationSummary* locations =
3410 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3411 locations->SetInAt(0, Location::RequiresRegister());
3412 locations->SetOut(Location::SameAsFirstInput());
3413}
3414
3415void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003416 LocationSummary* locations = bool_not->GetLocations();
3417 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3418 locations->Out().AsRegister<CpuRegister>().AsRegister());
3419 Location out = locations->Out();
3420 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3421}
3422
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003423void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003424 LocationSummary* locations =
3425 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003426 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3427 locations->SetInAt(i, Location::Any());
3428 }
3429 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003430}
3431
3432void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003433 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003434 LOG(FATAL) << "Unimplemented";
3435}
3436
Calin Juravle52c48962014-12-16 17:02:57 +00003437void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3438 /*
3439 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3440 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3441 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3442 */
3443 switch (kind) {
3444 case MemBarrierKind::kAnyAny: {
3445 __ mfence();
3446 break;
3447 }
3448 case MemBarrierKind::kAnyStore:
3449 case MemBarrierKind::kLoadAny:
3450 case MemBarrierKind::kStoreStore: {
3451 // nop
3452 break;
3453 }
3454 default:
3455 LOG(FATAL) << "Unexpected memory barier " << kind;
3456 }
3457}
3458
3459void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3460 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3461
Nicolas Geoffray39468442014-09-02 15:17:15 +01003462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003464 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003465 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3466 locations->SetOut(Location::RequiresFpuRegister());
3467 } else {
3468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3469 }
Calin Juravle52c48962014-12-16 17:02:57 +00003470}
3471
3472void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3473 const FieldInfo& field_info) {
3474 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3475
3476 LocationSummary* locations = instruction->GetLocations();
3477 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3478 Location out = locations->Out();
3479 bool is_volatile = field_info.IsVolatile();
3480 Primitive::Type field_type = field_info.GetFieldType();
3481 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3482
3483 switch (field_type) {
3484 case Primitive::kPrimBoolean: {
3485 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3486 break;
3487 }
3488
3489 case Primitive::kPrimByte: {
3490 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3491 break;
3492 }
3493
3494 case Primitive::kPrimShort: {
3495 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3496 break;
3497 }
3498
3499 case Primitive::kPrimChar: {
3500 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3501 break;
3502 }
3503
3504 case Primitive::kPrimInt:
3505 case Primitive::kPrimNot: {
3506 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3507 break;
3508 }
3509
3510 case Primitive::kPrimLong: {
3511 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3512 break;
3513 }
3514
3515 case Primitive::kPrimFloat: {
3516 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3517 break;
3518 }
3519
3520 case Primitive::kPrimDouble: {
3521 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3522 break;
3523 }
3524
3525 case Primitive::kPrimVoid:
3526 LOG(FATAL) << "Unreachable type " << field_type;
3527 UNREACHABLE();
3528 }
3529
Calin Juravle77520bc2015-01-12 18:45:46 +00003530 codegen_->MaybeRecordImplicitNullCheck(instruction);
3531
Calin Juravle52c48962014-12-16 17:02:57 +00003532 if (is_volatile) {
3533 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3534 }
Roland Levillain4d027112015-07-01 15:41:14 +01003535
3536 if (field_type == Primitive::kPrimNot) {
3537 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3538 }
Calin Juravle52c48962014-12-16 17:02:57 +00003539}
3540
3541void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3542 const FieldInfo& field_info) {
3543 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3544
3545 LocationSummary* locations =
3546 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003547 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003548 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003549 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003550
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003551 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003552 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3553 locations->SetInAt(1, Location::RequiresFpuRegister());
3554 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003555 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003556 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003557 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003558 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003559 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003560 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003561 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3562 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003563 locations->AddTemp(Location::RequiresRegister());
3564 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003565}
3566
Calin Juravle52c48962014-12-16 17:02:57 +00003567void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003568 const FieldInfo& field_info,
3569 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003570 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3571
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003572 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003573 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3574 Location value = locations->InAt(1);
3575 bool is_volatile = field_info.IsVolatile();
3576 Primitive::Type field_type = field_info.GetFieldType();
3577 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3578
3579 if (is_volatile) {
3580 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3581 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003582
3583 switch (field_type) {
3584 case Primitive::kPrimBoolean:
3585 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003586 if (value.IsConstant()) {
3587 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3588 __ movb(Address(base, offset), Immediate(v));
3589 } else {
3590 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3591 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003592 break;
3593 }
3594
3595 case Primitive::kPrimShort:
3596 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003597 if (value.IsConstant()) {
3598 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3599 __ movw(Address(base, offset), Immediate(v));
3600 } else {
3601 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3602 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003603 break;
3604 }
3605
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003607 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003608 if (value.IsConstant()) {
3609 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003610 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3611 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3612 // Note: if heap poisoning is enabled, no need to poison
3613 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003614 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003615 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003616 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3617 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3618 __ movl(temp, value.AsRegister<CpuRegister>());
3619 __ PoisonHeapReference(temp);
3620 __ movl(Address(base, offset), temp);
3621 } else {
3622 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3623 }
Mark Mendell40741f32015-04-20 22:10:34 -04003624 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003625 break;
3626 }
3627
3628 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003629 if (value.IsConstant()) {
3630 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3631 DCHECK(IsInt<32>(v));
3632 int32_t v_32 = v;
3633 __ movq(Address(base, offset), Immediate(v_32));
3634 } else {
3635 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3636 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003637 break;
3638 }
3639
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003640 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003641 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003642 break;
3643 }
3644
3645 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003646 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003647 break;
3648 }
3649
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003650 case Primitive::kPrimVoid:
3651 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003652 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003653 }
Calin Juravle52c48962014-12-16 17:02:57 +00003654
Calin Juravle77520bc2015-01-12 18:45:46 +00003655 codegen_->MaybeRecordImplicitNullCheck(instruction);
3656
3657 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3658 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3659 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003660 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003661 }
3662
Calin Juravle52c48962014-12-16 17:02:57 +00003663 if (is_volatile) {
3664 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3665 }
3666}
3667
3668void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3669 HandleFieldSet(instruction, instruction->GetFieldInfo());
3670}
3671
3672void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003673 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003674}
3675
3676void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003677 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003678}
3679
3680void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003681 HandleFieldGet(instruction, instruction->GetFieldInfo());
3682}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003683
Calin Juravle52c48962014-12-16 17:02:57 +00003684void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3685 HandleFieldGet(instruction);
3686}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003687
Calin Juravle52c48962014-12-16 17:02:57 +00003688void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3689 HandleFieldGet(instruction, instruction->GetFieldInfo());
3690}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003691
Calin Juravle52c48962014-12-16 17:02:57 +00003692void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3693 HandleFieldSet(instruction, instruction->GetFieldInfo());
3694}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695
Calin Juravle52c48962014-12-16 17:02:57 +00003696void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003697 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003698}
3699
3700void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003701 LocationSummary* locations =
3702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003703 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3704 ? Location::RequiresRegister()
3705 : Location::Any();
3706 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003707 if (instruction->HasUses()) {
3708 locations->SetOut(Location::SameAsFirstInput());
3709 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003710}
3711
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003712void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003713 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3714 return;
3715 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003716 LocationSummary* locations = instruction->GetLocations();
3717 Location obj = locations->InAt(0);
3718
3719 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3720 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3721}
3722
3723void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003724 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003725 codegen_->AddSlowPath(slow_path);
3726
3727 LocationSummary* locations = instruction->GetLocations();
3728 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003729
3730 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003731 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003732 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003734 } else {
3735 DCHECK(obj.IsConstant()) << obj;
3736 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3737 __ jmp(slow_path->GetEntryLabel());
3738 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003739 }
3740 __ j(kEqual, slow_path->GetEntryLabel());
3741}
3742
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003743void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3744 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3745 GenerateImplicitNullCheck(instruction);
3746 } else {
3747 GenerateExplicitNullCheck(instruction);
3748 }
3749}
3750
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003751void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003752 LocationSummary* locations =
3753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003754 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003755 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003756 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3757 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3758 } else {
3759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3760 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761}
3762
3763void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3764 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003765 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003766 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003767 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003768
Roland Levillain4d027112015-07-01 15:41:14 +01003769 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 case Primitive::kPrimBoolean: {
3771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003773 if (index.IsConstant()) {
3774 __ movzxb(out, Address(obj,
3775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3776 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 }
3779 break;
3780 }
3781
3782 case Primitive::kPrimByte: {
3783 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785 if (index.IsConstant()) {
3786 __ movsxb(out, Address(obj,
3787 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3788 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790 }
3791 break;
3792 }
3793
3794 case Primitive::kPrimShort: {
3795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003797 if (index.IsConstant()) {
3798 __ movsxw(out, Address(obj,
3799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 }
3803 break;
3804 }
3805
3806 case Primitive::kPrimChar: {
3807 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003808 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003809 if (index.IsConstant()) {
3810 __ movzxw(out, Address(obj,
3811 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3812 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003813 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 }
3815 break;
3816 }
3817
3818 case Primitive::kPrimInt:
3819 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003820 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3821 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824 if (index.IsConstant()) {
3825 __ movl(out, Address(obj,
3826 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3827 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003829 }
3830 break;
3831 }
3832
3833 case Primitive::kPrimLong: {
3834 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003836 if (index.IsConstant()) {
3837 __ movq(out, Address(obj,
3838 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3839 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003840 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003841 }
3842 break;
3843 }
3844
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003845 case Primitive::kPrimFloat: {
3846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003847 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003848 if (index.IsConstant()) {
3849 __ movss(out, Address(obj,
3850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003853 }
3854 break;
3855 }
3856
3857 case Primitive::kPrimDouble: {
3858 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003859 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003860 if (index.IsConstant()) {
3861 __ movsd(out, Address(obj,
3862 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3863 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003865 }
3866 break;
3867 }
3868
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003870 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003871 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003872 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003873 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003874
3875 if (type == Primitive::kPrimNot) {
3876 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3877 __ MaybeUnpoisonHeapReference(out);
3878 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879}
3880
3881void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003882 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003883
3884 bool needs_write_barrier =
3885 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3886 bool needs_runtime_call = instruction->NeedsTypeCheck();
3887
Nicolas Geoffray39468442014-09-02 15:17:15 +01003888 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003889 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3890 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003891 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003892 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3893 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3894 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003895 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003896 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003897 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003898 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3899 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003900 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003901 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003902 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3903 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003904 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003905 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003906 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003907
3908 if (needs_write_barrier) {
3909 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003910 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003911 locations->AddTemp(Location::RequiresRegister());
3912 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914}
3915
3916void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3917 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003918 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003919 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003920 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003921 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003922 bool needs_runtime_call = locations->WillCall();
3923 bool needs_write_barrier =
3924 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003925
3926 switch (value_type) {
3927 case Primitive::kPrimBoolean:
3928 case Primitive::kPrimByte: {
3929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930 if (index.IsConstant()) {
3931 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003932 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003934 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003935 __ movb(Address(obj, offset),
3936 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003937 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003939 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3941 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003944 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003947 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003948 break;
3949 }
3950
3951 case Primitive::kPrimShort:
3952 case Primitive::kPrimChar: {
3953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003954 if (index.IsConstant()) {
3955 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003956 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003957 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003958 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003959 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003960 __ movw(Address(obj, offset),
3961 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003962 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003964 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003965 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003966 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3967 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003968 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003969 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003970 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003971 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3972 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975 break;
3976 }
3977
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003978 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003980 if (!needs_runtime_call) {
3981 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3982 if (index.IsConstant()) {
3983 size_t offset =
3984 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3985 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003986 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3987 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3988 __ movl(temp, value.AsRegister<CpuRegister>());
3989 __ PoisonHeapReference(temp);
3990 __ movl(Address(obj, offset), temp);
3991 } else {
3992 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
3993 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003994 } else {
3995 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003996 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003997 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3998 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3999 // Note: if heap poisoning is enabled, no need to poison
4000 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004001 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004002 }
4003 } else {
4004 DCHECK(index.IsRegister()) << index;
4005 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004006 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4007 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4008 __ movl(temp, value.AsRegister<CpuRegister>());
4009 __ PoisonHeapReference(temp);
4010 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4011 } else {
4012 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4013 value.AsRegister<CpuRegister>());
4014 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004015 } else {
4016 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004017 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004018 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4019 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4020 // Note: if heap poisoning is enabled, no need to poison
4021 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004023 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004024 }
4025 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004026 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004027 if (needs_write_barrier) {
4028 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004029 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4030 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004031 codegen_->MarkGCCard(
4032 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004033 }
4034 } else {
4035 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004036 // Note: if heap poisoning is enabled, pAputObject takes cares
4037 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004038 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4039 instruction,
4040 instruction->GetDexPc(),
4041 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004042 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004043 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004044 break;
4045 }
4046
4047 case Primitive::kPrimLong: {
4048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004049 if (index.IsConstant()) {
4050 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004051 if (value.IsRegister()) {
4052 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4053 } else {
4054 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4055 DCHECK(IsInt<32>(v));
4056 int32_t v_32 = v;
4057 __ movq(Address(obj, offset), Immediate(v_32));
4058 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004059 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004060 if (value.IsRegister()) {
4061 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4062 value.AsRegister<CpuRegister>());
4063 } else {
4064 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4065 DCHECK(IsInt<32>(v));
4066 int32_t v_32 = v;
4067 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4068 Immediate(v_32));
4069 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004070 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004071 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072 break;
4073 }
4074
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004075 case Primitive::kPrimFloat: {
4076 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4077 if (index.IsConstant()) {
4078 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4079 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004081 } else {
4082 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004083 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4084 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004085 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004086 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004087 break;
4088 }
4089
4090 case Primitive::kPrimDouble: {
4091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4092 if (index.IsConstant()) {
4093 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4094 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004096 } else {
4097 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4099 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004100 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004101 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004102 break;
4103 }
4104
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 case Primitive::kPrimVoid:
4106 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004107 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108 }
4109}
4110
4111void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004112 LocationSummary* locations =
4113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004116}
4117
4118void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4119 LocationSummary* locations = instruction->GetLocations();
4120 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4122 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004123 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004124 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125}
4126
4127void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004128 LocationSummary* locations =
4129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004130 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004131 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004132 if (instruction->HasUses()) {
4133 locations->SetOut(Location::SameAsFirstInput());
4134 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135}
4136
4137void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4138 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004139 Location index_loc = locations->InAt(0);
4140 Location length_loc = locations->InAt(1);
4141 SlowPathCodeX86_64* slow_path =
4142 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143
Mark Mendell99dbd682015-04-22 16:18:52 -04004144 if (length_loc.IsConstant()) {
4145 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4146 if (index_loc.IsConstant()) {
4147 // BCE will remove the bounds check if we are guarenteed to pass.
4148 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4149 if (index < 0 || index >= length) {
4150 codegen_->AddSlowPath(slow_path);
4151 __ jmp(slow_path->GetEntryLabel());
4152 } else {
4153 // Some optimization after BCE may have generated this, and we should not
4154 // generate a bounds check if it is a valid range.
4155 }
4156 return;
4157 }
4158
4159 // We have to reverse the jump condition because the length is the constant.
4160 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4161 __ cmpl(index_reg, Immediate(length));
4162 codegen_->AddSlowPath(slow_path);
4163 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004164 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004165 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4166 if (index_loc.IsConstant()) {
4167 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4168 __ cmpl(length, Immediate(value));
4169 } else {
4170 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4171 }
4172 codegen_->AddSlowPath(slow_path);
4173 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004174 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004175}
4176
4177void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4178 CpuRegister card,
4179 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004180 CpuRegister value,
4181 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004183 if (value_can_be_null) {
4184 __ testl(value, value);
4185 __ j(kEqual, &is_null);
4186 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004187 __ gs()->movq(card, Address::Absolute(
4188 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4189 __ movq(temp, object);
4190 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004191 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004192 if (value_can_be_null) {
4193 __ Bind(&is_null);
4194 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004195}
4196
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004197void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4198 temp->SetLocations(nullptr);
4199}
4200
4201void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4202 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004203 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004204}
4205
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004206void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004207 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004208 LOG(FATAL) << "Unimplemented";
4209}
4210
4211void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004212 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4213}
4214
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004215void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4216 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4217}
4218
4219void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004220 HBasicBlock* block = instruction->GetBlock();
4221 if (block->GetLoopInformation() != nullptr) {
4222 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4223 // The back edge will generate the suspend check.
4224 return;
4225 }
4226 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4227 // The goto will generate the suspend check.
4228 return;
4229 }
4230 GenerateSuspendCheck(instruction, nullptr);
4231}
4232
4233void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4234 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004235 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004236 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4237 if (slow_path == nullptr) {
4238 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4239 instruction->SetSlowPath(slow_path);
4240 codegen_->AddSlowPath(slow_path);
4241 if (successor != nullptr) {
4242 DCHECK(successor->IsLoopHeader());
4243 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4244 }
4245 } else {
4246 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4247 }
4248
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004249 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004250 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004251 if (successor == nullptr) {
4252 __ j(kNotEqual, slow_path->GetEntryLabel());
4253 __ Bind(slow_path->GetReturnLabel());
4254 } else {
4255 __ j(kEqual, codegen_->GetLabelOf(successor));
4256 __ jmp(slow_path->GetEntryLabel());
4257 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004258}
4259
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004260X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4261 return codegen_->GetAssembler();
4262}
4263
4264void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4265 MoveOperands* move = moves_.Get(index);
4266 Location source = move->GetSource();
4267 Location destination = move->GetDestination();
4268
4269 if (source.IsRegister()) {
4270 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004271 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004272 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004273 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004274 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004275 } else {
4276 DCHECK(destination.IsDoubleStackSlot());
4277 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004278 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004279 }
4280 } else if (source.IsStackSlot()) {
4281 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004282 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004283 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004284 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004285 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004286 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004287 } else {
4288 DCHECK(destination.IsStackSlot());
4289 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4290 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4291 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004292 } else if (source.IsDoubleStackSlot()) {
4293 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004294 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004295 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004296 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004297 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4298 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004299 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004300 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004301 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4302 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4303 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004304 } else if (source.IsConstant()) {
4305 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004306 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4307 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004308 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004309 if (value == 0) {
4310 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4311 } else {
4312 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4313 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004314 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004315 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004316 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004317 }
4318 } else if (constant->IsLongConstant()) {
4319 int64_t value = constant->AsLongConstant()->GetValue();
4320 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004321 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004322 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004323 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004324 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004325 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004326 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004327 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004328 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004329 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004330 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4331 if (value == 0) {
4332 // easy FP 0.0.
4333 __ xorps(dest, dest);
4334 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004335 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004336 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004337 } else {
4338 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004339 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004340 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4341 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004342 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004343 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004344 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004345 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004346 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004347 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4348 if (value == 0) {
4349 __ xorpd(dest, dest);
4350 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004351 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004352 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004353 } else {
4354 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004355 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004356 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004357 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004358 } else if (source.IsFpuRegister()) {
4359 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004361 } else if (destination.IsStackSlot()) {
4362 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004363 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004364 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004365 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004366 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004367 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004368 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004369 }
4370}
4371
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004372void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004373 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004374 __ movl(Address(CpuRegister(RSP), mem), reg);
4375 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004376}
4377
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004378void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004379 ScratchRegisterScope ensure_scratch(
4380 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4381
4382 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4383 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4384 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4385 Address(CpuRegister(RSP), mem2 + stack_offset));
4386 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4387 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4388 CpuRegister(ensure_scratch.GetRegister()));
4389}
4390
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004391void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4392 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4393 __ movq(Address(CpuRegister(RSP), mem), reg);
4394 __ movq(reg, CpuRegister(TMP));
4395}
4396
4397void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4398 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004399 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004400
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004401 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4402 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4403 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4404 Address(CpuRegister(RSP), mem2 + stack_offset));
4405 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4406 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4407 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004408}
4409
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004410void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4411 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4412 __ movss(Address(CpuRegister(RSP), mem), reg);
4413 __ movd(reg, CpuRegister(TMP));
4414}
4415
4416void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4417 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4418 __ movsd(Address(CpuRegister(RSP), mem), reg);
4419 __ movd(reg, CpuRegister(TMP));
4420}
4421
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004422void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4423 MoveOperands* move = moves_.Get(index);
4424 Location source = move->GetSource();
4425 Location destination = move->GetDestination();
4426
4427 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004428 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004429 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004430 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004431 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004432 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004433 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004434 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4435 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004436 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004437 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004438 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004439 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4440 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004441 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4443 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4444 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004445 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004446 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004447 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004448 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004449 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004450 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004451 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004452 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004453 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004454 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004455 }
4456}
4457
4458
4459void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4460 __ pushq(CpuRegister(reg));
4461}
4462
4463
4464void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4465 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004466}
4467
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004468void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4469 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4470 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4471 Immediate(mirror::Class::kStatusInitialized));
4472 __ j(kLess, slow_path->GetEntryLabel());
4473 __ Bind(slow_path->GetExitLabel());
4474 // No need for memory fence, thanks to the X86_64 memory model.
4475}
4476
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004477void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004478 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4479 ? LocationSummary::kCallOnSlowPath
4480 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004481 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004482 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004483 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004484 locations->SetOut(Location::RequiresRegister());
4485}
4486
4487void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004488 LocationSummary* locations = cls->GetLocations();
4489 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4490 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004491 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004492 DCHECK(!cls->CanCallRuntime());
4493 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004494 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004495 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004496 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004497 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004498 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004499 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004500 __ MaybeUnpoisonHeapReference(out);
4501
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004502 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4503 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4504 codegen_->AddSlowPath(slow_path);
4505 __ testl(out, out);
4506 __ j(kEqual, slow_path->GetEntryLabel());
4507 if (cls->MustGenerateClinitCheck()) {
4508 GenerateClassInitializationCheck(slow_path, out);
4509 } else {
4510 __ Bind(slow_path->GetExitLabel());
4511 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004512 }
4513}
4514
4515void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4516 LocationSummary* locations =
4517 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4518 locations->SetInAt(0, Location::RequiresRegister());
4519 if (check->HasUses()) {
4520 locations->SetOut(Location::SameAsFirstInput());
4521 }
4522}
4523
4524void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004525 // We assume the class to not be null.
4526 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4527 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004528 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004529 GenerateClassInitializationCheck(slow_path,
4530 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004531}
4532
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004533void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4534 LocationSummary* locations =
4535 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004536 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004537 locations->SetOut(Location::RequiresRegister());
4538}
4539
4540void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4541 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4542 codegen_->AddSlowPath(slow_path);
4543
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004544 LocationSummary* locations = load->GetLocations();
4545 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4546 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004547 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004548 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004549 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004550 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004551 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004552 __ testl(out, out);
4553 __ j(kEqual, slow_path->GetEntryLabel());
4554 __ Bind(slow_path->GetExitLabel());
4555}
4556
David Brazdilcb1c0552015-08-04 16:22:25 +01004557static Address GetExceptionTlsAddress() {
4558 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4559}
4560
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004561void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4562 LocationSummary* locations =
4563 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4564 locations->SetOut(Location::RequiresRegister());
4565}
4566
4567void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004568 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4569}
4570
4571void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4572 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4573}
4574
4575void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4576 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004577}
4578
4579void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4580 LocationSummary* locations =
4581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4582 InvokeRuntimeCallingConvention calling_convention;
4583 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4584}
4585
4586void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004587 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4588 instruction,
4589 instruction->GetDexPc(),
4590 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004591}
4592
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004593void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004594 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4595 ? LocationSummary::kNoCall
4596 : LocationSummary::kCallOnSlowPath;
4597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4598 locations->SetInAt(0, Location::RequiresRegister());
4599 locations->SetInAt(1, Location::Any());
4600 locations->SetOut(Location::RequiresRegister());
4601}
4602
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004603void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004604 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004605 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004606 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004607 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004608 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4609 Label done, zero;
4610 SlowPathCodeX86_64* slow_path = nullptr;
4611
4612 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004613 // Avoid null check if we know obj is not null.
4614 if (instruction->MustDoNullCheck()) {
4615 __ testl(obj, obj);
4616 __ j(kEqual, &zero);
4617 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004618 // Compare the class of `obj` with `cls`.
4619 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004620 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004621 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004622 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004623 } else {
4624 DCHECK(cls.IsStackSlot()) << cls;
4625 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4626 }
4627 if (instruction->IsClassFinal()) {
4628 // Classes must be equal for the instanceof to succeed.
4629 __ j(kNotEqual, &zero);
4630 __ movl(out, Immediate(1));
4631 __ jmp(&done);
4632 } else {
4633 // If the classes are not equal, we go into a slow path.
4634 DCHECK(locations->OnlyCallsOnSlowPath());
4635 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004636 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004637 codegen_->AddSlowPath(slow_path);
4638 __ j(kNotEqual, slow_path->GetEntryLabel());
4639 __ movl(out, Immediate(1));
4640 __ jmp(&done);
4641 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004642
4643 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4644 __ Bind(&zero);
4645 __ movl(out, Immediate(0));
4646 }
4647
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004648 if (slow_path != nullptr) {
4649 __ Bind(slow_path->GetExitLabel());
4650 }
4651 __ Bind(&done);
4652}
4653
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004654void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4655 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4656 instruction, LocationSummary::kCallOnSlowPath);
4657 locations->SetInAt(0, Location::RequiresRegister());
4658 locations->SetInAt(1, Location::Any());
4659 locations->AddTemp(Location::RequiresRegister());
4660}
4661
4662void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4663 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004664 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004665 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004667 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4668 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4669 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4670 codegen_->AddSlowPath(slow_path);
4671
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004672 // Avoid null check if we know obj is not null.
4673 if (instruction->MustDoNullCheck()) {
4674 __ testl(obj, obj);
4675 __ j(kEqual, slow_path->GetExitLabel());
4676 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004677 // Compare the class of `obj` with `cls`.
4678 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004679 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004680 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004681 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004682 } else {
4683 DCHECK(cls.IsStackSlot()) << cls;
4684 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4685 }
Roland Levillain4d027112015-07-01 15:41:14 +01004686 // The checkcast succeeds if the classes are equal (fast path).
4687 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004688 __ j(kNotEqual, slow_path->GetEntryLabel());
4689 __ Bind(slow_path->GetExitLabel());
4690}
4691
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004692void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4693 LocationSummary* locations =
4694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4695 InvokeRuntimeCallingConvention calling_convention;
4696 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4697}
4698
4699void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004700 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4701 : QUICK_ENTRY_POINT(pUnlockObject),
4702 instruction,
4703 instruction->GetDexPc(),
4704 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004705}
4706
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004707void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4708void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4709void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4710
4711void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4712 LocationSummary* locations =
4713 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4714 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4715 || instruction->GetResultType() == Primitive::kPrimLong);
4716 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004717 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004718 locations->SetOut(Location::SameAsFirstInput());
4719}
4720
4721void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4722 HandleBitwiseOperation(instruction);
4723}
4724
4725void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4726 HandleBitwiseOperation(instruction);
4727}
4728
4729void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4730 HandleBitwiseOperation(instruction);
4731}
4732
4733void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4734 LocationSummary* locations = instruction->GetLocations();
4735 Location first = locations->InAt(0);
4736 Location second = locations->InAt(1);
4737 DCHECK(first.Equals(locations->Out()));
4738
4739 if (instruction->GetResultType() == Primitive::kPrimInt) {
4740 if (second.IsRegister()) {
4741 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004742 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004743 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004744 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004745 } else {
4746 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004747 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004748 }
4749 } else if (second.IsConstant()) {
4750 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4751 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004752 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004753 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004754 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004755 } else {
4756 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004757 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004758 }
4759 } else {
4760 Address address(CpuRegister(RSP), second.GetStackIndex());
4761 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004762 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004763 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004764 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004765 } else {
4766 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004767 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004768 }
4769 }
4770 } else {
4771 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004772 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4773 bool second_is_constant = false;
4774 int64_t value = 0;
4775 if (second.IsConstant()) {
4776 second_is_constant = true;
4777 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004778 }
Mark Mendell40741f32015-04-20 22:10:34 -04004779 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004780
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004781 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004782 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004783 if (is_int32_value) {
4784 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4785 } else {
4786 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4787 }
4788 } else if (second.IsDoubleStackSlot()) {
4789 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004790 } else {
4791 __ andq(first_reg, second.AsRegister<CpuRegister>());
4792 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004793 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004794 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004795 if (is_int32_value) {
4796 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4797 } else {
4798 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4799 }
4800 } else if (second.IsDoubleStackSlot()) {
4801 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004802 } else {
4803 __ orq(first_reg, second.AsRegister<CpuRegister>());
4804 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004805 } else {
4806 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004807 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004808 if (is_int32_value) {
4809 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4810 } else {
4811 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4812 }
4813 } else if (second.IsDoubleStackSlot()) {
4814 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004815 } else {
4816 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4817 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004818 }
4819 }
4820}
4821
Calin Juravleb1498f62015-02-16 13:13:29 +00004822void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4823 // Nothing to do, this should be removed during prepare for register allocator.
4824 UNUSED(instruction);
4825 LOG(FATAL) << "Unreachable";
4826}
4827
4828void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4829 // Nothing to do, this should be removed during prepare for register allocator.
4830 UNUSED(instruction);
4831 LOG(FATAL) << "Unreachable";
4832}
4833
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004834void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4835 DCHECK(codegen_->IsBaseline());
4836 LocationSummary* locations =
4837 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4838 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4839}
4840
4841void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4842 DCHECK(codegen_->IsBaseline());
4843 // Will be generated at use site.
4844}
4845
Mark Mendell92e83bf2015-05-07 11:25:03 -04004846void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4847 if (value == 0) {
4848 __ xorl(dest, dest);
4849 } else if (value > 0 && IsInt<32>(value)) {
4850 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4851 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4852 } else {
4853 __ movq(dest, Immediate(value));
4854 }
4855}
4856
Mark Mendellcfa410b2015-05-25 16:02:44 -04004857void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4858 DCHECK(dest.IsDoubleStackSlot());
4859 if (IsInt<32>(value)) {
4860 // Can move directly as an int32 constant.
4861 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4862 Immediate(static_cast<int32_t>(value)));
4863 } else {
4864 Load64BitValue(CpuRegister(TMP), value);
4865 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4866 }
4867}
4868
Mark Mendellf55c3e02015-03-26 21:07:46 -04004869void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4870 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004871 X86_64Assembler* assembler = GetAssembler();
4872 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004873 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4874 // byte values. If used for vectors at a later time, this will need to be
4875 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004876 assembler->Align(4, 0);
4877 constant_area_start_ = assembler->CodeSize();
4878 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004879 }
4880
4881 // And finish up.
4882 CodeGenerator::Finalize(allocator);
4883}
4884
4885/**
4886 * Class to handle late fixup of offsets into constant area.
4887 */
4888class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4889 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004890 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004891 : codegen_(codegen), offset_into_constant_area_(offset) {}
4892
4893 private:
4894 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4895 // Patch the correct offset for the instruction. We use the address of the
4896 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4897 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4898 int relative_position = constant_offset - pos;
4899
4900 // Patch in the right value.
4901 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4902 }
4903
Mark Mendell39dcf552015-04-09 20:42:42 -04004904 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004905
4906 // Location in constant area that the fixup refers to.
4907 int offset_into_constant_area_;
4908};
4909
4910Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4911 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4912 return Address::RIP(fixup);
4913}
4914
4915Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4916 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4917 return Address::RIP(fixup);
4918}
4919
4920Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4921 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4922 return Address::RIP(fixup);
4923}
4924
4925Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4926 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4927 return Address::RIP(fixup);
4928}
4929
Roland Levillain4d027112015-07-01 15:41:14 +01004930#undef __
4931
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004932} // namespace x86_64
4933} // namespace art