blob: a0f45ed73ef23da1939256249d48b89d0e30c6cd [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:
Calin Juravlebacfec32014-11-14 15:54:36 +000098 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
99 : 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:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
133 : 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()) {
3164 case Primitive::kPrimInt: {
3165 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003166 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003167 __ j(kEqual, slow_path->GetEntryLabel());
3168 } else if (value.IsStackSlot()) {
3169 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3170 __ j(kEqual, slow_path->GetEntryLabel());
3171 } else {
3172 DCHECK(value.IsConstant()) << value;
3173 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3174 __ jmp(slow_path->GetEntryLabel());
3175 }
3176 }
3177 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003178 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003179 case Primitive::kPrimLong: {
3180 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003182 __ j(kEqual, slow_path->GetEntryLabel());
3183 } else if (value.IsDoubleStackSlot()) {
3184 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3185 __ j(kEqual, slow_path->GetEntryLabel());
3186 } else {
3187 DCHECK(value.IsConstant()) << value;
3188 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3189 __ jmp(slow_path->GetEntryLabel());
3190 }
3191 }
3192 break;
3193 }
3194 default:
3195 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003196 }
Calin Juravled0d48522014-11-04 16:40:20 +00003197}
3198
Calin Juravle9aec02f2014-11-18 23:06:35 +00003199void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3200 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3201
3202 LocationSummary* locations =
3203 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3204
3205 switch (op->GetResultType()) {
3206 case Primitive::kPrimInt:
3207 case Primitive::kPrimLong: {
3208 locations->SetInAt(0, Location::RequiresRegister());
3209 // The shift count needs to be in CL.
3210 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3211 locations->SetOut(Location::SameAsFirstInput());
3212 break;
3213 }
3214 default:
3215 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3216 }
3217}
3218
3219void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3220 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3221
3222 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003224 Location second = locations->InAt(1);
3225
3226 switch (op->GetResultType()) {
3227 case Primitive::kPrimInt: {
3228 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003230 if (op->IsShl()) {
3231 __ shll(first_reg, second_reg);
3232 } else if (op->IsShr()) {
3233 __ sarl(first_reg, second_reg);
3234 } else {
3235 __ shrl(first_reg, second_reg);
3236 }
3237 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003238 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003239 if (op->IsShl()) {
3240 __ shll(first_reg, imm);
3241 } else if (op->IsShr()) {
3242 __ sarl(first_reg, imm);
3243 } else {
3244 __ shrl(first_reg, imm);
3245 }
3246 }
3247 break;
3248 }
3249 case Primitive::kPrimLong: {
3250 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003252 if (op->IsShl()) {
3253 __ shlq(first_reg, second_reg);
3254 } else if (op->IsShr()) {
3255 __ sarq(first_reg, second_reg);
3256 } else {
3257 __ shrq(first_reg, second_reg);
3258 }
3259 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003260 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003261 if (op->IsShl()) {
3262 __ shlq(first_reg, imm);
3263 } else if (op->IsShr()) {
3264 __ sarq(first_reg, imm);
3265 } else {
3266 __ shrq(first_reg, imm);
3267 }
3268 }
3269 break;
3270 }
3271 default:
3272 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3273 }
3274}
3275
3276void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3277 HandleShift(shl);
3278}
3279
3280void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3281 HandleShift(shl);
3282}
3283
3284void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3285 HandleShift(shr);
3286}
3287
3288void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3289 HandleShift(shr);
3290}
3291
3292void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3293 HandleShift(ushr);
3294}
3295
3296void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3297 HandleShift(ushr);
3298}
3299
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003300void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003301 LocationSummary* locations =
3302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003303 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003304 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003306 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003307}
3308
3309void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3310 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003311 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3312 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003313 // Note: if heap poisoning is enabled, the entry point takes cares
3314 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003315
3316 codegen_->InvokeRuntime(
3317 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3318 instruction,
3319 instruction->GetDexPc(),
3320 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003321
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003322 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003323}
3324
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003325void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3326 LocationSummary* locations =
3327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3328 InvokeRuntimeCallingConvention calling_convention;
3329 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003330 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003331 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003332 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003333}
3334
3335void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3336 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003337 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3338 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003339
Roland Levillain4d027112015-07-01 15:41:14 +01003340 // Note: if heap poisoning is enabled, the entry point takes cares
3341 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003342 codegen_->InvokeRuntime(
3343 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3344 instruction,
3345 instruction->GetDexPc(),
3346 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003347
3348 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003349}
3350
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003351void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003352 LocationSummary* locations =
3353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003354 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3355 if (location.IsStackSlot()) {
3356 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3357 } else if (location.IsDoubleStackSlot()) {
3358 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3359 }
3360 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003361}
3362
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003363void InstructionCodeGeneratorX86_64::VisitParameterValue(
3364 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003365 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003366}
3367
3368void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3369 LocationSummary* locations =
3370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3371 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3372}
3373
3374void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3375 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3376 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003377}
3378
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003379void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003380 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003381 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003382 locations->SetInAt(0, Location::RequiresRegister());
3383 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003384}
3385
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003386void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3387 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003388 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3389 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003390 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003391 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003392 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003393 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003394 break;
3395
3396 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003398 break;
3399
3400 default:
3401 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3402 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003403}
3404
David Brazdil66d126e2015-04-03 16:02:44 +01003405void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3406 LocationSummary* locations =
3407 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3408 locations->SetInAt(0, Location::RequiresRegister());
3409 locations->SetOut(Location::SameAsFirstInput());
3410}
3411
3412void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003413 LocationSummary* locations = bool_not->GetLocations();
3414 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3415 locations->Out().AsRegister<CpuRegister>().AsRegister());
3416 Location out = locations->Out();
3417 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3418}
3419
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003420void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003421 LocationSummary* locations =
3422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003423 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3424 locations->SetInAt(i, Location::Any());
3425 }
3426 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003427}
3428
3429void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003430 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003431 LOG(FATAL) << "Unimplemented";
3432}
3433
Calin Juravle52c48962014-12-16 17:02:57 +00003434void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3435 /*
3436 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3437 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3438 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3439 */
3440 switch (kind) {
3441 case MemBarrierKind::kAnyAny: {
3442 __ mfence();
3443 break;
3444 }
3445 case MemBarrierKind::kAnyStore:
3446 case MemBarrierKind::kLoadAny:
3447 case MemBarrierKind::kStoreStore: {
3448 // nop
3449 break;
3450 }
3451 default:
3452 LOG(FATAL) << "Unexpected memory barier " << kind;
3453 }
3454}
3455
3456void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3457 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3458
Nicolas Geoffray39468442014-09-02 15:17:15 +01003459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003461 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003462 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3463 locations->SetOut(Location::RequiresFpuRegister());
3464 } else {
3465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3466 }
Calin Juravle52c48962014-12-16 17:02:57 +00003467}
3468
3469void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3470 const FieldInfo& field_info) {
3471 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3472
3473 LocationSummary* locations = instruction->GetLocations();
3474 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3475 Location out = locations->Out();
3476 bool is_volatile = field_info.IsVolatile();
3477 Primitive::Type field_type = field_info.GetFieldType();
3478 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3479
3480 switch (field_type) {
3481 case Primitive::kPrimBoolean: {
3482 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3483 break;
3484 }
3485
3486 case Primitive::kPrimByte: {
3487 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3488 break;
3489 }
3490
3491 case Primitive::kPrimShort: {
3492 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3493 break;
3494 }
3495
3496 case Primitive::kPrimChar: {
3497 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3498 break;
3499 }
3500
3501 case Primitive::kPrimInt:
3502 case Primitive::kPrimNot: {
3503 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3504 break;
3505 }
3506
3507 case Primitive::kPrimLong: {
3508 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3509 break;
3510 }
3511
3512 case Primitive::kPrimFloat: {
3513 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3514 break;
3515 }
3516
3517 case Primitive::kPrimDouble: {
3518 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3519 break;
3520 }
3521
3522 case Primitive::kPrimVoid:
3523 LOG(FATAL) << "Unreachable type " << field_type;
3524 UNREACHABLE();
3525 }
3526
Calin Juravle77520bc2015-01-12 18:45:46 +00003527 codegen_->MaybeRecordImplicitNullCheck(instruction);
3528
Calin Juravle52c48962014-12-16 17:02:57 +00003529 if (is_volatile) {
3530 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3531 }
Roland Levillain4d027112015-07-01 15:41:14 +01003532
3533 if (field_type == Primitive::kPrimNot) {
3534 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3535 }
Calin Juravle52c48962014-12-16 17:02:57 +00003536}
3537
3538void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3539 const FieldInfo& field_info) {
3540 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3541
3542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003544 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003545 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003546 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003547
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003548 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003549 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3550 locations->SetInAt(1, Location::RequiresFpuRegister());
3551 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003552 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003553 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003554 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003555 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003556 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003557 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003558 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3559 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003560 locations->AddTemp(Location::RequiresRegister());
3561 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003562}
3563
Calin Juravle52c48962014-12-16 17:02:57 +00003564void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003565 const FieldInfo& field_info,
3566 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003567 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3568
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003569 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003570 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3571 Location value = locations->InAt(1);
3572 bool is_volatile = field_info.IsVolatile();
3573 Primitive::Type field_type = field_info.GetFieldType();
3574 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3575
3576 if (is_volatile) {
3577 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3578 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003579
3580 switch (field_type) {
3581 case Primitive::kPrimBoolean:
3582 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003583 if (value.IsConstant()) {
3584 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3585 __ movb(Address(base, offset), Immediate(v));
3586 } else {
3587 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3588 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003589 break;
3590 }
3591
3592 case Primitive::kPrimShort:
3593 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003594 if (value.IsConstant()) {
3595 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3596 __ movw(Address(base, offset), Immediate(v));
3597 } else {
3598 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3599 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003600 break;
3601 }
3602
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003604 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003605 if (value.IsConstant()) {
3606 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003607 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3608 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3609 // Note: if heap poisoning is enabled, no need to poison
3610 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003611 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003612 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003613 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3614 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3615 __ movl(temp, value.AsRegister<CpuRegister>());
3616 __ PoisonHeapReference(temp);
3617 __ movl(Address(base, offset), temp);
3618 } else {
3619 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3620 }
Mark Mendell40741f32015-04-20 22:10:34 -04003621 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003622 break;
3623 }
3624
3625 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003626 if (value.IsConstant()) {
3627 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3628 DCHECK(IsInt<32>(v));
3629 int32_t v_32 = v;
3630 __ movq(Address(base, offset), Immediate(v_32));
3631 } else {
3632 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3633 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003634 break;
3635 }
3636
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003637 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003638 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003639 break;
3640 }
3641
3642 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003643 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003644 break;
3645 }
3646
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647 case Primitive::kPrimVoid:
3648 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003649 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003650 }
Calin Juravle52c48962014-12-16 17:02:57 +00003651
Calin Juravle77520bc2015-01-12 18:45:46 +00003652 codegen_->MaybeRecordImplicitNullCheck(instruction);
3653
3654 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3655 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3656 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003657 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003658 }
3659
Calin Juravle52c48962014-12-16 17:02:57 +00003660 if (is_volatile) {
3661 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3662 }
3663}
3664
3665void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3666 HandleFieldSet(instruction, instruction->GetFieldInfo());
3667}
3668
3669void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003670 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003671}
3672
3673void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003674 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003675}
3676
3677void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003678 HandleFieldGet(instruction, instruction->GetFieldInfo());
3679}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003680
Calin Juravle52c48962014-12-16 17:02:57 +00003681void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3682 HandleFieldGet(instruction);
3683}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003684
Calin Juravle52c48962014-12-16 17:02:57 +00003685void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3686 HandleFieldGet(instruction, instruction->GetFieldInfo());
3687}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003688
Calin Juravle52c48962014-12-16 17:02:57 +00003689void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3690 HandleFieldSet(instruction, instruction->GetFieldInfo());
3691}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003692
Calin Juravle52c48962014-12-16 17:02:57 +00003693void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003694 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695}
3696
3697void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003698 LocationSummary* locations =
3699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003700 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3701 ? Location::RequiresRegister()
3702 : Location::Any();
3703 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003704 if (instruction->HasUses()) {
3705 locations->SetOut(Location::SameAsFirstInput());
3706 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003707}
3708
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003709void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003710 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3711 return;
3712 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003713 LocationSummary* locations = instruction->GetLocations();
3714 Location obj = locations->InAt(0);
3715
3716 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3717 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3718}
3719
3720void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003721 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003722 codegen_->AddSlowPath(slow_path);
3723
3724 LocationSummary* locations = instruction->GetLocations();
3725 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003726
3727 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003728 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003729 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003730 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003731 } else {
3732 DCHECK(obj.IsConstant()) << obj;
3733 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3734 __ jmp(slow_path->GetEntryLabel());
3735 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003736 }
3737 __ j(kEqual, slow_path->GetEntryLabel());
3738}
3739
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003740void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3741 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3742 GenerateImplicitNullCheck(instruction);
3743 } else {
3744 GenerateExplicitNullCheck(instruction);
3745 }
3746}
3747
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003748void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003749 LocationSummary* locations =
3750 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003751 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003752 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003753 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3754 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3755 } else {
3756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3757 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758}
3759
3760void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3761 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003762 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003764 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765
Roland Levillain4d027112015-07-01 15:41:14 +01003766 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767 case Primitive::kPrimBoolean: {
3768 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003769 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 if (index.IsConstant()) {
3771 __ movzxb(out, Address(obj,
3772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3773 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 }
3776 break;
3777 }
3778
3779 case Primitive::kPrimByte: {
3780 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003781 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 if (index.IsConstant()) {
3783 __ movsxb(out, Address(obj,
3784 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3785 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 }
3788 break;
3789 }
3790
3791 case Primitive::kPrimShort: {
3792 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 if (index.IsConstant()) {
3795 __ movsxw(out, Address(obj,
3796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3797 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 }
3800 break;
3801 }
3802
3803 case Primitive::kPrimChar: {
3804 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003805 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 if (index.IsConstant()) {
3807 __ movzxw(out, Address(obj,
3808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3809 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003810 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811 }
3812 break;
3813 }
3814
3815 case Primitive::kPrimInt:
3816 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003817 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3818 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003819 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003821 if (index.IsConstant()) {
3822 __ movl(out, Address(obj,
3823 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3824 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003825 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826 }
3827 break;
3828 }
3829
3830 case Primitive::kPrimLong: {
3831 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003832 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003833 if (index.IsConstant()) {
3834 __ movq(out, Address(obj,
3835 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3836 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003837 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003838 }
3839 break;
3840 }
3841
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003842 case Primitive::kPrimFloat: {
3843 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003844 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003845 if (index.IsConstant()) {
3846 __ movss(out, Address(obj,
3847 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3848 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003850 }
3851 break;
3852 }
3853
3854 case Primitive::kPrimDouble: {
3855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003857 if (index.IsConstant()) {
3858 __ movsd(out, Address(obj,
3859 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3860 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003861 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003862 }
3863 break;
3864 }
3865
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003866 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003867 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003868 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003870 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003871
3872 if (type == Primitive::kPrimNot) {
3873 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3874 __ MaybeUnpoisonHeapReference(out);
3875 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003876}
3877
3878void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003879 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003880
3881 bool needs_write_barrier =
3882 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3883 bool needs_runtime_call = instruction->NeedsTypeCheck();
3884
Nicolas Geoffray39468442014-09-02 15:17:15 +01003885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003886 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3887 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003889 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3890 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3891 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003892 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003893 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003894 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003895 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3896 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003897 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003898 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003899 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3900 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003901 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003902 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003903 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003904
3905 if (needs_write_barrier) {
3906 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003907 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003908 locations->AddTemp(Location::RequiresRegister());
3909 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003910 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911}
3912
3913void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3914 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003915 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003916 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003917 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003918 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003919 bool needs_runtime_call = locations->WillCall();
3920 bool needs_write_barrier =
3921 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922
3923 switch (value_type) {
3924 case Primitive::kPrimBoolean:
3925 case Primitive::kPrimByte: {
3926 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003927 if (index.IsConstant()) {
3928 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003929 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003931 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003932 __ movb(Address(obj, offset),
3933 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003934 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003935 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003936 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3938 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003939 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003941 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3942 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003944 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945 break;
3946 }
3947
3948 case Primitive::kPrimShort:
3949 case Primitive::kPrimChar: {
3950 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003951 if (index.IsConstant()) {
3952 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003953 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003954 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003955 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003956 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003957 __ movw(Address(obj, offset),
3958 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003959 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003960 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003961 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003962 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003963 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3964 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003965 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003966 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003967 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003968 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3969 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003970 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003971 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003972 break;
3973 }
3974
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003975 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003976 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003977 if (!needs_runtime_call) {
3978 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3979 if (index.IsConstant()) {
3980 size_t offset =
3981 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3982 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003983 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3984 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3985 __ movl(temp, value.AsRegister<CpuRegister>());
3986 __ PoisonHeapReference(temp);
3987 __ movl(Address(obj, offset), temp);
3988 } else {
3989 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
3990 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003991 } else {
3992 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003993 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003994 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3995 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3996 // Note: if heap poisoning is enabled, no need to poison
3997 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04003998 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003999 }
4000 } else {
4001 DCHECK(index.IsRegister()) << index;
4002 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004003 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4004 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4005 __ movl(temp, value.AsRegister<CpuRegister>());
4006 __ PoisonHeapReference(temp);
4007 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4008 } else {
4009 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4010 value.AsRegister<CpuRegister>());
4011 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004012 } else {
4013 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004014 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004015 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4016 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4017 // Note: if heap poisoning is enabled, no need to poison
4018 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004020 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004021 }
4022 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004023 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004024 if (needs_write_barrier) {
4025 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4027 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004028 codegen_->MarkGCCard(
4029 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004030 }
4031 } else {
4032 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004033 // Note: if heap poisoning is enabled, pAputObject takes cares
4034 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004035 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4036 instruction,
4037 instruction->GetDexPc(),
4038 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004039 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004041 break;
4042 }
4043
4044 case Primitive::kPrimLong: {
4045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004046 if (index.IsConstant()) {
4047 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004048 if (value.IsRegister()) {
4049 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4050 } else {
4051 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4052 DCHECK(IsInt<32>(v));
4053 int32_t v_32 = v;
4054 __ movq(Address(obj, offset), Immediate(v_32));
4055 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004056 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004057 if (value.IsRegister()) {
4058 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4059 value.AsRegister<CpuRegister>());
4060 } else {
4061 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4062 DCHECK(IsInt<32>(v));
4063 int32_t v_32 = v;
4064 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4065 Immediate(v_32));
4066 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004067 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004068 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004069 break;
4070 }
4071
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004072 case Primitive::kPrimFloat: {
4073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4074 if (index.IsConstant()) {
4075 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4076 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004077 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004078 } else {
4079 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4081 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004082 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004083 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004084 break;
4085 }
4086
4087 case Primitive::kPrimDouble: {
4088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4089 if (index.IsConstant()) {
4090 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4091 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004093 } else {
4094 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4096 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004097 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004098 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004099 break;
4100 }
4101
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004102 case Primitive::kPrimVoid:
4103 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004104 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 }
4106}
4107
4108void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004109 LocationSummary* locations =
4110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004111 locations->SetInAt(0, Location::RequiresRegister());
4112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004113}
4114
4115void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4116 LocationSummary* locations = instruction->GetLocations();
4117 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004118 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4119 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004121 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122}
4123
4124void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004125 LocationSummary* locations =
4126 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004127 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004128 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004129 if (instruction->HasUses()) {
4130 locations->SetOut(Location::SameAsFirstInput());
4131 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004132}
4133
4134void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4135 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004136 Location index_loc = locations->InAt(0);
4137 Location length_loc = locations->InAt(1);
4138 SlowPathCodeX86_64* slow_path =
4139 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004140
Mark Mendell99dbd682015-04-22 16:18:52 -04004141 if (length_loc.IsConstant()) {
4142 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4143 if (index_loc.IsConstant()) {
4144 // BCE will remove the bounds check if we are guarenteed to pass.
4145 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4146 if (index < 0 || index >= length) {
4147 codegen_->AddSlowPath(slow_path);
4148 __ jmp(slow_path->GetEntryLabel());
4149 } else {
4150 // Some optimization after BCE may have generated this, and we should not
4151 // generate a bounds check if it is a valid range.
4152 }
4153 return;
4154 }
4155
4156 // We have to reverse the jump condition because the length is the constant.
4157 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4158 __ cmpl(index_reg, Immediate(length));
4159 codegen_->AddSlowPath(slow_path);
4160 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004161 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004162 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4163 if (index_loc.IsConstant()) {
4164 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4165 __ cmpl(length, Immediate(value));
4166 } else {
4167 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4168 }
4169 codegen_->AddSlowPath(slow_path);
4170 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004171 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172}
4173
4174void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4175 CpuRegister card,
4176 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004177 CpuRegister value,
4178 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004180 if (value_can_be_null) {
4181 __ testl(value, value);
4182 __ j(kEqual, &is_null);
4183 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 __ gs()->movq(card, Address::Absolute(
4185 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4186 __ movq(temp, object);
4187 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004188 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004189 if (value_can_be_null) {
4190 __ Bind(&is_null);
4191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004192}
4193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004194void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4195 temp->SetLocations(nullptr);
4196}
4197
4198void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4199 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004200 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004201}
4202
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004203void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004204 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004205 LOG(FATAL) << "Unimplemented";
4206}
4207
4208void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004209 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4210}
4211
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004212void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4214}
4215
4216void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004217 HBasicBlock* block = instruction->GetBlock();
4218 if (block->GetLoopInformation() != nullptr) {
4219 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4220 // The back edge will generate the suspend check.
4221 return;
4222 }
4223 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4224 // The goto will generate the suspend check.
4225 return;
4226 }
4227 GenerateSuspendCheck(instruction, nullptr);
4228}
4229
4230void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4231 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004232 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004233 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4234 if (slow_path == nullptr) {
4235 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4236 instruction->SetSlowPath(slow_path);
4237 codegen_->AddSlowPath(slow_path);
4238 if (successor != nullptr) {
4239 DCHECK(successor->IsLoopHeader());
4240 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4241 }
4242 } else {
4243 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4244 }
4245
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004246 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004247 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004248 if (successor == nullptr) {
4249 __ j(kNotEqual, slow_path->GetEntryLabel());
4250 __ Bind(slow_path->GetReturnLabel());
4251 } else {
4252 __ j(kEqual, codegen_->GetLabelOf(successor));
4253 __ jmp(slow_path->GetEntryLabel());
4254 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004255}
4256
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004257X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4258 return codegen_->GetAssembler();
4259}
4260
4261void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4262 MoveOperands* move = moves_.Get(index);
4263 Location source = move->GetSource();
4264 Location destination = move->GetDestination();
4265
4266 if (source.IsRegister()) {
4267 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004268 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004269 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004270 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004271 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004272 } else {
4273 DCHECK(destination.IsDoubleStackSlot());
4274 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004275 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004276 }
4277 } else if (source.IsStackSlot()) {
4278 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004279 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004280 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004281 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004282 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004283 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004284 } else {
4285 DCHECK(destination.IsStackSlot());
4286 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4287 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4288 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004289 } else if (source.IsDoubleStackSlot()) {
4290 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004291 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004292 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004293 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004294 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4295 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004296 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004297 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004298 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4299 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4300 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004301 } else if (source.IsConstant()) {
4302 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004303 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4304 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004305 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004306 if (value == 0) {
4307 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4308 } else {
4309 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4310 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004311 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004312 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004313 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004314 }
4315 } else if (constant->IsLongConstant()) {
4316 int64_t value = constant->AsLongConstant()->GetValue();
4317 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004318 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004319 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004320 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004321 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004322 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004323 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004324 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004325 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004326 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004327 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4328 if (value == 0) {
4329 // easy FP 0.0.
4330 __ xorps(dest, dest);
4331 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004332 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004333 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004334 } else {
4335 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004336 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004337 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4338 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004339 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004340 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004341 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004342 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004343 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004344 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4345 if (value == 0) {
4346 __ xorpd(dest, dest);
4347 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004348 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004349 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004350 } else {
4351 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004352 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004353 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004354 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004355 } else if (source.IsFpuRegister()) {
4356 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004357 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004358 } else if (destination.IsStackSlot()) {
4359 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004361 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004362 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004363 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004364 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004365 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004366 }
4367}
4368
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004370 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004371 __ movl(Address(CpuRegister(RSP), mem), reg);
4372 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004373}
4374
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004375void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004376 ScratchRegisterScope ensure_scratch(
4377 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4378
4379 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4380 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4381 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4382 Address(CpuRegister(RSP), mem2 + stack_offset));
4383 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4384 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4385 CpuRegister(ensure_scratch.GetRegister()));
4386}
4387
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004388void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4389 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4390 __ movq(Address(CpuRegister(RSP), mem), reg);
4391 __ movq(reg, CpuRegister(TMP));
4392}
4393
4394void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4395 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004396 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004397
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004398 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4399 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4400 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4401 Address(CpuRegister(RSP), mem2 + stack_offset));
4402 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4403 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4404 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004405}
4406
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004407void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4408 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4409 __ movss(Address(CpuRegister(RSP), mem), reg);
4410 __ movd(reg, CpuRegister(TMP));
4411}
4412
4413void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4414 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4415 __ movsd(Address(CpuRegister(RSP), mem), reg);
4416 __ movd(reg, CpuRegister(TMP));
4417}
4418
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004419void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4420 MoveOperands* move = moves_.Get(index);
4421 Location source = move->GetSource();
4422 Location destination = move->GetDestination();
4423
4424 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004425 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004426 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004427 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004428 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004429 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004430 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004431 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4432 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004433 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004434 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004435 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004436 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4437 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004438 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004439 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4440 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4441 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004442 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004443 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004444 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004445 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004446 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004448 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004449 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004450 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004451 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004452 }
4453}
4454
4455
4456void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4457 __ pushq(CpuRegister(reg));
4458}
4459
4460
4461void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4462 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004463}
4464
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004465void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4466 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4467 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4468 Immediate(mirror::Class::kStatusInitialized));
4469 __ j(kLess, slow_path->GetEntryLabel());
4470 __ Bind(slow_path->GetExitLabel());
4471 // No need for memory fence, thanks to the X86_64 memory model.
4472}
4473
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004474void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004475 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4476 ? LocationSummary::kCallOnSlowPath
4477 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004478 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004479 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004480 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004481 locations->SetOut(Location::RequiresRegister());
4482}
4483
4484void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004485 LocationSummary* locations = cls->GetLocations();
4486 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4487 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004488 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004489 DCHECK(!cls->CanCallRuntime());
4490 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004491 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004492 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004493 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004494 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004495 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004496 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004497 __ MaybeUnpoisonHeapReference(out);
4498
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004499 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4500 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4501 codegen_->AddSlowPath(slow_path);
4502 __ testl(out, out);
4503 __ j(kEqual, slow_path->GetEntryLabel());
4504 if (cls->MustGenerateClinitCheck()) {
4505 GenerateClassInitializationCheck(slow_path, out);
4506 } else {
4507 __ Bind(slow_path->GetExitLabel());
4508 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004509 }
4510}
4511
4512void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4513 LocationSummary* locations =
4514 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4515 locations->SetInAt(0, Location::RequiresRegister());
4516 if (check->HasUses()) {
4517 locations->SetOut(Location::SameAsFirstInput());
4518 }
4519}
4520
4521void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004522 // We assume the class to not be null.
4523 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4524 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004526 GenerateClassInitializationCheck(slow_path,
4527 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004528}
4529
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004530void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4531 LocationSummary* locations =
4532 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004533 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004534 locations->SetOut(Location::RequiresRegister());
4535}
4536
4537void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4538 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4539 codegen_->AddSlowPath(slow_path);
4540
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004541 LocationSummary* locations = load->GetLocations();
4542 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4543 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004544 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004545 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004546 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004547 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004548 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004549 __ testl(out, out);
4550 __ j(kEqual, slow_path->GetEntryLabel());
4551 __ Bind(slow_path->GetExitLabel());
4552}
4553
David Brazdilcb1c0552015-08-04 16:22:25 +01004554static Address GetExceptionTlsAddress() {
4555 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4556}
4557
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004558void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4559 LocationSummary* locations =
4560 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4561 locations->SetOut(Location::RequiresRegister());
4562}
4563
4564void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004565 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4566}
4567
4568void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4569 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4570}
4571
4572void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4573 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004574}
4575
4576void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4577 LocationSummary* locations =
4578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4579 InvokeRuntimeCallingConvention calling_convention;
4580 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4581}
4582
4583void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004584 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4585 instruction,
4586 instruction->GetDexPc(),
4587 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004588}
4589
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004590void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004591 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4592 ? LocationSummary::kNoCall
4593 : LocationSummary::kCallOnSlowPath;
4594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4595 locations->SetInAt(0, Location::RequiresRegister());
4596 locations->SetInAt(1, Location::Any());
4597 locations->SetOut(Location::RequiresRegister());
4598}
4599
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004600void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004601 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004602 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004603 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004604 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004605 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4606 Label done, zero;
4607 SlowPathCodeX86_64* slow_path = nullptr;
4608
4609 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004610 // Avoid null check if we know obj is not null.
4611 if (instruction->MustDoNullCheck()) {
4612 __ testl(obj, obj);
4613 __ j(kEqual, &zero);
4614 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004615 // Compare the class of `obj` with `cls`.
4616 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004617 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004618 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004619 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004620 } else {
4621 DCHECK(cls.IsStackSlot()) << cls;
4622 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4623 }
4624 if (instruction->IsClassFinal()) {
4625 // Classes must be equal for the instanceof to succeed.
4626 __ j(kNotEqual, &zero);
4627 __ movl(out, Immediate(1));
4628 __ jmp(&done);
4629 } else {
4630 // If the classes are not equal, we go into a slow path.
4631 DCHECK(locations->OnlyCallsOnSlowPath());
4632 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004633 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004634 codegen_->AddSlowPath(slow_path);
4635 __ j(kNotEqual, slow_path->GetEntryLabel());
4636 __ movl(out, Immediate(1));
4637 __ jmp(&done);
4638 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004639
4640 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4641 __ Bind(&zero);
4642 __ movl(out, Immediate(0));
4643 }
4644
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004645 if (slow_path != nullptr) {
4646 __ Bind(slow_path->GetExitLabel());
4647 }
4648 __ Bind(&done);
4649}
4650
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004651void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4653 instruction, LocationSummary::kCallOnSlowPath);
4654 locations->SetInAt(0, Location::RequiresRegister());
4655 locations->SetInAt(1, Location::Any());
4656 locations->AddTemp(Location::RequiresRegister());
4657}
4658
4659void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4660 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004661 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004662 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004663 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4665 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4666 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4667 codegen_->AddSlowPath(slow_path);
4668
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004669 // Avoid null check if we know obj is not null.
4670 if (instruction->MustDoNullCheck()) {
4671 __ testl(obj, obj);
4672 __ j(kEqual, slow_path->GetExitLabel());
4673 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004674 // Compare the class of `obj` with `cls`.
4675 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004676 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004677 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004678 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004679 } else {
4680 DCHECK(cls.IsStackSlot()) << cls;
4681 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4682 }
Roland Levillain4d027112015-07-01 15:41:14 +01004683 // The checkcast succeeds if the classes are equal (fast path).
4684 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004685 __ j(kNotEqual, slow_path->GetEntryLabel());
4686 __ Bind(slow_path->GetExitLabel());
4687}
4688
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004689void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4690 LocationSummary* locations =
4691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4692 InvokeRuntimeCallingConvention calling_convention;
4693 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4694}
4695
4696void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004697 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4698 : QUICK_ENTRY_POINT(pUnlockObject),
4699 instruction,
4700 instruction->GetDexPc(),
4701 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004702}
4703
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4705void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4706void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4707
4708void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4709 LocationSummary* locations =
4710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4711 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4712 || instruction->GetResultType() == Primitive::kPrimLong);
4713 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004714 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004715 locations->SetOut(Location::SameAsFirstInput());
4716}
4717
4718void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4719 HandleBitwiseOperation(instruction);
4720}
4721
4722void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4723 HandleBitwiseOperation(instruction);
4724}
4725
4726void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4727 HandleBitwiseOperation(instruction);
4728}
4729
4730void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4731 LocationSummary* locations = instruction->GetLocations();
4732 Location first = locations->InAt(0);
4733 Location second = locations->InAt(1);
4734 DCHECK(first.Equals(locations->Out()));
4735
4736 if (instruction->GetResultType() == Primitive::kPrimInt) {
4737 if (second.IsRegister()) {
4738 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004739 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004740 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004741 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004742 } else {
4743 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004744 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004745 }
4746 } else if (second.IsConstant()) {
4747 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4748 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004749 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004750 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004751 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004752 } else {
4753 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004754 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004755 }
4756 } else {
4757 Address address(CpuRegister(RSP), second.GetStackIndex());
4758 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004759 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004760 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004761 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004762 } else {
4763 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004764 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004765 }
4766 }
4767 } else {
4768 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004769 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4770 bool second_is_constant = false;
4771 int64_t value = 0;
4772 if (second.IsConstant()) {
4773 second_is_constant = true;
4774 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004775 }
Mark Mendell40741f32015-04-20 22:10:34 -04004776 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004777
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004778 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004779 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004780 if (is_int32_value) {
4781 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4782 } else {
4783 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4784 }
4785 } else if (second.IsDoubleStackSlot()) {
4786 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004787 } else {
4788 __ andq(first_reg, second.AsRegister<CpuRegister>());
4789 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004790 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004791 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004792 if (is_int32_value) {
4793 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4794 } else {
4795 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4796 }
4797 } else if (second.IsDoubleStackSlot()) {
4798 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004799 } else {
4800 __ orq(first_reg, second.AsRegister<CpuRegister>());
4801 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004802 } else {
4803 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004804 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004805 if (is_int32_value) {
4806 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4807 } else {
4808 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4809 }
4810 } else if (second.IsDoubleStackSlot()) {
4811 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004812 } else {
4813 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4814 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004815 }
4816 }
4817}
4818
Calin Juravleb1498f62015-02-16 13:13:29 +00004819void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4820 // Nothing to do, this should be removed during prepare for register allocator.
4821 UNUSED(instruction);
4822 LOG(FATAL) << "Unreachable";
4823}
4824
4825void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4826 // Nothing to do, this should be removed during prepare for register allocator.
4827 UNUSED(instruction);
4828 LOG(FATAL) << "Unreachable";
4829}
4830
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004831void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4832 DCHECK(codegen_->IsBaseline());
4833 LocationSummary* locations =
4834 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4835 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4836}
4837
4838void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4839 DCHECK(codegen_->IsBaseline());
4840 // Will be generated at use site.
4841}
4842
Mark Mendell92e83bf2015-05-07 11:25:03 -04004843void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4844 if (value == 0) {
4845 __ xorl(dest, dest);
4846 } else if (value > 0 && IsInt<32>(value)) {
4847 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4848 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4849 } else {
4850 __ movq(dest, Immediate(value));
4851 }
4852}
4853
Mark Mendellcfa410b2015-05-25 16:02:44 -04004854void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4855 DCHECK(dest.IsDoubleStackSlot());
4856 if (IsInt<32>(value)) {
4857 // Can move directly as an int32 constant.
4858 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4859 Immediate(static_cast<int32_t>(value)));
4860 } else {
4861 Load64BitValue(CpuRegister(TMP), value);
4862 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4863 }
4864}
4865
Mark Mendellf55c3e02015-03-26 21:07:46 -04004866void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4867 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004868 X86_64Assembler* assembler = GetAssembler();
4869 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004870 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4871 // byte values. If used for vectors at a later time, this will need to be
4872 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004873 assembler->Align(4, 0);
4874 constant_area_start_ = assembler->CodeSize();
4875 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004876 }
4877
4878 // And finish up.
4879 CodeGenerator::Finalize(allocator);
4880}
4881
4882/**
4883 * Class to handle late fixup of offsets into constant area.
4884 */
4885class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4886 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004887 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004888 : codegen_(codegen), offset_into_constant_area_(offset) {}
4889
4890 private:
4891 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4892 // Patch the correct offset for the instruction. We use the address of the
4893 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4894 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4895 int relative_position = constant_offset - pos;
4896
4897 // Patch in the right value.
4898 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4899 }
4900
Mark Mendell39dcf552015-04-09 20:42:42 -04004901 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004902
4903 // Location in constant area that the fixup refers to.
4904 int offset_into_constant_area_;
4905};
4906
4907Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4908 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4909 return Address::RIP(fixup);
4910}
4911
4912Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4913 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4914 return Address::RIP(fixup);
4915}
4916
4917Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4918 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4919 return Address::RIP(fixup);
4920}
4921
4922Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4923 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4924 return Address::RIP(fixup);
4925}
4926
Roland Levillain4d027112015-07-01 15:41:14 +01004927#undef __
4928
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004929} // namespace x86_64
4930} // namespace art